Re: problem writing netCDF file using filevardef

From: David Ian Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Mon, 8 May 2006 15:42:47 -0600

Hi Saji,

It's not hard to add data to the unlimited dimension. Here's a little
script that demonstrates how to do it (I'm also including it as an
attachment).

;-----------------------------------------------------------
; remove old versions of "unlimited.nc"

system("rm -rf unlimited.nc")

; create "unlimited.nc"

f = addfile("unlimited.nc","c")

; define dimensions, coordinate variables, and a variable "x" with
; 3 dimensions

filedimdef(f,(/"time","lat","lon"/),(/-1,5,5/),(/True,False,False/))
filevardef(f,"time","float","time")
filevardef(f,"lat","float","lat")
filevardef(f,"lon","float","lon")
filevardef(f,"x","float",(/"time","lat","lon"/))

; assign dimension values
f->lat = (/0,15,30,45,60/)
f->lon = (/0,15,30,45,60/)

; time has 0 elements so far, but you can still assign attributes

f->time_at_units = "hours since 2006-05-08 12:00"
print(f)

x = new((/3,5,5/),"float")
x = 30.0
; assign first 3 elements of time
f->x = x
; make sure the coordinate variable gets assigned as well
f->time = (/0,6,12/)

; dump file contents
system("ncdump unlimited.nc")

; reread file in write mode
; now appending to an existing file

f = addfile("unlimited.nc","w")

; assign 4th element of time
f->x(3,:,:) = 31.0
f->time(3) = 18
printFileVarSummary(f,"x")

; assign 6th element; note 5th element set to fill value

f->x(5,:,:) = 32.0

; but define all the coordinate values
f->time(4:5) = (/ 24, 30 /)
printFileVarSummary(f,"x")

; assign 7th and 8th element of time
f->x(6:7,:,:) = 33.0
f->time(6:7) = (/ 36, 42 /)
printFileVarSummary(f,"x")

; dump file contents
system("ncdump unlimited.nc")
;-----------------------------------------------------------------------
---------------

On May 5, 2006, at 3:36 PM, Saji Njarackalazhikam Hameed wrote:

