Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ncks fails to create netCDF-4 files with "non-coordinate" variables named like dimensions #221

Open
vadim-z opened this issue Sep 21, 2020 · 1 comment
Labels

Comments

@vadim-z
Copy link

vadim-z commented Sep 21, 2020

Hello,

I use netCDF as the output format for my thermo-mechanical code in a field far away from climatology, therefore I haven't used conventions.

Conventionally, netCDF files may have coordinate variables, which have the same name as corresponding dimensions and contain grid coordinates.

Instead, I create scalar integer variables which have the same name and value as corresponding dimensions. Now I consider that decision a big mistake, but a lot of calculations has been performed already and some software depends on this format.

I use ncks to extract variable values from large datasets. But ncks fails to convert these extracted files to HDF-5 varieties. I have prepared trivial examples. File t1.cdl

netcdf t1 {

dimensions:
   lat = 3;

   variables:
       float lat (lat);
       float vvv (lat);

   data:
      vvv = 0., 10., 20.;
      lat = 30., 40., 50.;
}

which has a coordinate variable "lat" can be processed with ncgen:

ncgen -o t1.nc t1.cdl

and then successfully converted by ncks:

ncks -7 t1.nc t1_7.nc

But file t2.cdl

netcdf t2 {

dimensions:
   lat = 3;

   variables:
       int lat;
       float vvv (lat);

   data:
      vvv = 0., 10., 20.;
      lat = 3;
}

contains a scalar integer variable lat with the same value as dimension. It can also be processed with ncgen:

ncgen -o t2.nc t2.cdl

But conversion by ncks:

ncks -7 t2.nc t2_7.nc

fails with the

nco_err_exit(): ERROR Error code is -101. Translation into English with nc_strerror(-101) is "NetCDF: HDF error"

A netCDF format specification states that

An odd case may arise in which the user defines a variable with the same name as a dimension, but which is not intended to be the coordinate variable for that dimension. In this case the string "nc4_non_coord" is pre-pended to the name of the HDF5 dataset, and stripped from the name for the netCDF API.

It seems that I've encountered this odd corner case. Interestingly, nccopy converts both data files to netCDF-4 without any problem.

As a workaround, I will use ncks to extract variables to netCDF-3 file and then use nccopy to convert them to netCDF-4.

With best regards,
Vadim Zborovskii

@czender
Copy link
Member

czender commented Sep 21, 2020

I learned something new from your careful reporting, thank you. You have found a good work-around for this corner case, which we will try to address in due course. BTW, you can generate files directly to netCDF4 format with, e.g., ncgen -7 -o t2.nc t2.cdl.

@czender czender added the bug label Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants