netCDF adding variable to an existing file [message #83958] |
Tue, 16 April 2013 12:51  |
armor.uah
Messages: 4 Registered: April 2013
|
Junior Member |
|
|
All,
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. So, this file is open:
;1) Put the netcdf file in DEFINE mode (NCDF_CONTROL)
NCDF_CONTROL, netid, /REDEF
;2) If required, define new dimensions (NCDF_DIMDEF)
;This is the only dimension that is not already defined:
three_el_dim=NCDF_DIMDEF(netid, 'three', three_elev_ang)
;PROBLEM #1: I cannot define this new dimension: "% NCDF_DIMDEF: Unable to create dimension, name in use by another dimension (three)."
;The name "three" was not used anywhere else in my script, tried several other random string here and it breaks there every time.
;Problem #2: I cannot use the dimensions that already exist. I have tried using the outputs from NCDF_DIMINQ and would get errors in the "NCDF_VARDEF" telling me "% NCDF_VARDEF: Unable to define variable, bad dimension id ." so I decided to create new dimensions (I would rather not, but will if I have to!). These are the same size as the existing dimensions:
t_dim=NCDF_DIMDEF(netid,'times', time_dim)
r_dim=NCDF_DIMDEF(netid,'ranges', range_dim)
The error here is the same as above: "% NCDF_DIMDEF: Unable to create dimension, name in use by another dimension"
;For the sake of completeness, here are the other things I'm doing:
;3) Define the new variable (NCDF_VARDEF)
;Trying to use the dimensions that are already defined in the file but failed, these are here with the dimensions I tried to create:
LWCid = NCDF_VARDEF(netid, 'LWC',[t_dim, r_dim])
;errors: "% NCDF_VARDEF: Unable to define variable, name in use by another variable (LWC)." I thought: Sure, that's the name of my variable, so I changed 'LWC' to 'aLWC' and now am getting a different error: "% NCDF_VARDEF: Unable to define variable, bad dimension id ." ... maybe this means that when I get my dimensions issues figured out this error will fix itself??
;This variable has the new dimension I tried to define but failed:
Rid = NCDF_VARDEF(netid, 'Rain_rate', [three_el_dim, R_DIM])
;4) Repeat 2-3 as necessary for multiple dimensions/variables.
;5) Put the netcdf in DATA mode (NCDF_CONTROL)
NCDF_CONTROL, netid, /ENDEF
;6) Write the new variable data to the file (NCDF_VARPUT)
NCDF_VARPUT, netid, LWCid, LWC
NCDF_VARPUT, netid, Rid, R
;error: % NCDF_VARPUT: Variable Inquiry failed, -1 is not a valid variable id.
;7) Repeat 6 as necessary for multiple variables.
;8) Close the file (NCDF_CLOSE)
NCDF_CLOSE, netid
Any help would be appreciated!
|
|
|
Re: netCDF adding variable to an existing file [message #84130 is a reply to message #83958] |
Fri, 26 April 2013 11:08  |
armor.uah
Messages: 4 Registered: April 2013
|
Junior Member |
|
|
On Friday, April 26, 2013 12:58:58 PM UTC-5, David Fanning wrote:
> armor.uah@gmail.com writes:
>
>
>
>> 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
>
>
>
> For people who prefer not to spend weeks figuring this out on their own,
>
> I just point out that the NCDF_File object in the Coyote Library shields
>
> you from knowing all the details of how this works, and gives (perhaps)
>
> much more informative and descriptive error messages to alert you to
>
> problems. :-)
>
>
>
> 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.")
Amen. Your code has convinced me to fly to Colorado and take your object oriented IDL course, whenever you offer it again.
|
|
|
Re: netCDF adding variable to an existing file [message #84131 is a reply to message #83958] |
Fri, 26 April 2013 10:58  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
armor.uah@gmail.com writes:
> 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
For people who prefer not to spend weeks figuring this out on their own,
I just point out that the NCDF_File object in the Coyote Library shields
you from knowing all the details of how this works, and gives (perhaps)
much more informative and descriptive error messages to alert you to
problems. :-)
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.")
|
|
|