> Hi,
>
> Maybe this is the right occasion to ask this question. How can I keep
> adding data to the unlimited dimension? A small code sniplet would be
> helpful : say I want to keep writing data along the time dimension to
> the same output netcdf file.
>
> Thanks,
>
> saji
>
>
> * David Ian Brown <dbrown_at_ucar.edu> [2006-05-05 12:49:49 -0600]:
>
>> Hi Will,
>> The NetCDF library requires that only the left-most (slowest moving)
>> dimension
>> can be unlimited. In your script you define time as an unlimited
>> dimension and then
>> define the variable "slp_mn" with time as the right-most dimension.
>> That is not
>> allowed by NetCDF and that is what the message is telling you.
>
>> FYI, NetCDF 4, which is due to be released this fall, will allow
>> multiple
>> unlimited dimensions. We intend to support NetCDF 4 at the time of its
>> release.
>> -dave
>
>> On May 5, 2006, at 12:28 PM, Will Hobbs wrote:
>
>>> Hi
>
>>> I have a script that writes data to a netCDF, but I get the following
>>> error message
>
>>> ncvardef: ncid 10: NC_UNLIMITED in the wrong index
>>> fatal:FileAddVar: an error occurred while adding a variable to a
>>> file,
>>> check to make sure data type is supported by the output format
>>> fatal:Execute: Error occurred at or near line 162 in file
>>> station_daytomonth.ncl
>
>>> The command at line 162 is ' filevardef(fout, "slp_mn",
>>> typeof(slp_mn),
>>> (/"region","time"/))'
>
>>> The data type of slp_mn is float, as are 'region' and 'time', so I
>>> really
>>> can't see where the problem is. The full script is pasted below, and
>>> I've
>>> moved the datafile to my ptmp directory (i.e. /ptmp/whobbs/) on
>>> 'tempest', should you wish to try and run it.
>
>>> Many thanks
>
>>> Will
>
>
>
>>> ============================
>>> Room 3221C
>>> UCLA Department of Geography
>>> 1255 Bunche Hall
>>> Los Angeles CA 90095-1524
>
>
>
>>> ;
>>> =====================================================================
>>> ;
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>>> ;
>>> =====================================================================
>>> =
>>> ;Reads in sea_level_pressure for a number of station critical to SH
>>> ZW1,
>>> averages into regions of interest and into monthly means, then writes
>>> to file
>
>
>>> begin
>
>>> yrstrt = 1994
>>> yrend = 1999
>
>>> ;***********************************************
>>> ;read in data
>>> ;*************************************************
>
>>> FIL0 = "zw1_station_slp.nc"
>>> diri0 = "/whobbs/ptmp/"
>
>>> f0 = addfile(diri0+FIL0, "r")
>
>>> x = f0->pressure
>>> jul = f0->T
>
>>> p = short2flt(x(T|:,IWMO|:))
>>> delete(x)
>
>>> ;*******************************************
>>> ;Create new p-data array
>>> ;*********************************************
>
>>> dimz = dimsizes(p)
>>> days = dimz(0)
>
>>> stat = new((/3, days/), float)
>
>>> delete(dimz)
>
>>> ;first dim of stat is area of interest
>
>>> ;0 = subpolar trough (stns 619970, 619980, 689940)
>>> ;1 = subtrop trough (stns 919580, 939940, 939970, 919600)
>>> ;2 = subtrop ridge (stns 689020, 689060)
>
>>> ;get regional statn averages
>
>>> stat(0,:) = dim_avg(p(:,0:2))
>>> stat(1,:) = dim_avg(p(:,3:6))
>>> stat(2,:) = dim_avg(p(:,7:8))
>
>>> delete(p)
>
>>> ;assign attributes to time dimension
>
>>> stat!0 = "region"
>>> stat!1 = "time"
>
>>> stat&time = jul
>
>>> ;create new time array in format mmyyyy
>
>>> nmon = 12*(yrend-yrstrt+1) ; total number of months in data
>>> X = new((/nmon/), string) ; new time coord array
>
>>> mn = 0
>
>>> do yy = yrstrt, yrend ; concatenate month and years
>>> do mm = 1,12
>>> X(mn) = mm+""+yy
>>> mn = mn+1
>>> end do
>>> end do
>
>>> time = stringtofloat(X) ; need to convert for use as netCDF
>>> dimension
>
>>> delete(X)
>
>>> ;*******************************************
>>> ;convert daily data to monthly means
>>> ;*********************************************
>
>
>>> slp_mn = new((/3,nmon/), float)
>
>>> monlength = (/31,28,31,30,31,30,31,31,30,31,30,31/)
>
>>> mn = 0
>
>>> do yy = yrstrt, yrend
>>> do mm = 1, 12
>
>>> STR = doubletoint(greg2jul(yy, mm, 1, 0)) ; determine julian
>>> start
>>> day of month
>>> ll = monlength(mm-1)
>
>>> if (mm.eq.2) then ; for Feb in
>>> leapyear.....
>>> if (isleapyear(yy).eq."True") then
>>> ll = 29 ;....use ll = 29
>>> days...
>>> end if
>>> end if
>
>>> FIN = doubletoint(STR+ll)
>
>>> slp_mn(:,mn) = dim_avg(stat(:, {STR:FIN}))
>
>>> mn = mn+1
>>> end do
>>> end do
>
>>> printVarSummary(slp_mn)
>
>>> ; print(slp_mn(:,0:3))
>
>>> delete(stat)
>>> delete(jul)
>>> delete(monlength)
>
>>> ;****************************************************
>>> ;Write data to file
>>> ;****************************************************
>
>>> diri1 = "/u/whobbs/fourier/data/"
>>> FIL1 = "zw1_stn_slp.nc"
>
>>> system("rm "+diri1+FIL1)
>
>>> fout = addfile(diri1+FIL1, "c")
>
>>> ;create file attributes
>
>>> fAtt = True ; assign file attributes
>>> fAtt_at_title = "Station slp for SH ZW1-critical regions"
>>> fAtt_at_source_file = "station_daytomonth.ncl"
>>> fAtt_at_Conventions = "Region 0=subpolar_trough, 1=subtrop_trough,
>>> 2=subtrop_ridge"
>>> fAtt_at_creation_date = systemfunc ("date")
>
>>> fileattdef( fout, fAtt )
>
>>> ;set dimensions
>
>>> dimNames = (/"region","time"/)
>>> dimSizes = (/3, -1/)
>>> dimUnlim = (/False,True/)
>>> filedimdef(fout,dimNames,dimSizes,dimUnlim)
>
>>> ;set file variables
>
>>> region = fspan(1,3,1)
>
>>> filevardef(fout, "time" ,typeof(time),"time")
>>> filevardef(fout, "region", typeof(region), "region")
>>> filevardef(fout, "slp_mn", typeof(slp_mn), (/"region","time"/))
>
>>> ;set other attributes
>
>>> time_at_long_name = "month+year"
>>> slp_mn_at_long_name = "Monthly mean slp for each ZW1-critical region"
>>> slp_mn_at_units = "hPa"
>>> region_at_long_name = "ZW1-critical region"
>
>>> filevarattdef(fout,"slp_mn", slp_mn)
>>> filevarattdef(fout,"time",slp_mn&time)
>>> filevarattdef(fout,"region",slp_mn&region)
>
>
>>> ;populate file
>
>>> fout->time = (/time/)
>>> fout->region = (/region/)
>>> fout->slp_mn = (/slp_mn/)
>
>>> end
>
>
>
>>> _______________________________________________
>>> 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
>
> --
> Saji N. Hameed,
> Assistant Researcher, Tel:808 9569534
> International Pacific Research Center, email:saji_at_hawaii.edu
> 2525 Correa Rd, Honolulu, HI 96822 http://iprc.soest.hawaii.edu/~saji
> _______________________________________________
> 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

Received on Mon May 08 2006 - 15:42:47 MDT

This archive was generated by hypermail 2.2.0 : Tue May 09 2006 - 09:13:11 MDT