forked from jswhit/pygrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncepgrib2.py
executable file
·1432 lines (1360 loc) · 58.2 KB
/
ncepgrib2.py
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
__version__ = '2.0.0'
import g2clib
import struct
import string
import math
import warnings
import operator
from datetime import datetime
try:
from StringIO import StringIO
except ImportError:
from io import BytesIO as StringIO
import numpy as np
from numpy import ma
try:
import pyproj
except ImportError:
try:
from mpl_toolkits.basemap import pyproj
except:
raise ImportError("either pyproj or basemap required")
# Code Table 3.2: Shape of the Earth.
_earthparams={0:6367470.0,
1:'Spherical - radius specified in m by data producer',
2:(6378160.0,6356775.0),
3:'OblateSpheroid - major and minor axes specified in km by data producer',
4:(6378137.0,6356752.314),
5:'WGS84',
6:6371229.0,
7:'OblateSpheroid - major and minor axes specified in m by data producer',
8:6371200.0,
255:'Missing'}
for _n in range(192):
if not _n in _earthparams: _earthparams[_n]='Reserved'
for _n in range(192,255):
_earthparams[_n] = 'Reserved for local use'
_table0={1:('Melbourne (WMC)','ammc'),
2:('Melbourne - BMRC (WMC)',None),
3:('Melbourne (WMC)',None),
4:('Moscow (WMC)',None),
5:('Moscow (WMC)',None),
6:('Moscow (WMC)',None),
7:('US National Weather Service - NCEP (WMC)','kwbc'),
8:('US National Weather Service - NWSTG (WMC)',None),
9:('US National Weather Service - Other (WMC)',None),
10:('Cairo (RSMC/RAFC)',None),
11:('Cairo (RSMC/RAFC)',None),
12:('Dakar (RSMC/RAFC)',None),
13:('Dakar (RSMC/RAFC)',None),
14:('Nairobi (RSMC/RAFC)',None),
15:('Nairobi (RSMC/RAFC)',None),
16:('Casablanca',None),
17:('Tunis (RSMC)',None),
18:('Tunis-Casablanca (RSMC)',None),
19:('Tunis-Casablanca (RSMC)',None),
20:('Las Palmas (RAFC)',None),
21:('Algiers (RSMC)',None),
22:('ACMAD',None),
23:('Mozambique (NMC)',None),
24:('Pretoria (RSMC)',None),
25:('La Reunion (RSMC)',None),
26:('Khabarovsk (RSMC)',None),
27:('Khabarovsk (RSMC)',None),
28:('New Delhi (RSMC/RAFC)',None),
29:('New Delhi (RSMC/RAFC)',None),
30:('Novosibirsk (RSMC)',None),
31:('Novosibirsk (RSMC)',None),
32:('Tashkent (RSMC)',None),
33:('Jeddah (RSMC)',None),
34:('Japanese Meteorological Agency - Tokyo (RSMC)','rjtd'),
35:('Japanese Meteorological Agency - Tokyo (RSMC)',None),
36:('Bankok',None),
37:('Ulan Bator',None),
38:('Beijing (RSMC)','babj'),
39:('Beijing (RSMC)',None),
40:('Korean Meteorological Administration - Seoul','rksl'),
41:('Buenos Aires (RSMC/RAFC)',None),
42:('Buenos Aires (RSMC/RAFC)',None),
43:('Brasilia (RSMC/RAFC)',None),
44:('Brasilia (RSMC/RAFC)',None),
45:('Santiago',None),
46:('Brazilian Space Agency - INPE','sbsj'),
47:('Columbia (NMC)',None),
48:('Ecuador (NMC)',None),
49:('Peru (NMC)',None),
50:('Venezuela (NMC)',None),
51:('Miami (RSMC/RAFC)',None),
52:('Tropical Prediction Center (NHC), Miami (RSMC)',None),
53:('Canadian Meteorological Service - Montreal (RSMC)',None),
54:('Canadian Meteorological Service - Montreal (RSMC)','cwao'),
55:('San Francisco',None),
56:('ARINC Center',None),
57:('U.S. Air Force - Global Weather Center',None),
58:('US Navy - Fleet Numerical Oceanography Center','fnmo'),
59:('NOAA Forecast Systems Lab, Boulder CO',None),
60:('National Center for Atmospheric Research (NCAR), Boulder, CO',None),
61:('Service ARGOS - Landover, MD, USA',None),
62:('US Naval Oceanographic Office',None),
63:('Reserved',None),
64:('Honolulu',None),
65:('Darwin (RSMC)',None),
66:('Darwin (RSMC)',None),
67:('Melbourne (RSMC)',None),
68:('Reserved',None),
69:('Wellington (RSMC/RAFC)',None),
70:('Wellington (RSMC/RAFC)',None),
71:('Nadi (RSMC)',None),
72:('Singapore',None),
73:('Malaysia (NMC)',None),
74:('U.K. Met Office - Exeter (RSMC)','egrr'),
75:('U.K. Met Office - Exeter (RSMC)',None),
76:('Moscow (RSMC/RAFC)',None),
77:('Reserved',None),
78:('Offenbach (RSMC)','edzw'),
79:('Offenbach (RSMC)',None),
80:('Rome (RSMC)','cnmc'),
81:('Rome (RSMC)',None),
82:('Norrkoping',None),
83:('Norrkoping',None),
84:('French Weather Service - Toulouse','lfpw'),
85:('French Weather Service - Toulouse','lfpw'),
86:('Helsinki',None),
87:('Belgrade',None),
88:('Oslo',None),
89:('Prague',None),
90:('Episkopi',None),
91:('Ankara',None),
92:('Frankfurt/Main (RAFC)',None),
93:('London (WAFC)',None),
94:('Copenhagen',None),
95:('Rota',None),
96:('Athens',None),
97:('European Space Agency (ESA)',None),
98:('European Center for Medium-Range Weather Forecasts (RSMC)','ecmf'),
99:('De BiltNone), Netherlands',None),
100:('Brazzaville',None),
101:('Abidjan',None),
102:('Libyan Arab Jamahiriya (NMC)',None),
103:('Madagascar (NMC)',None),
104:('Mauritius (NMC)',None),
105:('Niger (NMC)',None),
106:('Seychelles (NMC)',None),
107:('Uganda (NMC)',None),
108:('Tanzania (NMC)',None),
109:('Zimbabwe (NMC)',None),
110:('Hong-Kong',None),
111:('Afghanistan (NMC)',None),
112:('Bahrain (NMC)',None),
113:('Bangladesh (NMC)',None),
114:('Bhutan (NMC)',None),
115:('Cambodia (NMC)',None),
116:("Democratic People's Republic of Korea (NMC)",None),
117:('Islamic Republic of Iran (NMC)',None),
118:('Iraq (NMC)',None),
119:('Kazakhstan (NMC)',None),
120:('Kuwait (NMC)',None),
121:('Kyrgyz Republic (NMC)',None),
122:("Lao People's Democratic Republic (NMC)",None),
123:('MacaoNone), China',None),
124:('Maldives (NMC)',None),
125:('Myanmar (NMC)',None),
126:('Nepal (NMC)',None),
127:('Oman (NMC)',None),
128:('Pakistan (NMC)',None),
129:('Qatar (NMC)',None),
130:('Republic of Yemen (NMC)',None),
131:('Sri Lanka (NMC)',None),
132:('Tajikistan (NMC)',None),
133:('Turkmenistan (NMC)',None),
134:('United Arab Emirates (NMC)',None),
135:('Uzbekistan (NMC)',None),
136:('Socialist Republic of Viet Nam (NMC)',None),
137:('Reserved',None),
138:('Reserved',None),
139:('Reserved',None),
140:('Bolivia (NMC)',None),
141:('Guyana (NMC)',None),
142:('Paraguay (NMC)',None),
143:('Suriname (NMC)',None),
144:('Uruguay (NMC)',None),
145:('French Guyana',None),
146:('Brazilian Navy Hydrographic Center',None),
147:('Reserved',None),
148:('Reserved',None),
149:('Reserved',None),
150:('Antigua and Barbuda (NMC)',None),
151:('Bahamas (NMC)',None),
152:('Barbados (NMC)',None),
153:('Belize (NMC)',None),
154:('British Caribbean Territories Center',None),
155:('San Jose',None),
156:('Cuba (NMC)',None),
157:('Dominica (NMC)',None),
158:('Dominican Republic (NMC)',None),
159:('El Salvador (NMC)',None),
160:('US NOAA/NESDIS',None),
161:('US NOAA Office of Oceanic and Atmospheric Research',None),
162:('Guatemala (NMC)',None),
163:('Haiti (NMC)',None),
164:('Honduras (NMC)',None),
165:('Jamaica (NMC)',None),
166:('Mexico',None),
167:('Netherlands Antilles and Aruba (NMC)',None),
168:('Nicaragua (NMC)',None),
169:('Panama (NMC)',None),
170:('Saint Lucia (NMC)',None),
171:('Trinidad and Tobago (NMC)',None),
172:('French Departments',None),
173:('Reserved',None),
174:('Reserved',None),
175:('Reserved',None),
176:('Reserved',None),
177:('Reserved',None),
178:('Reserved',None),
179:('Reserved',None),
180:('Reserved',None),
181:('Reserved',None),
182:('Reserved',None),
183:('Reserved',None),
184:('Reserved',None),
185:('Reserved',None),
186:('Reserved',None),
187:('Reserved',None),
188:('Reserved',None),
189:('Reserved',None),
190:('Cook Islands (NMC)',None),
191:('French Polynesia (NMC)',None),
192:('Tonga (NMC)',None),
193:('Vanuatu (NMC)',None),
194:('Brunei (NMC)',None),
195:('Indonesia (NMC)',None),
196:('Kiribati (NMC)',None),
197:('Federated States of Micronesia (NMC)',None),
198:('New Caledonia (NMC)',None),
199:('Niue',None),
200:('Papua New Guinea (NMC)',None),
201:('Philippines (NMC)',None),
202:('Samoa (NMC)',None),
203:('Solomon Islands (NMC)',None),
204:('Reserved',None),
205:('Reserved',None),
206:('Reserved',None),
207:('Reserved',None),
208:('Reserved',None),
209:('Reserved',None),
210:('Frascati',None),
211:('Lanion',None),
212:('Lisboa',None),
213:('Reykjavik',None),
214:('Madrid','lemm'),
215:('Zurich',None),
216:('Service ARGOS - ToulouseNone), FR',None),
217:('Bratislava',None),
218:('Budapest',None),
219:('Ljubljana',None),
220:('Warsaw',None),
221:('Zagreb',None),
222:('Albania (NMC)',None),
223:('Armenia (NMC)',None),
224:('Austria (NMC)',None),
225:('Azerbaijan (NMC)',None),
226:('Belarus (NMC)',None),
227:('Belgium (NMC)',None),
228:('Bosnia and Herzegovina (NMC)',None),
229:('Bulgaria (NMC)',None),
230:('Cyprus (NMC)',None),
231:('Estonia (NMC)',None),
232:('Georgia (NMC)',None),
233:('Dublin',None),
234:('Israel (NMC)',None),
235:('Jordan (NMC)',None),
236:('Latvia (NMC)',None),
237:('Lebanon (NMC)',None),
238:('Lithuania (NMC)',None),
239:('Luxembourg',None),
240:('Malta (NMC)',None),
241:('Monaco',None),
242:('Romania (NMC)',None),
243:('Syrian Arab Republic (NMC)',None),
244:('The former Yugoslav Republic of Macedonia (NMC)',None),
245:('Ukraine (NMC)',None),
246:('Republic of Moldova',None),
247:('Reserved',None),
248:('Reserved',None),
249:('Reserved',None),
250:('Reserved',None),
251:('Reserved',None),
252:('Reserved',None),
253:('Reserved',None),
254:('EUMETSAT Operations Center',None),
255:('Missing Value',None)}
def _dec2bin(val, maxbits = 8):
"""
A decimal to binary converter. Returns bits in a list.
"""
retval = []
for i in range(maxbits - 1, -1, -1):
bit = int(val / (2 ** i))
val = (val % (2 ** i))
retval.append(bit)
return retval
def _putieeeint(r):
"""convert a float to a IEEE format 32 bit integer"""
ra = np.array([r],'f')
ia = np.empty(1,'i')
g2clib.rtoi_ieee(ra,ia)
return ia[0]
def _getieeeint(i):
"""convert an IEEE format 32 bit integer to a float"""
ia = np.array([i],'i')
ra = np.empty(1,'f')
g2clib.itor_ieee(ia,ra)
return ra[0]
def _isString(string):
"""Test if string is a string like object if not return 0 """
try: string + ''
except: return 0
else: return 1
class Grib2Message:
"""
Class for accessing data in a GRIB Edition 2 message.
The L{Grib2Decode} function returns a list of these class instances,
one for each grib message in the file.
When a class instance is created, metadata in the GRIB2 file
is decoded and used to set various instance variables.
@ivar bitmap_indicator_flag: flag to indicate whether a bit-map is used (0 for yes, 255 for no).
@ivar data_representation_template: data representation template from section 5.
@ivar data_representation_template_number: data representation template number
from section 5
(U{Table 5.0
<http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table5-0.shtml>})
@ivar has_local_use_section: True if grib message contains a local use
section. If True the actual local use section is contained in the
C{_local_use_section} instance variable, as a raw byte string.
@ivar discipline_code: product discipline code for grib message
(U{Table 0.0
<http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table0-0.shtml>}).
@ivar earthRmajor: major (equatorial) earth radius.
@ivar earthRminor: minor (polar) earth radius.
@ivar grid_definition_info: grid definition section information from section 3.
See L{Grib2Encode.addgrid} for details.
@ivar grid_definition_template: grid definition template from section 3.
@ivar grid_definition_template_number: grid definition template number from section 3
(U{Table 3.1
<http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table3-1.shtml>}).
@ivar gridlength_in_x_direction: x (or longitudinal) direction grid length.
@ivar gridlength_in_y_direction: y (or latitudinal) direction grid length.
@ivar identification_section: data from identification section (section 1).
See L{Grib2Encode.__init__} for details.
@ivar latitude_first_gridpoint: latitude of first grid point on grid.
@ivar latitude_last_gridpoint: latitude of last grid point on grid.
@ivar longitude_first_gridpoint: longitude of first grid point on grid.
@ivar longitude_last_gridpoint: longitude of last grid point on grid.
@ivar originating_center: name of national/international originating center.
@ivar center_wmo_code: 4 character wmo code for originating center.
@ivar scanmodeflags: scanning mode flags from Table 3.4
(U{Table 3.4
<http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table3-4.shtml>}).
- bit 1:
0 - Points in the first row or column scan in the +i (+x) direction
1 - Points in the first row or column scan in the -i (-x) direction
- bit 2:
0 - Points in the first row or column scan in the -j (-y) direction
1 - Points in the first row or column scan in the +j (+y) direction
- bit 3:
0 - Adjacent points in the i (x) direction are consecutive (row-major order).
1 - Adjacent points in the j (y) direction are consecutive (column-major order).
- bit 4:
0 - All rows scan in the same direction
1 - Adjacent rows scan in the opposite direction
@ivar number_of_data_points_to_unpack: total number of data points in grib message.
@ivar points_in_x_direction: number of points in the x (longitudinal) direction.
@ivar points_in_y_direction: number of points in the y (latitudinal) direction.
@ivar product_definition_template: product definition template from section 4.
@ivar product_definition_template_number: product definition template number from section 4
(U{Table 4.0
<http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table4-0.shtml>}).
@ivar shape_of_earth: string describing the shape of the earth (e.g. 'Oblate Spheroid', 'Spheroid').
@ivar spectral_truncation_parameters: pentagonal truncation parameters that describe the
spherical harmonic truncation (only relevant for grid_definition_template_numbers 50-52).
For triangular truncation, all three of these numbers are the same.
@ivar latitude_of_southern_pole: the geographic latitude in degrees of the southern
pole of the coordinate system (for rotated lat/lon or gaussian grids).
@ivar longitude_of_southern_pole: the geographic longitude in degrees of the southern
pole of the coordinate system (for rotated lat/lon or gaussian grids).
@ivar angle_of_pole_rotation: The angle of rotation in degrees about the new
polar axis (measured clockwise when looking from the southern to the northern pole)
of the coordinate system. For rotated lat/lon or gaussian grids.
@ivar missing_value: primary missing value (for data_representation_template_numbers
2 and 3).
@ivar missing_value2: secondary missing value (for data_representation_template_numbers
2 and 3).
@ivar proj4_: instance variables with this prefix are used to set the map projection
parameters for U{PROJ.4<http://proj.maptools.org>}.
"""
def __init__(self,**kwargs):
"""
create a Grib2Decode class instance given a GRIB Edition 2 filename.
(used by L{Grib2Decode} function - not directly called by user)
"""
for k,v in kwargs.items():
setattr(self,k,v)
# grid information
gdsinfo = self.grid_definition_info
gdtnum = self.grid_definition_template_number
gdtmpl = self.grid_definition_template
reggrid = gdsinfo[2] == 0 # gdsinfo[2]=0 means regular 2-d grid
# shape of the earth.
if gdtnum not in [50,51,52,1200]:
earthR = _earthparams[gdtmpl[0]]
if earthR == 'Reserved': earthR = None
else:
earthR = None
if _isString(earthR) and (earthR.startswith('Reserved') or earthR=='Missing'):
self.shape_of_earth = earthR
self.earthRminor = None
self.earthRmajor = None
elif _isString(earthR) and earthR.startswith('Spherical'):
self.shape_of_earth = earthR
scaledearthR = gdtmpl[2]
earthRscale = gdtmpl[1]
self.earthRmajor = math.pow(10,-earthRscale)*scaledearthR
self.earthRminor = self.earthRmajor
elif _isString(earthR) and earthR.startswith('OblateSpheroid'):
self.shape_of_earth = earthR
scaledearthRmajor = gdtmpl[4]
earthRmajorscale = gdtmpl[3]
self.earthRmajor = math.pow(10,-earthRmajorscale)*scaledearthRmajor
self.earthRmajor = self.earthRmajor*1000. # convert to m from km
scaledearthRminor = gdtmpl[6]
earthRminorscale = gdtmpl[5]
self.earthRminor = math.pow(10,-earthRminorscale)*scaledearthRminor
self.earthRminor = self.earthRminor*1000. # convert to m from km
elif _isString(earthR) and earthR.startswith('WGS84'):
self.shape_of_earth = earthR
self.earthRmajor = 6378137.0
self.earthRminor = 6356752.3142
elif isinstance(earthR,tuple):
self.shape_of_earth = 'OblateSpheroid'
self.earthRmajor = earthR[0]
self.earthRminor = earthR[1]
else:
if earthR is not None:
self.shape_of_earth = 'Spherical'
self.earthRmajor = earthR
self.earthRminor = self.earthRmajor
if reggrid and gdtnum not in [50,51,52,53,100,120,1000,1200]:
self.points_in_x_direction = gdtmpl[7]
self.points_in_y_direction = gdtmpl[8]
if not reggrid and gdtnum == 40: # 'reduced' gaussian grid.
self.points_in_y_direction = gdtmpl[8]
if gdtnum in [0,1,203,205,32768]: # regular or rotated lat/lon grid
scalefact = float(gdtmpl[9])
divisor = float(gdtmpl[10])
if scalefact == 0: scalefact = 1.
if divisor <= 0: divisor = 1.e6
self.latitude_first_gridpoint = scalefact*gdtmpl[11]/divisor
self.longitude_first_gridpoint = scalefact*gdtmpl[12]/divisor
self.latitude_last_gridpoint = scalefact*gdtmpl[14]/divisor
self.longitude_last_gridpoint = scalefact*gdtmpl[15]/divisor
self.gridlength_in_x_direction = scalefact*gdtmpl[16]/divisor
self.gridlength_in_y_direction = scalefact*gdtmpl[17]/divisor
if self.latitude_first_gridpoint > self.latitude_last_gridpoint:
self.gridlength_in_y_direction = -self.gridlength_in_y_direction
if self.longitude_first_gridpoint > self.longitude_last_gridpoint:
self.gridlength_in_x_direction = -self.gridlength_in_x_direction
self.scanmodeflags = _dec2bin(gdtmpl[18])[0:4]
if gdtnum == 1:
self.latitude_of_southern_pole = scalefact*gdtmpl[19]/divisor
self.longitude_of_southern_pole = scalefact*gdtmpl[20]/divisor
self.angle_of_pole_rotation = gdtmpl[21]
elif gdtnum == 10: # mercator
self.latitude_first_gridpoint = gdtmpl[9]/1.e6
self.longitude_first_gridpoint = gdtmpl[10]/1.e6
self.latitude_last_gridpoint = gdtmpl[13]/1.e6
self.longitude_last_gridpoint = gdtmpl[14]/1.e6
self.gridlength_in_x_direction = gdtmpl[17]/1.e3
self.gridlength_in_y_direction= gdtmpl[18]/1.e3
self.proj4_lat_ts = gdtmpl[12]/1.e6
self.proj4_lon_0 = 0.5*(self.longitude_first_gridpoint+self.longitude_last_gridpoint)
self.proj4_proj = 'merc'
self.scanmodeflags = _dec2bin(gdtmpl[15])[0:4]
elif gdtnum == 20: # stereographic
projflag = _dec2bin(gdtmpl[16])[0]
self.latitude_first_gridpoint = gdtmpl[9]/1.e6
self.longitude_first_gridpoint = gdtmpl[10]/1.e6
self.proj4_lat_ts = gdtmpl[12]/1.e6
if projflag == 0:
self.proj4_lat_0 = 90
elif projflag == 1:
self.proj4_lat_0 = -90
else:
raise ValueError('Invalid projection center flag = %s'%projflag)
self.proj4_lon_0 = gdtmpl[13]/1.e6
self.gridlength_in_x_direction = gdtmpl[14]/1000.
self.gridlength_in_y_direction = gdtmpl[15]/1000.
self.proj4_proj = 'stere'
self.scanmodeflags = _dec2bin(gdtmpl[17])[0:4]
elif gdtnum == 30: # lambert conformal
self.latitude_first_gridpoint = gdtmpl[9]/1.e6
self.longitude_first_gridpoint = gdtmpl[10]/1.e6
self.gridlength_in_x_direction = gdtmpl[14]/1000.
self.gridlength_in_y_direction = gdtmpl[15]/1000.
self.proj4_lat_1 = gdtmpl[18]/1.e6
self.proj4_lat_2 = gdtmpl[19]/1.e6
self.proj4_lat_0 = gdtmpl[12]/1.e6
self.proj4_lon_0 = gdtmpl[13]/1.e6
self.proj4_proj = 'lcc'
self.scanmodeflags = _dec2bin(gdtmpl[17])[0:4]
elif gdtnum == 31: # albers equal area.
self.latitude_first_gridpoint = gdtmpl[9]/1.e6
self.longitude_first_gridpoint = gdtmpl[10]/1.e6
self.gridlength_in_x_direction = gdtmpl[14]/1000.
self.gridlength_in_y_direction = gdtmpl[15]/1000.
self.proj4_lat_1 = gdtmpl[18]/1.e6
self.proj4_lat_2 = gdtmpl[19]/1.e6
self.proj4_lat_0 = gdtmpl[12]/1.e6
self.proj4_lon_0 = gdtmpl[13]/1.e6
self.proj4_proj = 'aea'
self.scanmodeflags = _dec2bin(gdtmpl[17])[0:4]
elif gdtnum == 40 or gdtnum == 41: # gaussian grid.
scalefact = float(gdtmpl[9])
divisor = float(gdtmpl[10])
if scalefact == 0: scalefact = 1.
if divisor <= 0: divisor = 1.e6
self.points_between_pole_and_equator = gdtmpl[17]
self.latitude_first_gridpoint = scalefact*gdtmpl[11]/divisor
self.longitude_first_gridpoint = scalefact*gdtmpl[12]/divisor
self.latitude_last_gridpoint = scalefact*gdtmpl[14]/divisor
self.longitude_last_gridpoint = scalefact*gdtmpl[15]/divisor
if reggrid:
self.gridlength_in_x_direction = scalefact*gdtmpl[16]/divisor
if self.longitude_first_gridpoint > self.longitude_last_gridpoint:
self.gridlength_in_x_direction = -self.gridlength_in_x_direction
self.scanmodeflags = _dec2bin(gdtmpl[18])[0:4]
if gdtnum == 41:
self.latitude_of_southern_pole = scalefact*gdtmpl[19]/divisor
self.longitude_of_southern_pole = scalefact*gdtmpl[20]/divisor
self.angle_of_pole_rotation = gdtmpl[21]
elif gdtnum == 50: # spectral coefficients.
self.spectral_truncation_parameters = (gdtmpl[0],gdtmpl[1],gdtmpl[2])
self.scanmodeflags = [None,None,None,None] # doesn't apply
elif gdtnum == 90: # near-sided vertical perspective satellite projection
self.proj4_lat_0 = gdtmpl[9]/1.e6
self.proj4_lon_0 = gdtmpl[10]/1.e6
self.proj4_h = self.earthRmajor * (gdtmpl[18]/1.e6)
dx = gdtmpl[12]
dy = gdtmpl[13]
# if lat_0 is equator, it's a geostationary view.
if self.proj4_lat_0 == 0.: # if lat_0 is equator, it's a
self.proj4_proj = 'geos'
# general case of 'near-side perspective projection' (untested)
else:
self.proj4_proj = 'nsper'
msg = """
only geostationary perspective is supported.
lat/lon values returned by grid method may be incorrect."""
warnings.warn(msg)
# latitude of horizon on central meridian
lonmax = 90.-(180./np.pi)*np.arcsin(self.earthRmajor/self.proj4_h)
# longitude of horizon on equator
latmax = 90.-(180./np.pi)*np.arcsin(self.earthRminor/self.proj4_h)
# truncate to nearest thousandth of a degree (to make sure
# they aren't slightly over the horizon)
latmax = int(1000*latmax)/1000.
lonmax = int(1000*lonmax)/1000.
# h is measured from surface of earth at equator.
self.proj4_h = self.proj4_h - self.earthRmajor
# width and height of visible projection
P = pyproj.Proj(proj=self.proj4_proj,\
a=self.earthRmajor,b=self.earthRminor,\
lat_0=0,lon_0=0,h=self.proj4_h)
x1,y1 = P(0.,latmax); x2,y2 = P(lonmax,0.)
width = 2*x2; height = 2*y1
self.gridlength_in_x_direction = width/dx
self.gridlength_in_y_direction = height/dy
self.scanmodeflags = _dec2bin(gdtmpl[16])[0:4]
elif gdtnum == 110: # azimuthal equidistant.
self.proj4_lat_0 = gdtmpl[9]/1.e6
self.proj4_lon_0 = gdtmpl[10]/1.e6
self.gridlength_in_x_direction = gdtmpl[12]/1000.
self.gridlength_in_y_direction = gdtmpl[13]/1000.
self.proj4_proj = 'aeqd'
self.scanmodeflags = _dec2bin(gdtmpl[15])[0:4]
elif gdtnum == 204: # curvilinear orthogonal
self.scanmodeflags = _dec2bin(gdtmpl[18])[0:4]
# missing value.
drtnum = self.data_representation_template_number
drtmpl = self.data_representation_template
if (drtnum == 2 or drtnum == 3) and drtmpl[6] != 0:
self.missing_value = _getieeeint(drtmpl[7])
if drtmpl[6] == 2:
self.missing_value2 = _getieeeint(drtmpl[8])
def __repr__(self):
strings = []
keys = self.__dict__.keys()
keys.sort()
for k in keys:
if not k.startswith('_'):
strings.append('%s = %s\n'%(k,self.__dict__[k]))
return ''.join(strings)
def data(self,fill_value=9.9692099683868690e+36,masked_array=True,expand=True,order=None):
"""
returns an unpacked data grid. Can also be accomplished with L{values}
property.
@keyword fill_value: missing or masked data is filled with this value
(default 9.9692099683868690e+36).
@keyword masked_array: if True, return masked array if there is bitmap
for missing or masked data (default True).
@keyword expand: if True (default), ECMWF 'reduced' gaussian grids are
expanded to regular gaussian grids.
@keyword order: if 1, linear interpolation is used for expanding reduced
gaussian grids. if 0, nearest neighbor interpolation is used. Default
is 0 if grid has missing or bitmapped values, 1 otherwise.
@return: C{B{data}}, a float32 numpy regular or masked array
with shape (nlats,lons) containing the requested grid.
"""
# make sure scan mode is supported.
# if there is no 'scanmodeflags', then grid is not supported.
from redtoreg import _redtoreg
if not hasattr(self,'scanmodeflags'):
raise ValueError('unsupported grid definition template number %s'%self.grid_definition_template_number)
else:
if self.scanmodeflags[2]:
storageorder='F'
else:
storageorder='C'
bitmapflag = self.bitmap_indicator_flag
drtnum = self.data_representation_template_number
drtmpl = self.data_representation_template
# default order=0 is missing values or bitmap exists.
if order is None:
if ((drtnum == 3 or drtnum == 2) and drtmpl[6] != 0) or bitmapflag == 0:
order = 0
else:
order = 1
try:
f = open(self._grib_filename,'rb')
except (TypeError,IOError):
f = StringIO(self._grib_filename)
f.seek(self._grib_message_byteoffset)
gribmsg = f.read(self._grib_message_length)
f.close()
gdtnum = self.grid_definition_template_number
gdtmpl = self.grid_definition_template
ndpts = self.number_of_data_points_to_unpack
gdsinfo = self.grid_definition_info
ngrdpts = gdsinfo[1]
ipos = self._section7_byte_offset
fld1=g2clib.unpack7(gribmsg,gdtnum,gdtmpl,drtnum,drtmpl,ndpts,ipos,np.empty,storageorder=storageorder)
# apply bitmap.
if bitmapflag == 0:
bitmap=self._bitmap
fld = fill_value*np.ones(ngrdpts,'f')
np.put(fld,np.nonzero(bitmap),fld1)
if masked_array:
fld = ma.masked_values(fld,fill_value)
# missing values instead of bitmap
elif masked_array and hasattr(self,'missing_value'):
if hasattr(self, 'missing_value2'):
mask = np.logical_or(fld1 == self.missing_value, fld1 == self.missing_value2)
else:
mask = fld1 == self.missing_value
fld = ma.array(fld1,mask=mask)
else:
fld = fld1
nx = None; ny = None
if hasattr(self,'points_in_x_direction'):
nx = self.points_in_x_direction
if hasattr(self,'points_in_y_direction'):
ny = self.points_in_y_direction
if nx is not None and ny is not None: # rectangular grid.
if ma.isMA(fld):
fld = ma.reshape(fld,(ny,nx))
else:
fld = np.reshape(fld,(ny,nx))
else:
if gdsinfo[2] and gdtnum == 40: # ECMWF 'reduced' global gaussian grid.
if expand:
nx = 2*ny
lonsperlat = self.grid_definition_list
if ma.isMA(fld):
fld = ma.filled(fld)
fld = _redtoreg(nx, lonsperlat.astype(np.long),\
fld.astype(np.double), fill_value)
fld = ma.masked_values(fld,fill_value)
else:
fld = _redtoreg(nx, lonsperlat.astype(np.long),\
fld.astype(np.double), fill_value)
# check scan modes for rect grids.
if nx is not None and ny is not None:
# rows scan in the -x direction (so flip)
#if self.scanmodeflags[0]:
# fldsave = fld.astype('f') # casting makes a copy
# fld[:,:] = fldsave[:,::-1]
# columns scan in the -y direction (so flip)
#if not self.scanmodeflags[1]:
# fldsave = fld.astype('f') # casting makes a copy
# fld[:,:] = fldsave[::-1,:]
# adjacent rows scan in opposite direction.
# (flip every other row)
if self.scanmodeflags[3]:
fldsave = fld.astype('f') # casting makes a copy
fld[1::2,:] = fldsave[1::2,::-1]
return fld
values = property(data)
def latlons(self):
"""alias for L{grid}"""
return self.grid()
def grid(self):
"""
return lats,lons (in degrees) of grid.
currently can handle reg. lat/lon, global gaussian, mercator, stereographic,
lambert conformal, albers equal-area, space-view and azimuthal
equidistant grids. L{latlons} method does the same thing.
@return: C{B{lats},B{lons}}, float32 numpy arrays
containing latitudes and longitudes of grid (in degrees).
"""
from pygrib import gaulats
gdsinfo = self.grid_definition_info
gdtnum = self.grid_definition_template_number
gdtmpl = self.grid_definition_template
reggrid = gdsinfo[2] == 0 # gdsinfo[2]=0 means regular 2-d grid
projparams = {}
projparams['a']=self.earthRmajor
projparams['b']=self.earthRminor
if gdtnum == 0: # regular lat/lon grid
lon1, lat1 = self.longitude_first_gridpoint, self.latitude_first_gridpoint
lon2, lat2 = self.longitude_last_gridpoint, self.latitude_last_gridpoint
delon = self.gridlength_in_x_direction
delat = self.gridlength_in_y_direction
lats = np.arange(lat1,lat2+delat,delat)
lons = np.arange(lon1,lon2+delon,delon)
# flip if scan mode says to.
#if self.scanmodeflags[0]:
# lons = lons[::-1]
#if not self.scanmodeflags[1]:
# lats = lats[::-1]
projparams['proj'] = 'cyl'
lons,lats = np.meshgrid(lons,lats) # make 2-d arrays.
elif gdtnum == 40: # gaussian grid (only works for global!)
lon1, lat1 = self.longitude_first_gridpoint, self.latitude_first_gridpoint
lon2, lat2 = self.longitude_last_gridpoint, self.latitude_last_gridpoint
nlats = self.points_in_y_direction
if not reggrid: # ECMWF 'reduced' gaussian grid.
nlons = 2*nlats
delon = 360./nlons
else:
nlons = self.points_in_x_direction
delon = self.gridlength_in_x_direction
lons = np.arange(lon1,lon2+delon,delon)
# compute gaussian lats (north to south)
lats = gaulats(nlats)
if lat1 < lat2: # reverse them if necessary
lats = lats[::-1]
# flip if scan mode says to.
#if self.scanmodeflags[0]:
# lons = lons[::-1]
#if not self.scanmodeflags[1]:
# lats = lats[::-1]
projparams['proj'] = 'cyl'
lons,lats = np.meshgrid(lons,lats) # make 2-d arrays
# mercator, lambert conformal, stereographic, albers equal area, azimuthal equidistant
elif gdtnum in [10,20,30,31,110]:
nx = self.points_in_x_direction
ny = self.points_in_y_direction
dx = self.gridlength_in_x_direction
dy = self.gridlength_in_y_direction
lon1, lat1 = self.longitude_first_gridpoint, self.latitude_first_gridpoint
if gdtnum == 10: # mercator.
projparams['lat_ts']=self.proj4_lat_ts
projparams['proj']=self.proj4_proj
projparams['lon_0']=self.proj4_lon_0
pj = pyproj.Proj(projparams)
llcrnrx, llcrnry = pj(lon1,lat1)
x = llcrnrx+dx*np.arange(nx)
y = llcrnry+dy*np.arange(ny)
x, y = np.meshgrid(x, y)
lons, lats = pj(x, y, inverse=True)
elif gdtnum == 20: # stereographic
projparams['lat_ts']=self.proj4_lat_ts
projparams['proj']=self.proj4_proj
projparams['lat_0']=self.proj4_lat_0
projparams['lon_0']=self.proj4_lon_0
pj = pyproj.Proj(projparams)
llcrnrx, llcrnry = pj(lon1,lat1)
x = llcrnrx+dx*np.arange(nx)
y = llcrnry+dy*np.arange(ny)
x, y = np.meshgrid(x, y)
lons, lats = pj(x, y, inverse=True)
elif gdtnum in [30,31]: # lambert, albers
projparams['lat_1']=self.proj4_lat_1
projparams['lat_2']=self.proj4_lat_2
projparams['proj']=self.proj4_proj
projparams['lon_0']=self.proj4_lon_0
pj = pyproj.Proj(projparams)
llcrnrx, llcrnry = pj(lon1,lat1)
x = llcrnrx+dx*np.arange(nx)
y = llcrnry+dy*np.arange(ny)
x, y = np.meshgrid(x, y)
lons, lats = pj(x, y, inverse=True)
elif gdtnum == 110: # azimuthal equidistant
projparams['proj']=self.proj4_proj
projparams['lat_0']=self.proj4_lat_0
projparams['lon_0']=self.proj4_lon_0
pj = pyproj.Proj(projparams)
llcrnrx, llcrnry = pj(lon1,lat1)
x = llcrnrx+dx*np.arange(nx)
y = llcrnry+dy*np.arange(ny)
x, y = np.meshgrid(x, y)
lons, lats = pj(x, y, inverse=True)
elif gdtnum == 90: # satellite projection.
nx = self.points_in_x_direction
ny = self.points_in_y_direction
dx = self.gridlength_in_x_direction
dy = self.gridlength_in_y_direction
projparams['proj']=self.proj4_proj
projparams['lon_0']=self.proj4_lon_0
projparams['lat_0']=self.proj4_lat_0
projparams['h']=self.proj4_h
pj = pyproj.Proj(projparams)
x = dx*np.indices((ny,nx),'f')[1,:,:]
x = x - 0.5*x.max()
y = dy*np.indices((ny,nx),'f')[0,:,:]
y = y - 0.5*y.max()
lons, lats = pj(x,y,inverse=True)
# set lons,lats to 1.e30 where undefined
abslons = np.fabs(lons); abslats = np.fabs(lats)
lons = np.where(abslons < 1.e20, lons, 1.e30)
lats = np.where(abslats < 1.e20, lats, 1.e30)
else:
raise ValueError('unsupported grid')
self.projparams = projparams
return lats.astype('f'), lons.astype('f')
def Grib2Decode(filename,gribmsg=False):
"""
Read the contents of a GRIB2 file.
@param filename: name of GRIB2 file (default, gribmsg=False) or binary string
representing a grib message (if gribmsg=True).
@return: a list of L{Grib2Message} instances representing all of the
grib messages in the file. Messages with multiple fields are split
into separate messages (so that each L{Grib2Message} instance contains
just one data field). The metadata in each GRIB2 message can be
accessed via L{Grib2Message} instance variables, the actual data
can be read using L{Grib2Message.data}, and the lat/lon values of the grid
can be accesses using L{Grib2Message.grid}. If there is only one grib
message, just the L{Grib2Message} instance is returned, instead of a list
with one element.
"""
if gribmsg:
f = StringIO(filename)
else:
f = open(filename,'rb')
nmsg = 0
# loop over grib messages, read section 0, get entire grib message.
disciplines = []
startingpos = []
msglen = []
while 1:
# find next occurence of string 'GRIB' (or EOF).
nbyte = f.tell()
while 1:
f.seek(nbyte)
start = f.read(4).decode('ascii','ignore')
if start == '' or start == 'GRIB': break
nbyte = nbyte + 1
if start == '': break # at EOF
# otherwise, start (='GRIB') contains indicator message (section 0)
startpos = f.tell()-4
f.seek(2,1) # next two octets are reserved
# get discipline info.
disciplines.append(struct.unpack('>B',f.read(1))[0])
# check to see it's a grib edition 2 file.
vers = struct.unpack('>B',f.read(1))[0]
if vers != 2:
raise IOError('not a GRIB2 file (version number %d)' % vers)
lengrib = struct.unpack('>q',f.read(8))[0]
msglen.append(lengrib)
startingpos.append(startpos)
# read in entire grib message.
f.seek(startpos)
gribmsg = f.read(lengrib)
# make sure the message ends with '7777'
end = gribmsg[-4:lengrib].decode('ascii','ignore')
if end != '7777':
raise IOError('partial GRIB message (no "7777" at end)')
# do next message.
nmsg=nmsg+1
# if no grib messages found, nmsg is still 0 and it's not GRIB.
if nmsg==0:
raise IOError('not a GRIB file')
# now for each grib message, find number of fields.
numfields = []
f.seek(0) # rewind file.
for n in range(nmsg):
f.seek(startingpos[n])
gribmsg = f.read(msglen[n])
pos = 0
numflds = 0
while 1:
if gribmsg[pos:pos+4].decode('ascii','ignore') == 'GRIB':
sectnum = 0
lensect = 16
elif gribmsg[pos:pos+4].decode('ascii','ignore') == '7777':
break
else:
lensect = struct.unpack('>i',gribmsg[pos:pos+4])[0]
sectnum = struct.unpack('>B',gribmsg[pos+4:pos+5])[0]
if sectnum == 4: numflds=numflds+1
#if sectnum == 2: numlocal=numlocal+1
pos = pos + lensect
#print sectnum,lensect,pos
#print n+1,len(gribmsg),numfields,numlocal
numfields.append(numflds)
# decode each section in grib message (sections 1 and above).
gdtnum = [] # grid defn template number from sxn 3
gdtmpl = [] # grid defn template from sxn 3
gdeflist = [] # optional grid definition list from sxn 3
gdsinfo = [] # grid definition section info from sxn3
pdtmpl = [] # product defn template from sxn 4
pdtnum = [] # product defn template number from sxn 4
coordlist = [] # vertical coordinate info from sxn 4
drtmpl = [] # data representation template from sxn 5
drtnum = [] # data representation template number from sxn 5
ndpts = [] # number of data points to be unpacked (from sxn 5)
bitmapflag = [] # bit-map indicator flag from sxn 6
bitmap = [] # bitmap from sxn 6.
pos7 = [] # byte offset for section 7.
localsxn = [] # local use sections.
msgstart = [] # byte offset in file for message start.
msglength = [] # length of the message in bytes.
message = [] # the actual grib message.
identsect = [] # identification section (section 1).
discipline = [] # discipline code.
for n in range(nmsg):
spos = startingpos[n]
lengrib = msglen[n]
#gribmsg = gribmsgs[n]
f.seek(spos)
gribmsg = f.read(lengrib)
discipl = disciplines[n]
lensect0 = 16
# get length of section 1 and section number.
#lensect1 = struct.unpack('>i',gribmsg[lensect0:lensect0+4])[0]
#sectnum1 = struct.unpack('>B',gribmsg[lensect0+4])[0]
#print 'sectnum1, lensect1 = ',sectnum1,lensect1
# unpack section 1, octets 1-21 (13 parameters). This section
# can occur only once per grib message.
#idsect,pos = _unpack1(gribmsg,lensect0) # python version
idsect,pos = g2clib.unpack1(gribmsg,lensect0,np.empty) # c version
# loop over rest of sections in message.
gdtnums = []
gdtmpls = []
gdeflists = []
gdsinfos = []
pdtmpls = []
coordlists = []
pdtnums = []
drtmpls = []
drtnums = []
ndptslist = []