/********************************************************************************* * * File: uf.c * * Creates an urban fraction dataset from a dominant land category data set. * * The name of the input land category file is read from the command line, and * the resulting urban fraction field for the corresponding tile is written to * and output file named 'outfile.dat'. * * The mapping between land category and urban fraction is specified * programmatically with a case block in the program code, below, and the urban * fractions themselves can be specified on the command-line. * * NOTES: It is assumed that the dominant land category data use 1 byte of storage * for each category. * * * Michael G. Duda, NCAR/MMM * Initial version: 11 July 2012 * * Updated 26 March 2012: added command-line arguments to specify urban fractions * for different categories. * *********************************************************************************/ #include #include #include #include #include #include #include void help_mesg(char * cmd_name) { fprintf(stderr, "Usage: %s [options] \n", cmd_name); fprintf(stderr, " where options are any of:\n"); fprintf(stderr, " -L set the urban percentage (0 to 100) for low-density residential areas\n"); fprintf(stderr, " -H set the urban percentage (0 to 100) for high-density residential areas\n"); fprintf(stderr, " -I set the urban percentage (0 to 100) for commercial/industrial areas\n"); fprintf(stderr, " -h print this help message and quit\n"); } int main(int argc, char ** argv) { size_t datlen, i; struct stat st; int fdin, fdout; unsigned char * dat; int o; int l_pct, h_pct, i_pct; /* Set default percentages for low-density residential, high-density residential, and commercial/industrial areas */ l_pct = 50; h_pct = 90; i_pct = 95; if (argc < 2) { help_mesg(argv[0]); return 1; } /* Scan command-line arguments */ while ((o = getopt(argc, argv, "L:H:I:h")) != -1) { switch( o ) { case (int)'L': sscanf(optarg, "%i", &l_pct); break; case (int)'H': sscanf(optarg, "%i", &h_pct); break; case (int)'I': sscanf(optarg, "%i", &i_pct); break; case (int)'h': help_mesg(argv[0]); return 0; case (int)'?': if (optopt == (int)'L') fprintf(stderr, "Error: the -L option requires an argument\n"); else if (optopt == (int)'H') fprintf(stderr, "Error: the -H option requires an argument\n"); else if (optopt == (int)'I') fprintf(stderr, "Error: the -I option requires an argument\n"); else fprintf(stderr, "Error: unrecognized option -%c\n", optopt); return 1; } } if (optind >= argc) { help_mesg(argv[0]); return 1; } printf("Using low-density residential urban percentage of %i%%\n", l_pct); printf("Using high-density residential urban percentage of %i%%\n", h_pct); printf("Using commercial/industrial urban percentage of %i%%\n", i_pct); printf("Opening %s\n", argv[optind]); if ((fdin = open(argv[optind], O_RDONLY)) == -1) { fprintf(stderr, "Error opening input file %s\n", argv[optind]); return 1; } if ((fdout = open("outfile.dat", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) { fprintf(stderr, "Error opening output file outfile.dat\n"); return 1; } /* Get size of the input file */ i = fstat(fdin, &st); datlen = (size_t)st.st_size; fprintf(stderr, "Reading %ld bytes from %s\n", (long)datlen, argv[optind]); /* Allocate working array */ dat = (unsigned char *)malloc(datlen); read(fdin,dat,datlen); /* * Urban fractions are assigned in the range [0,100] (i.e., as a percentage), * which is converted to the range [0,1] in geogrid by specifying * scale_factor=0.01 in the index file for the urban fraction data set */ for(i=0; i