forked from NOAA-EMC/NCEPLIBS-g2c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg2cio.c
More file actions
491 lines (464 loc) · 13.8 KB
/
g2cio.c
File metadata and controls
491 lines (464 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
* @file
* @brief File I/O functions for the g2c library.
* @author Ed Hartnett @date Nov 11, 2022
*/
#include "grib2_int.h"
#define BITSHIFT_7 7 /**< 7 bits. */
#define BITSHIFT_15 15 /**< 15 bits. */
#define BITSHIFT_31 31 /**< 31 bits. */
#define BITSHIFT_63 63 /**< 63 bits. */
/**
* Read or write a big-endian integer type to an open file, with
* conversion between native and big-endian format.
*
* GRIB2 handles negative numbers in a special way. Instead of storing
* two-compliments, like every other programmer and computing
* organization in the world, GRIB2 flips the first bit, then stores
* the rest of the int as an unsigned number in the remaining 31
* bits. How exciting!
*
* This function takes the excitement out of GRIB2 negative numbers.
*
* @param f Pointer to the open FILE.
* @param write Non-zero if function should write, otherwise function
* will read.
* @param g2ctype The type to be read or written.
* @param var Pointer to the int to be written, or pointer to the
* storage that gets the int read.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett @date 11/7/22
*/
int
g2c_file_io(FILE *f, int write, int g2ctype, void *var)
{
void *void_be;
signed char *bvar = NULL;
short *svar = NULL;
int *ivar = NULL;
long long int *i64var = NULL;
unsigned long long int int64_be, int64_tmp;
unsigned int int_be, int_tmp;
unsigned short short_be, short_tmp;
unsigned char byte_be, byte_tmp;
int type_len;
/* Check inputs. */
if (!f || !var || g2ctype < G2C_BYTE || g2ctype > G2C_UINT64)
return G2C_EINVAL;
switch (g2ctype)
{
case G2C_BYTE:
case G2C_UBYTE:
type_len = ONE_BYTE;
bvar = var;
void_be = &byte_be;
if (write)
{
/* Are we writing a negative number? */
if (g2ctype == G2C_BYTE && *bvar < 0)
{
byte_tmp = -1 * *bvar; /* Store as positive. */
byte_tmp |= 1UL << BITSHIFT_7; /* Set sign bit. */
}
else
byte_tmp = *bvar;
/* Convert result to big-endian. */
byte_be = byte_tmp;
}
break;
case G2C_SHORT:
case G2C_USHORT:
type_len = TWO_BYTES;
svar = var;
void_be = &short_be;
if (write)
{
/* Are we writing a negative number? */
if (g2ctype == G2C_SHORT && *svar < 0)
{
short_tmp = -1 * *svar; /* Store as positive. */
short_tmp |= 1UL << BITSHIFT_15; /* Set sign bit. */
}
else
short_tmp = *svar;
/* Convert result to big-endian. */
short_be = ntohs(short_tmp);
}
break;
case G2C_INT:
case G2C_UINT:
type_len = FOUR_BYTES;
ivar = var;
void_be = &int_be;
if (write)
{
/* Are we writing a negative number? */
if (g2ctype == G2C_INT && *ivar < 0)
{
int_tmp = -1 * *ivar; /* Store as positive. */
int_tmp |= 1UL << BITSHIFT_31; /* Set sign bit. */
}
else
int_tmp = *ivar;
/* Convert result to big-endian. */
int_be = ntohl(int_tmp);
}
break;
case G2C_INT64:
case G2C_UINT64:
type_len = EIGHT_BYTES;
i64var = var;
void_be = &int64_be;
if (write)
{
/* Are we writing a negative number? */
if (g2ctype == G2C_INT64 && *i64var < 0)
{
int64_tmp = -1 * *i64var; /* Store as positive. */
int64_tmp |= 1ULL << BITSHIFT_63; /* Set sign bit. */
}
else
int64_tmp = *i64var;
/* Convert result to big-endian. */
int64_be = ntoh64(int64_tmp);
}
break;
default:
return G2C_EBADTYPE;
}
if (write)
{
if ((fwrite(void_be, type_len, 1, f)) != 1)
return G2C_EFILE;
}
else
{
/* Read from the file. */
if ((fread(void_be, type_len, 1, f)) != 1)
return G2C_EFILE;
switch (g2ctype)
{
case G2C_BYTE:
case G2C_UBYTE:
/* No conversion needed for one-byte values. */
*bvar = byte_be;
/* Did we read a negative number? Check the sign bit... */
if (g2ctype == G2C_BYTE && *bvar & 1 << BITSHIFT_7)
{
*bvar &= ~(1UL << BITSHIFT_7); /* Clear sign bit. */
*bvar *= -1; /* Make it negative. */
}
break;
case G2C_SHORT:
case G2C_USHORT:
/* Convert from big-endian. */
*svar = htons(short_be);
/* Did we read a negative number? Check the sign bit... */
if (g2ctype == G2C_SHORT && *svar & 1 << BITSHIFT_15)
{
*svar &= ~(1UL << BITSHIFT_15); /* Clear sign bit. */
*svar *= -1; /* Make it negative. */
}
break;
case G2C_INT:
case G2C_UINT:
/* Convert from big-endian. */
*ivar = htonl(int_be);
/* Did we read a negative number? Check the sign bit... */
if (g2ctype == G2C_INT && *ivar & 1 << BITSHIFT_31)
{
*ivar &= ~(1UL << BITSHIFT_31); /* Clear sign bit. */
*ivar *= -1; /* Make it negative. */
}
break;
case G2C_INT64:
case G2C_UINT64:
/* Convert from big-endian. */
*i64var = hton64(int64_be);
/* Did we read a negative number? Check the sign bit... */
if (g2ctype == G2C_INT64 && *i64var & 1ULL << BITSHIFT_63)
{
*i64var &= ~(1ULL << BITSHIFT_63); /* Clear sign bit. */
*i64var *= -1; /* Make it negative. */
}
break;
default:
return G2C_EBADTYPE;
}
}
return G2C_NOERROR;
}
/**
* Read or write a big-endian 4-byte signed int to an open GRIB2 file,
* with conversion between native and big-endian format, and special
* GRIB2 handling of negative numbers.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the int.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/7/22
*/
int
g2c_file_io_int(FILE *f, int write, int *var)
{
return g2c_file_io(f, write, G2C_INT, var);
}
/**
* Read or write a big-endian 4-byte unsigned int to an open GRIB2
* file, with conversion between native and big-endian format.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the unsigned int.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/7/22
*/
int
g2c_file_io_uint(FILE *f, int write, unsigned int *var)
{
return g2c_file_io(f, write, G2C_UINT, var);
}
/**
* Read or write a big-endian signed short to an open GRIB2 file, with
* conversion between native and big-endian format, and special GRIB2
* handling of negative numbers.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the short.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/13/22
*/
int
g2c_file_io_short(FILE *f, int write, short *var)
{
return g2c_file_io(f, write, G2C_SHORT, var);
}
/**
* Read or write a big-endian unsigned short to an open GRIB2 file,
* with conversion between native and big-endian format.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the unsigned short.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/13/22
*/
int
g2c_file_io_ushort(FILE *f, int write, unsigned short *var)
{
return g2c_file_io(f, write, G2C_USHORT, var);
}
/**
* Read or write a big-endian signed byte to an open GRIB2 file, with
* conversion between native and big-endian format, and special GRIB2
* handling of negative numbers.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the byte.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/13/22
*/
int
g2c_file_io_byte(FILE *f, int write, char *var)
{
return g2c_file_io(f, write, G2C_BYTE, var);
}
/**
* Read or write a big-endian unsigned byte to an open GRIB2 file,
* with conversion between native and big-endian format.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the unsigned byte.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/13/22
*/
int
g2c_file_io_ubyte(FILE *f, int write, unsigned char *var)
{
return g2c_file_io(f, write, G2C_UBYTE, var);
}
/**
* Read or write a big-endian signed long long to an open GRIB2 file, with
* conversion between native and big-endian format, and special GRIB2
* handling of negative numbers.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the long long.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/13/22
*/
int
g2c_file_io_longlong(FILE *f, int write, long long *var)
{
return g2c_file_io(f, write, G2C_INT64, var);
}
/**
* Read or write a big-endian unsigned long long to an open GRIB2 file,
* with conversion between native and big-endian format.
*
* @param f Pointer to the open GRIB2 FILE.
* @param write Non-zero to write, zero to read.
* @param var Pointer to the unsigned long long.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/13/22
*/
int
g2c_file_io_ulonglong(FILE *f, int write, unsigned long long *var)
{
return g2c_file_io(f, write, G2C_UINT64, var);
}
/**
* Read or write a big-endian 4-byte int or unsigned int from or to an
* open file, with conversion between native and big-endian format,
* and handling of GRIB negative numbers. This is for template values.
*
* With template values, if the map value is negative, then the
* template value may be negative.
*
* @param f Pointer to the open FILE.
* @param rw_flag Non-zero if function should write, otherwise function
* will read.
* @param map The map value for this template item.
* @param template_value Pointer to the template value to be written,
* or pointer to the storage that gets the templage value read.
*
* @return
* - :: G2C_NOERROR No error.
* - :: G2C_EINVAL Invalid input.
* - :: G2C_EFILE Error reading/writing file.
*
* @author Ed Hartnett 11/7/22
*/
int
g2c_file_io_template(FILE *f, int rw_flag, int map, long long int *template_value)
{
int ret;
/* Take the absolute value of map[t] because some of the
* numbers are negative - used to indicate that the
* cooresponding fields can contain negative data (needed for
* unpacking). */
switch (abs(map))
{
case ONE_BYTE:
if (map < 0)
{
char my_byte;
if (rw_flag)
my_byte = *template_value;
if ((ret = g2c_file_io_byte(f, rw_flag, &my_byte)))
return ret;
if (!rw_flag)
*template_value = my_byte;
}
else
{
unsigned char my_ubyte;
if (rw_flag)
my_ubyte = *template_value;
if ((ret = g2c_file_io_ubyte(f, rw_flag, &my_ubyte)))
return ret;
if (!rw_flag)
*template_value = my_ubyte;
}
/* FILE_BE_INT1P(f, rw_flag, template_value); */
break;
case TWO_BYTES:
if (map < 0)
{
short my_short;
if (rw_flag)
my_short = *template_value;
if ((ret = g2c_file_io_short(f, rw_flag, &my_short)))
return ret;
if (!rw_flag)
*template_value = my_short;
}
else
{
unsigned short my_ushort;
if (rw_flag)
my_ushort = *template_value;
if ((ret = g2c_file_io_ushort(f, rw_flag, &my_ushort)))
return ret;
if (!rw_flag)
*template_value = my_ushort;
}
/* FILE_BE_INT2P(f, rw_flag, template_value); */
break;
case FOUR_BYTES:
if (map < 0)
{
int my_int;
if (rw_flag)
my_int = *template_value;
if ((ret = g2c_file_io_int(f, rw_flag, &my_int)))
return ret;
if (!rw_flag)
*template_value = my_int;
}
else
{
unsigned int my_uint;
if (rw_flag)
my_uint = *template_value;
if ((ret = g2c_file_io_uint(f, rw_flag, &my_uint)))
return ret;
if (!rw_flag)
*template_value = my_uint;
}
break;
default:
return G2C_EBADTEMPLATE;
}
return G2C_NOERROR;
}