#include "h5augjpss.h" #include "h5aj_private.h" #include #include #include #include /* Global variables definitions */ const char *xmlfilename_g = NULL; const char *datafilename_g = NULL; const char *hide_unknown_g = NULL; const char *update_from_xml_g = NULL; const char *update_geoloc_g = NULL; const char *flatten_g = NULL; const char *restore_links1_g = NULL; const char *restore_links4_g = NULL; const char *add_links_idv_g = NULL; void usage(void) { fprintf(stderr, "\n"); fprintf(stderr, "usage:\n"); fprintf(stderr, "%s [-h | --help]\n", progname_g); fprintf(stderr, "%s [-l | --level augmentation_level] [xmlfile] h5file\n", progname_g); fprintf(stderr, " (level 2 requires xmlfile\n"); fprintf(stderr, "%s [-r | --restore] h5file\n", progname_g); fprintf(stderr, "%s xmlfile h5file", progname_g); fprintf(stderr, " (default - invokes options --level 1 --level 2 --level 3)\n"); fprintf(stderr, "OPTIONS\n" \ " -h, --help\n" " Print this usage message and exit\n" " -l, --level augmentation_level <1 - 3>\n" " 1 remove objects unknown to netCDF-4\n" " 2 update h5file(s) with information from xmlfile \n" " 3 update h5file with Geo-location information\n" " 4 create links in \"/\" to datasets in /All_Data/ " " and remove /All_Data/subgroup structure. \n" " -r, --restore <1,4>\n" " 1 restore links to unknown objects in h5file(s)\n" " 4 remove links in \"/\" to datasets originally in /All_Data/ " " and restore the /All_Data/ structure. \n" " \n" " Options 1 - 4 combined hide all group structures in the file,\n" " allowing IDV and other tools to open the file when certain attributes\n" " are added (with h5edit or hdfview) to make the file CF compliant.\n" " \n" " \n", progname_g); fprintf(stderr, "\n"); } /* usage() */ void leave(int ret){ /* about to leave the tool. Do some necessary cleanup and then quit. */ /* close all opened files */ /*if (commfile_g != NULL) HDfclose(commfile_g);*/ /*if (datafile_g >= 0) closedatafile(datafile_g); */ /* quit */ exit(ret); } /* Parse command line options. * Return if all is okay; * otherwise, print error message and leave(EXIT_FAILURE) * which does not return. * * */ void parse_options(int argc, const char **argv) { int argno; const char *s = NULL; const char *default_action = "enabled"; /* parse in options */ for (argno = 1; argno < (argc - 1) && '-' == argv[argno][0]; argno++){ if (streq(argv[argno], "--help") || streq(argv[argno], "-h")){ default_action = NULL; usage(); }else if(streq(argv[argno], "--level") || streq(argv[argno], "-l")){ if(streq(argv[++argno], "1")) { default_action = NULL; hide_unknown_g = "yes"; }else if(streq(argv[argno], "2")) { default_action = NULL; update_from_xml_g = "yes"; }else if(streq(argv[argno], "3")) { default_action = NULL; update_geoloc_g = "yes"; }else if(streq(argv[argno], "4")) { default_action = NULL; flatten_g = "yes"; } }else if(streq(argv[argno], "--restore") || streq(argv[argno], "-r")) { if(streq(argv[++argno], "1")) { default_action = NULL; restore_links1_g = "yes"; }else if(streq(argv[argno], "4")) { default_action = NULL; restore_links4_g = "yes"; } }else if(streq(argv[argno], "--tool") || streq(argv[argno], "-t")) { default_action = NULL; if(streq(argv[++argno], "IDV")) { add_links_idv_g="yes"; } } } if(default_action) { hide_unknown_g = "yes"; update_from_xml_g = "yes"; update_geoloc_g = "yes"; } if (argno >= argc) { fprintf(stderr, "%s: missing datafile argument(s)\n", progname_g); usage(); leave(EXIT_FAILURE); } /* check for xml file - last file must be hdf5 datafile. If 2 args remaining, * assume next is xml file. */ if (argno < (argc - 1)) { xmlfilename_g = argv[argno++]; /* Check to see that file exists and ends in .xml */ if (access(xmlfilename_g, R_OK) == -1) { printf("Can't access %s.\n", xmlfilename_g); leave(EXIT_FAILURE); } } if (update_from_xml_g && !xmlfilename_g) { fprintf(stderr, "\nError: level 2 requires xmlfile.\n"); usage(); leave(EXIT_FAILURE); } datafilename_g = argv[argno++]; } #define RANK 1 int h5aj_augment_file() { hid_t file = (-1), group = (-1), rt_group = (-1); /* Handles */ herr_t status; int i, j; H5ajpdPtr h5ajpdptr; char *level_2_executed = NULL; char *group_with_reference_types = "/Data_Products"; char subgroup_name[1024]; if(hide_unknown_g || update_from_xml_g || update_geoloc_g || flatten_g) { fprintf(stderr, "Open %s.\n", datafilename_g); file = H5Fopen(datafilename_g, H5F_ACC_RDWR, H5P_DEFAULT); rt_group = H5Gopen(file, "/", H5P_DEFAULT); if(hide_unknown_g) { if (0 < H5Lexists(file, group_with_reference_types, H5P_DEFAULT)) { printf("Disconnect Data_products group.\n"); h5aj_disconnect_group(file, group_with_reference_types); } else { printf("%s not found (already disconnected?)\n", group_with_reference_types); /* could check for name and address attributes */ } } if (update_from_xml_g || update_geoloc_g || flatten_g) { if ( 0 >= ( status = H5Lexists(rt_group, "All_Data", H5P_DEFAULT))) { printf("All_Data group can't be found (previously disconnected by executing option\n"); printf("--level 4?) To run levels 2 or 3, first run option --restore 4 to restore\n"); printf("the /All_Data group structure. It can later be rehidden with option --level 4.\n"); goto done; } else { status = h5aj_get_All_Data_sole_subgroup(file, &group, subgroup_name); if (status < 0) { printf("No child group of All_Data found.\n"); goto done; } } } if(update_from_xml_g) { fprintf(stderr, "Parse xml file %s.\n", xmlfilename_g); h5ajpdptr = parseJPSSDataProductFile(xmlfilename_g); if(h5ajpdptr) { if(strncmp(h5ajpdptr->collectionShortName, subgroup_name , strlen(h5ajpdptr->collectionShortName)) != 0) { fprintf(stderr, "Product profile's collection short name %s doesn't correspond to All_Data subgroup name %s.", h5ajpdptr->collectionShortName, subgroup_name); goto done; } for (i = 0; i < h5ajpdptr->nproductDatas; i++) { if (h5ajpdptr->productDatas[i] == NULL) return; fprintf(stderr, "Process fields.\n"); for (j = 0; j < h5ajpdptr->productDatas[i]->nFields; ++j) { h5aj_add_dimension_scales(group, h5ajpdptr->productDatas[i]->fields[j]); h5aj_add_attributes_for_datums(group, h5ajpdptr->productDatas[i]->fields[j]); } } } } if(update_geoloc_g) { fprintf(stderr, "Add geolocation datasets.\n"); h5aj_add_geolocation_datasets(file, group, datafilename_g); } if(flatten_g) { h5aj_link_group_objects(file, group); } done: if(group > 0) status = H5Gclose(group); if(rt_group > 0) status = H5Gclose(rt_group); if(file> 0) status = H5Fclose (file); } else if (restore_links1_g) { fprintf(stderr, "Open %s.\n", datafilename_g); file = H5Fopen(datafilename_g, H5F_ACC_RDWR, H5P_DEFAULT); h5aj_restore_group_link(file, "Data_Products"); if(file > 0) status = H5Fclose(file); } else if (restore_links4_g) { fprintf(stderr, "Open %s.\n", datafilename_g); file = H5Fopen(datafilename_g, H5F_ACC_RDWR, H5P_DEFAULT); h5aj_restore_group_link(file, "All_Data"); status = h5aj_get_All_Data_sole_subgroup(file, &group, subgroup_name); if (status < 0) { printf("No child group of All_Data found.\n"); } else { h5aj_remove_links_to_group_objects(file, group); } if(group > 0) status = H5Gclose(group); if(file > 0) status = H5Fclose(file); } return 0; }