Re: Append to record variable in Netcdf file

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu, 18 Sep 2008 12:09:43 -0600

All,

I had some trouble when trying to append data with a time coordinate
variable to a Netcdf file. Attached is a sample program that does
this correctly, in case others may find it helpful. The main issues
are:

(1) The time dimension must be unlimited in the original file.

(2) When appending, the added time coordinate(s) must be explicitly
written along with the added data. Otherwise you get missing values
in the time coordinate variable, which causes trouble with other
software.

Thanks to David Brown for his initial example, which got me on the
right track.

Dave Allured
CU/CIRES Climate Diagnostics Center (CDC)
http://cires.colorado.edu/science/centers/cdc/
NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
http://www.cdc.noaa.gov/

David Brown wrote:
> Hi Dave,
> OK, that's a good idea.
> -dave
>
> On Sep 17, 2008, at 3:36 PM, Dave Allured wrote:
>
>> Thanks Dave. It would be good to add a brief note about this in the
>> NCL docs for file I/O or Netcdf.
>>
>> --Dave
>>
>> David Brown wrote:
>>> Hi Dave,
>>> Yes of course. You can append to the unlimited dimension simply by
>>> specifying the element that you want to write,
>>> including leaving 'holes' that get filled with missing values.
>>> I am attaching a simple example.
>>> On Sep 16, 2008, at 12:20 PM, Dave Allured wrote:
>>>> Hello. Does NCL support physically appending data to a record
>>>> variable in a Netcdf file? The Netcdf API supports this as long as
>>>> Unlimited is set for the record dimension.
>>>>
>>>> I do not find any mention of this in NCL documentation for files and
>>>> file variables. If not available, this would be a good feature to
>>>> put on the wish list. Thank you.
>>>>
>>>> Dave Allured
>>>> CU/CIRES Climate Diagnostics Center (CDC)
>>>> http://cires.colorado.edu/science/centers/cdc/
>>>> NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
>>>> http://www.cdc.noaa.gov/
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> ncl-talk_at_ucar.edu
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk_at_ucar.edu
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

;----------------------------------------------------------------------------
; Test Append mode in NCL, with time coordinate variable.
; 2008-sep-18 By Dave Allured, NOAA-CIRES PSD/CDC.
; Portions borrowed from test-unlim.ncl, by David Brown, UCAR.
;----------------------------------------------------------------------------

begin

; Create Netcdf file with dummy data and unlimited time dimension.
   
   filename = "nc-append.nc"

   print ("Create test file: " + filename)

   if (isfilepresent (filename)) then
      system ("rm " + filename) ; remove previous file
   end if

   f = addfile (filename, "c") ; create new file

   dim_names = "time" ; create "time" as unlimited dimension
   dim_sizes = -1
   dimUnlim = True
   filedimdef (f, dim_names, dim_sizes, dimUnlim)

; Create dummy data variable in memory, with time coordinates attached.

   time = (/ 0d, 1d, 2d, 3d /) ; time = type double
   time_at_units = "days since 2000-1-1" ; make valid for ut_calendar
   
   data = (/ 100., 200., 300., 400. /) ; data = type float
   data!0 = "time"
   data&time = time

; Write first three time steps to file. The short method to create
; file variables "var" and "time" in one line is used.
;
; filevardef is not used in this example. However, either method
; for definining new variables would be fine.

   f->var = data(0:2) ; time and attributes are also automatically written

; Now append data at new time step.

; Note: Unlike previous, when "var" was first created, time coordinates
; will not be automatically written to file with each new data value.
; Therefore the new time coordinate must be written explicitly.
   
   print ("Append data at new time step.")

   f->time(3) = time(3)
   
   f->var(3) = (/ data(3) /) ; must use array designators (/ ... /)
                                           ; to prevent spurious attribute
   print ("File written.")

end

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Sep 18 2008 - 12:09:43 MDT

This archive was generated by hypermail 2.2.0 : Tue Sep 23 2008 - 14:12:01 MDT