Skip to content

Commit 774b10e

Browse files
authored
Add CHELSA-W5E5 tile compression. (#102)
* Compress tile files with nccopy. * Document changes in whatsnew.
1 parent 8e6da89 commit 774b10e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

doc/whatsnew.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ New features
3333
(aliased ``'cw5e5'``) as a new temperature and precipitation data source in
3434
:func:`hyoga.open.atmosphere` (:issue:`86`, :pull:`87`), aggregated on 30x30
3535
degree tiles (:issue:`88`, :pull:`92`), concatenated yearly (:issue:`99`,
36-
:pull:`100`).
36+
:pull:`100`), and compressed with netCDF-4 (:issue:`101`, :pull:`102`).
3737

3838
.. _CHELSA-W5E5: https://chelsa-climate.org/chelsa-w5e5-v1-0-daily-climate-data-at-1km-resolution/
3939

@@ -46,7 +46,7 @@ Internal changes
4646
~~~~~~~~~~~~~~~~
4747

4848
- Add aggregators in :mod:`hyoga.open.aggregator` (:issue:`86`, :issue:`88`,
49-
:issue:`99`, :pull:`87`, :pull:`92`, :pull:`100`).
49+
:issue:`99`, :issue:`101`, :pull:`87`, :pull:`92`, :pull:`100`, :pull:`102`).
5050

5151
.. _v0.3.1:
5252

hyoga/open/aggregator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"""
1010

1111
import os.path
12+
import subprocess
13+
import tempfile
1214
import xarray as xr
1315
import hyoga.open.downloader
1416

@@ -171,9 +173,17 @@ def aggregate(self, inputs, output, recipe='avg'):
171173
tile = tile.groupby('time.month')
172174
tile = getattr(tile, recipe)(keep_attrs=True)
173175

174-
# store output as netcdf and return path
176+
# store as uncompressed netcdf
175177
print(f"aggregating {tilepath} ...")
176178
tile.to_netcdf(tilepath)
177179

180+
# nccopy compression beats xarray by far
181+
print(f"compressing {tilepath} ...")
182+
dirname, basename = os.path.split(tilepath)
183+
with tempfile.NamedTemporaryFile(
184+
dir=dirname, prefix=basename+'.') as tmp:
185+
subprocess.run(['nccopy', '-sd6', tilepath, tmp.name])
186+
os.replace(tmp.name, tilepath)
187+
178188
# return multi-tile output pattern
179189
return output

0 commit comments

Comments
 (0)