Skip to content

Commit 75e74ee

Browse files
committed
add data loaders
1 parent 9cd3e95 commit 75e74ee

File tree

1 file changed

+86
-42
lines changed

1 file changed

+86
-42
lines changed

climate_toolbox/io/io.py

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,113 @@
33
from climate_toolbox.utils.utils import *
44

55

6-
def standardize_climate_data(ds):
7-
"""
8-
Read climate data and standardize units to:
9-
- lon and lat,
10-
- lon to -180 to 180 and
6+
def load_climate_data(data_type, file_path):
7+
""" load_climate_data(data_type, file_path)
8+
Read and prepare climate data
9+
10+
:param data_type: str
11+
datatype to be read, supported types:
12+
bcsd, gmfd, best, era5
1113
12-
Parameters
13-
----------
14-
ds: xr.Dataset
14+
:param file_path: str
15+
File path
1516
16-
Returns
17-
-------
18-
xr.Dataset
17+
:return: ds: xr.Dataset
18+
xarray dataset loaded in memory
1919
"""
2020

21-
ds = rename_coords_to_lon_and_lat(ds)
22-
ds = convert_lons_split(ds, lon_name='lon')
21+
return _load_climate_data(
22+
_find_loader(data_type),
23+
file_path
24+
)
25+
26+
27+
def _load_climate_data(loader, file_path):
28+
with xr.open_dataset(file_path) as ds:
29+
ds.load()
30+
31+
return loader(ds)
2332

24-
return ds
2533

34+
def load_min_max_temperatures(data_type, file_path_tmin, file_path_tmax):
35+
""" load_min_max_temperatures(data_type, file_path_tmin, file_path_tmax)
2636
27-
def load_bcsd(fp, varname, lon_name='lon', broadcast_dims=('time',)):
37+
:param data_type: str
38+
datatype to be read, supported types:
39+
bcsd, gmfd, best, era5
40+
41+
:param file_path_tmin: path for min temperature
42+
:param file_path_tmax: path for max temperature
43+
:return:
44+
ds_tasmax: xr.Dataset, ds_tasmin: xr.Dataset
2845
"""
29-
Read and prepare climate data
3046

31-
After reading data, this method also fills NA values using linear
32-
interpolation, and standardizes longitude to -180:180
47+
return _load_min_max_temperatures(
48+
_find_loader(data_type),
49+
file_path_tmin,
50+
file_path_tmax
51+
)
3352

34-
Parameters
35-
----------
36-
fp: str
37-
File path or dataset
3853

39-
varname: str
40-
Variable name to be read
54+
def _load_min_max_temperatures(loader, file_path_tmin, file_path_tmax):
55+
with xr.open_dataset(file_path_tmin) as ds_tasmin:
56+
ds_tasmin.load()
57+
with xr.open_dataset(file_path_tmax) as ds_tasmax:
58+
ds_tasmax.load()
4159

42-
lon_name : str, optional
43-
Name of the longitude dimension (defualt selects from ['lon' or
44-
'longitude'])
60+
return loader(ds_tasmin), loader(ds_tasmax)
4561

46-
Returns
47-
-------
48-
xr.Dataset
49-
xarray dataset loaded into memory
50-
"""
5162

52-
if lon_name is not None:
53-
lon_names = [lon_name]
63+
def _find_loader(data_type):
64+
""" Helper function to find climate data loader """
5465

55-
if hasattr(fp, 'sel_points'):
56-
ds = fp
66+
data_type = data_type.lower()
5767

68+
if 'bcsd' in data_type:
69+
loader = load_bcsd
70+
elif 'gmfd' in data_type:
71+
loader = load_gmfd
72+
elif 'best' in data_type:
73+
loader = load_best
74+
elif 'era' in data_type:
75+
loader = load_era5
5876
else:
59-
with xr.open_dataset(fp) as ds:
60-
ds.load()
77+
raise TypeError("'" + data_type + "' not supported. Supported data "
78+
"types are: NASA BCSD, GMFD, BEST, ERA5.")
79+
return loader
80+
6181

62-
return standardize_climate_data(ds)
82+
def standardize_climate_data(ds):
83+
""" standardize_climate_data(ds)
84+
Standardize climate data units to:
85+
- lon and lat,
86+
- lon to -180 to 180 and
87+
88+
:param ds: xr.Dataset
89+
:return: ds: xr.Dataset
90+
"""
6391

92+
ds = rename_coords_to_lon_and_lat(ds)
93+
ds = convert_lons_split(ds, lon_name='lon')
6494

65-
def load_gmfd(fp, varname, lon_name='lon', broadcast_dims=('time',)):
66-
pass
95+
return ds
96+
97+
98+
def load_bcsd(ds):
99+
return ds
67100

68101

69-
def load_best(fp, varname, lon_name='lon', broadcast_dims=('time',)):
102+
def load_gmfd(ds):
103+
if 'tmin' in ds.data_vars or 'tmax' in ds.data_vars:
104+
return standardize_climate_data(ds)
105+
if 'lat' not in ds.coords or 'lon' not in ds.coords:
106+
ds = rename_coords_to_lon_and_lat(ds)
107+
return convert_lons_split(ds, lon_name="lon")
108+
109+
110+
def load_best():
70111
pass
71112

113+
114+
def load_era5():
115+
pass

0 commit comments

Comments
 (0)