/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the files COPYING and Copyright.html. COPYING can be found at the root * * of the source code distribution tree; Copyright.html can be found at the * * root level of an installed copy of the electronic HDF5 document set and * * is linked from the top-level documents page. It can also be found at * * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * This program creates a group in the file and two datasets in the group. * Hard link to the group object is created and one of the datasets is accessed * under new name. * Iterator functions are used to find information about the objects * in the root group and in the created group. */ #include #include #include "h5augjpss.h" #include H5aj_dim_t* parseDimension(xmlDocPtr doc, xmlNodePtr cur) { H5aj_dim_t* ret = NULL; H5aj_dim_t* curdim = NULL; const char* curstr = NULL; int bnd = 0; /* * allocate the struct */ ret = (H5aj_dim_t*) malloc(sizeof(H5aj_dim_t)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(H5aj_dim_t)); cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *)"Name")) ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *)"GranuleBoundary")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); bnd = atoi(curstr); ret->bound = &bnd; } if (!xmlStrcmp(cur->name, (const xmlChar *)"Dynamic")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->dynamic = atoi(curstr); } if (!xmlStrcmp(cur->name, (const xmlChar *)"MinIndex")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->minidx = atoi(curstr); } if (!xmlStrcmp(cur->name, (const xmlChar *)"MaxIndex")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->maxidx = atoi(curstr); } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no Dimension\n"); return(ret); } H5aj_fillval_t* parseFillValue(xmlDocPtr doc, xmlNodePtr cur) { H5aj_fillval_t* ret = NULL; const char* curstr = NULL; /* * allocate the struct */ ret = (H5aj_fillval_t*) malloc(sizeof(H5aj_fillval_t)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(H5aj_fillval_t)); cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *)"Name")) ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *)"Value")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->value = atof(curstr); } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no FillValue\n"); return(ret); } H5aj_legent_t* parseLegendEntry(xmlDocPtr doc, xmlNodePtr cur) { H5aj_legent_t* ret = NULL; const char* curstr = NULL; /* * allocate the struct */ ret = (H5aj_legent_t*) malloc(sizeof(H5aj_legent_t)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(H5aj_legent_t)); cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *)"Name")) ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *)"Value")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->value = atof(curstr); } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no LegendEntry\n"); return(ret); } H5aj_datum_t* parseDatum(xmlDocPtr doc, xmlNodePtr cur) { H5aj_datum_t* ret = NULL; H5aj_fillval_t* curfv = NULL; H5aj_legent_t* curle = NULL; const char* curstr = NULL; float range_value1 = 0.0; float range_value2 = 0.0; int childFillValueCount = 0; int childLegendEntryCount = 0; xmlNodePtr childrencount = NULL; /* * allocate the struct */ ret = (H5aj_datum_t*) malloc(sizeof(H5aj_datum_t)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(H5aj_datum_t)); childrencount = cur->xmlChildrenNode; while (childrencount != NULL) { if (!xmlStrcmp(childrencount->name, (const xmlChar *) "FillValue")) { childFillValueCount++; } if (!xmlStrcmp(childrencount->name, (const xmlChar *) "LegendEntry")) { childLegendEntryCount++; } childrencount = childrencount->next; } ret->fv = malloc(childFillValueCount*sizeof(H5aj_fillval_t)); ret->le = malloc(childLegendEntryCount*sizeof(H5aj_legent_t)); cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *)"Description")) ret->description = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *)"DatumOffset")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->datumoffset = atoi(curstr); } if (!xmlStrcmp(cur->name, (const xmlChar *)"Scaled")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->scaled = atoi(curstr); } if (!xmlStrcmp(cur->name, (const xmlChar *)"ScaleFactorName")) ret->sf_name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *)"MeasurementUnits")) ret->m_units = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *)"RangeMin")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); range_value1 = (float)atof(curstr); ret->rng_min = &range_value1; } if (!xmlStrcmp(cur->name, (const xmlChar *)"RangeMax")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); range_value2 = (float)atof(curstr); ret->rng_max = &range_value2; } if (!xmlStrcmp(cur->name, (const xmlChar *)"DataType")) ret->data_type_string = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *) "FillValue")) { curfv = parseFillValue(doc, cur); if (curfv != NULL) ret->fv[ret->n_fill_val++] = *curfv; if (ret->n_fill_val > childFillValueCount) break; } if (!xmlStrcmp(cur->name, (const xmlChar *) "LegendEntry")) { curle = parseLegendEntry(doc, cur); if (curle != NULL) ret->le[ret->n_legent++] = *curle; if (ret->n_legent > childLegendEntryCount) break; } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no Datum\n"); return(ret); } H5aj_data_size_t* parseDataSize(xmlDocPtr doc, xmlNodePtr cur) { H5aj_data_size_t* ret = NULL; xmlChar* curstr = NULL; /* * allocate the struct */ ret = (H5aj_data_size_t*) malloc(sizeof(H5aj_data_size_t)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(H5aj_data_size_t)); cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *)"Count")) { curstr = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); ret->count = atoi(curstr); } if (!xmlStrcmp(cur->name, (const xmlChar *)"Type")) { ret->type = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no DataSize\n"); return(ret); } H5aj_field_t* parseField(xmlDocPtr doc, xmlNodePtr cur) { H5aj_field_t* ret = NULL; H5aj_dim_t* curdim = NULL; H5aj_datum_t* curdatum = NULL; H5aj_data_size_t* curds = NULL; int childDimensionCount = 0; int childDatumCount = 0; xmlNodePtr childrencount = NULL; /* * allocate the struct */ ret = (H5aj_field_t*) malloc(sizeof(H5aj_field_t)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(H5aj_field_t)); childrencount = cur->xmlChildrenNode; while (childrencount != NULL) { if (!xmlStrcmp(childrencount->name, (const xmlChar *) "Dimension")) { childDimensionCount++; } if (!xmlStrcmp(childrencount->name, (const xmlChar *) "Datum")) { childDatumCount++; } childrencount = childrencount->next; } /* ret->dims = malloc(childDimensionCount * sizeof(H5aj_dim_t)); */ ret->datum = malloc(childDatumCount * sizeof(H5aj_datum_t)); cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *)"Name")) ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *) "Dimension")) { curdim = parseDimension(doc, cur); if (curdim != NULL) ret->dims[ret->n_dims++] = *curdim; if (ret->n_dims > H5AJ_MAX_DIMS) break; } if (!xmlStrcmp(cur->name, (const xmlChar *) "Datum")) { curdatum = parseDatum(doc, cur); if (curdatum != NULL) ret->datum[ret->n_datum++] = *curdatum; if (ret->n_datum > childDatumCount) break; } if (!xmlStrcmp(cur->name, (const xmlChar *) "DataSize")) { curds = parseDataSize(doc, cur); if (curds != NULL) ret->ds = *curds; } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no Field\n"); return(ret); } /* * The code needed to parse the ProductData */ productDataPtr parseProductData(xmlDocPtr doc, xmlNodePtr cur) { productDataPtr ret = NULL; H5aj_field_t* curfield = NULL; /* * allocate the struct */ ret = (productDataPtr) malloc(sizeof(productData)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); return(NULL); } memset(ret, 0, sizeof(productData)); /* We don't care what the top level element name is */ cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *) "DataName")) ret->dataName = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *) "Field")) { curfield = parseField(doc, cur); if (curfield != NULL) ret->fields[ret->nFields++] = curfield; if (ret->nFields > 50) break; } cur = cur->next; } if (ret==NULL) fprintf(stderr,"no ProductData\n"); return(ret); } /* * The code needed to parse a ProductName/DataProduct */ H5ajpdPtr parseJPSSFile(const char *filename) { xmlDocPtr doc; H5ajpdPtr ret; productDataPtr curH5ajpd; xmlNodePtr cur; /* * build an XML tree from a the file; */ doc = xmlParseFile(filename); if (doc == NULL) { fprintf(stderr,"cannot parse file\n"); return(NULL); } /* * Check the document is of the right kind */ cur = xmlDocGetRootElement(doc); if (cur == NULL) { fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return(NULL); } if (xmlStrcmp(cur->name, (const xmlChar *) "NPOESSDataProduct")) { fprintf(stderr,"document of the wrong type, root node != NPOESSDataProduct"); xmlFreeDoc(doc); return(NULL); } /* * Allocate the structure to be returned. */ ret = (H5ajpdPtr) malloc(sizeof(H5ajpd)); if (ret == NULL) { fprintf(stderr,"out of memory\n"); xmlFreeDoc(doc); return(NULL); } memset(ret, 0, sizeof(H5ajpd)); /* * Now, walk the tree. */ cur = cur->xmlChildrenNode; while (cur != NULL) { if (!xmlStrcmp(cur->name, (const xmlChar *) "ProductName")) ret->productName = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *) "CollectionShortName")) ret->collectionShortName = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *) "DataProductID")) ret->category = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); if (!xmlStrcmp(cur->name, (const xmlChar *) "ProductData")) { curH5ajpd = parseProductData(doc, cur); if (curH5ajpd != NULL) ret->productDatas[ret->nproductDatas++] = curH5ajpd; if (ret->nproductDatas > 3) break; } cur = cur->next; } return(ret); } H5ajpdPtr parseJPSSDataProductFile(const char *xmlfile) { int i; H5ajpdPtr cur; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION xmlKeepBlanksDefault(0); cur = parseJPSSFile(xmlfile); #if(0) if ( cur ) printJPSSDataProduct(cur); else fprintf( stderr, "Error parsing file '%s'\n", xmlfile); #endif /* Clean up everything else before quitting. */ xmlCleanupParser(); return(cur); }