/** * Name: * subFRPSDRemapping. * Purpose: * Calculate 9km FRP standard deviation * Inputs: * - column and row indexes of the 3km and 9km grids. * - 3km grid FRP standard deviation. * - 3km grid FRP quality. * Output: * - 9km grid FRP stndard deviation. * Procedure: * (1) Aggregate the FRP values of the 3km grids with valid value and medium * or high quality, which are centered at a given 13km grid. * (2) Count the number of the 3km grids with valid value and medium or * high quality. * (3) Average the aggregated 3km FRP value by the number of * grids. * (4) Calculate 9km FRP standard deviation using individual 3km-grid FRP value and * averaged 3km FRP value centered at a given 9km grid. * * @param remappingindexfile The input file that contains column and row indexes * for 3km and 9km grids. * @param var3 The input array that contains the 3km grid FRP. * @param qaf3 The input array that contains the 3km grid quality. * @param var9 The output array that stores the remapped 9km FRP standard * deviation. * */ int subFRPSDRemapping( char *remappingindexfile, float var3[], short int qaf3[], float var13[]) { // Declare variables. FILE *fp; char buffer[MAX_LEN]; int i, m; long int k; int nps; char *token; int i9, j9; // column index (i9) and row index (j9) for 9km data. int i3, j3; // column index (i3) and row index (j3) for 3km data. float nnp; float sum; // Initialize. for (i = 0; i < LAST_HOURS * grid_yt_9km * grid_xt_9km; i++) var9[i] = 0.0; // Open the grid index file. fp = fopen(remappingindexfile, "r"); if (fp == NULL) { perror("Failed: "); return 1; } // To execute a while-loop to calculate 9km FRP value from 3km grids centerd at the 9km grid. while (fgets(buffer, MAX_LEN, fp)) { // Remove trailing newline characters from end of string and replace it // with '\0'. buffer[strcspn(buffer, "\n")] = 0; // Extract 9km grid column and row indexes. token = strtok(buffer, ","); i9 = atoi(token); token = strtok(NULL, ","); j9 = grid_yt_9km - 1 - atoi(token); m = j9 * LAST_HOURS * grid_xt_9km + i9; // Extract the number of 3km grids centered at a given 9km grid. token = strtok(NULL, ","); nps = atoi(token); // Read lats/lons of four angle points for a given 9km grid. for (i = 0; i < 4; i++) fgets(buffer, MAX_LEN, fp); // Initialize. nnp = 0.0; sum = 0.0; // To execute a for-loop to calculate 9km grid FRP. for (i = 0; i < nps; i++) { // Extract 3km grid column and row indexes. fgets(buffer, MAX_LEN, fp); token = strtok(buffer, ","); token = strtok(NULL, ","); i3 = atoi(token); token = strtok(NULL, ","); j3 = atoi(token); k = j3 * LAST_HOURS * grid_xt_3km + i3; /* Aggregate 3km FRP values only for those grids with valid value and medium or high quality. */ if ((qaf3[k] == 2 || qaf3[k] == 3) && var3[k] >= 0.0) // { sum = sum + var3[k]; nnp = nnp + 1.0; } } // Calculate mean FRP value for 9km grid and temporarily store it to var9 var9[m] = -1.0; if (nnp >= 1.0) var9[m] = sum / nnp; } // Close the open file stream. fclose(fp); // Re-open the grid index file. fp = fopen(remappingindexfile, "r"); if (fp == NULL) { perror("Failed: "); return 1; while (fgets(buffer, MAX_LEN, fp)) { buffer[strcspn(buffer, "\n")] = 0; token = strtok(buffer, ","); i9 = atoi(token); token = strtok(NULL, ","); j9 = grid_yt_9km - 1 - atoi(token); m = j9 * LAST_HOURS * grid_xt_9km + i9; token = strtok(NULL, ","); nps = atoi(token); for (i = 0; i < 4; i++) fgets(buffer, MAX_LEN, fp); nnp = 0.0; sum = 0.0; for (i = 0; i < nps; i++) { fgets(buffer, MAX_LEN, fp); token = strtok(buffer, ","); token = strtok(NULL, ","); i3 = atoi(token); token = strtok(NULL, ","); j3 = atoi(token); k = j3 * LAST_HOURS * grid_xt_3km + i3; if ((qaf3[k] == 2 || qaf3[k] == 3) && var3[k] >= 0.0 && var9[m] >= 0.0) { sum = sum + pow (var3[k] - var9[m],2); nnp = nnp + 1.0; } } // Calculate the FRP standard deviation var9[m] = -1.0; if (nnp >= 1.0) var13[m] = sqrt (sum / nnp) ; } // Close the open file stream. fclose(fp); return 0; }