Skip to content

Commit 0ba2ecd

Browse files
authored
Merge pull request #13 from gregstarr/develop
finalize southern hemisphere
2 parents 932cbba + a20ef65 commit 0ba2ecd

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

trough/_arb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_arb_paths(start_date, end_date, hemisphere, processed_dir):
3636
def get_arb_data(start_date, end_date, hemisphere, processed_dir=None):
3737
if processed_dir is None:
3838
processed_dir = config.processed_arb_dir
39-
data = xr.concat([xr.open_dataarray(file) for file in get_arb_paths(start_date, end_date, hemisphere, processed_dir)], 'time')
39+
data = utils.read_netcdfs(get_arb_paths(start_date, end_date, hemisphere, processed_dir), 'time')
4040
return data.sel(time=slice(start_date, end_date))
4141

4242

trough/_tec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def get_tec_paths(start_date, end_date, hemisphere, processed_dir):
7373
def get_tec_data(start_date, end_date, hemisphere, processed_dir=None):
7474
if processed_dir is None:
7575
processed_dir = config.processed_tec_dir
76-
data = xr.concat([xr.open_dataarray(file) for file in get_tec_paths(start_date, end_date, hemisphere, processed_dir)], 'time')
76+
data = utils.read_netcdfs(get_tec_paths(start_date, end_date, hemisphere, processed_dir), 'time')
7777
return data.sel(time=slice(start_date, end_date))
7878

7979

trough/_trough.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def get_model(tec_data, hemisphere, omni_file):
3333
logger.info(f"{kp.shape=}")
3434
apex = Apex(date=utils.datetime64_to_datetime(tec_data.time.values[0]))
3535
mlat = 65.5 * np.ones((tec_data.time.shape[0], tec_data.mlt.shape[0]))
36-
if hemisphere == 'south':
37-
mlat = mlat * -1
3836
for i in range(10):
3937
glat, glon = apex.convert(mlat, tec_data.mlt.values[None, :], 'mlt', 'geo', 350, tec_data.time.values[:, None])
4038
mlat = _model_subroutine_lat(tec_data.mlt.values[None, :], glon, kp[:, None], hemisphere)
39+
if hemisphere == 'south':
40+
mlat = mlat * -1
4141
tec_data['model'] = xr.DataArray(
4242
mlat,
4343
coords={'time': tec_data.time, 'mlt': tec_data.mlt},
@@ -311,7 +311,7 @@ def get_label_paths(start_date, end_date, hemisphere, processed_dir):
311311
def get_trough_labels(start_date, end_date, hemisphere, labels_dir=None):
312312
if labels_dir is None:
313313
labels_dir = config.processed_labels_dir
314-
data = xr.concat([xr.open_dataarray(file) for file in get_label_paths(start_date, end_date, hemisphere, labels_dir)], 'time')
314+
data = utils.read_netcdfs(get_label_paths(start_date, end_date, hemisphere, labels_dir), 'time')
315315
return data.sel(time=slice(start_date, end_date))
316316

317317

trough/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import warnings
44
import logging
5+
import xarray as xr
56
try:
67
import h5py
78
from skimage.util import view_as_windows
@@ -153,3 +154,20 @@ def check(start, end, dt, hemisphere, processed_file):
153154
return True
154155

155156
return check
157+
158+
159+
def read_netcdfs(files, dim):
160+
"""https://xarray.pydata.org/en/stable/user-guide/io.html#reading-multi-file-datasets
161+
"""
162+
def process_one_path(path):
163+
# use a context manager, to ensure the file gets closed after use
164+
with xr.open_dataarray(path) as ds:
165+
# load all data from the transformed dataset, to ensure we can
166+
# use it after closing each original file
167+
ds.load()
168+
return ds
169+
170+
paths = sorted(files)
171+
datasets = [process_one_path(p) for p in paths]
172+
combined = xr.concat(datasets, dim)
173+
return combined

0 commit comments

Comments
 (0)