#include #if 0 #define DEBUG 0 #endif /* Global variables definitions */ /* option flags & variables */ int ngranulesperfile=-1; /* -1 stands for not set. */ char **products_list=NULL; /* list of products requested */ int nproducts=0; char *outDir=NULL; char *origin_arg=NULL; char *domain_arg=NULL; char **inputfiles=NULL; /* list of input files */ int ninputfiles=0; /* number of input files in *inputfiles */ NPPFileName_t outputfiles[NAGG_outputfiles_max]; /* list of output files */ int noutputfiles=0; /* number of output files in outputfiles */ char *geoproduct=NULL; /* Geo-Product DPID */ geolocation_t geofiles_arg=GEOFILE_YES; /* Geolocation file is default to yes */ outfileformat_t outfileformat=PACKAGED; /* Output file format. Default is Packaged */ int debug_arg=0; /* debug mode. Default is off (0) */ /* Granule Information variables */ granule_p_t granule_info_array[NAGG_Granule_info_max]; /* array to hold products requested */ char *products_list_array[NAGG_Product_list_max]; /* array to hold a granule pattern for each product */ /* size is maximum number of products + 1 GEO product */ granule_pattern_p_t granule_pattern_array[NAGG_Product_list_max + 1]; const char *progname_g; /* program name */ /* local macro definitions */ /* Local variables definitions */ /* function definitions */ /* About to leave the tool. Do some necessary cleanup and exit with ret code */ void leave(int ret){ /* Cleanup */ /* Nothing yet */ /* quit */ exit(ret); } /* report what file is produced with how many granules in it. */ int nagg_report(const char *outfile, int ngranules) { /* sanity check */ assert(outfile); printf("Produced %3d granules in %s\n", ngranules, outfile); } /* Main body of the tool */ int main(int argc, char * const argv[]){ granule_p_t *granule_info; granule_p_t *sensor_p; /* sensor data granule */ granule_p_t *geo_p; /* Geolocation granule */ granule_p_t *next_p; /* Next granule in granule table*/ granule_p_t granules_selected[NAGG_Granules_selected_max]; /* table of */ /* selected granules to be written */ int total_granules_file; /* total granules per file */ int number_of_granules=0; int granules_remain; int gindex; /* index of granule_info */ char *geofilename; char Creation_date[Creation_date_size+1], *Creation_date_p; char file_create_date[Data_date_size+1]; /* null terminated */ char file_create_time[15]; /* HHMMSS.ssssssZ + null */ int i, j; int ret_code; int total_nproducts=0; /* number of products including separate geolocation file. */ /* initilization */ granule_info = NULL; noutputfiles=0; /* Turn off HDF5's automatic error printing. */ /* Should have an option to allow automatic error printing for debugging. */ if (H5Eset_auto2(H5E_DEFAULT, NULL, NULL) < 0){ fprintf(stderr, "%s: failed to turn off automatic error printing\n", progname_g); leave(EXIT_FAILURE); } /*=======================*/ /* parse command options */ /*=======================*/ if (0 != parse_options(argc, argv)) leave(EXIT_FAILURE); #if defined(DEBUG) printf("Options returned:\n"); printf("Program name=%s\n", progname_g); printf("nproducts=%d, ", nproducts); for (i=0; i= number_of_granules){ NAGG_ERROR("Get Granules returned illegal values\n"); fprintf(stderr, "granule_info=%x, number_of_granules=%d\n", granule_info, number_of_granules); leave(EXIT_FAILURE); } if(geoproduct) { total_nproducts = nproducts + 1; /* Add one extra for geolocation. */ } else { total_nproducts = nproducts; } #ifdef DEBUG printf("After return from nagg_get_granules, dumping granule_info:\n"); printf("number_of_granules=%d\n", number_of_granules); nagg_print_granules(granule_info, number_of_granules); printf("Dumping done\n"); #endif /* If debug is on, display all granules in the input files by printing the * granules table and leave with SUCCESS. */ if (get_option_debug){ nagg_print_granules(granule_info, number_of_granules); leave(EXIT_SUCCESS); } /*=============================*/ /* sort the granules info list */ /*=============================*/ nagg_sort_granules(granule_info, number_of_granules); #ifdef DEBUG printf("After return from nagg_sort_granules, dumping granule_info:\n"); printf("number_of_granules=%d\n", number_of_granules); nagg_print_granules(granule_info, number_of_granules); printf("Dumping done\n"); #endif /*=======================================================*/ /* select granules to write to file until all is written */ /*=======================================================*/ /* scan through the table of granules, collecting a set of ngranulesperfile * granules of each sensor product and Geolocation product. * Generate the output file names from this set of granules. * Send the granules to be written. * Repeat till all granules are processed. */ /* initial variables */ next_p = granule_info; sensor_p = granule_info; geo_p = granule_info; granules_remain = number_of_granules; gindex = 0; /* make sure granules_selected can hold all selected granules */ total_granules_file = ngranulesperfile*(total_nproducts); if (total_granules_file > NAGG_Granules_selected_max){ NAGG_ERROR("nagg main failed\n"); fprintf(stderr, "number of granules per file + geolocation granules " "per file (%d) is too large (max. %d)\n" "Reduce -n or -t\n", total_granules_file, NAGG_Granules_selected_max); leave(EXIT_FAILURE); }; while (granules_remain > 0) { #ifdef DEBUG printf("begin of while loop\n"); printf("granules_remain=%d, total_granules_file=%d, total_nproducts=%d, i=%d, " "j=%d, gindex=%d\n", granules_remain, total_granules_file, total_nproducts, i, j, gindex); #endif /*=======================================*/ /* get ngranulesperfile sets of granules */ /*=======================================*/ if (select_granules( granule_info, &gindex, products_list, nproducts, total_nproducts, geoproduct, granules_selected, ngranulesperfile, &granules_remain, &total_granules_file ) < 0){ NAGG_ERROR("select_granules failed\n"); leave(EXIT_FAILURE); } #ifdef DEBUG printf("granules_remain=%d, total_granules_file=%d, gindex=%d\n", granules_remain, total_granules_file, gindex); printf("After return from select_granules, dumping selected granules:\n"); printf("number_of_granules=%d\n", total_granules_file); nagg_print_granules(granules_selected, total_granules_file); printf("Dumping done\n"); #endif if (total_granules_file <=0){ /* nothing selected. Quit this while loop */ break; } /*================================*/ /* Compose the output files names */ /*================================*/ ret_code = compose_output_fname(granules_selected, total_granules_file, products_list, nproducts, geoproduct, ngranulesperfile, outfileformat, &noutputfiles, outputfiles, &geofilename, Creation_date); if (ret_code == FAIL) { NAGG_ERROR("compose_output_fname failed\n"); leave(EXIT_FAILURE); }; if (!noutputfiles && !geofilename){ /* No output nor geo files. This should not happen. */ NAGG_ERROR("compose_output_fname returned no output files.\n"); leave(EXIT_FAILURE); }; #ifdef DEBUG printf("compose_output_fname returned:\n"); printf("Creation_date=%s\n", Creation_date); for (i=0; i