cdf_util.c 9.78 KB
Newer Older
1
2
3
4
5
#include <string.h>
#include <ctype.h>
#include "dmemory.h"
#include "cdi.h"
#include "cdf_util.h"
6
#include "error.h"
7
8


9
char *strToLower(char *str)
10
{
11
12
  if (str)
    for (size_t i = 0; str[i]; ++i)
13
      str[i] = (char)tolower((int)str[i]);
14
15

  return str;
16
17
}

18

19
bool strStartsWith(const char *vstr, const char *cstr)
20
21
{
  bool is_equal = false;
22
  if (vstr && cstr)
23
    {
24
25
      size_t clen = strlen(cstr);
      size_t vlen = strlen(vstr);
26
27
      if (clen <= vlen) is_equal = (memcmp(vstr, cstr, clen) == 0);
    }
28
29
  return is_equal;
}
30
31
32
33
34

int get_timeunit(size_t len, const char *ptu)
{
  int timeunit = -1;

35
36
  while ( isspace(*ptu) && len ) { ptu++; len--; }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  // clang-format off
38
39
  if ( len > 2 )
    {
40
41
42
43
44
45
46
      if      ( strStartsWith(ptu, "sec") )            timeunit = TUNIT_SECOND;
      else if ( strStartsWith(ptu, "minute") )         timeunit = TUNIT_MINUTE;
      else if ( strStartsWith(ptu, "hour") )           timeunit = TUNIT_HOUR;
      else if ( strStartsWith(ptu, "day") )            timeunit = TUNIT_DAY;
      else if ( strStartsWith(ptu, "month") )          timeunit = TUNIT_MONTH;
      else if ( strStartsWith(ptu, "calendar_month") ) timeunit = TUNIT_MONTH;
      else if ( strStartsWith(ptu, "year") )           timeunit = TUNIT_YEAR;
47
    }
48
  else if ( len == 1 && ptu[0] == 's' )  timeunit = TUNIT_SECOND;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
  // clang-format on
50
51
52
53
54
55
56

  return timeunit;
}


bool is_time_units(const char *timeunits)
{
57
58
  while ( isspace(*timeunits) ) timeunits++; 

59
60
61
62
63
64
65
  bool status = strStartsWith(timeunits, "sec")
             || strStartsWith(timeunits, "minute")
             || strStartsWith(timeunits, "hour")
             || strStartsWith(timeunits, "day")
             || strStartsWith(timeunits, "month")
             || strStartsWith(timeunits, "calendar_month")
             || strStartsWith(timeunits, "year");
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

  return status;
}


bool is_timeaxis_units(const char *timeunits)
{
  bool status = false;

  size_t len = strlen(timeunits);
  char *tu = (char *) Malloc((len+1)*sizeof(char));
  memcpy(tu, timeunits, (len+1) * sizeof(char));
  char *ptu = tu;

  for ( size_t i = 0; i < len; i++ ) ptu[i] = (char)tolower((int)ptu[i]);

  int timeunit = get_timeunit(len, ptu);
  if ( timeunit != -1 )
    {
      while ( ! isspace(*ptu) && *ptu != 0 ) ptu++;
      if ( *ptu )
        {
          while ( isspace(*ptu) ) ptu++;

90
91
          int timetype = strStartsWith(ptu, "as") ? TAXIS_ABSOLUTE :
                         strStartsWith(ptu, "since") ? TAXIS_RELATIVE : -1;
92
93
94
95
96
97
98
99
100
101
102
103
104

          status = timetype != -1;
        }
    }

  Free(tu);

  return status;
}


bool is_height_units(const char *units)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
  const int u0 = units[0];
106

Thomas Jahns's avatar
Thomas Jahns committed
107
108
  bool status
    = (u0=='m' && (!units[1] || strncmp(units, "meter", 5) == 0))
109
110
111
112
113
    || (!units[2] && units[1]=='m' && (u0=='c' || u0=='d' || u0=='k'))
    || (strncmp(units, "decimeter", 9) == 0)
    || (strncmp(units, "centimeter", 10) == 0)
    || (strncmp(units, "millimeter", 10) == 0)
    || (strncmp(units, "kilometer", 9) == 0);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

  return status;
}


bool is_pressure_units(const char *units)
{
  bool status = false;

  if ( strncmp(units, "millibar", 8) == 0 ||
       strncmp(units, "mb", 2)       == 0 ||
       strncmp(units, "hectopas", 8) == 0 ||
       strncmp(units, "hPa", 3)      == 0 ||
       strncmp(units, "Pa", 2)       == 0 )
    {
      status = true;
    }

  return status;
}


