comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Add a new variable to netCDF file?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Add a new variable to netCDF file? [message #78063] Tue, 25 October 2011 06:49 Go to next message
Kenneth P. Bowman is currently offline  Kenneth P. Bowman
Messages: 585
Registered: May 2000
Senior Member
In article <j86e3h$5of$1@speranza.aioe.org>,
Paul van Delst <paul.vandelst@noaa.gov> wrote:

> Hello,
>
> Ashley Berg wrote:
>> So, I know how to overwrite a variable in a netCDF file, but how do I
>> add a new one?
>
> 0) Open the existing file for writing (NCDF_OPEN)
.
.
.
> 8) Close the file (NCDF_CLOSE)
>
> cheers,
>
> paulv

And, by the way, this usually requires creating a new file and
copying the contents of the original file. So, it may not be very
efficient, and you probably want to operate on a *copy* of your input
file in case something goes wrong during the process.

Cheers, Ken Bowman
Re: Add a new variable to netCDF file? [message #78065 is a reply to message #78063] Tue, 25 October 2011 06:53 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Paul van Delst writes:

> 0) Open the existing file for writing (NCDF_OPEN)
> 1) Put the netcdf file in DEFINE mode (NCDF_CONTROL)
> 2) If required, define new dimensions (NCDF_DIMDEF)
> 3) Define the new variable (NCDF_VARDEF)
> 4) Repeat 2-3 as necessary for multiple dimensions/variables.
> 5) Put the netcdf in DATA mode (NCDF_CONTROL)
> 6) Write the new variable data to the file (NCDF_VARPUT)
> 7) Repeat 7 as necessary for multiple variables.
> 8) Close the file (NCDF_CLOSE)

My memory is shaking on this topic, but I thought it
was not possible to add a variable to a netCDF file.
Although I seem to remember some provision
for this in the latest version of netCDF. My memory
is so fuzzy, I have no confidence in it, but I recall
this being an issue with *some* version of the scientific
data sets.

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Add a new variable to netCDF file? [message #78066 is a reply to message #78063] Tue, 25 October 2011 06:37 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
Hello,

Ashley Berg wrote:
> So, I know how to overwrite a variable in a netCDF file, but how do I
> add a new one?
>
> I have a netCDF file with about 30 variables and I want to add two new
> ones. It would be a pain to have to copy over all 30 vars plus the
> new ones into a new file, so is there a way to add variables using the
> netCDF operators? I've been experimenting but haven't been able to
> find a way to do it (when I try to write a new variable to the file,
> it complains that it can't find the variable, which implies it's
> looking to overwrite an existing one rather than tack on the new one).

0) Open the existing file for writing (NCDF_OPEN)
1) Put the netcdf file in DEFINE mode (NCDF_CONTROL)
2) If required, define new dimensions (NCDF_DIMDEF)
3) Define the new variable (NCDF_VARDEF)
4) Repeat 2-3 as necessary for multiple dimensions/variables.
5) Put the netcdf in DATA mode (NCDF_CONTROL)
6) Write the new variable data to the file (NCDF_VARPUT)
7) Repeat 7 as necessary for multiple variables.
8) Close the file (NCDF_CLOSE)

cheers,

paulv

>
> Thanks!
Re: Add a new variable to netCDF file? [message #78158 is a reply to message #78063] Tue, 25 October 2011 08:02 Go to previous message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
hello,

Kenneth P. Bowman wrote:
> In article <j86e3h$5of$1@speranza.aioe.org>,
> Paul van Delst <paul.vandelst@noaa.gov> wrote:
>
>> Hello,
>>
>> Ashley Berg wrote:
>>> So, I know how to overwrite a variable in a netCDF file, but how do I
>>> add a new one?
>> 0) Open the existing file for writing (NCDF_OPEN)
> .
> .
> .
>> 8) Close the file (NCDF_CLOSE)
>>
>> cheers,
>>
>> paulv
>
> And, by the way, this usually requires creating a new file and
> copying the contents of the original file. So, it may not be very
> efficient, and you probably want to operate on a *copy* of your input
> file in case something goes wrong during the process.

Huh. I have never encountered this.

From the "Adding new dimensions, variables, and attributes" section of the "netCDF Fortran90 interface guide":

<quote>
An existing netCDF dataset can be extensively altered. New dimensions, variables, and attributes can be added and
existing ones renamed, and existing attributes can be deleted.
</quote>

Also, from the "Variables" section of the same document:

<quote>
Variables for a netCDF dataset are defined when the dataset is created, while the netCDF dataset is in define mode.
Other variables may be added later by reentering define mode.
</quote>

Seems quite clear to me. I've been doing it in v3.x of netCDF for years now. I would consider *any* failure of an API to
do this correctly as a critical bug.

I will second Ken's comment about efficiency: adding a new dimension, or a variable attribute, or a global attribute, to
a large existing netCDF dataset can be quite slow since all of the following data has to be "pushed down" to make way
for it. At least, that's my perception of the process for netcdf v3.x. NetCDF 4.x may be supa-efficient regardless.

cheers,

paulv
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Religious Wars
Next Topic: How to determine if ENVI is loaded?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:11:08 PDT 2025

Total time taken to generate the page: 0.00689 seconds