Skip to content

Commit 6b467c6

Browse files
authored
Use only strings in xarray.Dataset metadata (fatiando#25)
The `harmonica.load_icgem_gdf` function populated the `Dataset.attrs` with metadata derived from the file header. Two of these fields (`attributes` and `attributes_units`) were lists. This was fine while using xarray with netcdf4 but it does not work if trying to use scipy to save/load the `Dataset`. And since xarray 0.11 stopped installing netcdf4 automatically, we can't load our sample gravity and topography data without adding netcdf4 as a dependency. Instead of doing that, `load_icgem_gdf` now converts the metadata to strings. New copies of the two sample grids have been generated using scipy (netcdf3).
1 parent f526f83 commit 6b467c6

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

data/etopo1-0.5deg.nc.xz

-7.78 KB
Binary file not shown.

data/gravity-earth-0.5deg.nc.xz

-10.2 KB
Binary file not shown.

harmonica/datasets/registry.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
etopo1-0.5deg.nc.xz bb44b4aaa49778159dc3655ae0c0c23ad88789328829557ba7ea0113f26baa7f
2-
gravity-earth-0.5deg.nc.xz 84340b771ac91695b0874639a1497b6b81e0f5a8d21ac51cc221d1c48598f5a7
1+
etopo1-0.5deg.nc.xz 9f21f7c946e649389dce28631de94fa05217750e9ebc77df4311458c33f0682b
2+
gravity-earth-0.5deg.nc.xz 02c62a251225e2e76722a80d87c488160fbdcae2c1a8bbc2386c32e68ea8f43a

harmonica/datasets/sample_data.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def fetch_gravity_earth():
4242
(``height_over_ell``). Coordinates are latitude and longitude.
4343
4444
"""
45-
return _load_xz_compressed_grid(POOCH.fetch("gravity-earth-0.5deg.nc.xz"))
45+
fname = POOCH.fetch("gravity-earth-0.5deg.nc.xz")
46+
data = _load_xz_compressed_grid(fname, engine="scipy")
47+
return data
4648

4749

4850
def fetch_topography_earth():
@@ -65,7 +67,9 @@ def fetch_topography_earth():
6567
The topography grid (in meters). Coordinates are latitude and longitude.
6668
6769
"""
68-
return _load_xz_compressed_grid(POOCH.fetch("etopo1-0.5deg.nc.xz"))
70+
fname = POOCH.fetch("etopo1-0.5deg.nc.xz")
71+
data = _load_xz_compressed_grid(fname, engine="scipy")
72+
return data
6973

7074

7175
def _load_xz_compressed_grid(fname, **kwargs):

harmonica/io.py

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def load_icgem_gdf(fname, **kwargs):
4949
if "height_over_ell" in metadata:
5050
height = float(metadata["height_over_ell"].split()[0])
5151
data_vars["height_over_ell"] = (dims, np.full(shape, height))
52+
# Can't have lists in the Dataset metadata to make it compatible with netCDF3. This
53+
# way the data can be saved using only scipy as a dependency instead of netcdf4.
54+
metadata["attributes"] = " ".join(metadata["attributes"])
55+
metadata["attributes_units"] = " ".join(metadata["attributes_units"])
5256
grid = xr.Dataset(data_vars, coords=coords, attrs=metadata)
5357
# Check area from header equals to area from data in cols
5458
area = [

0 commit comments

Comments
 (0)