1 | # FROM: https://pypi.org/project/pupygrib/ |
---|
2 | import numpy as np |
---|
3 | import pupygrib |
---|
4 | import os |
---|
5 | from netCDF4 import Dataset as NetCDFFile |
---|
6 | import generic_tools as gen |
---|
7 | import nc_var_tools as ncvar |
---|
8 | |
---|
9 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
10 | |
---|
11 | ifile = 'ERAI_pl200503_130.grib' |
---|
12 | |
---|
13 | ofile = 'ERAI_pl_130.nc' |
---|
14 | |
---|
15 | def grib128_ncname(varid): |
---|
16 | """ Function to provide CF name from a ECMWF GRIB table 128 variable ID |
---|
17 | >>> grib128_ncname(130) |
---|
18 | ['T', 'Temperature', 'K', 'air_temperature', 'air temperature'] |
---|
19 | """ |
---|
20 | fname = 'grib128_ncname' |
---|
21 | |
---|
22 | folder = os.path.dirname(os.path.realpath(__file__)) |
---|
23 | |
---|
24 | infile = folder + '/transform_128ECMWF.html' |
---|
25 | |
---|
26 | if not os.path.isfile(infile): |
---|
27 | print errormsg |
---|
28 | print ' ' + fname + ": File '" + infile + "' does not exist !!" |
---|
29 | quit(-1) |
---|
30 | |
---|
31 | of = open(infile, 'r') |
---|
32 | |
---|
33 | idS = str(varid).zfill(3) |
---|
34 | |
---|
35 | for line in of: |
---|
36 | if len(line) > 28: |
---|
37 | #print line[0:27], ':', idS, line[0:27] == '<td align="center">' + idS + '</td>' |
---|
38 | if line[0:27] == '<td align="center">' + idS + '</td>': |
---|
39 | linevals = line.replace('\n','').split('</td>') |
---|
40 | varn = linevals[1].replace('<td align="center">','') |
---|
41 | Lvarn = linevals[2].replace('<td align="left">','') |
---|
42 | units = linevals[3].replace('<td align="left">','') |
---|
43 | units = units.replace('<sup>','').replace('</sup>','') |
---|
44 | units = units.replace('<sub>','').replace('</sub>','') |
---|
45 | units = units.replace('<nobr>','').replace('</nobr>','') |
---|
46 | |
---|
47 | #print varn, Lvarn, units |
---|
48 | |
---|
49 | cfvalues = gen.variables_values(varn) |
---|
50 | |
---|
51 | break |
---|
52 | |
---|
53 | return [varn,Lvarn,units,cfvalues[0], cfvalues[1], cfvalues[4].replace('|', ' ')] |
---|
54 | |
---|
55 | timeu = 'minutes since 1949-12-01 00:00:00' |
---|
56 | |
---|
57 | ####### ####### |
---|
58 | ## MAIN |
---|
59 | ####### |
---|
60 | |
---|
61 | with open(ifile, 'rb') as stream: |
---|
62 | for i, msg in enumerate(pupygrib.read(stream), 1): |
---|
63 | if i == 1: |
---|
64 | lons, lats = msg.get_coordinates() |
---|
65 | onewnc = NetCDFFile(ofile, 'w') |
---|
66 | |
---|
67 | # Create dimension |
---|
68 | dimx = lons.shape[1] |
---|
69 | dimy = lons.shape[0] |
---|
70 | |
---|
71 | newdim = onewnc.createDimension('lon',dimx) |
---|
72 | newdim = onewnc.createDimension('lat',dimy) |
---|
73 | newdim = onewnc.createDimension('time',None) |
---|
74 | newdim = onewnc.createDimension('Lstring',64) |
---|
75 | |
---|
76 | dlon = msg[2].longitudeOfLastGridPoint - msg[2].longitudeOfFirstGridPoint |
---|
77 | dlat = msg[2].latitudeOfLastGridPoint - msg[2].latitudeOfFirstGridPoint |
---|
78 | print dlon, dlat |
---|
79 | |
---|
80 | if dlat < 0.: |
---|
81 | lons[:,:] = lons[::-1,:] |
---|
82 | lats[:,:] = lats[::-1,:] |
---|
83 | |
---|
84 | # Create dimension-varibale |
---|
85 | newvar = onewnc.createVariable('lon', 'f8', ('lat', 'lon')) |
---|
86 | newvar[:] = lons[:] |
---|
87 | ncvar.basicvardef(newvar, 'lon', 'Longitude', 'degrees_east') |
---|
88 | newvar.setncattr('axis', 'X') |
---|
89 | newvar.setncattr('_CoordinateAxisType', 'Lon') |
---|
90 | |
---|
91 | newvar = onewnc.createVariable('lat', 'f8', ('lat', 'lon')) |
---|
92 | newvar[:] = lats[:] |
---|
93 | ncvar.basicvardef(newvar, 'lat', 'Latitude', 'degrees_north') |
---|
94 | newvar.setncattr('axis', 'Y') |
---|
95 | newvar.setncattr('_CoordinateAxisType', 'Lat') |
---|
96 | |
---|
97 | newvartime = onewnc.createVariable('time', 'f8', ('time')) |
---|
98 | ncvar.basicvardef(newvartime, 'time', 'time', timeu) |
---|
99 | newvar.setncattr('axis', 'T') |
---|
100 | newvar.setncattr('_CoordinateAxisType', 'Time') |
---|
101 | |
---|
102 | newvartimeS = onewnc.createVariable('timeS', 'c', ('time', 'Lstring')) |
---|
103 | ncvar.basicvardef(newvartimeS, 'time', 'time', 'YmdHMS') |
---|
104 | newvar.setncattr('axis', 'T') |
---|
105 | newvar.setncattr('_CoordinateAxisType', 'Time') |
---|
106 | |
---|
107 | values = msg.get_values() |
---|
108 | century = msg[1].centuryOfReferenceTimeOfData |
---|
109 | year = msg[1].yearOfCentury |
---|
110 | month = msg[1].month |
---|
111 | day = msg[1].day |
---|
112 | hour = msg[1].hour |
---|
113 | minute = msg[1].minute |
---|
114 | levtype = msg[1].indicatorOfTypeOfLevel |
---|
115 | level = msg[1].level |
---|
116 | varid = msg[1].indicatorOfParameter |
---|
117 | offset = msg[1].offset |
---|
118 | scalefactor = msg[1].decimalScaleFactor |
---|
119 | timerange = msg[1].timeRangeIndicator |
---|
120 | |
---|
121 | if levtype == 100: |
---|
122 | if not onewnc.variables.has_key('press'): |
---|
123 | print ' we got a variable with a pressure value !!' |
---|
124 | pressv = [] |
---|
125 | newdim = onewnc.createDimension('press', None) |
---|
126 | newvarpress = onewnc.createVariable('press', 'f8', ('press')) |
---|
127 | newvarpress[0] = level*100. |
---|
128 | ncvar.basicvardef(newvarpress, 'air_pressure', 'air pressure', 'Pa') |
---|
129 | newvarpress.setncattr('axis', 'Z') |
---|
130 | newvarpress.setncattr('_CoordinateAxisType', 'Press') |
---|
131 | if not gen.searchInlist(pressv, level*100.): |
---|
132 | pressv.append(level*100.) |
---|
133 | iz = gen.index_vec(pressv, level*100.) |
---|
134 | newvarpress[iz] = level*100. |
---|
135 | else: |
---|
136 | iz = gen.index_vec(pressv, level*100.) |
---|
137 | elif levtype == 1: |
---|
138 | if not onewnc.variables.has_key('height'): |
---|
139 | print ' we got a variable with a height value !!' |
---|
140 | heightv = [] |
---|
141 | newdim = onewnc.createDimension('height', None) |
---|
142 | newvarheight = onewnc.createVariable('height', 'f8', ('height')) |
---|
143 | newvarheight[0] = level |
---|
144 | ncvar.basicvardef(newvarheight, 'height', 'height', 'm') |
---|
145 | newvarheight.setncattr('axis', 'Z') |
---|
146 | newvarheight.setncattr('_CoordinateAxisType', 'Height') |
---|
147 | if not gen.searchInlist(heightv, level): |
---|
148 | heightv.append(level) |
---|
149 | iz = gen.index_vec(heightv, level) |
---|
150 | newvarheight[iz] = level |
---|
151 | else: |
---|
152 | iz = gen.index_vec(heightv, level) |
---|
153 | |
---|
154 | #print "Message :", i, values.mean(), values.shape, ':', msg[1].centuryOfReferenceTimeOfData, \ |
---|
155 | # msg[1].yearOfCentury, msg[1].month, msg[1].day, msg[1].hour, msg[1].minute, \ |
---|
156 | # msg[1].indicatorOfTypeOfLevel, msg[1].level, msg[1].localDefinitionNumber, msg[1].indicatorOfParameter, \ |
---|
157 | # msg[1].offset, msg[1].decimalScaleFactor, msg[1].timeRangeIndicator |
---|
158 | #print 'msg:', dir(msg) |
---|
159 | #for i in range(6): |
---|
160 | # print 'msg ' + str(i) + ':', dir(msg[i]) |
---|
161 | |
---|
162 | varparams = grib128_ncname(varid) |
---|
163 | if i==1: print varparams |
---|
164 | |
---|
165 | timeS=str((century-1)*100 + year) + str(month).zfill(2) + str(day).zfill(2)+\ |
---|
166 | str(hour).zfill(2) + str(minute).zfill(2) + '00' |
---|
167 | |
---|
168 | if i == 1: timevs = [timeS] |
---|
169 | |
---|
170 | if not gen.searchInlist(timevs, timeS): timevs.append(timeS) |
---|
171 | it = gen.index_vec(timevs, timeS) |
---|
172 | #print 'timeS:', timeS, 'it:', it, 'lev:', level, 'iz:', iz |
---|
173 | cftime = gen.datetimeStr_conversion(timeS,'YmdHMS','cfTime,'+timeu) |
---|
174 | |
---|
175 | newvartimeS[it,0:14] = timeS |
---|
176 | newvartime[it] = cftime |
---|
177 | |
---|
178 | if not onewnc.variables.has_key(varparams[3]): |
---|
179 | if levtype == 1: |
---|
180 | dimns = ['time', 'height', 'lat', 'lon'] |
---|
181 | elif levtype == 100: |
---|
182 | dimns = ['time', 'press', 'lat', 'lon'] |
---|
183 | else: |
---|
184 | dimns = ['time', 'lat', 'lon'] |
---|
185 | |
---|
186 | newvar = onewnc.createVariable(varparams[3], 'f', tuple(dimns)) |
---|
187 | ncvar.basicvardef(newvar, varparams[4], varparams[5], varparams[2]) |
---|
188 | else: |
---|
189 | newvar = onewnc.variables[varparams[3]] |
---|
190 | |
---|
191 | if dlat < 0.: |
---|
192 | values[:] = values[::-1,:] |
---|
193 | |
---|
194 | if levtype == 1 or levtype == 100: |
---|
195 | newvar[it,iz,:,:] = values[:] |
---|
196 | else: |
---|
197 | newvar[it,:,:] = values[:] |
---|
198 | |
---|
199 | onewnc.sync() |
---|
200 | |
---|
201 | # Global values |
---|
202 | ncvar.add_global_PyNCplot(onewnc, 'red_grib.py', ' ', '0.1') |
---|
203 | |
---|
204 | onewnc.close() |
---|
205 | print "Successfull writting of '" + ofile + "' !!" |
---|