/* make_mask.c Program defines a coordinate system and then call read_srtm_mask (double lat, double lon) to build a bitmask for the 1 -r 2-minute formats 0 = water 1 = land but I think this really means 0 = ocean, 1 = not ocean. Writes binary bitmask to stdout. */ #include #include #include #include /* #include #include */ #include "blockstat.h" /* Used for coordinate system definition */ int main (int argc, char **argv) { struct BM_CSYS coords; double x, y; int coord_choice = -1; int i, j, k; int nerrs = 0; int nbytes, val; unsigned char *bitmask; static unsigned char maskset[8] = {128, 64, 32, 16, 8, 4, 2, 1}; for (k = 1; k < argc; k++) { if (argv[k][0] == '-') { switch (argv[k][1]) { case 'C': nerrs += ((sscanf(&argv[k][2], "%d", &coord_choice)) != 1); break; default: nerrs++; break; } } else { nerrs++; } } if (init_coords (&coords, coord_choice)) { fprintf (stderr, "You must set a coordinate system choice.\n"); nerrs++; } if (nerrs || (argc == 1) ) { fprintf (stderr, "usage: make_mask -C<#> > landsea.bits\n\n"); if (argc == 1) exit (EXIT_SUCCESS); fprintf (stderr, "\tUse -C<#> to set the block coordinate system, where <#> is one of:\n"); fprintf (stderr, "\t-C%d to select the SRTM30 (lon/lat, 30 arc sec, to +/- 90, pixel) system.\n", GTOPO30); fprintf (stderr, "\t-C%d to select the IMG1M72 (mercator, 1 arc min, to +/- 72.005977, pixel) system.\n", IMG1M72); fprintf (stderr, "\t-C%d to select the IMG1M81 (mercator, 1 arc min, to +/- 80.738009, pixel) system.\n", IMG1M81); fprintf (stderr, "\t-C%d to select the IMG2M72 (mercator, 2 arc min, to +/- 72.005977, pixel) system.\n", IMG2M72); fprintf (stderr, "\t-C%d to select the IMG2M81 (mercator, 2 arc min, to +/- 80.738009, pixel) system.\n\n", IMG2M81); exit (EXIT_FAILURE); } nbytes = coords.ntot / 8; /* All our coordinate systems have ntot divisible by 8 */ /*printf("%d \n",nbytes);*/ if ( (bitmask = calloc ((size_t)nbytes, (size_t)1)) == NULL) { fprintf (stderr, "make_mask: Error. Failed to calloc %d bytes for the mask.\n", nbytes); exit (EXIT_FAILURE); } for (i = 0, k = 0; i < nbytes; i++) { for (j = 0; j < 8; j++, k++) { lonlat_from_k (k, &x, &y, &coords); if (x > 180.0) x -= 360.0; val = (int) read_srtm_mask (&x, &y); /* This fn returns a type NV_INT32 */ if (val < 0 || val > 1) { fprintf (stderr, "make_mask: read_srtm_mask returns %d at k = %d, x = %.8lg, y = %.8lg\n", val, k, x, y); exit (EXIT_FAILURE); } if (val) bitmask[i] |= maskset[j]; /*if(i > 4200000) printf("%d %f %f %d \n",i,x,y,val);*/ } } fwrite ((void *)bitmask, (size_t)1, (size_t)nbytes, stdout); free ((void *)bitmask); exit (EXIT_SUCCESS); }