OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
read_mask.c
Go to the documentation of this file.
1 #include "l12_proto.h"
2 #include "nc_gridutils.h"
3 
4 /* global variables */
5 static const char* landnames[] = {"watermask", "landmask", "z", NULL};
6 static const char* dem_names[] = {"height", "z", "depth", NULL};
7 static const char* watersurface_names[] = {"water_surface_height", NULL};
8 
9 
10 static grid_info_t* landmask_grid = {0};
11 static grid_info_t* dem_grid={0};
12 static grid_info_t* dem_aux_grid = {0};
13 
14 static grid_info_t* watersurface_grid={0};
15 
16 // support for legacy b128 land/shallow water mask
17 static int landindex = 0;
18 static int bathindex = 1;
19 static int use_b128_land = FALSE;
20 static int use_b128_bath = FALSE;
21 
22 /* Land Mask tools */
24  int status;
25 
26  /* load structure from NetCDF file*/
27  landmask_grid = allocate_gridinfo();
28  status = init_gridinfo(input->land, landnames, landmask_grid);
29 
30  /* if not NetCDF, free structure and read binary file */
31  if (status != NC_NOERR) {
32  free(landmask_grid);
33  landmask_grid = NULL;
34  status = b128_msk_init(input->land, landindex);
35  if (status == 0)
36  use_b128_land = TRUE;
37  }
38 
39  return status;
40 
41 }
42 
43 int dem_init() {
44  int status;
45  dem_grid = allocate_gridinfo();
46  status = init_gridinfo(input->demfile, dem_names, dem_grid);
47  if (status == NC_NOERR) {
48  watersurface_grid = allocate_gridinfo();
49  status = init_gridinfo(input->demfile, watersurface_names, watersurface_grid);
50  if (status != NC_NOERR) {
51  free(watersurface_grid);
52  watersurface_grid = NULL;
53  printf("-E- Unable to initialize grid for water surface height \n");
54  return EXIT_FAILURE;
55  }
56  if (input->dem_auxfile != NULL && input->dem_auxfile[0] != 0) {
57  dem_aux_grid = allocate_gridinfo();
58  status = init_gridinfo(input->dem_auxfile, dem_names, dem_aux_grid);
59 
60  if (status) {
61  printf("-E- Unable to initialize grid for auxiliary digital elevation\n");
62  exit(1);
63  }
64  }
65  } else {
66  free(dem_grid);
67  dem_grid = NULL;
68  status = b128_msk_init(input->water, bathindex);
69  use_b128_bath = TRUE;
70  }
71 
72  if (status != 0){
73  printf("-E- %s : Unable to initialize digital elevation map\n", __FILE__);
74  return EXIT_FAILURE;
75  }
76 
77  return status;
78 }
79 
80 int land_mask(float lat, float lon) {
81  double value;
82  int status;
83  static int messagePrinted = 0;
84  /* get value from NetCDF file*/
85  if (landmask_grid != NULL) {
86  status = get_bylatlon(landmask_grid, lat, lon, &value);
87  if (status) {
88  if (!messagePrinted) {
89  printf("-W- file contains locations not contained in the landmask: %s\n...assuming those are water.\n",
90  landmask_grid->file);
91  messagePrinted = 1;
92  }
93  return EXIT_SUCCESS;
94  }
95  return ( (short) value != 1); // convert from water=1 to land=1
96  }
97  /* otherwise get from binary file */
98  else return b128_msk_get(lat, lon, landindex);
99 }
100 
101 /* Bathymetry placeholders */
102 int bath_mask_init(char *file) {
103  int status;
104  status = b128_msk_init(file, bathindex);
105  return status;
106 }
107 
108 int bath_mask(float lat, float lon) {
109  return b128_msk_get(lat, lon, bathindex);
110 }
111 
112 float get_dem(float lat, float lon) {
113  int status;
114  double dem;
115  status = get_bylatlon(dem_aux_grid, lat, lon, &dem);
116  if ((status != 0) || (dem == BAD_FLT)) {
117  status = get_bylatlon(dem_grid, lat, lon, &dem);
118  }
119  return (float) dem;
120 }
121 
122 /*
123  * Set land and shallow water flags
124  */
125 int land_bath_mask(l1str *l1rec,int32_t ip)
126 {
127  double value;
128  int status=0;
129  static int landMessagePrinted = 0;
130  static int bathMessagePrinted = 0;
131 
132  double height_floor, height_surface;
133  height_floor = (double) l1rec->dem[ip];
134 
135  float lat=l1rec->lat[ip];
136  float lon=l1rec->lon[ip];
137 
138  if (use_b128_land) {
139  l1rec->land[ip] = b128_msk_get(lat, lon, landindex);
140  } else {
141  /* get value from NetCDF file*/
142  if (landmask_grid != NULL) {
143  status = get_bylatlon(landmask_grid, lat, lon, &value);
144  if (status) {
145  if (!landMessagePrinted) {
146  fprintf(stderr, "-W- file contains locations not contained in the landmask: %s\n...assuming those are water.\n",
147  landmask_grid->file);
148  landMessagePrinted = 1;
149  }
150  return EXIT_SUCCESS;
151  }
152  l1rec->land[ip]=( (short) value != 1); // convert from water=1 to land=1
153  }
154  }
155  //if land mask is off, set bathymetry flags
156  if(!l1rec->land[ip]){
157  if (use_b128_bath) {
158  l1rec->swater[ip] = l1rec->land[ip] = b128_msk_get(lat, lon, bathindex);
159  if (input->shallow_water_depth != 30) {
160  if (!bathMessagePrinted) {
161  fprintf(stderr, "-W- shallow water value (%6.2f) is not consistent with the bathymetry file used: shallow water flag set to 30m",
162  input->shallow_water_depth);
163  bathMessagePrinted = 1;
164  }
165  }
166  } else {
167  //status = get_bylatlon(dem_grid, lat, lon, &height_floor);
168  status = get_bylatlon(watersurface_grid, lat, lon, &height_surface);
169 
170  if(fabs(height_floor-dem_grid->FillValue)>DBL_EPSILON && fabs(height_surface-watersurface_grid->FillValue)>DBL_EPSILON){
171  if((height_surface-height_floor)<input->shallow_water_depth)
172  l1rec->swater[ip]=ON;
173  }
174  }
175  }
176 
177  return EXIT_SUCCESS;
178 }
179 
int32 value
Definition: Granule.c:1235
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
int status
Definition: l1_czcs_hdf.c:32
#define FALSE
Definition: rice.h:164
#define NULL
Definition: decode_rs.h:63
int land_bath_mask(l1str *l1rec, int32_t ip)
Definition: read_mask.c:125
read l1rec
struct @126 dem
#define TRUE
Definition: rice.h:165
float * lat
#define ON
Definition: l1.h:43
float get_dem(float lat, float lon)
Definition: read_mask.c:112
grid_info_t * allocate_gridinfo()
Definition: nc_gridutils.c:107
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
instr * input
character(len=1000) if
Definition: names.f90:13
integer, parameter double
int b128_msk_init(char *landfile, int msknum)
Definition: b128_msk_get.c:30
int get_bylatlon(grid_info_t *grid, float lat, float lon, double *value)
Definition: nc_gridutils.c:368
#define BAD_FLT
Definition: jplaeriallib.h:19
#define fabs(a)
Definition: misc.h:93
int land_mask(float lat, float lon)
Definition: read_mask.c:80
int bath_mask(float lat, float lon)
Definition: read_mask.c:108
float * lon
int dem_init()
Definition: read_mask.c:43
int b128_msk_get(float lat, float lon, int msknum)
Definition: b128_msk_get.c:183
int bath_mask_init(char *file)
Definition: read_mask.c:102
int init_gridinfo(char *filename, const char *varnames[], grid_info_t *grid)
Definition: nc_gridutils.c:132
int land_mask_init()
Definition: read_mask.c:23