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);
554 if ((
node = productNode.child(
"reference"))) {
555 if (productInfo->reference)
556 free(productInfo->reference);
560 if ((
node = productNode.child(
"comment"))) {
561 if (productInfo->comment)
562 free(productInfo->comment);
567 if (strcmp(productInfo->paramDesignator,
"none") != 0) {
568 productInfo->prod_ix = paramVal;
571 readRange(productInfo, productNode, paramVal);
587 if (algorithmInfo->algorithmName) {
588 free(algorithmInfo->algorithmName);
589 algorithmInfo->algorithmName =
NULL;
591 if (algorithmInfo->prefix) {
592 free(algorithmInfo->prefix);
593 algorithmInfo->prefix =
NULL;
595 if (algorithmInfo->suffix) {
596 free(algorithmInfo->suffix);
597 algorithmInfo->suffix =
NULL;
601 if ((attr = algorithmNode.attribute(
"name"))) {
604 algorithmInfo->algorithmName =
strdup(
"");
610 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
611 strcpy(defaultPrefix, algorithmInfo->productName);
613 strcpy(defaultPrefix, algorithmInfo->productName);
614 strcat(defaultPrefix,
"_");
617 if ((
node = algorithmNode.child(
"prefix"))) {
620 algorithmInfo->prefix =
strdup(defaultPrefix);
622 if ((
node = algorithmNode.child(
"suffix"))) {
625 if (strlen(algorithmInfo->algorithmName) > 0) {
626 algorithmInfo->suffix = (
char*) malloc(strlen(algorithmInfo->algorithmName) + 2);
627 strcpy(algorithmInfo->suffix,
"_");
628 strcat(algorithmInfo->suffix, algorithmInfo->algorithmName);
630 algorithmInfo->suffix =
strdup(
"");
646 algorithmInfo->cat_ix = atoi(algorithmNode.child_value(
"cat_ix"));
647 if ((
node = algorithmNode.child(
"rank")))
648 algorithmInfo->rank = atoi(
node.child_value());
651 if ((
node = algorithmNode.child(
"units"))) {
652 if (algorithmInfo->units)
653 free(algorithmInfo->units);
657 if ((
node = algorithmNode.child(
"fillValue")))
658 algorithmInfo->fillValue = atof(
node.child_value());
660 node = algorithmNode.child(
"description");
662 printf(
"-E- Need to define \"description\" in product \"%s_%s\" in product.xml\n",
663 productInfo->productName, productInfo->algorithmName);
670 if (algorithmInfo->titleFormat)
671 free(algorithmInfo->titleFormat);
672 algorithmInfo->titleFormat =
strdup(tmpStr);
673 if (algorithmInfo->description)
674 free(algorithmInfo->description);
675 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
676 if ((
node = algorithmNode.child(
"prod_ix")))
677 algorithmInfo->prod_ix = atoi(
node.child_value());
680 algorithmInfo->description = (
char*) malloc(strlen(tmpStr) + 64);
681 algorithmInfo->prod_ix = paramVal;
682 sprintf(algorithmInfo->description, tmpStr, algorithmInfo->prod_ix);
685 if ((
node = algorithmNode.child(
"reference"))) {
686 if (algorithmInfo->reference)
687 free(algorithmInfo->reference);
691 if ((
node = algorithmNode.child(
"comment"))) {
692 if (algorithmInfo->comment)
693 free(algorithmInfo->comment);
697 if ((
node = algorithmNode.child(
"type"))) {
698 if (algorithmInfo->dataType)
699 free(algorithmInfo->dataType);
703 readRange(algorithmInfo, algorithmNode, paramVal);
710 productNode = productsNode.child(
"product");
712 printf(
"%s Line %d: could not find first product tag in XML file = %s\n",
713 __FILE__, __LINE__, productXMLFileName);
719 algorithmNode = productNode.child(
"algorithm");
720 if (!algorithmNode) {
721 printf(
"%s Line %d: could not find first algorithm tag for product=%s in XML file = %s\n",
722 __FILE__, __LINE__, productInfo->productName, productXMLFileName);
735 algorithmNode = algorithmNode.next_sibling(
"algorithm");
736 if (!algorithmNode) {
739 productNode = productNode.next_sibling(
"product");
745 algorithmNode = productNode.child(
"algorithm");
746 if (!algorithmNode) {
747 printf(
"%s Line %d: could not find first algorithm tag for product=%s in XML file = %s\n",
748 __FILE__, __LINE__, productInfo->productName, productXMLFileName);
768 if (strncmp(productFullName, algorithmInfo->prefix, strlen(algorithmInfo->prefix)))
772 if (strcmp(algorithmInfo->paramDesignator,
"none") == 0) {
773 strcpy(tmpStr, algorithmInfo->prefix);
774 strcat(tmpStr, algorithmInfo->suffix);
775 if (strcmp(tmpStr, productFullName) == 0) {
782 int nameLen = strlen(productFullName);
783 int prefixLen = strlen(algorithmInfo->prefix);
784 int suffixLen = strlen(algorithmInfo->suffix);
785 int paramLen = nameLen - prefixLen - suffixLen;
792 if (strcmp(algorithmInfo->suffix, productFullName + nameLen - suffixLen) != 0)
800 strncat(paramStr, productFullName + prefixLen, paramLen);
805 int i = atoi(paramStr);
806 if (strcmp(algorithmInfo->paramDesignator,
"wave") == 0) {
807 if (algorithmInfo->paramWaveMin <=
i && i <= algorithmInfo->paramWaveMax) {
813 numBands =
rdsensorinfo(sensorId, 0,
"iwave", (
void**) &iwave);
816 if (numBands != -1) {
817 for (waveIndex = 0; waveIndex < numBands; waveIndex++) {
818 if (
i == iwave[waveIndex]) {
871 extern "C" int findProductInfo(
const char* productName,
int sensorId, productInfo_t* info) {
873 const char* tmpProductName = productName;
876 if (strcmp(productName,
"chl_ocx") == 0) {
892 tmpProductName =
"chl_oc4";
904 tmpProductName =
"chl_oc3";
909 tmpProductName =
"chl_oc2";
912 printf(
"%s Line %d: need a default chlorophyll algorithm for this sensor\n",
927 if (strcmp(productName,
"chl_ocx") == 0) {
929 info->suffix =
strdup(
"_ocx");
946 if (strcmp(info->paramDesignator,
"none") == 0) {
948 strcat(
name, info->suffix);
950 sprintf(
name,
"%s%d%s", info->prefix, info->prod_ix, info->suffix);
962 printf(
"fullProductName=%s\n", productFullName);
963 printf(
"description=%s\n", info->description);
964 printf(
"units=%s\n", info->units);
965 printf(
"palette=%s\n", info->palette);
966 printf(
"paramDesignator=%s\n", info->paramDesignator);
967 printf(
"paramWaveMin=%d\n", info->paramWaveMin);
968 printf(
"paramWaveMax=%d\n", info->paramWaveMax);
969 printf(
"standardName=%s\n", info->standardName);
970 printf(
"category=%s\n", info->category);
971 printf(
"dataType=%s\n", info->dataType);
972 printf(
"prefix=%s\n", info->prefix);
973 printf(
"suffix=%s\n", info->suffix);
974 printf(
"algorithmName=%s\n", info->algorithmName);
975 printf(
"productName=%s\n", info->productName);
976 printf(
"cat_ix=%d\n", info->cat_ix);
977 printf(
"prod_ix=%d\n", info->prod_ix);
978 printf(
"rank=%d\n", info->rank);
979 printf(
"fillValue=%g\n", info->fillValue);
980 printf(
"validMin=%g\n", info->validMin);
981 printf(
"validMax=%g\n", info->validMax);
982 printf(
"displayScale=%s\n", info->displayScale);
983 printf(
"displayMin=%g\n", info->displayMin);
984 printf(
"displayMax=%g\n", info->displayMax);
985 printf(
"scaleFactor=%g\n", info->scaleFactor);
986 printf(
"addOffset=%g\n", info->addOffset);
987 printf(
"reference=%s\n", info->reference);
988 printf(
"comment=%s\n", info->comment);
989 printf(
"titleFormat=%s\n", info->titleFormat);