10 #define XML_STRING_SIZE 512
17 static bool productInitialized =
false;
18 static char productXMLFileName[FILENAME_MAX];
19 static xml_document rootNode;
20 static xml_node productsNode;
21 static xml_node productNode;
22 static xml_node algorithmNode;
24 static productInfo_t* productInfo =
NULL;
25 static productInfo_t* algorithmInfo =
NULL;
30 static char* duplicateString(
const char* src) {
43 if (info->description)
44 free(info->description);
52 if (info->paramDesignator)
53 free(info->paramDesignator);
57 if (info->standardName)
58 free(info->standardName);
72 if (info->algorithmName)
73 free(info->algorithmName);
75 if (info->productName)
76 free(info->productName);
84 if (info->displayScale)
85 free(info->displayScale);
92 free(info->reference);
97 if (info->titleFormat)
98 free(info->titleFormat);
108 bzero(info,
sizeof (productInfo_t));
119 productInfo_t* info = (productInfo_t*)
allocateMemory(
sizeof (productInfo_t),
131 if (info->description)
132 free(info->description);
137 if (info->paramDesignator)
138 free(info->paramDesignator);
139 if (info->standardName)
140 free(info->standardName);
142 free(info->category);
144 free(info->dataType);
149 if (info->algorithmName)
150 free(info->algorithmName);
151 if (info->productName)
152 free(info->productName);
153 if (info->displayScale)
154 free(info->displayScale);
156 free(info->reference);
159 if (info->titleFormat)
160 free(info->titleFormat);
171 if (
dest->productName)
172 free(
dest->productName);
173 dest->productName = duplicateString(src->productName);
174 if (
dest->paramDesignator)
175 free(
dest->paramDesignator);
176 dest->paramDesignator = duplicateString(src->paramDesignator);
177 dest->paramWaveMin = src->paramWaveMin;
178 dest->paramWaveMax = src->paramWaveMax;
188 if (
dest->description && strlen(
dest->description) > 0)
189 free(
dest->description);
190 dest->description = duplicateString(src->description);
193 dest->units = duplicateString(src->units);
196 dest->palette = duplicateString(src->palette);
197 if (
dest->paramDesignator)
198 free(
dest->paramDesignator);
199 dest->paramDesignator = duplicateString(src->paramDesignator);
200 dest->paramWaveMin = src->paramWaveMin;
201 dest->paramWaveMax = src->paramWaveMax;
202 if (
dest->standardName)
203 free(
dest->standardName);
204 dest->standardName = duplicateString(src->standardName);
206 free(
dest->category);
207 dest->category = duplicateString(src->category);
209 free(
dest->dataType);
210 dest->dataType = duplicateString(src->dataType);
213 dest->prefix = duplicateString(src->prefix);
216 dest->suffix = duplicateString(src->suffix);
217 if (
dest->algorithmName)
218 free(
dest->algorithmName);
219 dest->algorithmName = duplicateString(src->algorithmName);
220 if (
dest->productName)
221 free(
dest->productName);
222 dest->productName = duplicateString(src->productName);
223 dest->cat_ix = src->cat_ix;
224 dest->prod_ix = src->prod_ix;
225 dest->rank = src->rank;
226 dest->fillValue = src->fillValue;
227 dest->validMin = src->validMin;
228 dest->validMax = src->validMax;
229 if (
dest->displayScale)
230 free(
dest->displayScale);
231 dest->displayScale = duplicateString(src->displayScale);
232 dest->displayMin = src->displayMin;
233 dest->displayMax = src->displayMax;
234 dest->scaleFactor = src->scaleFactor;
235 dest->addOffset = src->addOffset;
237 free(
dest->reference);
238 dest->reference = duplicateString(src->reference);
241 dest->comment = duplicateString(src->comment);
242 if (
dest->titleFormat)
243 free(
dest->titleFormat);
244 dest->titleFormat = duplicateString(src->titleFormat);
253 if (productInitialized)
255 productInitialized =
true;
258 if ((dataRoot = getenv(
"OCDATAROOT")) ==
NULL) {
259 printf(
"OCDATAROOT environment variable is not defined.\n");
262 strcpy(productXMLFileName, dataRoot);
263 strcat(productXMLFileName,
"/common/product.xml");
265 xml_parse_result
result = rootNode.load_file(productXMLFileName);
267 printf(
"%s Line %d: could not open product XML file = %s\n",
268 __FILE__, __LINE__, productXMLFileName);
269 printf(
" %s",
result.description());
272 productsNode = rootNode.child(
"products");
274 printf(
"%s Line %d: could not find products tag in XML file = %s\n",
275 __FILE__, __LINE__, productXMLFileName);
290 tmpStr = productNode.name();
291 if (strcmp(tmpStr,
"product") != 0) {
292 printf(
"%s Line %d: \"product\" node expected, found \"%s\" in file %s\n",
293 __FILE__, __LINE__, tmpStr, productXMLFileName);
304 tmpStr = algorithmNode.name();
305 if (strcmp(tmpStr,
"algorithm") != 0) {
306 printf(
"%s Line %d: \"algorithm\" node expected, found \"%s\" in file %s\n",
307 __FILE__, __LINE__, tmpStr, productXMLFileName);
320 tmpNode =
node.child(
"paramDesignator");
324 free(info->paramDesignator);
334 tmpStr1 = tmpNode.child_value();
337 if (strcmp(tmpStr,
"none") == 0 ||
338 strcmp(tmpStr,
"band") == 0 ||
339 strcmp(tmpStr,
"int") == 0) {
340 free(info->paramDesignator);
341 info->paramDesignator = tmpStr;
345 }
else if (strcmp(tmpStr,
"uv") == 0) {
346 if (strcmp(info->paramDesignator,
"wave") != 0) {
347 free(info->paramDesignator);
348 info->paramDesignator =
strdup(
"wave");
351 info->paramWaveMin > 100)
352 info->paramWaveMin = 100;
354 info->paramWaveMax < 400)
355 info->paramWaveMax = 400;
356 }
else if (strcmp(tmpStr,
"visible") == 0) {
357 if (strcmp(info->paramDesignator,
"wave") != 0) {
358 free(info->paramDesignator);
359 info->paramDesignator =
strdup(
"wave");
362 info->paramWaveMin > 400)
363 info->paramWaveMin = 400;
365 info->paramWaveMax < 725)
366 info->paramWaveMax = 725;
367 }
else if (strcmp(tmpStr,
"nir") == 0) {
368 if (strcmp(info->paramDesignator,
"wave") != 0) {
369 free(info->paramDesignator);
370 info->paramDesignator =
strdup(
"wave");
373 info->paramWaveMin > 725)
374 info->paramWaveMin = 725;
376 info->paramWaveMax < 1400)
377 info->paramWaveMax = 1400;
378 }
else if (strcmp(tmpStr,
"swir") == 0) {
379 if (strcmp(info->paramDesignator,
"wave") != 0) {
380 free(info->paramDesignator);
381 info->paramDesignator =
strdup(
"wave");
384 info->paramWaveMin > 1400)
385 info->paramWaveMin = 1400;
387 info->paramWaveMax < 3000)
388 info->paramWaveMax = 3000;
389 }
else if (strcmp(tmpStr,
"emissive") == 0) {
390 if (strcmp(info->paramDesignator,
"wave") != 0) {
391 free(info->paramDesignator);
392 info->paramDesignator =
strdup(
"wave");
395 info->paramWaveMin > 3000)
396 info->paramWaveMin = 3000;
398 info->paramWaveMax < 15000)
399 info->paramWaveMax = 15000;
401 printf(
"%s Line %d: \"paramDesignator\" node has illegal value \"%s\" in file %s\n",
402 __FILE__, __LINE__, tmpStr, productXMLFileName);
409 tmpNode = tmpNode.next_sibling(
"paramDesignator");
421 if ((
node = rangeNode.child(
"validMin")))
422 productInfo->validMin = atof(
node.child_value());
423 if ((
node = rangeNode.child(
"validMax")))
424 productInfo->validMax = atof(
node.child_value());
425 if ((
node = rangeNode.child(
"displayMin")))
426 productInfo->displayMin = atof(
node.child_value());
427 if ((
node = rangeNode.child(
"displayMax")))
428 productInfo->displayMax = atof(
node.child_value());
429 if ((
node = rangeNode.child(
"scaleFactor")))
430 productInfo->scaleFactor = atof(
node.child_value());
431 if ((
node = rangeNode.child(
"addOffset")))
432 productInfo->addOffset = atof(
node.child_value());
442 void readRange(productInfo_t* productInfo, xml_node productNode,
int paramVal) {
448 rangeNode = productNode.child(
"range");
452 if (rangeNode.attribute(
"min"))
454 if (rangeNode.attribute(
"max"))
459 }
while ((rangeNode = rangeNode.next_sibling(
"range")));
462 rangeNode = productNode.child(
"range");
465 if ((attr = rangeNode.attribute(
"min"))) {
467 if ((attr = rangeNode.attribute(
"max"))) {
469 if (
min <= paramVal && paramVal <=
max) {
475 }
while ((rangeNode = rangeNode.next_sibling(
"range")));
485 if (productInfo->productName)
486 free(productInfo->productName);
487 productInfo->productName =
trimBlanksDup(productNode.attribute(
"name").value());
490 if (productInfo->paramDesignator)
491 free(productInfo->paramDesignator);
505 if (productInfo->productName)
506 free(productInfo->productName);
507 productInfo->productName =
trimBlanksDup(productNode.attribute(
"name").value());
509 if ((
node = productNode.child(
"standardName"))) {
510 if (productInfo->standardName)
511 free(productInfo->standardName);
515 node = productNode.child(
"units");
517 printf(
"-E- Need to define \"units\" in product \"%s\" in product.xml\n",
518 productInfo->productName);
521 if (productInfo->units)
522 free(productInfo->units);
526 if ((
node = productNode.child(
"palette"))) {
527 if (productInfo->palette)
528 free(productInfo->palette);
532 node = productNode.child(
"category");
534 printf(
"-E- Need to define \"category\" in product \"%s\" in product.xml\n",
535 productInfo->productName);
538 if (productInfo->category)
539 free(productInfo->category);
542 if ((
node = productNode.child(
"displayScale"))) {
543 if (productInfo->displayScale)
544 free(productInfo->displayScale);
548 if ((
node = productNode.child(
"type"))) {
549 if (productInfo->dataType)
550 free(productInfo->dataType);
552 if(!strcmp(productInfo->dataType,
"byte")) {
556 }
else if(!strcmp(productInfo->dataType,
"ubyte")) {
560 }
else if(!strcmp(productInfo->dataType,
"ushort")) {
564 }
else if(!strcmp(productInfo->dataType,
"uint")) {
571 if ((
node = productNode.child(
"reference"))) {
572 if (productInfo->reference)
573 free(productInfo->reference);
577 if ((
node = productNode.child(
"comment"))) {
578 if (productInfo->comment)
579 free(productInfo->comment);
584 if (strcmp(productInfo->paramDesignator,
"none") != 0) {
585 productInfo->prod_ix = paramVal;
588 readRange(productInfo, productNode, paramVal);
604 if (algorithmInfo->algorithmName) {
605 free(algorithmInfo->algorithmName);
606 algorithmInfo->algorithmName =
NULL;
608 if (algorithmInfo->prefix) {
609 free(algorithmInfo->prefix);
610 algorithmInfo->prefix =
NULL;
612 if (algorithmInfo->suffix) {
613 free(algorithmInfo->suffix);
614 algorithmInfo->suffix =
NULL;
618 if ((attr = algorithmNode.attribute(
"name"))) {
621 algorithmInfo->algorithmName =
strdup(
"");
627 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
628 strcpy(defaultPrefix, algorithmInfo->productName);
630 strcpy(defaultPrefix, algorithmInfo->productName);
631 strcat(defaultPrefix,
"_");
634 if ((
node = algorithmNode.child(
"prefix"))) {
637 algorithmInfo->prefix =
strdup(defaultPrefix);
639 if ((
node = algorithmNode.child(
"suffix"))) {
642 if (strlen(algorithmInfo->algorithmName) > 0) {
643 algorithmInfo->suffix = (
char*) malloc(strlen(algorithmInfo->algorithmName) + 2);
644 strcpy(algorithmInfo->suffix,
"_");
645 strcat(algorithmInfo->suffix, algorithmInfo->algorithmName);
647 algorithmInfo->suffix =
strdup(
"");
663 algorithmInfo->cat_ix = atoi(algorithmNode.child_value(
"cat_ix"));
664 if ((
node = algorithmNode.child(
"rank")))
665 algorithmInfo->rank = atoi(
node.child_value());
668 if ((
node = algorithmNode.child(
"units"))) {
669 if (algorithmInfo->units)
670 free(algorithmInfo->units);
674 if ((
node = algorithmNode.child(
"fillValue")))
675 algorithmInfo->fillValue = atof(
node.child_value());
677 node = algorithmNode.child(
"description");
679 printf(
"-E- Need to define \"description\" in product \"%s_%s\" in product.xml\n",
680 productInfo->productName, productInfo->algorithmName);
687 if (algorithmInfo->titleFormat)
688 free(algorithmInfo->titleFormat);
689 algorithmInfo->titleFormat =
strdup(tmpStr);
690 if (algorithmInfo->description)
691 free(algorithmInfo->description);
692 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
693 if ((
node = algorithmNode.child(
"prod_ix")))
694 algorithmInfo->prod_ix = atoi(
node.child_value());
697 algorithmInfo->description = (
char*) malloc(strlen(tmpStr) + 64);
698 algorithmInfo->prod_ix = paramVal;
699 sprintf(algorithmInfo->description, tmpStr, algorithmInfo->prod_ix);
702 if ((
node = algorithmNode.child(
"reference"))) {
703 if (algorithmInfo->reference)
704 free(algorithmInfo->reference);
708 if ((
node = algorithmNode.child(
"comment"))) {
709 if (algorithmInfo->comment)
710 free(algorithmInfo->comment);
714 if ((
node = algorithmNode.child(
"type"))) {
715 if (algorithmInfo->dataType)
716 free(algorithmInfo->dataType);
718 if(!strcmp(algorithmInfo->dataType,
"byte")) {
722 }
else if(!strcmp(algorithmInfo->dataType,
"ubyte")) {
726 }
else if(!strcmp(algorithmInfo->dataType,
"ushort")) {
730 }
else if(!strcmp(algorithmInfo->dataType,
"uint")) {
737 readRange(algorithmInfo, algorithmNode, paramVal);
744 productNode = productsNode.child(
"product");
746 printf(
"%s Line %d: could not find first product tag in XML file = %s\n",
747 __FILE__, __LINE__, productXMLFileName);
753 algorithmNode = productNode.child(
"algorithm");
754 if (!algorithmNode) {
755 printf(
"%s Line %d: could not find first algorithm tag for product=%s in XML file = %s\n",
756 __FILE__, __LINE__, productInfo->productName, productXMLFileName);
769 algorithmNode = algorithmNode.next_sibling(
"algorithm");
770 if (!algorithmNode) {
773 productNode = productNode.next_sibling(
"product");
779 algorithmNode = productNode.child(
"algorithm");
780 if (!algorithmNode) {
781 printf(
"%s Line %d: could not find first algorithm tag for product=%s in XML file = %s\n",
782 __FILE__, __LINE__, productInfo->productName, productXMLFileName);
802 if (strncmp(productFullName, algorithmInfo->prefix, strlen(algorithmInfo->prefix)))
806 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
807 strcpy(tmpStr, algorithmInfo->prefix);
808 strcat(tmpStr, algorithmInfo->suffix);
809 if (strcmp(tmpStr, productFullName) == 0) {
816 int nameLen = strlen(productFullName);
817 int prefixLen = strlen(algorithmInfo->prefix);
818 int suffixLen = strlen(algorithmInfo->suffix);
819 int paramLen = nameLen - prefixLen - suffixLen;
826 if (strcmp(algorithmInfo->suffix, productFullName + nameLen - suffixLen) != 0)
834 strncat(paramStr, productFullName + prefixLen, paramLen);
839 int i = atoi(paramStr);
840 if (strcmp(algorithmInfo->paramDesignator,
"wave") == 0) {
841 if (algorithmInfo->paramWaveMin <=
i && i <= algorithmInfo->paramWaveMax) {
847 numBands =
rdsensorinfo(sensorId, 0,
"iwave", (
void**) &iwave);
850 if (numBands != -1) {
851 for (waveIndex = 0; waveIndex < numBands; waveIndex++) {
852 if (
i == iwave[waveIndex]) {
905 extern "C" int findProductInfo(
const char* productName,
int sensorId, productInfo_t* info) {
907 const char* tmpProductName = productName;
910 if (strcmp(productName,
"chl_ocx") == 0) {
926 tmpProductName =
"chl_oc4";
939 tmpProductName =
"chl_oc3";
944 tmpProductName =
"chl_oc2";
947 printf(
"%s Line %d: need a default chlorophyll algorithm for this sensor\n",
962 if (strcmp(productName,
"chl_ocx") == 0) {
964 info->suffix =
strdup(
"_ocx");
981 if (strcmp(info->paramDesignator,
"none") == 0) {
983 strcat(
name, info->suffix);
985 sprintf(
name,
"%s%d%s", info->prefix, info->prod_ix, info->suffix);
997 printf(
"fullProductName=%s\n", productFullName);
998 printf(
"description=%s\n", info->description);
999 printf(
"units=%s\n", info->units);
1000 printf(
"palette=%s\n", info->palette);
1001 printf(
"paramDesignator=%s\n", info->paramDesignator);
1002 printf(
"paramWaveMin=%d\n", info->paramWaveMin);
1003 printf(
"paramWaveMax=%d\n", info->paramWaveMax);
1004 printf(
"standardName=%s\n", info->standardName);
1005 printf(
"category=%s\n", info->category);
1006 printf(
"dataType=%s\n", info->dataType);
1007 printf(
"prefix=%s\n", info->prefix);
1008 printf(
"suffix=%s\n", info->suffix);
1009 printf(
"algorithmName=%s\n", info->algorithmName);
1010 printf(
"productName=%s\n", info->productName);
1011 printf(
"cat_ix=%d\n", info->cat_ix);
1012 printf(
"prod_ix=%d\n", info->prod_ix);
1013 printf(
"rank=%d\n", info->rank);
1014 printf(
"fillValue=%g\n", info->fillValue);
1015 printf(
"validMin=%g\n", info->validMin);
1016 printf(
"validMax=%g\n", info->validMax);
1017 printf(
"displayScale=%s\n", info->displayScale);
1018 printf(
"displayMin=%g\n", info->displayMin);
1019 printf(
"displayMax=%g\n", info->displayMax);
1020 printf(
"scaleFactor=%g\n", info->scaleFactor);
1021 printf(
"addOffset=%g\n", info->addOffset);
1022 printf(
"reference=%s\n", info->reference);
1023 printf(
"comment=%s\n", info->comment);
1024 printf(
"titleFormat=%s\n", info->titleFormat);