/************************************************************************ * readgrd routine to read an srtm mask and return 1-land or 0-water * *************************************************************************/ /************************************************************************ * Creator: David T. Sandwell Scripps Institution of Oceanography * * Date : 02/12/07 * ************************************************************************/ /************************************************************************ * Modification history: * ************************************************************************/ #include #include #include unsigned char maskset[8] = { 128, 64, 32, 16, 8, 4, 2, 1}; int read_srtm_mask(rln,rlt) double *rln; /* longitude -180 to 180 */ double *rlt; /* latitude -90 to 90 */ { char fname[120]="/seasat2/grids/masks/srtm.bitmask"; FILE *fp; static unsigned char *a; static int ifirst = 1; int i, j, kk, idry, nmask = 116640000; /* if this is the first call to this routine then malloc the memory and read the bitmask */ if(ifirst == 1) { if(( a = (unsigned char *) malloc(nmask)) == NULL){ printf("sorry, couldn't allocate memory for mask\n"); exit(-1); } if ( (fp = fopen(fname, "r")) == NULL) { fprintf(stderr,"read_srtm_mask: ERROR. Cannot open w %s\n", fname); exit(-1); } if ((fread(a, 1, nmask, fp)) != nmask) { fprintf(stderr,"read_srtm_mask: ERROR during read of %s\n", fname); exit(-1); } ifirst = 0; } /* compute the index in the mask */ j = (int)floor((*rln + 180.)*120.); i = (int)floor((90. - *rlt )*120.); kk = 120*360*i + j; idry = 0; if(a[kk/8] & maskset[kk%8]) idry = 1; return (idry); }