@@ -15,62 +15,61 @@ from numpy import ma
15
15
import pyproj
16
16
npc.import_array()
17
17
18
- ctypedef fused my_type :
18
+ ctypedef fused float_type :
19
19
float
20
20
double
21
21
22
- def _redtoreg (cython.Py_ssize_t nlons , my_type[:] redgrid_data , long[:] lonsperlat , my_type missval ):
22
+ def redtoreg (float_type[:] redgrid_data , long[:] lonsperlat , missval = None ):
23
+ """
24
+ redtoreg(redgrid_data, lonsperlat, missval=None)
25
+
26
+ Takes 1-d array on ECMWF reduced gaussian grid (redgrid_data), linearly interpolates
27
+ to corresponding regular gaussian grid.
28
+
29
+ Reduced gaussian grid defined by lonsperlat array, regular gaussian
30
+ grid has the same number of latitudes and max(lonsperlat) longitudes.
31
+
32
+ Includes handling of missing values using nearest neighbor interpolation.
33
+ """
34
+
35
+ cdef cython.Py_ssize_t nlons = np.max(lonsperlat)
23
36
cdef cython.Py_ssize_t nlats = lonsperlat.shape[0 ]
24
- cdef cython.Py_ssize_t i,j,n,indx,ilons,im,ip
25
- cdef my_type zxi, zdx, flons
26
- if my_type is float :
27
- dtype = np.float32
28
- elif my_type is double :
29
- dtype = np.double
30
- reggrid_data = np.full((nlats,nlons), missval, dtype)
31
- cdef my_type[:, ::1 ] reggrid_data_view = reggrid_data
37
+ cdef cython.Py_ssize_t i,j,indx,ilons,im,ip,nlona
38
+ cdef float_type zxi, zdx, flons, missvalc
39
+ if float_type is float :
40
+ float_dtype = np.float32
41
+ elif float_type is double :
42
+ float_dtype = np.double
43
+ if missval is None :
44
+ missval = np.nan
45
+ missvalc = missval
46
+ reggrid_data = np.full((nlats, nlons), missval, float_dtype)
47
+ cdef float_type[:, ::1 ] reggrid_data_view = reggrid_data
32
48
indx = 0
33
49
for j in range (nlats):
34
- ilons = lonsperlat[j]
35
- flons = < my_type> ilons
36
- for i in range (nlons):
37
- # zxi is the grid index (relative to the reduced grid)
38
- # of the i'th point on the full grid.
39
- zxi = i * flons / nlons # goes from 0 to ilons
40
- im = < long > zxi
41
- zdx = zxi - < my_type> im
42
- if ilons != 0 :
50
+ ilons = lonsperlat[j]; flons = ilons
51
+ if ilons != 0 :
52
+ for i in range (nlons):
53
+ # zxi is the grid index (relative to the reduced grid)
54
+ # of the i'th point on the full grid.
55
+ zxi = i * flons / nlons # goes from 0 to ilons
56
+ im = < cython.Py_ssize_t> zxi; zdx = zxi - im
43
57
im = (im + ilons)% ilons
44
58
ip = (im + 1 + ilons)% ilons
45
59
# if one of the nearest values is missing, use nearest
46
60
# neighbor interpolation.
47
- if redgrid_data[indx+ im] == missval or \
48
- redgrid_data[indx+ ip] == missval :
61
+ if redgrid_data[indx+ im] == missvalc or \
62
+ redgrid_data[indx+ ip] == missvalc :
49
63
if zdx < 0.5 :
50
64
reggrid_data_view[j,i] = redgrid_data[indx+ im]
51
65
else :
52
66
reggrid_data_view[j,i] = redgrid_data[indx+ ip]
53
67
else : # linear interpolation.
54
68
reggrid_data_view[j,i] = redgrid_data[indx+ im]* (1. - zdx) + \
55
69
redgrid_data[indx+ ip]* zdx
56
- indx = indx + ilons
70
+ indx = indx + ilons
57
71
return reggrid_data
58
72
59
- def redtoreg (redgrid_data , lonsperlat , missval = None ):
60
- """
61
- redtoreg(redgrid_data, lonsperlat, missval=None)
62
-
63
- Takes 1-d array on ECMWF reduced gaussian grid (``redgrid_data``), linearly interpolates to corresponding
64
- regular gaussian grid (given by ``lonsperlat`` array, with max(lonsperlat) longitudes).
65
- If any values equal to specified missing value (``missval``, default NaN), a masked array is returned."""
66
-
67
- if missval is None :
68
- missval = np.nan
69
- datarr = _redtoreg(lonsperlat.max(),redgrid_data,lonsperlat,missval)
70
- if np.count_nonzero(datarr== missval):
71
- datarr = ma.masked_values(datarr, missval)
72
- return datarr
73
-
74
73
cdef extern from " stdlib.h" :
75
74
ctypedef long size_t
76
75
void * malloc(size_t size)
@@ -1314,8 +1313,8 @@ cdef class gribmessage(object):
1314
1313
else :
1315
1314
missval = 1.e30
1316
1315
if self .expand_reduced:
1317
- nx = 2 * ny
1318
- datarr = _redtoreg( 2 * ny, datarr, self [' pl' ], missval)
1316
+ nx = self [ ' pl ' ].max()
1317
+ datarr = redtoreg( datarr, self [' pl' ], missval = missval)
1319
1318
else :
1320
1319
nx = None
1321
1320
elif self .has_key(' Nx' ) and self .has_key(' Ny' ):
@@ -1550,7 +1549,7 @@ cdef class gribmessage(object):
1550
1549
lats = self [' distinctLatitudes' ]
1551
1550
if lat2 < lat1 and lats[- 1 ] > lats[0 ]: lats = lats[::- 1 ]
1552
1551
ny = self [' Nj' ]
1553
- nx = 2 * ny
1552
+ nx = self [ ' pl ' ].max()
1554
1553
lon1 = self [' longitudeOfFirstGridPointInDegrees' ]
1555
1554
lon2 = self [' longitudeOfLastGridPointInDegrees' ]
1556
1555
lons = np.linspace(lon1,lon2,nx)
@@ -1561,7 +1560,7 @@ cdef class gribmessage(object):
1561
1560
elif self [' gridType' ] == ' reduced_ll' : # reduced lat/lon grid
1562
1561
if self .expand_reduced:
1563
1562
ny = self [' Nj' ]
1564
- nx = 2 * ny
1563
+ nx = self [ ' pl ' ].max()
1565
1564
lat1 = self [' latitudeOfFirstGridPointInDegrees' ]
1566
1565
lat2 = self [' latitudeOfLastGridPointInDegrees' ]
1567
1566
lon1 = self [' longitudeOfFirstGridPointInDegrees' ]
0 commit comments