14 #include <rapidjson/document.h>
15 #include <rapidjson/istreamwrapper.h>
17 #define XML_STRING_SIZE 512
21 using namespace rapidjson;
26 static bool productInitialized =
false;
27 static char productXMLFileName[FILENAME_MAX];
28 static xml_document rootNode;
29 static xml_node productsNode;
30 static xml_node productNode;
31 static xml_node algorithmNode;
33 static productInfo_t* productInfo =
NULL;
34 static productInfo_t* algorithmInfo =
NULL;
36 static string aliasFileName =
"product_alias.json";
38 static map<string, ProductAlias> aliasMap;
39 static int aliasSensorId = -1;
45 static char* duplicateString(
const char* src) {
58 if (info->description)
59 free(info->description);
67 if (info->paramDesignator)
68 free(info->paramDesignator);
72 if (info->standardName)
73 free(info->standardName);
87 if (info->algorithmName)
88 free(info->algorithmName);
90 if (info->productName)
91 free(info->productName);
99 if (info->displayScale)
100 free(info->displayScale);
107 free(info->reference);
112 if (info->titleFormat)
113 free(info->titleFormat);
123 bzero(info,
sizeof (productInfo_t));
134 productInfo_t* info = (productInfo_t*)
allocateMemory(
sizeof (productInfo_t),
146 if (info->description)
147 free(info->description);
152 if (info->paramDesignator)
153 free(info->paramDesignator);
154 if (info->standardName)
155 free(info->standardName);
157 free(info->category);
159 free(info->dataType);
164 if (info->algorithmName)
165 free(info->algorithmName);
166 if (info->productName)
167 free(info->productName);
168 if (info->displayScale)
169 free(info->displayScale);
171 free(info->reference);
174 if (info->titleFormat)
175 free(info->titleFormat);
186 if (
dest->productName)
187 free(
dest->productName);
188 dest->productName = duplicateString(src->productName);
189 if (
dest->paramDesignator)
190 free(
dest->paramDesignator);
191 dest->paramDesignator = duplicateString(src->paramDesignator);
192 dest->paramWaveMin = src->paramWaveMin;
193 dest->paramWaveMax = src->paramWaveMax;
203 if (
dest->description && strlen(
dest->description) > 0)
204 free(
dest->description);
205 dest->description = duplicateString(src->description);
208 dest->units = duplicateString(src->units);
211 dest->palette = duplicateString(src->palette);
212 if (
dest->paramDesignator)
213 free(
dest->paramDesignator);
214 dest->paramDesignator = duplicateString(src->paramDesignator);
215 dest->paramWaveMin = src->paramWaveMin;
216 dest->paramWaveMax = src->paramWaveMax;
217 if (
dest->standardName)
218 free(
dest->standardName);
219 dest->standardName = duplicateString(src->standardName);
221 free(
dest->category);
222 dest->category = duplicateString(src->category);
224 free(
dest->dataType);
225 dest->dataType = duplicateString(src->dataType);
228 dest->prefix = duplicateString(src->prefix);
231 dest->suffix = duplicateString(src->suffix);
232 if (
dest->algorithmName)
233 free(
dest->algorithmName);
234 dest->algorithmName = duplicateString(src->algorithmName);
235 if (
dest->productName)
236 free(
dest->productName);
237 dest->productName = duplicateString(src->productName);
238 dest->cat_ix = src->cat_ix;
239 dest->prod_ix = src->prod_ix;
240 dest->rank = src->rank;
241 dest->fillValue = src->fillValue;
242 dest->validMin = src->validMin;
243 dest->validMax = src->validMax;
244 if (
dest->displayScale)
245 free(
dest->displayScale);
246 dest->displayScale = duplicateString(src->displayScale);
247 dest->displayMin = src->displayMin;
248 dest->displayMax = src->displayMax;
249 dest->scaleFactor = src->scaleFactor;
250 dest->addOffset = src->addOffset;
252 free(
dest->reference);
253 dest->reference = duplicateString(src->reference);
256 dest->comment = duplicateString(src->comment);
257 if (
dest->titleFormat)
258 free(
dest->titleFormat);
259 dest->titleFormat = duplicateString(src->titleFormat);
268 if (productInitialized)
270 productInitialized =
true;
273 if ((dataRoot = getenv(
"OCDATAROOT")) ==
NULL) {
274 printf(
"OCDATAROOT environment variable is not defined.\n");
277 strcpy(productXMLFileName, dataRoot);
278 strcat(productXMLFileName,
"/common/product.xml");
280 xml_parse_result
result = rootNode.load_file(productXMLFileName);
282 printf(
"%s Line %d: could not open product XML file = %s\n",
283 __FILE__, __LINE__, productXMLFileName);
284 printf(
" %s",
result.description());
287 productsNode = rootNode.child(
"products");
289 printf(
"%s Line %d: could not find products tag in XML file = %s\n",
290 __FILE__, __LINE__, productXMLFileName);
305 tmpStr = productNode.name();
306 if (strcmp(tmpStr,
"product") != 0) {
307 printf(
"%s Line %d: \"product\" node expected, found \"%s\" in file %s\n",
308 __FILE__, __LINE__, tmpStr, productXMLFileName);
319 tmpStr = algorithmNode.name();
320 if (strcmp(tmpStr,
"algorithm") != 0) {
321 printf(
"%s Line %d: \"algorithm\" node expected, found \"%s\" in file %s\n",
322 __FILE__, __LINE__, tmpStr, productXMLFileName);
335 tmpNode =
node.child(
"paramDesignator");
339 free(info->paramDesignator);
349 tmpStr1 = tmpNode.child_value();
352 if (strcmp(tmpStr,
"none") == 0 ||
353 strcmp(tmpStr,
"band") == 0 ||
354 strcmp(tmpStr,
"int") == 0) {
355 free(info->paramDesignator);
356 info->paramDesignator = tmpStr;
360 }
else if (strcmp(tmpStr,
"uv") == 0) {
361 if (strcmp(info->paramDesignator,
"wave") != 0) {
362 free(info->paramDesignator);
363 info->paramDesignator =
strdup(
"wave");
366 info->paramWaveMin > 100)
367 info->paramWaveMin = 100;
369 info->paramWaveMax < 400)
370 info->paramWaveMax = 400;
371 }
else if (strcmp(tmpStr,
"visible") == 0) {
372 if (strcmp(info->paramDesignator,
"wave") != 0) {
373 free(info->paramDesignator);
374 info->paramDesignator =
strdup(
"wave");
377 info->paramWaveMin > 400)
378 info->paramWaveMin = 400;
380 info->paramWaveMax < 725)
381 info->paramWaveMax = 725;
382 }
else if (strcmp(tmpStr,
"nir") == 0) {
383 if (strcmp(info->paramDesignator,
"wave") != 0) {
384 free(info->paramDesignator);
385 info->paramDesignator =
strdup(
"wave");
388 info->paramWaveMin > 725)
389 info->paramWaveMin = 725;
391 info->paramWaveMax < 1400)
392 info->paramWaveMax = 1400;
393 }
else if (strcmp(tmpStr,
"swir") == 0) {
394 if (strcmp(info->paramDesignator,
"wave") != 0) {
395 free(info->paramDesignator);
396 info->paramDesignator =
strdup(
"wave");
399 info->paramWaveMin > 1400)
400 info->paramWaveMin = 1400;
402 info->paramWaveMax < 3000)
403 info->paramWaveMax = 3000;
404 }
else if (strcmp(tmpStr,
"emissive") == 0) {
405 if (strcmp(info->paramDesignator,
"wave") != 0) {
406 free(info->paramDesignator);
407 info->paramDesignator =
strdup(
"wave");
410 info->paramWaveMin > 3000)
411 info->paramWaveMin = 3000;
413 info->paramWaveMax < 15000)
414 info->paramWaveMax = 15000;
416 printf(
"%s Line %d: \"paramDesignator\" node has illegal value \"%s\" in file %s\n",
417 __FILE__, __LINE__, tmpStr, productXMLFileName);
424 tmpNode = tmpNode.next_sibling(
"paramDesignator");
436 if ((
node = rangeNode.child(
"validMin")))
437 productInfo->validMin = atof(
node.child_value());
438 if ((
node = rangeNode.child(
"validMax")))
439 productInfo->validMax = atof(
node.child_value());
440 if ((
node = rangeNode.child(
"displayMin")))
441 productInfo->displayMin = atof(
node.child_value());
442 if ((
node = rangeNode.child(
"displayMax")))
443 productInfo->displayMax = atof(
node.child_value());
444 if ((
node = rangeNode.child(
"scaleFactor")))
445 productInfo->scaleFactor = atof(
node.child_value());
446 if ((
node = rangeNode.child(
"addOffset")))
447 productInfo->addOffset = atof(
node.child_value());
457 void readRange(productInfo_t* productInfo, xml_node productNode,
int paramVal) {
463 rangeNode = productNode.child(
"range");
467 if (rangeNode.attribute(
"min"))
469 if (rangeNode.attribute(
"max"))
474 }
while ((rangeNode = rangeNode.next_sibling(
"range")));
477 rangeNode = productNode.child(
"range");
480 if ((attr = rangeNode.attribute(
"min"))) {
482 if ((attr = rangeNode.attribute(
"max"))) {
484 if (
min <= paramVal && paramVal <=
max) {
490 }
while ((rangeNode = rangeNode.next_sibling(
"range")));
500 if (productInfo->productName)
501 free(productInfo->productName);
502 productInfo->productName =
trimBlanksDup(productNode.attribute(
"name").value());
505 if (productInfo->paramDesignator)
506 free(productInfo->paramDesignator);
520 if (productInfo->productName)
521 free(productInfo->productName);
522 productInfo->productName =
trimBlanksDup(productNode.attribute(
"name").value());
524 if ((
node = productNode.child(
"standardName"))) {
525 if (productInfo->standardName)
526 free(productInfo->standardName);
530 if ((
node = productNode.child(
"units"))) {
531 if (productInfo->units)
532 free(productInfo->units);
537 if ((
node = productNode.child(
"palette"))) {
538 if (productInfo->palette)
539 free(productInfo->palette);
543 node = productNode.child(
"category");
545 printf(
"-E- Need to define \"category\" in product \"%s\" in product.xml\n",
546 productInfo->productName);
549 if (productInfo->category)
550 free(productInfo->category);
553 if ((
node = productNode.child(
"displayScale"))) {
554 if (productInfo->displayScale)
555 free(productInfo->displayScale);
559 if ((
node = productNode.child(
"type"))) {
560 if (productInfo->dataType)
561 free(productInfo->dataType);
563 if(!strcmp(productInfo->dataType,
"byte")) {
567 }
else if(!strcmp(productInfo->dataType,
"ubyte")) {
571 }
else if(!strcmp(productInfo->dataType,
"ushort")) {
575 }
else if(!strcmp(productInfo->dataType,
"uint")) {
582 if ((
node = productNode.child(
"reference"))) {
583 if (productInfo->reference)
584 free(productInfo->reference);
588 if ((
node = productNode.child(
"comment"))) {
589 if (productInfo->comment)
590 free(productInfo->comment);
595 if (strcmp(productInfo->paramDesignator,
"none") != 0) {
596 productInfo->prod_ix = paramVal;
599 readRange(productInfo, productNode, paramVal);
615 if (algorithmInfo->algorithmName) {
616 free(algorithmInfo->algorithmName);
617 algorithmInfo->algorithmName =
NULL;
619 if (algorithmInfo->prefix) {
620 free(algorithmInfo->prefix);
621 algorithmInfo->prefix =
NULL;
623 if (algorithmInfo->suffix) {
624 free(algorithmInfo->suffix);
625 algorithmInfo->suffix =
NULL;
629 if ((attr = algorithmNode.attribute(
"name"))) {
632 algorithmInfo->algorithmName =
strdup(
"");
638 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
639 strcpy(defaultPrefix, algorithmInfo->productName);
641 strcpy(defaultPrefix, algorithmInfo->productName);
642 strcat(defaultPrefix,
"_");
645 if ((
node = algorithmNode.child(
"prefix"))) {
648 algorithmInfo->prefix =
strdup(defaultPrefix);
650 if ((
node = algorithmNode.child(
"suffix"))) {
653 if (strlen(algorithmInfo->algorithmName) > 0) {
654 algorithmInfo->suffix = (
char*) malloc(strlen(algorithmInfo->algorithmName) + 2);
655 strcpy(algorithmInfo->suffix,
"_");
656 strcat(algorithmInfo->suffix, algorithmInfo->algorithmName);
658 algorithmInfo->suffix =
strdup(
"");
674 algorithmInfo->cat_ix = atoi(algorithmNode.child_value(
"cat_ix"));
675 if ((
node = algorithmNode.child(
"rank")))
676 algorithmInfo->rank = atoi(
node.child_value());
679 if ((
node = algorithmNode.child(
"units"))) {
680 if (algorithmInfo->units)
681 free(algorithmInfo->units);
685 if ((
node = algorithmNode.child(
"fillValue")))
686 algorithmInfo->fillValue = atof(
node.child_value());
688 node = algorithmNode.child(
"description");
690 printf(
"-E- Need to define \"description\" in product \"%s_%s\" in product.xml\n",
691 productInfo->productName, productInfo->algorithmName);
698 if (algorithmInfo->titleFormat)
699 free(algorithmInfo->titleFormat);
700 algorithmInfo->titleFormat =
strdup(tmpStr);
701 if (algorithmInfo->description)
702 free(algorithmInfo->description);
703 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
704 if ((
node = algorithmNode.child(
"prod_ix")))
705 algorithmInfo->prod_ix = atoi(
node.child_value());
708 algorithmInfo->description = (
char*) malloc(strlen(tmpStr) + 64);
709 algorithmInfo->prod_ix = paramVal;
710 sprintf(algorithmInfo->description, tmpStr, algorithmInfo->prod_ix);
713 if ((
node = algorithmNode.child(
"reference"))) {
714 if (algorithmInfo->reference)
715 free(algorithmInfo->reference);
719 if ((
node = algorithmNode.child(
"comment"))) {
720 if (algorithmInfo->comment)
721 free(algorithmInfo->comment);
725 if ((
node = algorithmNode.child(
"type"))) {
726 if (algorithmInfo->dataType)
727 free(algorithmInfo->dataType);
729 if(!strcmp(algorithmInfo->dataType,
"byte")) {
733 }
else if(!strcmp(algorithmInfo->dataType,
"ubyte")) {
737 }
else if(!strcmp(algorithmInfo->dataType,
"ushort")) {
741 }
else if(!strcmp(algorithmInfo->dataType,
"uint")) {
748 readRange(algorithmInfo, algorithmNode, paramVal);
755 productNode = productsNode.child(
"product");
757 printf(
"%s Line %d: could not find first product tag in XML file = %s\n",
758 __FILE__, __LINE__, productXMLFileName);
764 algorithmNode = productNode.child(
"algorithm");
765 if (!algorithmNode) {
766 printf(
"%s Line %d: could not find first algorithm tag for product=%s in XML file = %s\n",
767 __FILE__, __LINE__, productInfo->productName, productXMLFileName);
780 algorithmNode = algorithmNode.next_sibling(
"algorithm");
781 if (!algorithmNode) {
784 productNode = productNode.next_sibling(
"product");
790 algorithmNode = productNode.child(
"algorithm");
791 if (!algorithmNode) {
792 printf(
"%s Line %d: could not find first algorithm tag for product=%s in XML file = %s\n",
793 __FILE__, __LINE__, productInfo->productName, productXMLFileName);
813 if (strncmp(productFullName, algorithmInfo->prefix, strlen(algorithmInfo->prefix)))
817 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
818 strcpy(tmpStr, algorithmInfo->prefix);
819 strcat(tmpStr, algorithmInfo->suffix);
820 if (strcmp(tmpStr, productFullName) == 0) {
827 int nameLen = strlen(productFullName);
828 int prefixLen = strlen(algorithmInfo->prefix);
829 int suffixLen = strlen(algorithmInfo->suffix);
830 int paramLen = nameLen - prefixLen - suffixLen;
837 if (strcmp(algorithmInfo->suffix, productFullName + nameLen - suffixLen) != 0)
845 strncat(paramStr, productFullName + prefixLen, paramLen);
850 int i = atoi(paramStr);
851 if (strcmp(algorithmInfo->paramDesignator,
"wave") == 0) {
852 if (algorithmInfo->paramWaveMin <=
i && i <= algorithmInfo->paramWaveMax) {
858 numBands =
rdsensorinfo(sensorId, 0,
"iwave", (
void**) &iwave);
861 if (numBands != -1) {
862 for (waveIndex = 0; waveIndex < numBands; waveIndex++) {
863 if (
i == iwave[waveIndex]) {
919 char fileNameFixed[FILENAME_MAX];
923 if (access(fileNameFixed, R_OK) != -1) {
924 ifstream ifs(fileNameFixed);
925 IStreamWrapper isw(ifs);
927 document.ParseStream(isw);
929 for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) {
931 alias.name = itr->value[
"name"].GetString();
932 alias.prefix = itr->value[
"prefix"].GetString();
933 alias.suffix = itr->value[
"suffix"].GetString();
935 aliasMap[itr->name.GetString()] = alias;
941 if(sensorId != aliasSensorId) {
942 aliasSensorId = sensorId;
946 string fileName = (
string)
"$OCDATAROOT/common/" + aliasFileName;
955 if(subsensorId != -1) {
967 productAlias = aliasMap.at(productName);
969 }
catch (
const std::out_of_range& oor) {
983 extern "C" int findProductInfo(
const char* productName,
int sensorId, productInfo_t* info) {
985 const char* tmpProductName = productName;
988 ProductAlias productAlias;
991 tmpProductName = productAlias.name.c_str();
1005 info->prefix =
strdup(productAlias.prefix.c_str());
1007 info->suffix =
strdup(productAlias.suffix.c_str());
1025 if (strcmp(info->paramDesignator,
"none") == 0) {
1027 strcat(
name, info->suffix);
1029 sprintf(
name,
"%s%d%s", info->prefix, info->prod_ix, info->suffix);
1041 printf(
"fullProductName=%s\n", productFullName);
1042 printf(
"description=%s\n", info->description);
1043 printf(
"units=%s\n", info->units);
1044 printf(
"palette=%s\n", info->palette);
1045 printf(
"paramDesignator=%s\n", info->paramDesignator);
1046 printf(
"paramWaveMin=%d\n", info->paramWaveMin);
1047 printf(
"paramWaveMax=%d\n", info->paramWaveMax);
1048 printf(
"standardName=%s\n", info->standardName);
1049 printf(
"category=%s\n", info->category);
1050 printf(
"dataType=%s\n", info->dataType);
1051 printf(
"prefix=%s\n", info->prefix);
1052 printf(
"suffix=%s\n", info->suffix);
1053 printf(
"algorithmName=%s\n", info->algorithmName);
1054 printf(
"productName=%s\n", info->productName);
1055 printf(
"cat_ix=%d\n", info->cat_ix);
1056 printf(
"prod_ix=%d\n", info->prod_ix);
1057 printf(
"rank=%d\n", info->rank);
1058 printf(
"fillValue=%g\n", info->fillValue);
1059 printf(
"validMin=%g\n", info->validMin);
1060 printf(
"validMax=%g\n", info->validMax);
1061 printf(
"displayScale=%s\n", info->displayScale);
1062 printf(
"displayMin=%g\n", info->displayMin);
1063 printf(
"displayMax=%g\n", info->displayMax);
1064 printf(
"scaleFactor=%g\n", info->scaleFactor);
1065 printf(
"addOffset=%g\n", info->addOffset);
1066 printf(
"reference=%s\n", info->reference);
1067 printf(
"comment=%s\n", info->comment);
1068 printf(
"titleFormat=%s\n", info->titleFormat);