-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgrib.c
3022 lines (2744 loc) · 105 KB
/
wgrib.c
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <math.h>
#include <float.h>
/* version 1.2.1 of grib headers w. ebisuzaki */
#ifndef INT2
#define INT2(a,b) ((1-(int) ((unsigned) (a & 0x80) >> 6)) * (int) (((a & 0x7f) << 8) + b))
#endif
#define BDS_LEN(bds) ((int) ((bds[0]<<16)+(bds[1]<<8)+bds[2]))
#define BDS_Flag(bds) (bds[3])
#define BDS_Grid(bds) ((bds[3] & 128) == 0)
#define BDS_Harmonic(bds) (bds[3] & 128)
#define BDS_Packing(bds) ((bds[3] & 64) != 0)
#define BDS_SimplePacking(bds) ((bds[3] & 64) == 0)
#define BDS_ComplexPacking(bds) ((bds[3] & 64) != 0)
#define BDS_OriginalType(bds) ((bds[3] & 32) != 0)
#define BDS_OriginalFloat(bds) ((bds[3] & 32) == 0)
#define BDS_OriginalInt(bds) ((bds[3] & 32) != 0)
#define BDS_MoreFlags(bds) ((bds[3] & 16) != 0)
#define BDS_UnusedBits(bds) ((int) (bds[3] & 15))
#define BDS_BinScale(bds) INT2(bds[4],bds[5])
#define BDS_RefValue(bds) (ibm2flt(bds+6))
#define BDS_NumBits(bds) ((int) bds[10])
#define BDS_DataStart(bds) ((int) (11 + BDS_MoreFlags(bds)*3))
/* breaks if BDS_NumBits(bds) == 0 */
#define BDS_NValues(bds) (((BDS_LEN(bds) - BDS_DataStart(bds))*8 - \
BDS_UnusedBits(bds)) / BDS_NumBits(bds))
/*
#define BDS_NValues(bds) ((BDS_NumBits(bds) == 0) ? 0 : \
(((BDS_LEN(bds) - BDS_DataStart(bds))*8 - \
BDS_UnusedBits(bds)) / BDS_NumBits(bds)))
*/
/* undefined value -- if bitmap */
#define UNDEFINED 9.999e20
/* version 1.2 of grib headers w. ebisuzaki */
#define BMS_LEN(bms) ((bms) == NULL ? 0 : (bms[0]<<16)+(bms[1]<<8)+bms[2])
#define BMS_UnusedBits(bms) ((bms) == NULL ? 0 : bms[3])
#define BMS_StdMap(bms) ((bms) == NULL ? 0 : ((bms[4]<<8) + bms[5]))
#define BMS_bitmap(bms) ((bms) == NULL ? NULL : (bms)+6)
#define BMS_nxny(bms) ((((bms) == NULL) || BMS_StdMap(bms)) \
? 0 : (BMS_LEN(bms)*8 - 48 - BMS_UnusedBits(bms)))
/* cnames_file.c */
/* search order for parameter names
*
* #define P_TABLE_FIRST
* look at external parameter table first
*
* otherwise use builtin NCEP-2 or ECMWF-160 first
*/
/* #define P_TABLE_FIRST */
/* search order for external parameter table
* 1) environment variable GRIBTAB
* 2) environment variable gribtab
* 3) the file 'gribtab' in current directory
*/
/* cnames.c */
/* then default values */
char *k5toa(unsigned char *pds);
char *k5_comments(unsigned char *pds);
int setup_user_table(int center, int subcenter, int ptable);
struct ParmTable {
char *name, *comment;
};
/* version 1.4.1 of grib headers w. ebisuzaki */
/* this version is incomplete */
#ifndef INT3
#define INT3(a,b,c) ((1-(int) ((unsigned) (a & 0x80) >> 6)) * (int) (((a & 127) << 16)+(b<<8)+c))
#endif
#ifndef INT2
#define INT2(a,b) ((1-(int) ((unsigned) (a & 0x80) >> 6)) * (int) (((a & 127) << 8) + b))
#endif
#ifndef UINT3
#define UINT3(a,b,c) ((int) ((a << 16) + (b << 8) + (c)))
#endif
#ifndef UINT2
#define UINT2(a,b) ((int) ((a << 8) + (b)))
#endif
#define GDS_Len1(gds) (gds[0])
#define GDS_Len2(gds) (gds[1])
#define GDS_Len3(gds) (gds[2])
#define GDS_LEN(gds) ((int) ((gds[0]<<16)+(gds[1]<<8)+gds[2]))
#define GDS_NV(gds) (gds[3])
#define GDS_DataType(gds) (gds[5])
#define GDS_LatLon(gds) (gds[5] == 0)
#define GDS_Mercator(gds) (gds[5] == 1)
#define GDS_Gnomonic(gds) (gds[5] == 2)
#define GDS_Lambert(gds) (gds[5] == 3)
#define GDS_Gaussian(gds) (gds[5] == 4)
#define GDS_Polar(gds) (gds[5] == 5)
#define GDS_RotLL(gds) (gds[5] == 10)
#define GDS_Harmonic(gds) (gds[5] == 50)
#define GDS_ssEgrid(gds) (gds[5] == 201) /* semi-staggered E grid */
#define GDS_fEgrid(gds) (gds[5] == 202) /* filled E grid */
#define GDS_LatLon_nx(gds) ((int) ((gds[6] << 8) + gds[7]))
#define GDS_LatLon_ny(gds) ((int) ((gds[8] << 8) + gds[9]))
#define GDS_LatLon_La1(gds) INT3(gds[10],gds[11],gds[12])
#define GDS_LatLon_Lo1(gds) INT3(gds[13],gds[14],gds[15])
#define GDS_LatLon_mode(gds) (gds[16])
#define GDS_LatLon_La2(gds) INT3(gds[17],gds[18],gds[19])
#define GDS_LatLon_Lo2(gds) INT3(gds[20],gds[21],gds[22])
#define GDS_LatLon_dx(gds) INT2(gds[23],gds[24])
#define GDS_LatLon_dy(gds) INT2(gds[25],gds[26])
#define GDS_Gaussian_nlat(gds) ((gds[25]<<8)+gds[26])
#define GDS_LatLon_scan(gds) (gds[27])
#define GDS_Polar_nx(gds) ((gds[6] << 8) + gds[7])
#define GDS_Polar_ny(gds) ((gds[8] << 8) + gds[9])
#define GDS_Polar_La1(gds) (INT3(gds[10],gds[11],gds[12]))
#define GDS_Polar_Lo1(gds) (INT3(gds[13],gds[14],gds[15]))
#define GDS_Polar_Lov(gds) (INT3(gds[17],gds[18],gds[19]))
#define GDS_Polar_scan(gds) (gds[27])
#define GDS_Polar_Dx(gds) (INT3(gds[20], gds[21], gds[22]))
#define GDS_Polar_Dy(gds) (INT3(gds[23], gds[24], gds[25]))
#define GDS_Polar_pole(gds) ((gds[26] & 128) == 128)
#define GDS_Lambert_nx(gds) ((gds[6] << 8) + gds[7])
#define GDS_Lambert_ny(gds) ((gds[8] << 8) + gds[9])
#define GDS_Lambert_La1(gds) INT3(gds[10],gds[11],gds[12])
#define GDS_Lambert_Lo1(gds) INT3(gds[13],gds[14],gds[15])
#define GDS_Lambert_mode(gds) (gds[16])
#define GDS_Lambert_Lov(gds) INT3(gds[17],gds[18],gds[19])
#define GDS_Lambert_dx(gds) INT3(gds[20],gds[21],gds[22])
#define GDS_Lambert_dy(gds) INT3(gds[23],gds[24],gds[25])
#define GDS_Lambert_NP(gds) ((gds[26] & 128) == 0)
#define GDS_Lambert_scan(gds) (gds[27])
#define GDS_Lambert_Latin1(gds) INT3(gds[28],gds[29],gds[30])
#define GDS_Lambert_Latin2(gds) INT3(gds[31],gds[32],gds[33])
#define GDS_Lambert_LatSP(gds) INT3(gds[34],gds[35],gds[36])
#define GDS_Lambert_LonSP(gds) INT3(gds[37],gds[37],gds[37])
#define GDS_ssEgrid_n(gds) UINT2(gds[6],gds[7])
#define GDS_ssEgrid_n_dum(gds) UINT2(gds[8],gds[9])
#define GDS_ssEgrid_La1(gds) INT3(gds[10],gds[11],gds[12])
#define GDS_ssEgrid_Lo1(gds) INT3(gds[13],gds[14],gds[15])
#define GDS_ssEgrid_mode(gds) (gds[16])
#define GDS_ssEgrid_La2(gds) UINT3(gds[17],gds[18],gds[19])
#define GDS_ssEgrid_Lo2(gds) UINT3(gds[20],gds[21],gds[22])
#define GDS_ssEgrid_di(gds) INT2(gds[23],gds[24])
#define GDS_ssEgrid_dj(gds) INT2(gds[25],gds[26])
#define GDS_ssEgrid_scan(gds) (gds[27])
#define GDS_fEgrid_n(gds) UINT2(gds[6],gds[7])
#define GDS_fEgrid_n_dum(gds) UINT2(gds[8],gds[9])
#define GDS_fEgrid_La1(gds) INT3(gds[10],gds[11],gds[12])
#define GDS_fEgrid_Lo1(gds) INT3(gds[13],gds[14],gds[15])
#define GDS_fEgrid_mode(gds) (gds[16])
#define GDS_fEgrid_La2(gds) UINT3(gds[17],gds[18],gds[19])
#define GDS_fEgrid_Lo2(gds) UINT3(gds[20],gds[21],gds[22])
#define GDS_fEgrid_di(gds) INT2(gds[23],gds[24])
#define GDS_fEgrid_dj(gds) INT2(gds[25],gds[26])
#define GDS_fEgrid_scan(gds) (gds[27])
#define GDS_Merc_nx(gds) UINT2(gds[6],gds[7])
#define GDS_Merc_ny(gds) UINT2(gds[8],gds[9])
#define GDS_Merc_La1(gds) INT3(gds[10],gds[11],gds[12])
#define GDS_Merc_Lo1(gds) INT3(gds[13],gds[14],gds[15])
#define GDS_Merc_mode(gds) (gds[16])
#define GDS_Merc_La2(gds) INT3(gds[17],gds[18],gds[19])
#define GDS_Merc_Lo2(gds) INT3(gds[20],gds[21],gds[22])
#define GDS_Merc_Latin(gds) INT3(gds[23],gds[24],gds[25])
#define GDS_Merc_scan(gds) (gds[27])
#define GDS_Merc_dx(gds) INT3(gds[28],gds[29],gds[30])
#define GDS_Merc_dy(gds) INT3(gds[31],gds[32],gds[33])
/* rotated Lat-lon grid */
#define GDS_RotLL_nx(gds) UINT2(gds[6],gds[7])
#define GDS_RotLL_ny(gds) UINT2(gds[8],gds[9])
#define GDS_RotLL_La1(gds) INT3(gds[10],gds[11],gds[12])
#define GDS_RotLL_Lo1(gds) INT3(gds[13],gds[14],gds[15])
#define GDS_RotLL_mode(gds) (gds[16])
#define GDS_RotLL_La2(gds) INT3(gds[17],gds[18],gds[19])
#define GDS_RotLL_Lo2(gds) INT3(gds[20],gds[21],gds[22])
#define GDS_RotLL_dx(gds) INT2(gds[23],gds[24])
#define GDS_RotLL_dy(gds) INT2(gds[25],gds[26])
#define GDS_RotLL_scan(gds) (gds[27])
#define GDS_RotLL_LaSP(gds) INT3(gds[32],gds[33],gds[34])
#define GDS_RotLL_LoSP(gds) INT3(gds[35],gds[36],gds[37])
#define GDS_RotLL_RotAng(gds) ibm2flt(&(gds[38]))
/* index of NV and PV */
#define GDS_PV(gds) ((gds[3] == 0) ? -1 : (int) gds[4] - 1)
#define GDS_PL(gds) ((gds[4] == 255) ? -1 : (int) gds[3] * 4 + (int) gds[4] - 1)
enum Def_NCEP_Table {rean, opn, rean_nowarn, opn_nowarn};
unsigned char *seek_grib(FILE *file, long *pos, long *len_grib,
unsigned char *buffer, unsigned int buf_len);
int read_grib(FILE *file, long pos, long len_grib, unsigned char *buffer);
double ibm2flt(unsigned char *ibm);
void BDS_unpack(float *flt, unsigned char *bits, unsigned char *bitmap,
int n_bits, int n, double ref, double scale);
double int_power(double x, int y);
int flt2ieee(float x, unsigned char *ieee);
int wrtieee(float *array, int n, int header, FILE *output);
int wrtieee_header(unsigned int n, FILE *output);
char *PDStimes(int time_range, int p1, int p2, int time_unit);
int missing_points(unsigned char *bitmap, int n);
void EC_ext(unsigned char *pds, char *prefix, char *suffix);
int GDS_grid(unsigned char *gds, int *nx, int *ny, long int *nxny);
void GDS_prt_thin_lon(unsigned char *gds);
int PDS_date(unsigned char *pds, int option, int verf_time);
int add_time(int *year, int *month, int *day, int *hour, int dtime, int unit);
int verf_time(unsigned char *pds, int *year, int *month, int *day, int *hour);
void print_pds(unsigned char *pds, int print_PDS, int print_PDS10, int verbose);
void print_gds(unsigned char *gds, int print_GDS, int print_GDS10, int verbose);
void ensemble(unsigned char *pds, int mode);
/* version 3.4 of grib headers w. ebisuzaki */
/* this version is incomplete */
#ifndef INT2
#define INT2(a,b) ((1-(int) ((unsigned) (a & 0x80) >> 6)) * (int) (((a & 0x7f) << 8) + b))
#endif
#define PDS_Len1(pds) (pds[0])
#define PDS_Len2(pds) (pds[1])
#define PDS_Len3(pds) (pds[2])
#define PDS_LEN(pds) ((int) ((pds[0]<<16)+(pds[1]<<8)+pds[2]))
#define PDS_Vsn(pds) (pds[3])
#define PDS_Center(pds) (pds[4])
#define PDS_Model(pds) (pds[5])
#define PDS_Grid(pds) (pds[6])
#define PDS_HAS_GDS(pds) ((pds[7] & 128) != 0)
#define PDS_HAS_BMS(pds) ((pds[7] & 64) != 0)
#define PDS_PARAM(pds) (pds[8])
#define PDS_L_TYPE(pds) (pds[9])
#define PDS_LEVEL1(pds) (pds[10])
#define PDS_LEVEL2(pds) (pds[11])
#define PDS_KPDS5(pds) (pds[8])
#define PDS_KPDS6(pds) (pds[9])
#define PDS_KPDS7(pds) ((int) ((pds[10]<<8) + pds[11]))
/* this requires a 32-bit default integer machine */
#define PDS_Field(pds) ((pds[8]<<24)+(pds[9]<<16)+(pds[10]<<8)+pds[11])
#define PDS_Year(pds) (pds[12])
#define PDS_Month(pds) (pds[13])
#define PDS_Day(pds) (pds[14])
#define PDS_Hour(pds) (pds[15])
#define PDS_Minute(pds) (pds[16])
#define PDS_ForecastTimeUnit(pds) (pds[17])
#define PDS_P1(pds) (pds[18])
#define PDS_P2(pds) (pds[19])
#define PDS_TimeRange(pds) (pds[20])
#define PDS_NumAve(pds) ((int) ((pds[21]<<8)+pds[22]))
#define PDS_NumMissing(pds) (pds[23])
#define PDS_Century(pds) (pds[24])
#define PDS_Subcenter(pds) (pds[25])
#define PDS_DecimalScale(pds) INT2(pds[26],pds[27])
/* old #define PDS_Year4(pds) (pds[12] + 100*(pds[24] - (pds[12] != 0))) */
#define PDS_Year4(pds) (pds[12] + 100*(pds[24] - 1))
/* various centers */
#define NMC 7
#define ECMWF 98
/* ECMWF Extensions */
#define PDS_EcLocalId(pds) (PDS_LEN(pds) >= 41 ? (pds[40]) : 0)
#define PDS_EcClass(pds) (PDS_LEN(pds) >= 42 ? (pds[41]) : 0)
#define PDS_EcType(pds) (PDS_LEN(pds) >= 43 ? (pds[42]) : 0)
#define PDS_EcStream(pds) (PDS_LEN(pds) >= 45 ? (INT2(pds[43], pds[44])) : 0)
#define PDS_EcENS(pds) (PDS_LEN(pds) >= 52 && pds[40] == 1 && \
pds[43] * 256 + pds[44] == 1035 && pds[50] != 0)
#define PDS_EcFcstNo(pds) (pds[50])
#define PDS_EcNoFcst(pds) (pds[51])
/* NCEP Extensions */
#define PDS_NcepENS(pds) (PDS_LEN(pds) >= 44 && pds[25] == 2 && pds[40] == 1)
#define PDS_NcepFcstType(pds) (pds[41])
#define PDS_NcepFcstNo(pds) (pds[42])
#define PDS_NcepFcstProd(pds) (pds[43])
#define VERSION "v1.6.2.5 (5-08-98) Wesley Ebisuzaki"
#define CHECK_GRIB
/*
* wgrib.c extract/inventory grib records
*
* Wesley Ebisuzaki
*
* 11/94 - v1.0
* 11/94 - v1.1: arbitary size grids, -i option
* 11/94 - v1.2: bug fixes, ieee option, more info
* 1/95 - v1.2.4: fix headers for SUN acc
* 2/95 - v1.2.5: add num_ave in -s listing
* 2/95 - v1.2.6: change %d to %ld
* 2/95 - v1.2.7: more output, added some polar stereographic support
* 2/95 - v1.2.8: max min format changed %f to %g, tidying up more info
* 3/95 - v1.3.0: fix bug with bitmap, allow numbers > UNDEFINED
* 3/95 - v1.3.1: print number of missing points (verbose)
* 3/95 - v1.3.2: -append option added
* 4/95 - v1.3.2a,b: more output, polar stereo support (-V option)
* 4/95 - v1.3.3: added ECMWF parameter table (prelim)
* 6/95 - v1.3.4: nxny from BDS rather than gds?
* 9/95 - v1.3.4d: speedup in grib write
* 11/95 - v1.3.4f: new ECMWF parameter table (from Mike Fiorino), EC logic
* 2/96 - v1.3.4g-h: prelim fix for GDS-less grib files
* 2/96 - v1.3.4i: faster missing(), -V: "pos n" -> "n" (field 2)
* 3/96 - v1.4: fix return code (!inventory), and short records near EOF
* 6/96 - v1.4.1a: faster grib->binary decode, updated ncep parameter table, mod. in clim. desc
* 7/96 - v1.5.0: parameter-table aware, -v option changed, added "comments"
* increased NTRY to 100 in seek_grib
* 11/96 - v1.5.0b: added ECMWF parameter table 128
* 1/97 - v1.5.0b2: if nxny != nx*ny { nx = nxny; ny = 1 }
* 3/97 - v1.5.0b5: added: -PDS -GDS, Lambert Conformal
* 3/97 - v1.5.0b6: added: -verf
* 4/97 - v1.5.0b7: added -PDS10, -GDS10 and enhanced -PDS -GDS
* 4/97 - v1.5.0b8: "bitmap missing x" -> "bitmap: x undef"
* 5/97 - v1.5.0b9: thinned grids meta data
* 5/97 - v1.5.0b10: changed 0hr fcst to anal for TR=10 and P1=P2=0
* 5/97 - v1.5.0b10: added -H option
* 6/97 - v1.5.0b12: thinned lat-long grids -V option
* 6/97 - v1.5.0b13: -4yr
* 6/97 - v1.5.0b14: fix century mark Y=100 not 0
* 7/97 - v1.5.0b15: add ncep opn grib table
* 12/97 - v1.6.1.a: made ncep_opn the default table
* 12/97 - v1.6.1.b: changed 03TOT to O3TOT in operational ncep table
* 1/98 - v1.6.2: added Arakawa E grid meta-data
* 1/98 - v1.6.2.1: added some mode data, Scan -> scan
* 4/98 - v1.6.2.4: reanalysis id code: subcenter==0 && process==180
*
*/
/*
* MSEEK = I/O buffer size for seek_grib
*/
#define MSEEK 1024
#define BUFF_ALLOC0 40000
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) < (b) ? (b) : (a))
#endif
#ifndef DEF_T62_NCEP_TABLE
#define DEF_T62_NCEP_TABLE rean
#endif
enum Def_NCEP_Table def_ncep_table = DEF_T62_NCEP_TABLE;
char *levels(int, int, int);
void print_pds(unsigned char *pds, int print_PDS, int print_PDS10, int verbose) {
int i, j;
j = PDS_LEN(pds);
if (verbose < 2) {
if (print_PDS && verbose < 2) {
printf(":PDS=");
for (i = 0; i < j; i++) {
printf("%2.2x", (int) pds[i]);
}
}
if (print_PDS10 && verbose < 2) {
printf(":PDS10=");
for (i = 0; i < j; i++) {
printf(" %d", (int) pds[i]);
}
}
}
else {
if (print_PDS) {
printf(" PDS(1..%d)=",j);
for (i = 0; i < j; i++) {
if (i % 20 == 0) printf("\n %4d:",i+1);
printf(" %3.2x", (int) pds[i]);
}
printf("\n");
}
if (print_PDS10) {
printf(" PDS10(1..%d)=",j);
for (i = 0; i < j; i++) {
if (i % 20 == 0) printf("\n %4d:",i+1);
printf(" %3d", (int) pds[i]);
}
printf("\n");
}
}
}
void print_gds(unsigned char *gds, int print_GDS, int print_GDS10, int verbose) {
int i, j;
j = GDS_LEN(gds);
if (verbose < 2) {
if (print_GDS && verbose < 2) {
printf(":GDS=");
for (i = 0; i < j; i++) {
printf("%2.2x", (int) gds[i]);
}
}
if (print_GDS10 && verbose < 2) {
printf(":GDS10=");
for (i = 0; i < j; i++) {
printf(" %d", (int) gds[i]);
}
}
}
else {
if (print_GDS) {
printf(" GDS(1..%d)=",j);
for (i = 0; i < j; i++) {
if (i % 20 == 0) printf("\n %4d:",i+1);
printf(" %3.2x", (int) gds[i]);
}
printf("\n");
}
if (print_GDS10) {
printf(" GDS10(1..%d)=",j);
for (i = 0; i < j; i++) {
if (i % 20 == 0) printf("\n %4d:",i+1);
printf(" %3d", (int) gds[i]);
}
printf("\n");
}
}
}
/*
* find next grib header
*
* file = what do you think?
* pos = initial position to start looking at ( = 0 for 1st call)
* returns with position of next grib header (units=bytes)
* len_grib = length of the grib record (bytes)
* buffer[buf_len] = buffer for reading/writing
*
* returns (char *) to start of GRIB header+PDS
* NULL if not found
*
* adapted from SKGB (Mark Iredell)
*
* v1.1 9/94 Wesley Ebisuzaki
* v1.2 3/96 Wesley Ebisuzaki handles short records at end of file
* v1.3 8/96 Wesley Ebisuzaki increase NTRY from 3 to 100 for the folks
* at Automation decided a 21 byte WMO bulletin header wasn't long
* enough and decided to go to an 8K header.
*
*/
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
#define NTRY 100
/* #define LEN_HEADER_PDS (28+42+100) */
#define LEN_HEADER_PDS (28+8)
unsigned char *seek_grib(FILE *file, long *pos, long *len_grib,
unsigned char *buffer, unsigned int buf_len) {
int i, j, len;
for (j = 0; j < NTRY; j++) {
if (fseek(file, *pos, SEEK_SET) == -1) {
*len_grib = 0;
return (unsigned char *) NULL;
}
i = fread(buffer, sizeof (unsigned char), buf_len, file);
len = i - LEN_HEADER_PDS;
for (i = 0; i < len; i++) {
if (buffer[i] == 'G' && buffer[i+1] == 'R' && buffer[i+2] == 'I'
&& buffer[i+3] == 'B' && buffer[i+7] == 1) {
*pos = i + *pos;
*len_grib = (buffer[i+4] << 16) + (buffer[i+5] << 8) +
buffer[i+6];
return (buffer+i);
}
}
*pos = *pos + (buf_len - LEN_HEADER_PDS);
}
*len_grib = 0;
return (unsigned char *) NULL;
}
/* wesley ebisuzaki v 1.1 */
double ibm2flt(unsigned char *ibm) {
int positive, power;
unsigned int abspower;
long int mant;
double value, exp;
positive = (ibm[0] & 0x80) == 0;
mant = (ibm[1] << 16) + (ibm[2] << 8) + ibm[3];
power = (int) (ibm[0] & 0x7f) - 64;
abspower = power > 0 ? power : -power;
/* calc exp */
exp = 16.0;
value = 1.0;
while (abspower) {
if (abspower & 1) {
value *= exp;
}
exp = exp * exp;
abspower >>= 1;
}
if (power < 0) value = 1.0 / value;
value = value * mant / 16777216.0;
if (positive == 0) value = -value;
return value;
}
/*
* read_grib.c
*
* v1.0 9/94 Wesley Ebisuzaki
*
*/
int read_grib(FILE *file, long pos, long len_grib, unsigned char *buffer) {
int i;
if (fseek(file, pos, SEEK_SET) == -1) {
return 0;
}
i = fread(buffer, sizeof (unsigned char), len_grib, file);
return (i == len_grib);
}
/*
* w. ebisuzaki
*
* return x**y
*
*
* input: double x
* int y
*/
double int_power(double x, int y) {
double value;
if (y < 0) {
y = -y;
x = 1.0 / x;
}
value = 1.0;
while (y) {
if (y & 1) {
value *= x;
}
x = x * x;
y >>= 1;
}
return value;
}
/* cnames.c Wesley Ebisuzaki
*
* returns strings with either variable name or comment field
* v1.4 4/98
* reanalysis can use process 180 and subcenter 0
*/
extern struct ParmTable parm_table_ncep_opn[256];
extern struct ParmTable parm_table_ncep_reanal[256];
extern struct ParmTable parm_table_omb[256];
extern struct ParmTable parm_table_ecmwf_128[256];
extern struct ParmTable parm_table_ecmwf_160[256];
extern struct ParmTable parm_table_user[256];
extern enum Def_NCEP_Table def_ncep_table;
/*
* returns pointer to the parameter table
*/
static struct ParmTable *Parm_Table(unsigned char *pds) {
int i, center, subcenter, ptable, process;
static int missing_count = 0, reanal_opn_count = 0;
center = PDS_Center(pds);
subcenter = PDS_Subcenter(pds);
ptable = PDS_Vsn(pds);
#ifdef P_TABLE_FIRST
i = setup_user_table(center, subcenter, ptable);
if (i == 1) return &parm_table_user[0];
#endif
/* NCEP opn and reanal */
if (center == NMC && ptable <= 3) {
if (subcenter == 1) return &parm_table_ncep_reanal[0];
process = PDS_Model(pds);
if (subcenter != 0 || (process != 80 && process != 180) ||
(ptable != 1 && ptable != 2))
return &parm_table_ncep_opn[0];
/* at this point could be either the opn or reanalysis table */
if (def_ncep_table == opn_nowarn) return &parm_table_ncep_opn[0];
if (def_ncep_table == rean_nowarn) return &parm_table_ncep_reanal[0];
if (reanal_opn_count++ == 0) {
fprintf(stderr, "Using NCEP %s table, see -ncep_opn, -ncep_rean options\n",
(def_ncep_table == opn) ? "opn" : "reanalysis");
}
return (def_ncep_table == opn) ? &parm_table_ncep_opn[0]
: &parm_table_ncep_reanal[0];
}
if (center == NMC && ptable == 128) return &parm_table_omb[0];
if (center == ECMWF && ptable == 128) return &parm_table_ecmwf_128[0];
if (center == ECMWF && ptable == 160) return &parm_table_ecmwf_160[0];
#ifndef P_TABLE_FIRST
i = setup_user_table(center, subcenter, ptable);
if (i == 1) return &parm_table_user[0];
#endif
if ((ptable > 3 || (PDS_PARAM(pds)) > 127) && missing_count++ == 0) {
fprintf(stderr,
"\nUndefined parameter table (center %d-%d table %d), using NCEP-opn\n",
center, subcenter, ptable);
}
return &parm_table_ncep_opn[0];
}
/*
* return name field of PDS_PARAM(pds)
*/
char *k5toa(unsigned char *pds) {
return (Parm_Table(pds) + PDS_PARAM(pds))->name;
}
/*
* return comment field of the PDS_PARAM(pds)
*/
char *k5_comments(unsigned char *pds) {
return (Parm_Table(pds) + PDS_PARAM(pds))->comment;
}
/* 1996 wesley ebisuzaki
*
* 7/97 v1.2 slight modifications
*
* returns a filled array
*/
static unsigned int mask[] = {0,1,3,7,15,31,63,127,255};
static unsigned int map_masks[8] = {128, 64, 32, 16, 8, 4, 2, 1};
void BDS_unpack(float *flt, unsigned char *bits, unsigned char *bitmap,
int n_bits, int n, double ref, double scale) {
int i, mask_idx, t_bits, c_bits, j_bits;
unsigned int j, map_mask, tbits, jmask, bbits;
long int jj;
tbits = bbits = map_mask = 0;
/* assume integer has 32+ bits */
if (n_bits <= 25) {
jmask = (1 << n_bits) - 1;
t_bits = 0;
if (bitmap) {
for (i = 0; i < n; i++) {
/* check bitmap */
mask_idx = i & 7;
if (mask_idx == 0) bbits = *bitmap++;
if ((bbits & map_masks[mask_idx]) == 0) {
*flt++ = UNDEFINED;
continue;
}
while (t_bits < n_bits) {
tbits = (tbits * 256) + *bits++;
t_bits += 8;
}
t_bits -= n_bits;
j = (tbits >> t_bits) & jmask;
*flt++ = ref + scale*j;
}
}
else {
for (i = 0; i < n; i++) {
while (t_bits < n_bits) {
tbits = (tbits * 256) + *bits++;
t_bits += 8;
}
t_bits -= n_bits;
flt[i] = (tbits >> t_bits) & jmask;
}
/* at least this vectorizes :) */
for (i = 0; i < n; i++) {
flt[i] = ref + scale*flt[i];
}
}
}
else {
/* older unoptimized code, not often used */
c_bits = 8;
while (n-- > 0) {
if (bitmap) {
j = (*bitmap & map_mask);
if ((map_mask >>= 1) == 0) {
map_mask = 128;
bitmap++;
}
if (j == 0) {
*flt++ = UNDEFINED;
continue;
}
}
jj = 0;
j_bits = n_bits;
while (c_bits <= j_bits) {
if (c_bits == 8) {
jj = (jj << 8) + *bits++;
j_bits -= 8;
}
else {
jj = (jj << c_bits) + (*bits & mask[c_bits]);
bits++;
j_bits -= c_bits;
c_bits = 8;
}
}
if (j_bits) {
c_bits -= j_bits;
jj = (jj << j_bits) + ((*bits >> c_bits) & mask[j_bits]);
}
*flt++ = ref + scale*jj;
}
}
return;
}
/*
* convert a float to an ieee single precision number v1.1
* (big endian)
* Wesley Ebisuzaki
*
* bugs: doesn't handle subnormal numbers
* bugs: assumes length of integer >= 25 bits
*/
int flt2ieee(float x, unsigned char *ieee) {
int sign, exp;
unsigned int umant;
double mant;
if (x == 0.0) {
ieee[0] = ieee[1] = ieee[2] = ieee[3] = 0;
return 0;
}
/* sign bit */
if (x < 0.0) {
sign = 128;
x = -x;
}
else sign = 0;
mant = frexp((double) x, &exp);
/* 2^24 = 16777216 */
umant = mant * 16777216 + 0.5;
if (umant >= 16777216) {
umant = umant / 2;
exp++;
}
/* bit 24 should be a 1 .. not used in ieee format */
exp = exp - 1 + 127;
if (exp < 0) {
/* signed zero */
ieee[0] = sign;
ieee[1] = ieee[2] = ieee[3] = 0;
return 0;
}
if (exp > 255) {
/* signed infinity */
ieee[0] = sign + 127;
ieee[1] = 128;
ieee[2] = ieee[3] = 0;
return 0;
}
/* normal number */
ieee[0] = sign + (exp >> 1);
ieee[3] = umant & 255;
ieee[2] = (umant >> 8) & 255;
ieee[1] = ((exp & 1) << 7) + ((umant >> 16) & 127);
return 0;
}
/* wesley ebisuzaki v1.2
*
* write ieee file -- big endian format
*
* input float *array data to be written
* int n size of array
* int header 1 for f77 style header 0 for none
* (header is 4 byte header
* FILE *output output file
*
* v1.2 7/97 buffered, faster
*/
#define BSIZ 1024*4
int wrtieee(float *array, int n, int header, FILE *output) {
unsigned long int l;
int i, nbuf;
unsigned char buff[BSIZ];
unsigned char h4[4];
nbuf = 0;
if (header) {
l = n * 4;
for (i = 0; i < 4; i++) {
h4[i] = l & 255;
l >>= 8;
}
buff[nbuf++] = h4[3];
buff[nbuf++] = h4[2];
buff[nbuf++] = h4[1];
buff[nbuf++] = h4[0];
}
for (i = 0; i < n; i++) {
if (nbuf >= BSIZ) {
fwrite(buff, 1, BSIZ, output);
nbuf = 0;
}
flt2ieee(array[i], buff + nbuf);
nbuf += 4;
}
if (header) {
if (nbuf == BSIZ) {
fwrite(buff, 1, BSIZ, output);
nbuf = 0;
}
buff[nbuf++] = h4[3];
buff[nbuf++] = h4[2];
buff[nbuf++] = h4[1];
buff[nbuf++] = h4[0];
}
if (nbuf) fwrite(buff, 1, nbuf, output);
return 0;
}
/* write a big-endian 4 byte integer .. f77 IEEE header */
int wrtieee_header(unsigned int n, FILE *output) {
unsigned h4[4];
h4[0] = n & 255;
h4[1] = (n >> 8) & 255;
h4[2] = (n >> 16) & 255;
h4[2] = (n >> 24) & 255;
putc(h4[3],output);
putc(h4[2],output);
putc(h4[1],output);
putc(h4[0],output);
return 0;
}
/* wesley ebisuzaki v1.0
*
* levels.c
*
* prints out a simple description of kpds6, kpds7
* (level/layer data)
* kpds6 = octet 10 of the PDS
* kpds7 = octet 11 and 12 of the PDS
* (kpds values are from NMC's grib routines)
*
* the description of the levels is
* (1) incomplete
* (2) include some NMC-only values (>= 200?)
*/
char *levels(int kpds6, int o11, int o12) {
int kpds7;
static char buf[256];
/* octets 11 and 12
o11 = kpds7 / 256;
o12 = kpds7 % 256; */
kpds7 = o11 * 256 + o12;
memset(buf, 0, sizeof(buf));
switch (kpds6) {
case 1: sprintf(buf,"sfc");
break;
case 2: sprintf(buf,"cld base");
break;
case 3: sprintf(buf,"cld top");
break;
case 204:
case 4: sprintf(buf,"0C isotherm");
break;
case 5: sprintf(buf,"cond lev");
break;
case 6: sprintf(buf,"max wind lev");
break;
case 7: sprintf(buf,"tropopause");
break;
case 8: sprintf(buf,"nom. top");
break;
case 9: sprintf(buf,"sea bottom");
break;
case 200:
case 10: sprintf(buf,"atmos col");
break;
case 12:
case 212: sprintf(buf,"low cld bot");
break;
case 13:
case 213: sprintf(buf,"low cld top");
break;
case 14:
case 214: sprintf(buf,"low cld lay");