pythonncopynco

pyNCO: extract/subset/slice netcdf by dimensions (bbox) in a single command - syntax issues


With NCO I can easily create subset from netCDF file based on given coordinates, e.g.:

ncks -d latitude,40.,50. -d longitude,10.,30. in.nc out.nc

But I do not know to reproduce such command with pyNCO. Analogical syntax could be

nco.ncks(input='in.nc', output='out.nc', dimension="longitude,10.,15.", dimension="latitude,45.,50.")

but I get an error:

SyntaxError: keyword argument repeated

I found no suitable example in documentation or forums. I managed to do some workaround with single keyword and via tempfile (which is slower) with more lines of code... but I believe there is must be more elegant syntax.

Can anyone provide some guidance here?


Solution

  • This is Python error as you have (... dimension=..., dimension = ...) and this results in keyword argument repeated. Unfortunately I have not used PyNCO, but just look at the examples at https://pynco.readthedocs.io/en/latest/#usage. It seems like the correct way to give the limits is by using options and for several dimensions options are in list.

    In your case:

    opt = [
        c.Limit("lat", 45.0, 50.0),
        c.Limit(dmn_name="lon", srt=10.0, end=15.0),
    ]
    nco.ncks(input='in.nc', output='out.nc', options = opt)