-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMyFirstTest.cpp
More file actions
154 lines (135 loc) · 4.36 KB
/
Copy pathMyFirstTest.cpp
File metadata and controls
154 lines (135 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* The information in this file is
* Copyright(c) 2007 Ball Aerospace & Technologies Corporation
* and is subject to the terms and conditions of the
* GNU Lesser General Public License Version 2.1
* The license text is available from
* http://www.gnu.org/licenses/lgpl.html
*/
#include "AppConfig.h"
#include "AppVerify.h"
#include "DataAccessor.h"
#include "DataAccessorImpl.h"
#include "DataRequest.h"
#include "MessageLogResource.h"
#include "ObjectResource.h"
#include "PlugInArgList.h"
#include "PlugInManagerServices.h"
#include "PlugInRegistration.h"
#include "PlugInResource.h"
#include "Progress.h"
#include "RasterDataDescriptor.h"
#include "RasterElement.h"
#include "StringUtilities.h"
#include "switchOnEncoding.h"
#include "MyFirstTest.h"
#include <limits>
REGISTER_PLUGIN_BASIC(OpticksTutorial, MyFirstTest);
namespace
{
template<typename T>
void updateStatistics(T* pData, double& total)
{
total += *pData;
}
};
MyFirstTest::MyFirstTest()
{
setDescriptorId("{C5B6AF80-1E0B-4751-A5DC-B77837797A75}");
setName("My First Test");
setDescription("Calculate mean.");
setCreator("Yiwei Zhang");
setVersion("Sample");
setCopyright("Copyright (C) 2008, Ball Aerospace & Technologies Corp.");
setProductionStatus(false);
setType("Sample");
setSubtype("Statistics");
setMenuLocation("[Tutorial]/My First Test");
setAbortSupported(true);
}
MyFirstTest::~MyFirstTest()
{
}
bool MyFirstTest::getInputSpecification(PlugInArgList* &pInArgList)
{
VERIFY(pInArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pInArgList->addArg<Progress>(Executable::ProgressArg(), NULL, "Progress reporter");
pInArgList->addArg<RasterElement>(Executable::DataElementArg(), "Calculate mean for this raster element");
return true;
}
bool MyFirstTest::getOutputSpecification(PlugInArgList*& pOutArgList)
{
VERIFY(pOutArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pOutArgList->addArg<double>("Mean", "The average value");
return true;
}
bool MyFirstTest::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList)
{
StepResource pStep("My First Test", "app", "0FD3C564-041D-4f8f-BBF8-96A7A165AB61");
if (pInArgList == NULL || pOutArgList == NULL)
{
return false;
}
Progress* pProgress = pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg());
RasterElement* pCube = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg());
if (pCube == NULL)
{
std::string msg = "A raster cube must be specified.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
RasterDataDescriptor* pDesc = static_cast<RasterDataDescriptor*>(pCube->getDataDescriptor());
VERIFY(pDesc != NULL);
FactoryResource<DataRequest> pRequest;
pRequest->setInterleaveFormat(BSQ);
DataAccessor pAcc = pCube->getDataAccessor(pRequest.release());
double total = 0.0;
for (unsigned int row = 0; row < pDesc->getRowCount(); ++row)
{
if (isAborted())
{
std::string msg = getName() + " has been aborted.";
pStep->finalize(Message::Abort, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ABORT);
}
return false;
}
if (!pAcc.isValid())
{
std::string msg = "Unable to access the cube data.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", row * 100 / pDesc->getRowCount(), NORMAL);
}
for (unsigned int col = 0; col < pDesc->getColumnCount(); ++col)
{
switchOnEncoding(pDesc->getDataType(), updateStatistics, pAcc->getColumn(), total);
pAcc->nextColumn();
}
pAcc->nextRow();
}
unsigned int count = pDesc->getColumnCount() * pDesc->getRowCount();
double mean = total / count;
if (pProgress != NULL)
{
std::string msg = "Average: " + StringUtilities::toDisplayString(mean);
pProgress->updateProgress(msg, 100, NORMAL);
}
pStep->addProperty("Mean", mean);
pOutArgList->setPlugInArgValue("Mean", &mean);
pStep->finalize();
return true;
}