/* record2gridpt.c */ /* ----------------------- * INPUT FILE: geog3.nc * OUTPUT: STDOUT * ----------------------- */ /* nanr 26May98 */ #include #include #include /* VEMAP users: customize Netcdf path here */ #include "/contrib/netcdf-3.4/include/netcdf.h" void background(int *row,int *col,int *gridpt,int record); void grid(int gridpt, float *olat, float *olon); int main(argc, argv) int argc; char *argv[]; { /* misc ids */ int i=0; char BUFFER[512]; int gridpt; /* For VEMAP US Grid 1 thru 5520 */ int record; /* Gridpt record position */ int row; /* Row number */ int col; /* Column number */ float olat; /* cell center latitude */ float olon; /* cell center longitude */ /* --- Command line instructions --- */ if(argc != 2) { printf("\n syntax is 3261)>\n"); printf(" example: a.out 3261 \n\n"); printf(" Note that although records number from 1->3261, record \n"); printf(" number does not necessarily equal netCDF position index:\n\n"); printf(" e.g. netCDF position index for record 1:\n"); printf(" C = 0 \n"); printf(" Fortran = 1\n\n"); return(0); } record = atoi(argv[1]); if(record > 3261 || record <= 0) { printf("\n Record %d is out of range. \n",record); printf(" Records number from 1->3261. Note that record \n"); printf(" number does not necessarily equal netCDF position index.\n\n"); printf(" e.g. netCDF position index for record 1:\n"); printf(" C = 0 \n"); printf(" Fortran = 1\n\n"); exit(1); } /* account for 0-based file positioning in C */ record -= 1; /* translate record number to gridpt & screen for background point */ background(&row,&col,&gridpt,record); /* translate record to row/col, lat/lon */ grid(gridpt,&olat,&olon); /* --- Print gridpoint information --- */ printf("-----------------------------------------\n\n"); printf("\n NOTE: Records number from 1->3261.\n"); printf(" Therefore record number does not \n"); printf(" necessarily equal netCDF position index:\n\n"); printf(" record number = %d\n", record+1); printf(" row = %d\n", row); printf(" column = %d\n", col); printf(" latitude = %6.2f\n", olat); printf(" longitude = %6.2f\n", olon); printf(" gridcell = %d\n\n", gridpt); printf("-----------------------------------------\n"); } /*------------------------------------------------------------*/ void grid(int gridpt, float *olat, float *olon) { /* Program converts gridpoint to 2D matrix (row and col) */ /* and returns gridcell center latitude and longitude. */ /* nanr 12-4-95 */ int z; int row, col; for (z=0; z<48; z++) { if ((gridpt > z*115) && (gridpt <= (z+1)*115)) { row = z+1; col = gridpt-z*115; } } *olat = 49.0 + (0.5 * (1 - row)) - 0.25; *olon = 124.5 + (0.5 * (1 - col)) - 0.25; return; } void background(int *row,int *col,int *gridpt,int record) { int backgrpt=0, datapt=0, sum=0; int varid, ncid; int nrows=48, ncols=115; int irow=0, i=0; long int count[2] = {1, 115}; long int start[2]; short var[115]; FILE *fopen(), *fp_in; if(!(ncid = ncopen("geog3.nc",NC_NOWRITE))) { printf("ERROR: Can't find 'geog3.nc' file. \n"); exit(0); } varid = ncvarid(ncid, "mask"); while(irow < nrows) { start[0] = irow; start[1] = 0; count[0] = 1; count[1] = ncols; ncvarget(ncid, varid, start, count, var); for(i=0; i