-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorial7.cpp
164 lines (139 loc) · 4.82 KB
/
Tutorial7.cpp
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
#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 "Tutorial3.h"
#include <limits>
REGISTER_PLUGIN_BASIC(OpticksTutorial, Tutorial7);
namespace
{
template<typename T>
void updateStatistics(T* pData, double& min, double& max, double& total)
{
min=std::min(min, static_cast<double>(*pData));
max=std::max(max, static_cast<double>(*pData));
total+=*pData;
}
};
Tutorial7::Tutorial7()
{
setDescriptorId("{7BA22101-36DA-456E-89BD-C12196198170}");
setName("Tutorial 4");
setDescription("Accessing cube data.");
setCreator("Opticks Community");
setVersion("Sample");
setCopyright("Copyright (C) 2008, Ball Aerospace & Technologies Corp.");
setProductionStatus(false);
setType("Sample");
setSubtype("Statistics");
setMenuLocation("[Tutorial]/Tutorial7");
setAbortSupported(true);
}
bool Tutorial7::getInputSpecification(PlugInArgList* &pInArgList)
{
VERIFY(pInArgList=Service<PlugInManagerServices>()->getPlugInArgList());
pInArgList->addArg<Progress>(Executable::ProgressArg(), NULL, "Progress reporter");
pInArgList->addArg<RasterElement>(Executable::DataElementArg(), "Generate statistics for this raster element");
return true;
}
bool Tutorial7::getOutputSpecification(PlugInArgList*& pOutArgList)
{
VERIFY(pInArgList=Service<PlugInManagerServices>()->getPlugInArgList());
pOutArgList->addArg<double>("Minimum", "The minimum value");
pOutArgList->addArg<double>("Maximum", "The maximum value");
pOutArgList->addArg<unsigned int>("Count", "The number of pixels");
pOutArgList->addArg<double>("Mean", "The average value");
return true;
}
bool Tutorial7::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList)
{
StepResource pStep("Tutorial 4", "vijay", "{ED9E2B5D-4C1C-4689-B1AF-DC8FF42DCEF8}");
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());
FactoryResource<DataRequest> pRequest;
pRequest->setInterleaveFormat(BSQ);
DataAccessor pAcc = pCube->getDataAccessor(pRequest.release());
double min = std::numeric_limits<double>::max();
double max = -min;
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(), min, max, total);
pAcc->nextColumn();
}
pAcc->nextRow();
}
unsigned int count = pDesc->getColumnCount() * pDesc->getRowCount();
double mean = total / count;
if (pProgress != NULL)
{
std::string msg = "Minimum value: " + StringUtilities::toDisplayString(min) + "\n"
+ "Maximum value: " + StringUtilities::toDisplayString(max) + "\n"
+ "Number of pixels: " + StringUtilities::toDisplayString(count) + "\n"
+ "Average: " + StringUtilities::toDisplayString(mean);
pProgress->updateProgress(msg, 100, NORMAL);
}
pStep->addProperty("Minimum", min);
pStep->addProperty("Maximum", max);
pStep->addProperty("Count", count);
pStep->addProperty("Mean", mean);
pOutArgList->setPlugInArgValue("Minimum", &min);
pOutArgList->setPlugInArgValue("Maximum", &max);
pOutArgList->setPlugInArgValue("Count", &count);
pOutArgList->setPlugInArgValue("Mean", &mean);
pStep->finalize();
return true;
}