1

I'm working with gdal library after studying the Netcdf-4 one. Is there a way to produce a json metadata output similar to the one offered by the latter lib?

this is what i got so far:

from osgeo import gdal

gdal.UseExceptions()

file = gdal.Open("NETCDF:../files/ECMWF_ERA-40_subset.nc")

filemeta = file.GetMetadata()

subsmeta = []

for subs in  file.GetSubDatasets():

    subsmeta.append(gdal.Open(subs[0]).GetMetadata())

but the output is much more confusing than the relative "ncdump -h" version and doesn't include dimensions metas.

1

You would be much better off with the Python xarray library. Along with a bunch of other nice features, metadata stored via the attrs attribute are OrderedDict objects.

A quick example:

> import xarray as xr
> ds = xr.open_dataset('zonal_wind.nc')
> ds
<xarray.Dataset>
Dimensions:     (lat: 240, lon: 240, rho_lvl: 50, time: 1)
Coordinates:
  * time        (time) datetime64[ns] 2011-08-11
  * lat         (lat) float32 -34.05 -34.1 -34.15 -34.2 -34.25 -34.3 -34.35 ...
  * lon         (lon) float32 139.0 139.05 139.1 139.15 139.2 139.25 139.3 ...
  * rho_lvl     (rho_lvl) float32 9.99777 50.0014 130.003 250.001 410.003 ...
Data variables:
    seg_type    (time) |S4 ...
    ...
    zonal_wnd   (time, rho_lvl, lat, lon) float64 ...
    merid_wnd   (time, rho_lvl, lat, lon) float64 ...
Attributes:
    positive:    up
    convention:  COARDS
    source:      Australian Bureau of Meteorology
    modl_vrsn:   ACCESS-V
    expt_id:     0001

> ds.attrs
OrderedDict([('positive', 'up'),
             ('convention', 'COARDS'),
             ('source', 'Australian Bureau of Meteorology'),
             ('modl_vrsn', 'ACCESS-V'),
             ('expt_id', '0001')])

If you're working with spatial data as well, as an added bonus xarray has an open_rasterio method to open spatial raster data built on rasterio, which in turn is built on GDAL.

  • that is true, however gdal has some nice Open bindings that allows to open files from remote sources, like an S3 bucket, for example – darkpirate Feb 3 '18 at 14:33
  • since i need to analyze the dataset remotely, i find desirable the ability to "download" only the specified chunks, without having to acquire the entire file beforehand (which to my knowledge is how gdal works), does xarray provides the same feature? – darkpirate Feb 3 '18 at 14:36
  • @darkpirate according to this answer rasterio can read from S3 as well. So in theory the open_rasterio should be fine, as it backs onto rasterio directly. Worth a shot at any rate. – om_henners Feb 4 '18 at 11:44
  • @darkpirate Although, looking at it, it appears from the GDAL docs that unfortunately NetCDF filed are supported for vfs access. – om_henners Feb 4 '18 at 11:53
  • xarray can work on remote subsets of netcdf data served by OpenDAP. See xarray.pydata.org/en/stable/io.html#opendap. How are you currently getting your subsets? – Dave X Feb 6 '18 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.