Re: netCDF adding variable to an existing file [message #83940] |
Wed, 17 April 2013 14:50  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
armor.uah@gmail.com writes:
> I'm worried about having to create a new file and copy things over because this is a "real-time" system, so every second counts. I didn't want to introduce any delays by closing the file, creating a new file, and copying all the variables over including the new variables. I don't know how much of a delay that would introduce though, so I'll find out when I implement it!
I'm not suggesting that you have to create a new file and copy things
over. The example just hows how to do either operation. In the
application for which I wrote the code I had a file containing a lot of
information I wanted in a new file. It was far easier to copy it into
the new file than it was to create it de novo every time.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
|
Re: netCDF adding variable to an existing file [message #83957 is a reply to message #83941] |
Tue, 16 April 2013 13:35   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
operator writes:
> I have been attempting to use some of the information I have gathered in this group (From Paul V. on 10/25/2011) to add a couple of variables to my netCDF file (it is a CFradial format, if that matters). I'm running into problems, and I believe the root of it all are the dimensions -- getting them and adding one more.
I can't tell you exactly what is wrong with your code, but I do remember
how frustrating it was to learn how to create a netCDF file, or copy
data from one netCDF file to another. So frustrating that I eventually
wrote my own code to do it, with much better error handling!
You can read about my programs here:
http://www.idlcoyote.com/fileio_tips/ncdf_browser.html
Pay particular attention to the "Further Updates" section at the bottom
of that page. The code I am talking about is the code I wrote for the
ncdf_file object. There is an example program there, NCDF_File_Examples,
that shows you how to create a new file from scratch, copy data from one
file to another, and read data out of a netCDF file. It is possible you
may find that helpful.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: netCDF adding variable to an existing file [message #84132 is a reply to message #83940] |
Fri, 26 April 2013 10:48  |
armor.uah
Messages: 4 Registered: April 2013
|
Junior Member |
|
|
All,
I finally figured out what was going wrong and the ways to fix it.
What was going wrong: Even though the appending procedures would fail, some data was getting appended to the file. This is where the "already defined" errors came from. The way to fix this was to remove the netCDF file and replace the file with an unmodified file every time you run the program while troubleshooting.
About getting dimension information to append to the file: I was not aware of the NCDF_DIMID call and it is required. Here is the correct code in case someone needs it in the future:
;1) Put the netcdf file in DEFINE mode (NCDF_CONTROL)
NCDF_CONTROL, netid, /REDEF
;2) If required, define new dimensions (NCDF_DIMDEF)
te_dim=NCDF_DIMDEF(netid, 'elev', three_elev)
;GET THE CURRENT DIMENSION INFORMATION IF NEEDED TO DEFINE VARIABLE(S):
tim_id = NCDF_DIMID( netid, 'time' )
ran_id = NCDF_DIMID( netid, 'range' )
;3) Define the new variable (NCDF_VARDEF). Repeat as necessary for multiple dimensions/variables.
my_var_id = NCDF_VARDEF(netid, 'My_variable', [ran_id, tim_id], /FLOAT)
other_var_id = NCDF_VARDEF(netid, 'Other_New_Variable', [ran_id, te_dim], /FLOAT)
NCDF_ATTPUT, netid, my_var_id, 'standard_name','Variable na.e'
.
.
.
;4) Put the netcdf in DATA mode (NCDF_CONTROL)
NCDF_CONTROL, netid, /ENDEF
;5) Write the new variable data to the file (NCDF_VARPUT). Repeat 6 as necessary for multiple variables.
NCDF_VARPUT, netid, my_var_id, actual_variable_values
NCDF_VARPUT, netid, other_var_id, actual_variable_values
;6) Close the file (NCDF_CLOSE)
NCDF_CLOSE, netid
|
|
|