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
|
|
|