/* Name- processGOES.c Language- C Type- MAIN Version- 1.0 Date- 4/10/2017 Programmer- Mike Pettey (IMSG) Function- This program extracts data from P72 EDR files in HDF5 format and writes the data to an EDGE orbital file. The selected data is defined in the run script. Also in the run script are the starting date (YYYYMMDD) and hour and the ending date and hour. */ #include #include #include #include #include #include #include "nprovs_to_netcdf.h" #define ERR(e) {printf("Error: %s, error num = %d\n", nc_strerror(e),e);} #define NDIMS 1 int defineVariable(int nc_group_id, char *var_name, nc_type var_type, int ndims, int *dimids, char *attr_string, char *attr); void writeVariableFloat(int group_id, int var_id, size_t *index, size_t *num_vals, float value); void writeVariableShort(int group_id, int var_id, size_t *index, size_t *num_vals, short value); void writeVariableInteger(int group_id, int var_id, size_t *index, size_t *num_vals, long value); void writeArrayFloat(int group_id, int var_id, size_t *index, size_t *num_vals, float *value); void writeVariableByte(int group_id, int var_id, size_t *index, size_t *num_vals, char value); void writeAttributeShort(int grp, char *attrVariable, short attrValue); void writeAttributeText(int grp, char *attrVariable, char *attrValue); void processGOES(int date_to_process, struct file_data *files, struct platform *platform_ptr, int num_collocations, int platform_index, int data_type, int nc_date_group) { int n, i, date_type, recnum, collocation_num, retval; int nc_group_id, collocation_group_id; size_t num_colls, index[1], num_vals[1], index_2D[2], num_vals_2D[2]; float missing_float, latitude, longitude, level_values[40], channel_data[18]; int year, month, day, hour, minute, second; long offset, yyyymmdd, hhmmss; char col_dir_name[50]; char *string[1]; short *buffer; int col_scalar_dim; int scalar_dim, dim_levels, dim_channels; int dimid_scalar[1], col_dimid_scalar[1]; int dimid_levels[2], dimid_channels[2]; int col_vid_lat, col_vid_lon, col_vid_date, col_vid_time; int vid_lat, vid_lon, vid_date, vid_time; int vid_sat, vid_modflag, vid_serialid, vid_cflag1, vid_cflag2, vid_cflag3; int vid_qflag1, vid_qflag2, vid_skinflag, vid_recflag, vid_detnum; int vid_rettype, vid_bireflect, vid_solzen, vid_satzen; int vid_cape, vid_li, vid_fgli, vid_skintemp, vid_tpw, vid_fgtpw; int vid_pw1, vid_pw2, vid_pw3, vid_flux; int vid_surfpress, vid_surftemp, vid_surfdwpt, vid_surfheight, vid_fgsurftemp, vid_fgsurfdwpt; int vid_press, vid_temp, vid_fgtemp, vid_dwpt, vid_fgdwpt, vid_heights; int vid_tbo, vid_tbb, vid_tbc, vid_tbr; FILE *in; num_colls = (size_t)num_collocations; missing_float = -32768.0; // If this is the baseline platform, then create another subgroup // for the collocation information if (platform_index == 0) { sprintf(col_dir_name, "Collocation_Info"); if ((retval = nc_def_grp(nc_date_group, col_dir_name, &collocation_group_id))) { if (retval != -42) ERR(retval); } writeAttributeShort(collocation_group_id, "Baseline_Data_Type", platform_ptr->type); writeAttributeText(collocation_group_id, "Baseline_Platform", platform_ptr->description); writeAttributeShort(collocation_group_id, "Baseline_Platform_ID", platform_ptr->id); } // Create a subgroup for this system platform if ((retval = nc_def_grp(nc_date_group, platform_ptr->dir_name, &nc_group_id))) { //if (retval != -42) ERR(retval); } // Set the platform type attribute to the data group type writeAttributeText(nc_group_id, "Platform_Name", platform_ptr->description); // clean_description? writeAttributeShort(nc_group_id, "Platform_ID", platform_ptr->id); writeAttributeShort(nc_group_id, "Platform_Type", platform_ptr->type); writeAttributeText(nc_group_id, "Platform_Data_Source", "GOES"); //writeAttributeText(nc_group_id, "Platform_NPROVS_Source_Name", nprovs_name); writeAttributeText(nc_group_id, "Platform_NPROVS_Source_Name", platform_ptr->description); // clean_description? // Define the dimensions if ((retval = nc_def_dim(nc_group_id, "Num_Collocations", num_colls, &scalar_dim))) ERR(retval); dimid_scalar[0] = scalar_dim; if ((retval = nc_def_dim(nc_group_id, "Levels", 40, &dim_levels))) ERR(retval); dimid_levels[0] = scalar_dim; dimid_levels[1] = dim_levels; if ((retval = nc_def_dim(nc_group_id, "Channels", 18, &dim_channels))) ERR(retval); dimid_channels[0] = scalar_dim; dimid_channels[1] = dim_channels; if (platform_index == 0) { if ((retval = nc_def_dim(collocation_group_id, "Num_Collocations", num_colls, &col_scalar_dim))) ERR(retval); col_dimid_scalar[0] = col_scalar_dim; } // Define the variables if (platform_index == 0) { col_vid_lat = defineVariable(collocation_group_id, "latitude", NC_FLOAT, 1, col_dimid_scalar, "units", "degrees_north"); col_vid_lon = defineVariable(collocation_group_id, "longitude", NC_FLOAT, 1, col_dimid_scalar, "units", "degrees_east"); col_vid_date = defineVariable(collocation_group_id, "date", NC_INT, 1, col_dimid_scalar, "format", "yyyymmdd"); col_vid_time = defineVariable(collocation_group_id, "time", NC_INT, 1, col_dimid_scalar, "format", "hhmmss"); } vid_lat = defineVariable(nc_group_id, "latitude", NC_FLOAT, 1, dimid_scalar, "units", "degrees_north"); vid_lon = defineVariable(nc_group_id, "longitude", NC_FLOAT, 1, dimid_scalar, "units", "degrees_east"); vid_date = defineVariable(nc_group_id, "date", NC_INT, 1, dimid_scalar, "format", "yyyymmdd"); vid_time = defineVariable(nc_group_id, "time", NC_INT, 1, dimid_scalar, "format", "hhmmss"); vid_sat = defineVariable(nc_group_id, "satellite", NC_BYTE, 1, dimid_scalar, "values", "1=east, 2=west"); vid_modflag = defineVariable(nc_group_id, "user_modification_flag", NC_BYTE, 1, dimid_scalar, NULL, NULL); vid_serialid = defineVariable(nc_group_id, "serial_id", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_cflag1 = defineVariable(nc_group_id, "channel_flags_1_to_6", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_cflag2 = defineVariable(nc_group_id, "channel_flags_7_to_12", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_cflag3 = defineVariable(nc_group_id, "channel_flags_13_to_18", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_qflag1 = defineVariable(nc_group_id, "quality_test_flags_1_to_5", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_qflag2 = defineVariable(nc_group_id, "quality_test_flags_6_to_10", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_skinflag = defineVariable(nc_group_id, "skin_temperature_channel_selection_flag", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_recflag = defineVariable(nc_group_id, "recursive_filter_flag", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_detnum = defineVariable(nc_group_id, "detector_number", NC_BYTE, 1, dimid_scalar, NULL, NULL); vid_rettype = defineVariable(nc_group_id, "retrieval_type", NC_SHORT, 1, dimid_scalar, NULL, NULL); vid_bireflect = defineVariable(nc_group_id, "bi_directional_reflectance", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_solzen = defineVariable(nc_group_id, "solar_zenith_angle", NC_FLOAT, 1, dimid_scalar, "units", "degrees"); vid_satzen = defineVariable(nc_group_id, "satellite_zenith_angle", NC_FLOAT, 1, dimid_scalar, "units", "degrees"); vid_cape = defineVariable(nc_group_id, "convective_available_potential_energy", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_li = defineVariable(nc_group_id, "lifted_index", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_fgli = defineVariable(nc_group_id, "first_guess_lifted_index", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_skintemp = defineVariable(nc_group_id, "skin_temperature", NC_FLOAT, 1, dimid_scalar, "units", "K"); vid_tpw = defineVariable(nc_group_id, "total_precipitable_water", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_fgtpw = defineVariable(nc_group_id, "first_guess_total_precipitable_water", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_pw1 = defineVariable(nc_group_id, "precipitable_water_1000_to_900hPa", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_pw2 = defineVariable(nc_group_id, "precipitable_water_900_to_700hPa", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_pw3 = defineVariable(nc_group_id, "precipitable_water_700_to_300hPa", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_flux = defineVariable(nc_group_id, "long_wave_flux", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_surfpress = defineVariable(nc_group_id, "surface_pressure", NC_FLOAT, 1, dimid_scalar, "units", "hPa"); vid_surftemp = defineVariable(nc_group_id, "surface_temperature", NC_FLOAT, 1, dimid_scalar, "units", "K"); vid_surfdwpt = defineVariable(nc_group_id, "surface_dewpoint_temperature", NC_FLOAT, 1, dimid_scalar, "units", "K"); vid_surfheight = defineVariable(nc_group_id, "surface_geopotential_height", NC_FLOAT, 1, dimid_scalar, NULL, NULL); vid_fgsurftemp = defineVariable(nc_group_id, "first_guess_surface_temperature", NC_FLOAT, 1, dimid_scalar, "units", "K"); vid_fgsurfdwpt = defineVariable(nc_group_id, "first_guess_surface_dewpoint_temperature", NC_FLOAT, 1, dimid_scalar, "units", "K"); vid_press = defineVariable(nc_group_id, "pressure", NC_FLOAT, 2, dimid_levels, "units", "hPa"); vid_temp = defineVariable(nc_group_id, "temperature", NC_FLOAT, 2, dimid_levels, "units", "K"); vid_fgtemp = defineVariable(nc_group_id, "first_guess_temperature", NC_FLOAT, 2, dimid_levels, "units", "K"); vid_dwpt = defineVariable(nc_group_id, "dewpoint_temperature", NC_FLOAT, 2, dimid_levels, "units", "K"); vid_fgdwpt = defineVariable(nc_group_id, "first_guess_dewpoint_temperature", NC_FLOAT, 2, dimid_levels, "units", "K"); vid_heights = defineVariable(nc_group_id, "geopotential_heights", NC_FLOAT, 2, dimid_levels, NULL, NULL); vid_tbo = defineVariable(nc_group_id, "observed_brightness_temperature", NC_FLOAT, 2, dimid_channels, "units", "K"); vid_tbb = defineVariable(nc_group_id, "bias_corrected_brightness_temperature", NC_FLOAT, 2, dimid_channels, "units", "K"); vid_tbc = defineVariable(nc_group_id, "first_guess_brightness_temperature", NC_FLOAT, 2, dimid_channels, "units", "K"); vid_tbr = defineVariable(nc_group_id, "sounding_brightness_temperature", NC_FLOAT, 2, dimid_channels, "units", "K"); // Allocate the memory used by the data buffer buffer = (short *)malloc(platform_ptr->length); collocation_num = -1; // Loop through each record in all of the files and read the data for // this data group into the buffer for (date_type=0; date_type<3; date_type++) { if (date_type == 0) { if ((in=fopen("prev_day.file", "r")) == NULL) { printf("\n\nThe previous day file could not be opened for input.\n"); printf("It will be skipped.\n\n"); in = NULL; } } else if (date_type == 1) { if ((in=fopen("in.file", "r")) == NULL) { printf("\n\nThe input file could not be opened for input.\n"); printf("It will be skipped.\n\n"); in = NULL; } } else { if ((in=fopen("next_day.file", "r")) == NULL) { printf("\n\nThe next day file could not be opened for input.\n"); printf("It will be skipped.\n\n"); in = NULL; } } for (recnum=0; recnumoffset; fseek(in, offset, SEEK_SET); fread(buffer, platform_ptr->length, 1, in); // Process this record if it matches the date to process if (files[date_type].use_record[recnum] == TRUE) { collocation_num++; // Index and num_vals are used to save a single value within the // variable array index[0] = collocation_num; num_vals[0] = 1; // Latitude and longitude if (buffer[3] != -32768) latitude = buffer[3] / 128.0; else latitude = -32768.0; if (buffer[4] != -32768) longitude = buffer[4] / 128.0; else longitude = -32768.0; writeVariableFloat(nc_group_id, vid_lat, index, num_vals, latitude); writeVariableFloat(nc_group_id, vid_lon, index, num_vals, longitude); // Date and time if ((buffer[5] != -32768) && (buffer[6] != -32768) && (buffer[7] != -32768) && (buffer[8] != -32768)) { year = buffer[5]; month = buffer[6] / 100; day = buffer[6] % 100; hour = buffer[7]; minute = buffer[8] / 100; second = buffer[8] % 100; yyyymmdd = (year * 10000) + (month * 100) + day; hhmmss = (hour * 10000) + (minute * 100) + second; } else { yyyymmdd = -32768; hhmmss = -32768; } writeVariableInteger(nc_group_id, vid_date, index, num_vals, yyyymmdd); writeVariableInteger(nc_group_id, vid_time, index, num_vals, hhmmss); // If this is the baseline system, save the collocation information if (platform_index == 0) { writeVariableFloat(collocation_group_id, col_vid_lat, index, num_vals, latitude); writeVariableFloat(collocation_group_id, col_vid_lon, index, num_vals, longitude); writeVariableInteger(collocation_group_id, col_vid_date, index, num_vals, yyyymmdd); writeVariableInteger(collocation_group_id, col_vid_time, index, num_vals, hhmmss); } // Save the user modification flag and serial id if (buffer[397] != -32768) writeVariableByte(nc_group_id, vid_sat, index, num_vals, (char)buffer[397]); else writeVariableByte(nc_group_id, vid_sat, index, num_vals, (char)-128); if (buffer[1] != -32768) writeVariableByte(nc_group_id, vid_modflag, index, num_vals, (char)buffer[1]); else writeVariableByte(nc_group_id, vid_modflag, index, num_vals, (char)-128); writeVariableShort(nc_group_id, vid_serialid, index, num_vals, buffer[2]); writeVariableShort(nc_group_id, vid_cflag1, index, num_vals, buffer[9]); writeVariableShort(nc_group_id, vid_cflag2, index, num_vals, buffer[10]); writeVariableShort(nc_group_id, vid_cflag3, index, num_vals, buffer[11]); writeVariableShort(nc_group_id, vid_qflag1, index, num_vals, buffer[12]); writeVariableShort(nc_group_id, vid_qflag2, index, num_vals, buffer[13]); writeVariableShort(nc_group_id, vid_skinflag, index, num_vals, buffer[14]); writeVariableShort(nc_group_id, vid_recflag, index, num_vals, buffer[15]); if (buffer[16] != -32768) writeVariableByte(nc_group_id, vid_detnum, index, num_vals, (char)buffer[16]); else writeVariableByte(nc_group_id, vid_detnum, index, num_vals, (char)-128); // Retrieval type writeVariableShort(nc_group_id, vid_rettype, index, num_vals, buffer[95]); if (buffer[89] != -32768) writeVariableFloat(nc_group_id, vid_bireflect, index, num_vals, (buffer[89]/100.0)); else writeVariableFloat(nc_group_id, vid_bireflect, index, num_vals, -32768.0); if (buffer[90] != -32768) writeVariableFloat(nc_group_id, vid_solzen, index, num_vals, (buffer[90]/100.0)); else writeVariableFloat(nc_group_id, vid_solzen, index, num_vals, -32768.0); if (buffer[91] != -32768) writeVariableFloat(nc_group_id, vid_satzen, index, num_vals, (buffer[91]/100.0)); else writeVariableFloat(nc_group_id, vid_satzen, index, num_vals, -32768.0); // Cape if (buffer[93] != -32768) writeVariableFloat(nc_group_id, vid_cape, index, num_vals, (buffer[93]/1.0)); else writeVariableFloat(nc_group_id, vid_cape, index, num_vals, -32768.0); if (buffer[98] != -32768) writeVariableFloat(nc_group_id, vid_li, index, num_vals, (buffer[98]/100.0)); else writeVariableFloat(nc_group_id, vid_li, index, num_vals, -32768.0); if (buffer[100] != -32768) writeVariableFloat(nc_group_id, vid_fgli, index, num_vals, (buffer[100]/100.0)); else writeVariableFloat(nc_group_id, vid_fgli, index, num_vals, -32768.0); // Skin temperature if (buffer[96] != -32768) writeVariableFloat(nc_group_id, vid_skintemp, index, num_vals, (buffer[96]/100.0)); else writeVariableFloat(nc_group_id, vid_skintemp, index, num_vals, -32768.0); if (buffer[97] != -32768) writeVariableFloat(nc_group_id, vid_tpw, index, num_vals, (buffer[97]/100.0)); else writeVariableFloat(nc_group_id, vid_tpw, index, num_vals, -32768.0); if (buffer[99] != -32768) writeVariableFloat(nc_group_id, vid_fgtpw, index, num_vals, (buffer[99]/100.0)); else writeVariableFloat(nc_group_id, vid_fgtpw, index, num_vals, -32768.0); if (buffer[101] != -32768) writeVariableFloat(nc_group_id, vid_pw1, index, num_vals, (buffer[101]/100.0)); else writeVariableFloat(nc_group_id, vid_pw1, index, num_vals, -32768.0); if (buffer[102] != -32768) writeVariableFloat(nc_group_id, vid_pw2, index, num_vals, (buffer[102]/100.0)); else writeVariableFloat(nc_group_id, vid_pw2, index, num_vals, -32768.0); if (buffer[103] != -32768) writeVariableFloat(nc_group_id, vid_pw3, index, num_vals, (buffer[103]/100.0)); else writeVariableFloat(nc_group_id, vid_pw3, index, num_vals, -32768.0); if (buffer[104] != -32768) writeVariableFloat(nc_group_id, vid_flux, index, num_vals, (buffer[104]/100.0)); else writeVariableFloat(nc_group_id, vid_flux, index, num_vals, -32768.0); // Surface values if (buffer[115] != -32768) writeVariableFloat(nc_group_id, vid_surfpress, index, num_vals, (buffer[115]/10.0)); else writeVariableFloat(nc_group_id, vid_surfpress, index, num_vals, -32768.0); if (buffer[156] != -32768) writeVariableFloat(nc_group_id, vid_surftemp, index, num_vals, (buffer[156]/100.0)); else writeVariableFloat(nc_group_id, vid_surftemp, index, num_vals, -32768.0); if (buffer[197] != -32768) writeVariableFloat(nc_group_id, vid_surfdwpt, index, num_vals, (buffer[197]/100.0)); else writeVariableFloat(nc_group_id, vid_surfdwpt, index, num_vals, -32768.0); if (buffer[238] != -32768) writeVariableFloat(nc_group_id, vid_surfheight, index, num_vals, (buffer[238]/1.0)); else writeVariableFloat(nc_group_id, vid_surfheight, index, num_vals, -32768.0); if (buffer[279] != -32768) writeVariableFloat(nc_group_id, vid_fgsurftemp, index, num_vals, (buffer[279]/100.0)); else writeVariableFloat(nc_group_id, vid_fgsurftemp, index, num_vals, -32768.0); if (buffer[320] != -32768) writeVariableFloat(nc_group_id, vid_fgsurfdwpt, index, num_vals, (buffer[320]/100.0)); else writeVariableFloat(nc_group_id, vid_fgsurfdwpt, index, num_vals, -32768.0); // Pressures. Normally, the word number will be one less than the value listed in the // file format due to the arrays starting at 0 in C. But the first value in the 41 // value array is actually the surface value and not part of the profile data. So, // in this case, the value used will be increased by one (116+n instead of 115+nn). // This applies to all of the 41 values arrays listed in the file format. Also, // this GOES profiles are listed from the bottom of the atmosphere to the top. Since // this is the opposite of every other satellite platform, they are reversed here in // order to create fewer headaches later. index_2D[0] = collocation_num; index_2D[1] = 0; num_vals_2D[0] = 1; num_vals_2D[1] = 40; for (n=0; n<40; n++) { if (buffer[116+n] != -32768) level_values[39-n] = buffer[116+n] / 10.0; else level_values[39-n] = -32768.0; } writeArrayFloat(nc_group_id, vid_press, index_2D, num_vals_2D, level_values); // Temperatures for (n=0; n<40; n++) { if (buffer[157+n] != -32768) level_values[39-n] = buffer[157+n] / 100.0; else level_values[39-n] = -32768.0; } writeArrayFloat(nc_group_id, vid_temp, index_2D, num_vals_2D, level_values); for (n=0; n<40; n++) { if (buffer[280+n] != -32768) level_values[39-n] = buffer[280+n] / 100.0; else level_values[39-n] = -32768.0; } writeArrayFloat(nc_group_id, vid_fgtemp, index_2D, num_vals_2D, level_values); // Dewpoint temperatures for (n=0; n<40; n++) { if ((buffer[198+n] == -32768) || (buffer[198+n] < 0)) level_values[39-n] = -32768.0; else level_values[39-n] = buffer[198+n] / 100.0; } writeArrayFloat(nc_group_id, vid_dwpt, index_2D, num_vals_2D, level_values); for (n=0; n<40; n++) { if (buffer[321+n] != -32768) level_values[39-n] = buffer[321+n] / 100.0; else level_values[39-n] = -32768.0; } writeArrayFloat(nc_group_id, vid_fgdwpt, index_2D, num_vals_2D, level_values); // Geopotential heights for (n=0; n<40; n++) { if (buffer[239+n] != -32768) level_values[39-n] = buffer[239+n] / 1.0; else level_values[39-n] = -32768.0; } writeArrayFloat(nc_group_id, vid_heights, index_2D, num_vals_2D, level_values); // Brightess temperatures index_2D[0] = collocation_num; index_2D[1] = 0; num_vals_2D[0] = 1; num_vals_2D[1] = 18; for (n=0; n<18; n++) { if (buffer[17+n] != -32768) channel_data[n] = buffer[17+n] / 100.0; else channel_data[n] = -32768.0; } writeArrayFloat(nc_group_id, vid_tbo, index_2D, num_vals_2D, channel_data); for (n=0; n<18; n++) { if (buffer[35+n] != -32768) channel_data[n] = buffer[35+n] / 100.0; else channel_data[n] = -32768.0; } writeArrayFloat(nc_group_id, vid_tbb, index_2D, num_vals_2D, channel_data); for (n=0; n<18; n++) { if (buffer[53+n] != -32768) channel_data[n] = buffer[53+n] / 100.0; else channel_data[n] = -32768.0; } writeArrayFloat(nc_group_id, vid_tbc, index_2D, num_vals_2D, channel_data); for (n=0; n<18; n++) { if (buffer[71+n] != -32768) channel_data[n] = buffer[71+n] / 100.0; else channel_data[n] = -32768.0; } writeArrayFloat(nc_group_id, vid_tbr, index_2D, num_vals_2D, channel_data); } // if (yyyymmdd == date_to_process... } // for (recnum=0... if (in != NULL) fclose(in); } // for (date_type=0... // Free the memory used by the buffer free(buffer); } // end of file