bool is_DBL_axis(/*const char *units,*/ const char *longname)
{
  bool status = false;

  if ( strcmp(longname, "depth below land")         == 0 ||
       strcmp(longname, "depth_below_land")         == 0 ||
       strcmp(longname, "levels below the surface") == 0 )
    {
      /*
      if ( strcmp(ncvars[ncvarid].units, "cm") == 0 ||
           strcmp(ncvars[ncvarid].units, "dm") == 0 ||
           strcmp(ncvars[ncvarid].units, "m")  == 0 )
      */
        status = true;
    }

  return status;
}


bool is_depth_axis(const char *stdname, const char *longname)
{
  bool status = false;

  if ( strcmp(stdname, "depth") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
162
163
164
165
166
167
168
    {
      status = true;
    }
  else if ( strcmp(longname, "depth_below_sea") == 0 ||
            strcmp(longname, "depth below sea") == 0 )
    {
      status = true;
    }
169
170
171
172
173
174
175
176
177
178

  return status;
}


bool is_height_axis(const char *stdname, const char *longname)
{
  bool status = false;

  if ( strcmp(stdname, "height") == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179
180
181
182
183
184
185
186
    {
      status = true;
    }
  else if ( strcmp(longname, "height") == 0 ||
            strcmp(longname, "height above the surface") == 0 )
    {
      status = true;
    }
187
188
189
190

  return status;
}

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
bool is_altitude_axis(const char *stdname, const char *longname)
{
  bool status = false;

  if ( strcmp(stdname, "altitude") == 0 )
    {
      status = true;
    }
  else if ( strcmp(longname, "altitude") == 0 )
    {
      status = true;
    }

  return status;
}

207
208
209
210
211
212
213
214

bool is_lon_axis(const char *units, const char *stdname)
{
  bool status = false;
  char lc_units[16];

  memcpy(lc_units, units, 15);
  lc_units[15] = 0;
215
  strToLower(lc_units);
216

217
218
  if ( (strStartsWith(lc_units, "degree") || strStartsWith(lc_units, "radian")) &&
       (strStartsWith(stdname, "grid_longitude") || strStartsWith(stdname, "longitude")) )
219
220
221
    {
      status = true;
    }
222
223
224
  else if ( strStartsWith(lc_units, "degree")
            && !strStartsWith(stdname, "grid_latitude")
            && !strStartsWith(stdname, "latitude") )
225
226
227
    {
      int ioff = 6;
      if ( lc_units[ioff] == 's' ) ioff++;
228
      if ( lc_units[ioff] == ' ' ) ioff++;
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
      if ( lc_units[ioff] == '_' ) ioff++;
      if ( lc_units[ioff] == 'e' ) status = true;
    }

  return status;
}


bool is_lat_axis(const char *units, const char *stdname)
{
  bool status = false;
  char lc_units[16];

  memcpy(lc_units, units, 15);
  lc_units[15] = 0;
244
  strToLower(lc_units);
245

246
247
  if ( (strStartsWith(lc_units, "degree") || strStartsWith(lc_units, "radian")) &&
        (strStartsWith(stdname, "grid_latitude") || strStartsWith(stdname, "latitude")) )
248
249
250
    {
      status = true;
    }
251
252
253
  else if ( strStartsWith(lc_units, "degree")
            && !strStartsWith(stdname, "grid_longitude")
            && !strStartsWith(stdname, "longitude") )
254
255
256
    {
      int ioff = 6;
      if ( lc_units[ioff] == 's' ) ioff++;
257
      if ( lc_units[ioff] == ' ' ) ioff++;
258
259
260
261
262
263
      if ( lc_units[ioff] == '_' ) ioff++;
      if ( lc_units[ioff] == 'n' || lc_units[ioff] == 's' ) status = true;
    }

  return status;
}
264
265


266
267
bool is_x_axis(const char *units, const char *stdname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
  (void)units;
269
  return (strcmp(stdname, "projection_x_coordinate") == 0);
270
271
272
273
274
}


bool is_y_axis(const char *units, const char *stdname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
  (void)units;
276
  return (strcmp(stdname, "projection_y_coordinate") == 0);
277
278
279
}


280
281
void set_gridtype(const char *attstring, int *gridtype)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  // clang-format off
  if      ( strcmp(attstring, "gaussian_reduced") == 0 ) *gridtype = GRID_GAUSSIAN_REDUCED;
  else if ( strcmp(attstring, "gaussian") == 0 ) *gridtype = GRID_GAUSSIAN;
  else if ( strncmp(attstring, "spectral", 8) == 0 ) *gridtype = GRID_SPECTRAL;
  else if ( strncmp(attstring, "fourier", 7) == 0 ) *gridtype = GRID_FOURIER;
  else if ( strcmp(attstring, "trajectory") == 0 ) *gridtype = GRID_TRAJECTORY;
  else if ( strcmp(attstring, "generic") == 0 ) *gridtype = GRID_GENERIC;
  else if ( strcmp(attstring, "cell") == 0 ) *gridtype = GRID_UNSTRUCTURED;
  else if ( strcmp(attstring, "unstructured") == 0 ) *gridtype = GRID_UNSTRUCTURED;
  else if ( strcmp(attstring, "curvilinear") == 0 ) ;
  else if ( strcmp(attstring, "characterxy") == 0 ) *gridtype = GRID_CHARXY;
  else if ( strcmp(attstring, "sinusoidal") == 0 ) ;
  else if ( strcmp(attstring, "laea") == 0 ) ;
  else if ( strcmp(attstring, "lcc2") == 0 ) ;
  else if ( strcmp(attstring, "linear") == 0 ) ; // ignore grid type linear
297
298
299
300
301
302
303
304
305
  else
    {
      static bool warn = true;
      if ( warn )
        {
          warn = false;
          Warning("NetCDF attribute grid_type='%s' unsupported!", attstring);
        }
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
  // clang-format on
307
308
309
310
311
}


void set_zaxistype(const char *attstring, int *zaxistype)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
  // clang-format off
313
  if      ( strcmp(attstring, "toa") == 0 ) *zaxistype = ZAXIS_TOA;
314
  else if ( strcmp(attstring, "tropopause") == 0 ) *zaxistype = ZAXIS_TROPOPAUSE;
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  else if ( strcmp(attstring, "cloudbase") == 0 ) *zaxistype = ZAXIS_CLOUD_BASE;
  else if ( strcmp(attstring, "cloudtop") == 0 ) *zaxistype = ZAXIS_CLOUD_TOP;
  else if ( strcmp(attstring, "isotherm0") == 0 ) *zaxistype = ZAXIS_ISOTHERM_ZERO;
  else if ( strcmp(attstring, "seabottom") == 0 ) *zaxistype = ZAXIS_SEA_BOTTOM;
  else if ( strcmp(attstring, "lakebottom") == 0 ) *zaxistype = ZAXIS_LAKE_BOTTOM;
  else if ( strcmp(attstring, "sedimentbottom") == 0 ) *zaxistype = ZAXIS_SEDIMENT_BOTTOM;
  else if ( strcmp(attstring, "sedimentbottomta") == 0 ) *zaxistype = ZAXIS_SEDIMENT_BOTTOM_TA;
  else if ( strcmp(attstring, "sedimentbottomtw") == 0 ) *zaxistype = ZAXIS_SEDIMENT_BOTTOM_TW;
  else if ( strcmp(attstring, "mixlayer") == 0 ) *zaxistype = ZAXIS_MIX_LAYER;
  else if ( strcmp(attstring, "atmosphere") == 0 ) *zaxistype = ZAXIS_ATMOSPHERE;
  else
    {
      static bool warn = true;
      if ( warn )
        {
          warn = false;
          Warning("NetCDF attribute level_type='%s' unsupported!", attstring);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
334
    }
  // clang-format on
335
}
336
337
338
339


void set_calendar(const char *attstring, int *calendar)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
341
342
343
344
345
  // clang-format off
  if      ( strStartsWith(attstring, "standard") ) *calendar = CALENDAR_STANDARD;
  else if ( strStartsWith(attstring, "gregorian") ) *calendar = CALENDAR_GREGORIAN;
  else if ( strStartsWith(attstring, "none") ) *calendar = CALENDAR_NONE;
  else if ( strStartsWith(attstring, "proleptic") ) *calendar = CALENDAR_PROLEPTIC;
  else if ( strStartsWith(attstring, "360") ) *calendar = CALENDAR_360DAYS;
346
  else if ( strStartsWith(attstring, "365") ||
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
            strStartsWith(attstring, "noleap") ) *calendar = CALENDAR_365DAYS;
348
  else if ( strStartsWith(attstring, "366") ||
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
            strStartsWith(attstring, "all_leap") ) *calendar = CALENDAR_366DAYS;
  else Warning("calendar >%s< unsupported!", attstring);
  // clang-format on
352
}