Hi Ben,
Thank you, that helped. I can now create a new file that has all the same
variables and dimensions. I had to move to NetCDF4 using the C library, but
that appeared to be working fine until I actually tried adding some data to the
new file using NetcdfFileWriter#write(Variable, int[], Array).
I now get the following exception:
Exception in thread "main" java.lang.ClassCastException:
ucar.nc2.iosp.netcdf3.N3header$Vinfo cannot be cast to
ucar.nc2.jni.netcdf.Nc4Iosp$Vinfo
at ucar.nc2.jni.netcdf.Nc4Iosp.writeData(Nc4Iosp.java:2799)
at ucar.nc2.NetcdfFileWriter.write(NetcdfFileWriter.java:958)
Which makes me think that there is a version 3 <-> 4 conflict/issue here, but I
can't seem to figure out what's wrong. The file I'm reading from is a v3 file
and I'm trying to write to v4.
I tried googling for the error, but couldn't find much.
Do you have any idea what I need to do here?
Many thanks,
Sebastian
-----Original Message-----
From: Ben Caradoc-Davies [mailto:ben@xxxxxxxxxxxx]
Sent: 15 February 2018 20:06
To: Sebastian Raubach <Sebastian.Raubach@xxxxxxxxxxxx>;
netcdf-java@xxxxxxxxxxxxxxxx
Subject: 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
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