ocssw V2020
elev.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* elevNames[] = {"height", "z", "depth", NULL};
6 static int elev_initialized = 0;
7 static grid_info_t* elev_global_gridinfo = {0};
8 static grid_info_t* elev_aux_gridinfo = {0};
9 static int elev_global_depthmode = -1;
10 static int elev_aux_depthmode = -1;
11 
12 int initElevFile(char* elevFilename, grid_info_t* elevGrid, int* depth_mode) {
13  int status;
14  nc_type type;
15  size_t len;
16 
17  /* initial file tests */
18  if (elevFilename == NULL) {
19  fprintf(stderr, "-E- %s line %d: Elevation file is NULL.\n",
20  __FILE__, __LINE__);
21  return 1;
22  }
23  status = nc_open(elevFilename, NC_NOWRITE, &elevGrid->ncid);
24  if (status != NC_NOERR) {
25  fprintf(stderr, "-E- %s line %d: Could not open netCDF File \"%s\".\n",
26  __FILE__, __LINE__, elevFilename);
27  return 1;
28  }
29 
30  /* allocate and load grid info */
31  status = init_gridinfo(elevFilename, elevNames, elevGrid);
32  if (status != NC_NOERR) {
33  fprintf(stderr,
34  "-E- %s line %d: Could not read grid info from \"%s\".\n",
35  __FILE__, __LINE__, elevFilename);
36  return status;
37  }
38 
39  /* read depth_mode attribute */
40  /* positive = "up" means pos values for mountains */
41  /* positive = "down" means pos values for ocean depth */
42  status = nc_inq_att(elevGrid->ncid, elevGrid->varid,
43  "positive", &type, &len);
44  if ((status == NC_NOERR) && (type == NC_CHAR) && (len > 0)) {
45  char* value = (char*) malloc(len + 1);
46  status = nc_get_att_text(elevGrid->ncid, elevGrid->varid,
47  "positive", value);
48  value[len] = '\0'; // null terminate
49  lowcase(value);
50  depth_mode[0] = (strcmp(value, "down") == 0); // 0=up, 1=down
51  free(value);
52  } /* otherwise get depth_mode from variable name */
53  else {
54  /* (commented out: not valid for SWIM input)
55  char name[NC_MAX_NAME];
56  status = nc_inq_varname(elevGrid->ncid, elevGrid->varid, name);
57  printf("varname = %s\n",name);
58  depth_mode[0] = (strcmp(name, "depth") == 0);
59  */
60  status = 0;
61  depth_mode[0] = 0;
62  }
63  return status;
64 }
65 
66 void elev_init(char* elevGlobalFilename, char* elevAuxFilename) {
67  int status;
68  if (elevGlobalFilename != NULL && elevGlobalFilename[0] != 0) {
69  elev_global_gridinfo = allocate_gridinfo();
70  status = initElevFile(elevGlobalFilename,
71  elev_global_gridinfo,
72  &elev_global_depthmode);
73  if (status) {
74  fprintf(stderr, "-E- %s line %d: "
75  "Could not read Global Elevation file \"%s\".\n",
76  __FILE__, __LINE__, elevGlobalFilename);
77  exit(1);
78  }
79  }
80  if (elevAuxFilename != NULL && elevAuxFilename[0] != 0) {
81  elev_aux_gridinfo = allocate_gridinfo();
82  status = initElevFile(elevAuxFilename,
83  elev_aux_gridinfo,
84  &elev_aux_depthmode);
85  if (status) {
86  fprintf(stderr, "-E- %s line %d: "
87  "Could not read Auxiliary Elevation file \"%s\".\n",
88  __FILE__, __LINE__, elevAuxFilename);
89  exit(1);
90  }
91  }
92  elev_initialized = 1;
93 }
94 
95 float readElev(grid_info_t* elevGrid, int depth_mode, float lat, float lon) {
96  int status;
97  double elev = BAD_FLT;
98 
99  if (elevGrid != NULL) {
100  status = get_bylatlon(elevGrid, lat, lon, &elev);
101  if ((!status) && (depth_mode))
102  elev *= -1;
103  }
104 
105  return (float) elev;
106 }
107 
108 float get_elev(float lat, float lon) {
109  float elev;
110  elev = readElev(elev_aux_gridinfo, elev_aux_depthmode, lat, lon);
111  if (elev == BAD_FLT) {
112  elev = readElev(elev_global_gridinfo, elev_global_depthmode, lat, lon);
113  }
114  /* printf(" = %d (get_elev)\n",elev); */
115  return elev;
116 }
int32 value
Definition: Granule.c:1235
int status
Definition: l1_czcs_hdf.c:31
char * lowcase(char *instr)
Definition: lowcase.c:10
float get_elev(float lat, float lon)
Definition: elev.c:108
#define NULL
Definition: decode_rs.h:63
float * lat
grid_info_t * allocate_gridinfo()
Definition: nc_gridutils.c:107
#define BAD_FLT
Definition: jplaeriallib.h:19
int initElevFile(char *elevFilename, grid_info_t *elevGrid, int *depth_mode)
Definition: elev.c:12
int get_bylatlon(grid_info_t *grid, float lat, float lon, double *value)
Definition: nc_gridutils.c:374
float * lon
float readElev(grid_info_t *elevGrid, int depth_mode, float lat, float lon)
Definition: elev.c:95
int init_gridinfo(char *filename, const char *varnames[], grid_info_t *grid)
Definition: nc_gridutils.c:132
void elev_init(char *elevGlobalFilename, char *elevAuxFilename)
Definition: elev.c:66