Skip to content

Commit 54cbe3b

Browse files
committed
PIOMAS model file custom geodataset
1 parent b2cd4a5 commit 54cbe3b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

geodataset/custom_geodataset.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import datetime as dt
44
import numpy as np
55
import pyproj
6+
import calendar
7+
from functools import cached_property
68

79
from geodataset.geodataset import GeoDatasetRead
810
from geodataset.utils import InvalidDatasetError
@@ -237,3 +239,67 @@ def datetime_bounds(self):
237239
dto - dt.timedelta(hours=12),
238240
dto + dt.timedelta(hours=12),
239241
])]
242+
243+
244+
class PIOMASModelFile(CustomDatasetRead):
245+
246+
grid_mapping = (pyproj.CRS.from_proj4(
247+
'+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 '
248+
'+ellps=WGS84 +units=m +no_defs'), 'absent')
249+
pattern = re.compile(r'PIOMAS_\d{4}.nc')
250+
251+
@cached_property
252+
def is_lonlat_dim(self):
253+
"""
254+
Returns:
255+
--------
256+
is_lonlat_dim : bool
257+
True if lon,lat are dimensions
258+
"""
259+
return True
260+
261+
def get_lonlat_arrays(self, ij_range=(None, None, None, None), **kwargs):
262+
""" Get array with longitude latidtude arrays
263+
264+
Parameters
265+
----------
266+
ij_range : tuple with 4 ints
267+
start/stop along i and j (y and x) axis
268+
kwargs : dict
269+
dummy
270+
271+
Returns
272+
-------
273+
lon : numpy.ndarray
274+
2D array with longitude
275+
lat : numpy.ndarray
276+
2D array with latitude
277+
"""
278+
i0, i1, j0, j1 = ij_range
279+
lon = self['longitude'][i0:i1, j0:j1]
280+
lat = self['latitude'][i0:i1, j0:j1]
281+
return lon, lat
282+
283+
@property
284+
def datetime_bounds(self):
285+
"""
286+
Datetimes are the start and end of day
287+
288+
Returns:
289+
--------
290+
datetime_bounds : list(np.array)
291+
each element is an array [dto1, dto2], where
292+
hi
293+
dto1: datetime.datetime
294+
start of observation interval
295+
dto2: datetime.datetime
296+
end of observation interval
297+
"""
298+
bnds = []
299+
for dto in self.datetimes:
300+
ym = (dto.year, dto.month)
301+
dto1 = dt.datetime(*ym, 1)
302+
_, ndays = calendar.monthrange(*ym)
303+
dto2 = dto1 + dt.timedelta(ndays)
304+
bnds += [np.array([dto1, dto2])]
305+
return bnds

0 commit comments

Comments
 (0)