/* Name- copyMirsDataToOutputFile.c Language- C Type- MAIN Version- 1.0 Date- 2/28/2026 Programmer- Mike Pettey (STC) Function- This program extracts data from NUCAPS granules and collocates the data to selected collocations times and locations. */ #include #include #include #include #include #include #include #include #include "uthash.h" #include "mirs_collocator.h" int copyMirsSNDToOutputFile(struct collocation_data *collocations, int num_collocations, char *file_name, size_t file_size, void* nc_buffer, int out_id); int copyMirsIMGToOutputFile(struct collocation_data *collocations, int num_collocations, char *file_name, size_t file_size, void* nc_buffer, int out_id); int copyMirsSFRToOutputFile(struct collocation_data *collocations, int num_collocations, char *file_name, size_t file_size, void* nc_buffer, int out_id); void copyGranuleName(char *granule_name, int out_id, int out_index, int out_var_id); long convertDateAndTimeToEpochTime(int date, int time); void convertEpochTimeToDateAndTime(double epoch_time, int *yr, int *md, int *hr, int *ms); void interpolateProfileLogLinear(int num_before_levels, float *before_pressures, float *before_data, int num_after_levels, float *after_pressures, float *after_data); void interpolateProfileLogLog(int num_before_levels, float *before_pressures, float *before_data, int num_after_levels, float *after_pressures, float *after_data); void interpolateHydrometeorProfile(int num_before_levels, float *before_pressures, float *before_data, int num_after_levels, float *after_pressures, float *after_data); void convertFromLevelsToLayers(int num_before_levels, float *before_pressures, float *before_data, int num_after_levels, float *after_pressures, float *after_data); int fileStartsWith(char *file_name, char *token); int fileEndsWith(char *file_name, char *token); int copyMirsDataToOutputFile(struct collocation_data *collocations, int num_collocations, struct FileEntry *collocated_files_hash, char *tarball_name, int out_id) { int num_collocations_copied; char file_name[500]; struct FileEntry *s; struct archive *a; struct archive_entry *entry; int r; num_collocations_copied = 0; printf("\nCopying MIRS data to the output file\n"); // Loop through the tarball to find the closest SND footprints a = archive_read_new(); archive_read_support_filter_gzip(a); archive_read_support_format_tar(a); r = archive_read_open_filename(a, tarball_name, 10240); if (r != ARCHIVE_OK) { printf("Error opening tarball for Pass 1: %s\n", tarball_name); archive_read_free(a); //exit(1); return 0; } while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { const char *internal_filename = archive_entry_pathname(entry); size_t file_size = archive_entry_size(entry); sprintf(file_name, "%s", internal_filename); // If the file is in the hash table of collocated files, then process it HASH_FIND_STR(collocated_files_hash, file_name, s); if (s != NULL) { //printf(" File name: %s\n", file_name); // Allocate memory into which the file will be read file_size = archive_entry_size(entry); void *nc_buffer = malloc(file_size); if (nc_buffer != NULL) { // Read the file into the allocated memory archive_read_data(a, nc_buffer, file_size); if ((fileStartsWith(file_name, "NPR-MIRS-SND") == TRUE) && (fileEndsWith(file_name, ".nc") == TRUE)) { //printf("Processing SND file: %s\n", file_name); num_collocations_copied = copyMirsSNDToOutputFile(collocations, num_collocations, file_name, file_size, nc_buffer, out_id); } if ((fileStartsWith(file_name, "NPR-MIRS-IMG") == TRUE) && (fileEndsWith(file_name, ".nc") == TRUE)) { //printf("Processing IMG file: %s\n", file_name); num_collocations_copied = copyMirsIMGToOutputFile(collocations, num_collocations, file_name, file_size, nc_buffer, out_id); } if ((fileStartsWith(file_name, "SFR_") == TRUE) && (fileEndsWith(file_name, ".nc") == TRUE)) { //printf("Processing SFR file: %s\n", file_name); num_collocations_copied = copyMirsSFRToOutputFile(collocations, num_collocations, file_name, file_size, nc_buffer, out_id); } // Free the memory that the file was read into free(nc_buffer); } // if (nc_buffer != NULL... } // if (s != NULL... } // while (archive_read_next_header... return num_collocations_copied; } // end of file