Search code examples
pythonpackagenetcdfncocdo-climate

Importing nco package: TypeError: object of type 'NoneType' has no len()


I'm trying to use the nco package to merge multiple netcdf files using ncrcat. This in itself is easy to do, but I'm unable to get past the second line:

from nco import Nco
nco=Nco()

mrms1= nc.Dataset("/Users/arian/project/data/mrms_20190501/20190501_190000.nc")
mrms2= nc.Dataset("/Users/arian/project/data/mrms_20190501/20190501_190500.nc")

nco.ncrcat(input=[mrms1, mrms2], output='testnet.nc')

Where the nco=Nco() gives the error:

TypeError: object of type 'NoneType' has no len()

Ordinarly I know this error arises when you try to get the length from nothing, but from what I understand this instance has to be created in order to use ncrcat in the script.

I made sure an updated version of both nco and pynco is on anaconda. I also tried using the cdo package to merge the files the files instead, but I get the same error. I've also used ncrcat as a command line in the terminal, but I get a "term is not recognized as a cmdlet, function, script file, or operable program" so that's a separate can of worms. Also, netCDF4.MFDataset doesn't work because the data doesn't have time dimension to aggregate, nor does xarray, which gives a TypeError: coercing to Unicode: need string or buffer, Dataset found.

Based on many hours of research, I feel the nco len() Typerror is the easiest to fix, but all help from anything I mentioned is welcome. Thanks in advance.


Solution

  • Two points

    1) input must be a string of a filename or a list of strings of filenames
    e.g input =["20190501_190000.nc", "20190501_190500.nc"]

    2) I am no expert on conda but to run pynco the nco commands must be in your PATH environment variable