/** * Name: * landCoverInterPolate. * Purpose: * To remap 3km land cover type to 9km. * Procedure: * Search the majority type of the 3km grids centered at the 9km grid * and assign it to the 9km grid. * Inputs: * - column and row indexes for 3km and 9km grids. * - 3km grid land cover types. * Output: * - 9km grid land cover types. * * @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 land cover types. * @param var9 The output array that stores the remapped 9km land cover types. * */ int landCoverInterPolate(char *remappingindexfile, short int var3[], short 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. short int ntype[6]; // 3km land cover types. short int Type_Select; // Selected 9km land cover type. // Initialize. for (i = 0; i < grid_xt_9km * grid_yt_9km; i++) var9[i] = 0; // Open the grid index file. fp = fopen(remappingindexfile, "r"); if (fp == NULL) { perror("Failed: "); return 1; } // To excute a do-while-loop to get 9km land cover type grid by grid. while (fgets(buffer, MAX_LEN, fp)) { // Remove trailing newline from end of string and replace it with '\0'. buffer[strcspn(buffer, "\n")] = 0; // Extract 9km grid indexes. token = strtok(buffer, ","); // Break the string into a series of tokens. i9 = atoi(token); token = strtok(NULL, ","); // Reverse 'j' index order because the AQM-v8 model data have opposite // order. j9 = grid_yt_9km - 1 - atoi(token); m = j9 * grid_xt_13km + i13; // 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 13km grid. for (i = 0; i < 4; i++) fgets(buffer, MAX_LEN, fp); // Initialize land cover types. ntype[0] = 0; ntype[1] = 0; ntype[2] = 0; ntype[3] = 0; ntype[4] = 0; ntype[5] = 0; // To execute the for-loop to count the number of individual type. for (i = 0; i < nps; i++) { // Read in 3km grid data fgets(buffer, MAX_LEN, fp); token = strtok(buffer, ","); token = strtok(NULL, ","); i3 = atoi(token); token = strtok(NULL, ","); j3 = atoi(token); k = j3 * grid_xt_3km + i3; // Count the individual type for each 3km grid. if (var3[k] == 0) { ntype[0] = ntype[0] + 1; } if (var3[k] == 1) { ntype[1] = ntype[1] + 1; } if (var3[k] == 2) { ntype[2] = ntype[2] + 1; } if (var3[k] == 3) { ntype[3] = ntype[3] + 1; } if (var3[k] == 4) { ntype[4] = ntype[4] + 1; } // Updated on 11/10/2022. if (var3[k] == 5) { ntype[5] = ntype[5] + 1; } } // Initialize the 9km grid land cover type. Type_Select = 0; // Find the majority type with largest number; for (i = 1; i <= 5; i++) { if (ntype[0] < ntype[i]) { ntype[0] = ntype[i]; Type_Select = i; } } // Assign the land cover type to var9. var9[m] = Type_Select; } // Close the open file stream. fclose(fp); return 0; }