/* * SUMMARY: This programs reads a file with latitude,longitude, value * and grids it into an asciigrid file. This file can then be * imported into ArcView or ArcInfo. * * USAGE: Used together with the VIC model. * * AUTHOR: Bernt Viggo Matheussen * ORG: University of Washington, Department of Civil Engineering * ORIG-DATE: 13-Oct-98 at 11:52:06 * LAST-MOD: Fri Nov 20 16:37:39 1998 by Bernt Viggo Matheussen * Tue Oct 24 2000 by Ed Maurer * DESCRIPTION: * DESCRIP-END. * FUNCTIONS: * COMMENTS: The final maskfile is rectangular even though the * latitude and longitudes given in input file do not * necessarily make a rectangle. * The program seeks the maximum long/lat and minimum long/lat * and makes a rectangle out of these max and min coordinates. * Alternatively, a defined box can be input for the max/min * dimensions for the final grid. * All the gridcells are given the value that corresponds to * the lat/long in the input file. * Gridcells with no value associated are set to the void number. */ /****************************************************************************/ #include #include #include #include #include #include /* Returns number of lines in file */ int line_count(char file[200]); /* Calloc memory to a two dimensional array */ float **allocate_memory(int rows, int cols); /* Free memory */ void free_memory(float **LATLON, int rows, int cols); /* Reads in data into array */ void read_data(float **LATLON, int cells, char latlonfile[200]); /* Function returns cell value given latitude and longitude */ /* If the cell is not present the function returns the void number */ float find_gridcellvalue(float lat, float lon, float void_nr, int cells, float **LATLON); void main(int argc, char **argv) { char latlong[200]; float resolution; int cells, i,j; float **LATLON; float void_nr; float maxlat, minlat, maxlong, minlong; int rows,cols; float latitude,longitude, cellvalue; float MINLAT, MINLONG; float min_lat1,max_lat1,min_lon1,max_lon1; if (argc!=4 && argc!=8) { /* Must be exactly 3 arguments behind the program name */ fprintf(stderr,"Incorrect number of commandline arguments \n"); fprintf(stderr,"usage: grid_ll infile resolution void_nr > outfile\n"); fprintf(stderr," or: grid_ll infile resolution void_nr min_lat max_lat min_lon max_lon > outfile\n"); fprintf(stderr," longitude uses -180 to 180 convention\n"); exit(EXIT_FAILURE); } strcpy(latlong,argv[1]); /* Infile with latitude,longitude and value */ fprintf(stderr,"Inputfile: %s\n",latlong); resolution = atof(argv[2]); /* Converts argument 2 to resolution */ fprintf(stderr,"Resolution: %2.4f\n",resolution); void_nr = atof(argv[3]); /* Converts argument 3 to void number */ fprintf(stderr,"void_nr: %6.0f\n",void_nr); min_lat1=max_lat1=min_lon1=max_lon1=0.0; if(argc==8){ /* optional declaration of box bounds for arc/info ascii grid */ min_lat1 = atof(argv[4]); max_lat1 = atof(argv[5]); min_lon1 = atof(argv[6]); max_lon1 = atof(argv[7]); } cells = line_count(latlong); fprintf(stderr,"lines %d\n",cells); /* Allocates memory to a matrix LATLON[Cells][3] */ LATLON = allocate_memory(cells,3); read_data(LATLON, cells,latlong); /* initialize the outer grid dimensions */ maxlat = minlat = LATLON[0][0]; /* Set initial values */ maxlong = minlong = LATLON[0][1]; /* Set initial values */ for(i=0;i LATLON[i][0]) minlat = LATLON[i][0]; if (maxlong < LATLON[i][1]) maxlong= LATLON[i][1]; if (minlong > LATLON[i][1]) minlong= LATLON[i][1]; } /* check if bounding box has been defined, compare with data area */ if(argc==8){ if(maxlat>max_lat1 || minlatmax_lon1 || minlongmax_lat1) max_lat1=maxlat+(resolution/2.0); if(minlatmax_lon1) max_lon1=maxlong+(resolution/2.0); if(minlong