/** * Name: * subCFRemapping. * Purpose: * Calculate 9km grid cloud fraction (CF). * Inputs: * - column and row indexes for 3km and 9km grids. * - 3km grid cloud fraction. * - 3km grid area. * Output: * - 9km grid cloud fraction. * Procedure: * (1) Aggregate the product of CF times AREA for each 3km grid. * (2) Aggregate the AREA for each 3km grid. * (3) Average the aggregated 3km grid CF by the aggregated area. * * @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 cloud fraction. * @param area3 The input array that contains the 3km grid area. * @param var9 The output array that stores the remapped 9km land cover types. * */ int subCFRemapping(char *remappingindexfile, int var3[], float area3[], int var9[]) { // Declares 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 wa, wb, nnp; // Initialize var9. for (i = 0; i < LAST_HOURS * grid_yt_9km * grid_xt_9km; i++) var9[i] = 0; // Open the grid index file. fp = fopen(remappingindexfile, "r"); if (fp == NULL) { perror("Failed: "); return 1; } // To execute a while-loop to calculate Cloud Fractor grid by 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 3km grid column and row indexes. token = strtok(buffer, ","); // Break the string into a series of tokens. i9 = atoi(token); token = strtok(NULL, ","); // Reverse 'j' index order becasue the AQM-v8 model data have oppsite order j9 = grid_yt_9km - 1 - atoi(token); m = j9 * LAST_HOURS * grid_xt_9km + i9; // Get 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. wa = 0.0; wb = 0.0; nnp = 0.0; // To excute a for-loop to agrregate 3km grid area. 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; token = strtok(NULL, ","); // Aggregate variable values for those grids with valid values (>0). if (var3[k] > 0) { wa = wa + (float)var3[k] * area3[k]; // sum of the product of CF times area. wb = wb + area3[k]; // sum of area. nnp = nnp + 1.0; } } // Calculate 9km grid Cloud Fraction. var9[m] = -1; if (nnp >= 1.0) var9[m] = (int)(wa / wb); } // Close the open file stream. fclose(fp); return 0; }