cdf_util.c 9.24 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
void str_tolower(char *str)
10
11
12
13
14
15
{
  if ( str )
    for ( size_t i = 0; str[i]; ++i )
      str[i] = (char)tolower((int)str[i]);
}

16

17
18
19
20
21
22
23
24
25
bool str_is_equal(const char *vstr, const char *cstr)
{
  bool is_equal = false;
  size_t clen = (cstr != NULL) ? strlen(cstr) : 0;

  if ( vstr && *vstr ) is_equal = (memcmp(vstr, cstr, clen) == 0);

  return is_equal;
}
26
27
28
29
30

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

31
32
  while ( isspace(*ptu) && len ) { ptu++; len--; }

33
34
  if ( len > 2 )
    {
35
36
37
38
39
40
41
      if      ( str_is_equal(ptu, "sec") )            timeunit = TUNIT_SECOND;
      else if ( str_is_equal(ptu, "minute") )         timeunit = TUNIT_MINUTE;
      else if ( str_is_equal(ptu, "hour") )           timeunit = TUNIT_HOUR;
      else if ( str_is_equal(ptu, "day") )            timeunit = TUNIT_DAY;
      else if ( str_is_equal(ptu, "month") )          timeunit = TUNIT_MONTH;
      else if ( str_is_equal(ptu, "calendar_month") ) timeunit = TUNIT_MONTH;
      else if ( str_is_equal(ptu, "year") )           timeunit = TUNIT_YEAR;
42
    }
43
  else if ( len == 1 && ptu[0] == 's' )  timeunit = TUNIT_SECOND;
44
45
46
47
48
49
50

  return timeunit;
}


bool is_time_units(const char *timeunits)
{
51
52
  while ( isspace(*timeunits) ) timeunits++; 

53
54
55
56
57
58
59
  bool status = str_is_equal(timeunits, "sec")
             || str_is_equal(timeunits, "minute")
             || str_is_equal(timeunits, "hour")
             || str_is_equal(timeunits, "day")
             || str_is_equal(timeunits, "month")
             || str_is_equal(timeunits, "calendar_month")
             || str_is_equal(timeunits, "year");
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

  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++;

84
85
          int timetype = str_is_equal(ptu, "as") ? TAXIS_ABSOLUTE :
                         str_is_equal(ptu, "since") ? TAXIS_RELATIVE : -1;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

          status = timetype != -1;
        }
    }

  Free(tu);

  return status;
}


bool is_height_units(const char *units)
{
  int u0 = units[0];

Thomas Jahns's avatar
Thomas Jahns committed
101
102
  bool status
    = (u0=='m' && (!units[1] || strncmp(units, "meter", 5) == 0))
103
104
105
106
107
    || (!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);
108
109
110
111
112
113
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

  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 )
    status = true;
  else
    if ( strcmp(longname, "depth_below_sea") == 0 ||
         strcmp(longname, "depth below sea") == 0 )
      {
        status = true;
      }

  return status;
}


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

  if ( strcmp(stdname, "height") == 0 )
    status = true;
  else
    if ( strcmp(longname, "height") == 0 ||
         strcmp(longname, "height above the surface") == 0 )
      {
        status = true;
      }

  return status;
}


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;
191
  str_tolower(lc_units);
192

193
194
  if ( (str_is_equal(lc_units, "degree") || str_is_equal(lc_units, "radian")) &&
       (str_is_equal(stdname, "grid_longitude") || str_is_equal(stdname, "longitude")) )
195
196
197
    {
      status = true;
    }
198
199
200
  else if ( str_is_equal(lc_units, "degree")
            && !str_is_equal(stdname, "grid_latitude")
            && !str_is_equal(stdname, "latitude") )
201
202
203
    {
      int ioff = 6;
      if ( lc_units[ioff] == 's' ) ioff++;
204
      if ( lc_units[ioff] == ' ' ) ioff++;
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
      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;
220
  str_tolower(lc_units);
221

222
223
  if ( (str_is_equal(lc_units, "degree") || str_is_equal(lc_units, "radian")) &&
        (str_is_equal(stdname, "grid_latitude") || str_is_equal(stdname, "latitude")) )
224
225
226
    {
      status = true;
    }
227
228
229
  else if ( str_is_equal(lc_units, "degree")
            && !str_is_equal(stdname, "grid_longitude")
            && !str_is_equal(stdname, "longitude") )
230
231
232
    {
      int ioff = 6;
      if ( lc_units[ioff] == 's' ) ioff++;
233
      if ( lc_units[ioff] == ' ' ) ioff++;
234
235
236
237
238
239
      if ( lc_units[ioff] == '_' ) ioff++;
      if ( lc_units[ioff] == 'n' || lc_units[ioff] == 's' ) status = true;
    }

  return status;
}
240
241


242
243
bool is_x_axis(const char *units, const char *stdname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
  (void)units;
245
  return (strcmp(stdname, "projection_x_coordinate") == 0);
246
247
248
249
250
}


bool is_y_axis(const char *units, const char *stdname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
  (void)units;
252
  return (strcmp(stdname, "projection_y_coordinate") == 0);
253
254
255
}


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
void set_gridtype(const char *attstring, int *gridtype)
{
  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 )
271
    *gridtype = GRID_UNSTRUCTURED;
272
  else if ( strcmp(attstring, "unstructured") == 0 )
273
    *gridtype = GRID_UNSTRUCTURED;
274
  else if ( strcmp(attstring, "curvilinear") == 0 )
275
    ;
276
277
  else if ( strcmp(attstring, "characterxy") == 0 )
    *gridtype = GRID_CHARXY;
278
279
280
281
282
283
  else if ( strcmp(attstring, "sinusoidal") == 0 )
    ;
  else if ( strcmp(attstring, "laea") == 0 )
    ;
  else if ( strcmp(attstring, "lcc2") == 0 )
    ;
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  else if ( strcmp(attstring, "linear") == 0 ) // ignore grid type linear
    ;
  else
    {
      static bool warn = true;
      if ( warn )
        {
          warn = false;
          Warning("NetCDF attribute grid_type='%s' unsupported!", attstring);
        }
    }
}


void set_zaxistype(const char *attstring, int *zaxistype)
{
  if      ( strcmp(attstring, "toa") == 0 ) *zaxistype = ZAXIS_TOA;
  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);
        }
    }  
}
321
322
323
324


void set_calendar(const char *attstring, int *calendar)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
  if ( str_is_equal(attstring, "standard") )
326
    *calendar = CALENDAR_STANDARD;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327
328
  else if ( str_is_equal(attstring, "gregorian") )
    *calendar = CALENDAR_GREGORIAN;
329
  else if ( str_is_equal(attstring, "none") )
330
    *calendar = CALENDAR_NONE;
331
  else if ( str_is_equal(attstring, "proleptic") )
332
    *calendar = CALENDAR_PROLEPTIC;
333
  else if ( str_is_equal(attstring, "360") )
334
    *calendar = CALENDAR_360DAYS;
335
336
  else if ( str_is_equal(attstring, "365") ||
            str_is_equal(attstring, "noleap") )
337
    *calendar = CALENDAR_365DAYS;
338
339
  else if ( str_is_equal(attstring, "366") ||
            str_is_equal(attstring, "all_leap") )
340
341
342
343
    *calendar = CALENDAR_366DAYS;
  else
    Warning("calendar >%s< unsupported!", attstring);
}