Re: [netcdf-java] Questions

Sebastian,

please try using NetcdfFileWriter.addDimension to create each dimension and add it to the target file, after testing that it does not already exist. You can use NetcdfFileWriter.findDimension to locate dimensions in the target file. In your code below, your new Dimension instances have not been yet been added to the target file so cannot be used to define variables.

Kind regards,
Ben.

On 16/02/18 03:02, Sebastian Raubach wrote:
Hello,

I've been tasked with running some data manipulation across a netcdf file. It's 
supposed to work off an existing file, but create a new one that has the same 
structure (although dimensions will have different lengths) and then change 
some of the cell values by applying a formula.

I'm currently trying to replicate the structure of the existing file in the new 
file. I can copy the global attributes just fine and wanted to re-create the 
variables and dimensions next. To do so, I wrote this code:

                 @Override
                 public void process(NetcdfFile original, NetcdfFileWriter 
target, Config config) throws IOException
                 {
                                 for(Attribute att : 
original.getGlobalAttributes())
                                                 
target.addGroupAttribute(att.getGroup(), att);

                                 for(Variable var : original.getVariables())
                                 {
                                                 Group group = 
target.addGroup(null, var.getGroup().getShortName());
                                                 List<Dimension> dimensions = new 
ArrayList<>();

                                                 for(Dimension dim : 
var.getDimensions())
                                                 {
                                                                 
dimensions.add(new Dimension(dim.getShortName(), dim.getLength(), 
dim.isShared(), dim.isUnlimited(), dim.isVariableLength()));
//                                                            
dimensions.add(new Dimension(dim.getShortName(), dim));
                                                 }

                                                 target.addVariable(group, 
var.getShortName(), var.getDataType(), dimensions);
                                 }
                 }

I ran into several issues. The code as it stands throws this exception:

Exception in thread "main" java.lang.IllegalStateException: unknown Dimension 
== time = UNLIMITED;   // (53970 currently)
                 at 
ucar.nc2.iosp.netcdf3.N3header.findDimensionIndex(N3header.java:970)
                 at ucar.nc2.iosp.netcdf3.N3header.writeVars(N3header.java:910)
                 at 
ucar.nc2.iosp.netcdf3.N3header.writeHeader(N3header.java:666)
                 at ucar.nc2.iosp.netcdf3.N3header.create(N3header.java:602)
                 at ucar.nc2.iosp.netcdf3.N3iosp.create(N3iosp.java:683)
                 at ucar.nc2.NetcdfFileWriter.create(NetcdfFileWriter.java:804)
                 at jhi.netcdf.Main.main(Main.java:58)

I can't really find any information online about how to best approach this 
scenario or what exactly the exception means. If anyone can give me any clues 
or has experience with this kind of thing, that'd really be appreciated.
Maybe there is a more convenient way to re-create an existing file structure 
(not the actual data) that I'm not aware of.

Regards,
Sebastian Raubach


The James Hutton Institute is a Scottish charitable company limited by 
guarantee.
Registered in Scotland No. SC374831
Registered Office: The James Hutton Institute, Invergowrie Dundee DD2 5DA.
Charity No. SC041796



_______________________________________________
NOTE: All exchanges posted to Unidata maintained email lists are
recorded in the Unidata inquiry tracking system and made publicly
available through the web.  Users who post to any of the lists we
maintain are reminded to remove any personal information that they
do not want to be made public.


netcdf-java mailing list
netcdf-java@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit: 
http://www.unidata.ucar.edu/mailing_lists/


--
Ben Caradoc-Davies <ben@xxxxxxxxxxxx>
Director
Transient Software Limited <https://transient.nz/>
New Zealand



  • 2018 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: