problem with filedimdef function

From: donna Cote <d-cote_at_nyahnyahspammersnyahnyah>
Date: Wed Mar 30 2011 - 23:12:06 MDT

I cannot figure out what is the matter with my filedimdef statement.
Anyone see something I've done wrong in my code? I thank you all in
advance! -Donna

A snippet of my code is this:
    ...
  fout = addfile(diro+WRITEFN,"c"
  setfileoption(fout,"DefineMode",True)
   ndims = 3
   string_FillValue = "-99999"
   integer_FillValue = -99999
   logical_FillValue = 0

   dimNames = new(ndims,"string",string_FillValue)
   dimSizes = new(ndims,"integer",integer_FillValue)
   dimUnlim = new(ndims,"logical",logical_FillValue)

   dimUnlim = False ; none of the dimensions will be unlimited
; dimUnlim(0) = True

   dimNames(0) = "recordcount"
   dimNames(1) = "ST_TIME_StrLen"
   dimNames(2) = "ST_ID_StrLen"

   dimSizes(0) = ndata
   dimSizes(1) = ST_TIME_StrLen
   dimSizes(2) = ST_ID_StrLen

   filedimdef(fout,dimNames,dimSizes,dimUnlim)
     ...

The error simply says:
fatal:Execute: Error occurred at or near line 221 in file create-nc-adcp.ncl
(where line 221 is the filedimdef line)

Here is my sample input:
cat /tmp/testcase-adcp.txt
423616 2010-06-01T00:06:00 27.55 -92.49 51.0 249 15.5 1 3 333393330
423616 2010-06-01T00:06:00 27.55 -92.49 67.0 270 10.0 2 3 333393330
423616 2010-06-01T00:06:00 27.55 -92.49 83.0 204 4.9 3 3 333393330
423616 2010-06-01T00:06:00 27.55 -92.49 99.0 261 9.6 4 3 333393330
423616 2010-06-01T00:06:00 27.55 -92.49 115.0 213 8.3 5 3 333393330
423616 2010-06-01T00:06:00 27.55 -92.49 131.0 249 9.7 6 3 333393330
423616 2010-06-01T00:06:00 27.55 -92.49 147.0 279 10.1 7 3 333393330
423616 2010-06-27T18:16:00 27.55 -92.49 771.0 300 4.0 46 3 333393333
423616 2010-06-27T18:16:00 27.55 -92.49 787.0 333 1.1 47 3 333393333
423616 2010-06-27T18:16:00 27.55 -92.49 803.0 45 2.1 48 3 333393333

and my code:
$ cat create-nc-adcp.ncl
;###
;### Read ADCP data from ascii file and write to NetCDF classic (netcdf
3.6)
;### G. Creager, based on work from Jonathon Vigh (NCAR/MMM) with input
and suggestions
;### from David Allured, NCAR/CISL/NCL
;### 20110327 Requires: buoyID (5 digit string), Month (1 or 2
digit), Year (4-digit)
;###
;### D. Cote, modified input suggestions for a pre-processed column
style data input
;### file which also supplies the buoyID (6 digit string)
;### 20110330
;###
;### This script assumes it's being executed locally to the .txt files.
;### Modify it if that's not the case. It also ONLY processes one .txt
;### file to one similar .nc file at a time.
;###
;### No care has been taken to try to get these files into CF compliance
yet.
;####
;
;
;********************************************************
; read_space_delimited.ncl
;********************************************************
;
; The purpose of this program is to read a space-delimited
; ASCII file and convert it to netcdf.
;
; The read portion of this code is based on our modified version of
; the ascii_delim.ncl script available on the NCL
; site at http://www.ncl.ucar.edu/Applications/ascii_delim.shtml
;
; I have changed the output method to match the standard predefinition
of netCDF.
;
; Author: Jonathan Vigh
; Date: 03/25/2011
;********************************************************
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

;============================================================
; Main code
;============================================================
begin

   READFN = "testcase-adcp.txt"
   WRITEFN = "adcp-testcase.nc" ; netCDF file to write.

;********************************
; get data from file
;********************************
; dlc, pre-processing has removed comment lines and coordinates lines
;
; raw_data = asciiread(input_filename,-1,"string")
; nlines = dimsizes(raw_data) ; Number of rows
; mask_header = (str_get_cols (raw_data, 0, 1) .eq. "#") .or.
(str_get_cols (raw_data, 0, 1) .eq. "#")
; mask_coords = (str_get_cols (raw_data, 24, 31) .eq. "LATITUDE")
;; mask_data = .not. (mask_header .or. mask_coords)
; masked_data = .not. (mask_header) .and. .not. (mask_coords)
; data = raw_data( ind(masked_data) )

;********************************
; get data from file, this creates an array of strings called data
; each line in the input file is stored as a string in the array
;********************************
   data = asciiread("/tmp/"+READFN,-1,"string")
   ndata = dimsizes(data)

;************************************************
; Now extract the fields from the delimited file
; and store it into arrays as they will be represented
; in your netcdf output file
;************************************************
   delim = " "
; dlc, columns time, lon, lat will be coordinates
; dlc, columns depth, dir, speed, status, flags, bin will be data arrays
(4D)
; dlc, stationid will be a simple (1D) variable (more may be included)

;************************************************
; first we define some 'reading' variables with whatever
; fill value is used in the ASCII file
;************************************************

;stationid timestampstring latitude longitude depth dir speed bin
profilestatus recordflags
   ST_ID = new(ndata,"string","")
   ST_TIME = new(ndata,"string","")
   ST_STATUS = new(ndata,"integer",-9999)
   ST_LAT = new(ndata,"float",-9999.0)
   ST_LON = new(ndata,"float",-9999.0)
   Bin = new(ndata,"integer",-9999)
   Depth = new(ndata,"float",-9999.0)
   Dir = new(ndata,"integer",0)
   Speed = new(ndata,"float",-9999.0)
   ST_Flags = new(ndata,"integer",-9999)

   Depth@_FillValue = -9999.0
   Dir@_FillValue = 0
   Speed@_FillValue = -9999.0

;************************************************
; read in each variable
;************************************************
; arranged column numbers for column-formatted pre-processed file -dlc
   ST_ID = str_get_cols(data,0,5)
   ST_TIME = str_get_cols(data,7,25)
   ST_LAT = stringtofloat(str_left_strip(str_get_cols(data,27,31)))
   ST_LON = stringtofloat(str_left_strip(str_get_cols(data,33,38)))
   Depth = stringtofloat(str_left_strip(str_get_cols(data,40,44)))
   Dir = stringtointeger(str_left_strip(str_get_cols(data,46,48)))
   Speed = stringtofloat(str_left_strip(str_get_cols(data,50,53)))
   Bin = stringtointeger(str_left_strip(str_get_cols(data,55,56)))
   ST_STATUS = stringtointeger(str_get_cols(data,58,58))
   ST_Flags = stringtointeger(str_get_cols(data,60,68))

;===================================================================
; Finished with reading data from the input file
; have 1D arrays for these variables -dlc
;===================================================================

   Depth@_FillValue = -9999.0
   Dir@_FillValue = 0
   Speed@_FillValue = -9999.0

   Depth@units = "meter"
   Speed@units = "cm/sec"
   Dir@units = "degree_T"
   ST_Flags@units = "Flags"
   ST_STATUS@units = "PROFILE STATUS"
   Bin@units = "obs counter"
; ST_TIME@units = ""

; timecharacters=stringtochar(ST_ID(0))
; 2010-06-01T00:06:00
; 0123456789012345678
; yr=timecharacters(0:3)
; mo=timecharacters(5:6)
; dy=timecharacters(8:9)
; hr=timecharacters(11:12)
; mn=timecharacters(14:15)
; timeUnits = "minutes since "+yr+"-"+mo+"-"+dy+" "+hr+":"+mn+":00"
; datatime = ut_inv_calendar( yearint, monint, dayint, hrint, minint,
00, timeUnits, 0 )
; datatime@units = timeUnits

   StationID=stringtochar(ST_ID)
; StationID!0="iddim0"
; StationID!1="StationID_StrLen"
   Time=stringtochar(ST_TIME)
; Time!0="timedim0"
; Time!1="Time_StrLen"

; ST_TIME_StrLen=(dimsizes(StationID(0,:)) + 1) ; add 1 to account for
the terminator character
;print("StrLen for StationID is "+(dimsizes(StationID(0,:))+1))
; ST_ID_StrLen=(dimsizes(Time(0,:)) + 1) ; add 1 to account for
the terminator character
   ST_TIME_StrLen=max(strlen(ST_TIME)) + 1 ; add 1 to account for the
terminator character
   ST_ID_StrLen=max(strlen(ST_ID)) + 1 ; add 1 to account for the
terminator character

;===================================================================
; Define output file
;===================================================================
   diro = "/tmp/"
   if(isfilepresent(diro+WRITEFN))
     system("/bin/rm -f " + diro + WRITEFN) ; remove any pre-existing file
   end if
   fout = addfile(diro+WRITEFN,"c")

;===================================================================
;Explicitly declare file definition mode (improves efficiency)
;===================================================================
   setfileoption(fout,"DefineMode",True) ; Enter predefine phase.

;===================================================================
; create global attributes of the file
;===================================================================

   fAtt = True

   fAtt@Description = "Gulf of Mexico moored ADCP data
                                   No care has been taken to try to get
this file into CF compliance yet."
   fAtt@source_file = READFN
   fAtt@DateProcessed = systemfunc("date")

   fileattdef(fout,fAtt)

; make time an UNLIMITED dimension ; recommended for most applications
; filedimdef(fout,"time",-1,True)
;===================================================================
; Predefine the coordinate variables and their dimensionality
;
; Note: to get an UNLIMITED record dimension, we set the dimensionality
; to -1 (or the actual size) and set the dimension name to True.
;===================================================================
; dimNames = (/"time", "lat", "lon", "lev"/)
; dimSizes = (/ -1 , nlat, nlon, nlev /)
; dimUnlim = (/ True , False, False, False/)
; filedimdef(fout,dimNames,dimSizes,dimUnlim)

;; dimNames = (/"recordcount"/)
;; dimSizes = (/ ndata /)
;; dimUnlim = (/ False /)

; We also need to write a dimension name for every character
; variable, which will indicate the maximum string length for that
; variable.

   ndims = 3
   string_FillValue = "-99999"
   integer_FillValue = -99999
   logical_FillValue = 0

   dimNames = new(ndims,"string",string_FillValue)
   dimSizes = new(ndims,"integer",integer_FillValue)
   dimUnlim = new(ndims,"logical",logical_FillValue)

   dimUnlim = False ; none of the dimensions will be unlimited
; dimUnlim(0) = True

   dimNames(0) = "recordcount"
   dimNames(1) = "ST_TIME_StrLen"
   dimNames(2) = "ST_ID_StrLen"

   dimSizes(0) = ndata
   dimSizes(1) = ST_TIME_StrLen ; added 1 to account for the
terminator character
   dimSizes(2) = ST_ID_StrLen ; added 1 to account for the
terminator character

   filedimdef(fout,dimNames,dimSizes,dimUnlim)

;===================================================================
; Predefine the the dimensionality of the variables to be written out
;===================================================================
; Here we are using NCL functions to facilitate defining
; each variable's dimension name(s) and type.
; The following could be replaced with explicit, user defined dimension
; names different from those associated with the variable in memory.
; Say, PS(time,lat,lon) in the NCL script. They could be redefined for
the file via:
; filevardef(fout, "PS" ,typeof(PS) ,(/"TIME","latitude","longitude"/))
;===================================================================
; filevardef(fout, "time" ,typeof(time),getvardims(time))
; filevardef(fout, "lev" ,typeof(lev),getvardims(lev) )

; filevardef(fout, "lat" ,typeof(lat),getvardims(lat))

; filevardef(fout, "lon" ,typeof(lon),getvardims(lon))
; filevardef(fout, "T" ,typeof(T) ,getvardims(T))
; filevardef(fout, "PS" ,typeof(PS) ,getvardims(PS))

   filevardef(fout,"depth","float",(/"recordcount"/))
   filevardef(fout,"lat","float",(/"recordcount"/))
   filevardef(fout,"lon","float",(/"recordcount"/))
   filevardef(fout,"dir","integer",(/"recordcount"/))
   filevardef(fout,"speed","float",(/"recordcount"/))
   filevardef(fout,"bin","integer",(/"recordcount"/))
   filevardef(fout,"status","integer",(/"recordcount"/))
   filevardef(fout,"flags","integer",(/"recordcount"/))
 
filevardef(fout,"stationid","character",(/"recordcount","StationID!1+1"/))
   filevardef(fout,"time","character",(/"recordcount","Time!1+1"/))
; filevardef(fout,"stationid","character",(/"recordcount","ST_ID_StrLen"/))
; filevardef(fout,"time","character",(/"recordcount","ST_TIME_StrLen"/))

;===================================================================
; Assign attributes associated with each variable to the file
;
; We want to make sure that the _FillValue's of each variable will be
; preserved in the netCDF file so that the variable can be restored
; to it's original form when it is read from the netCDF file. To do this,
; we use a function called assign_netcdf_FillValue. This function ensures
; that strings and logicals will have a _FillValue within the netCDF file
; consistent with the character and integer types needed to represent those
; variables within the netCDF file.
;====================================================================
; Here is the regular way to do this, variable by variable:
;
; filevarattdef(fout,"T",T) ; copy T attributes
; filevarattdef(fout,"time" ,time) ; copy time
attributes
; filevarattdef(fout,"lev" ,lev) ; copy lev attributes

;===================================================================
; explicitly exit file definition mode. **NOT REQUIRED**
;===================================================================
   setfileoption(fout,"DefineMode",False)

;===================================================================
; Write the data out to the file.
;
; Output only the data values since the dimensionality and such have
; been predefined. Because strings and logical variables need to be
; transformed, we first pass the data through a function called
; transform_to_netcdf_format which does the conversion for us. (It also
; strips off the attributes to workaround an NCL bug that would mess
; up the _FillValue's for strings. For whatever reason, the normal "(/",
"/)"
; syntax isn't sufficient to do this.
;
; The regular way to do this, variable by variable would be:
; fout->time = (/time/)
; fout->lev = (/lev/)
; fout->lat = (/lat/)
; fout->lon = (/lon/)
; fout->T = (/T/)
; fout->Z = (/PS/)
; fout->TOPOG = (/ORO/)
;
; The "(/", "/)" syntax tells NCL to only output the data values to
; the predefined locations on the file.
;====================================================================`
;stationid timestampstring latitude longitude depth dir speed bin
profilestatus recordflags
   fout->depth = (/Depth/)
   fout->dir = (/Dir/)
   fout->speed = (/Speed/)
   fout->flags = (/ST_Flags/)
   fout->time = (/Time/)

   fout->stationid = (/StationID/)
   fout->lon = (/ST_LON/)
   fout->lat = (/ST_LAT/)
   fout->status = (/ST_STATUS/)
   fout->bin = (/Bin/)

;===================================================================
; explicit end of script
;===================================================================
end

; column names
;stationid timestampstring latitude longitude depth dir speed bin
profilestatus recordflags
;sta timestampstring lat lon depth dir cm/s bin
profilestatus recordflags
;$ head -3 /tmp/testadcp.txt
;423616 2010-06-01T00:06:00 27.55 -92.49 51.0 249 15.5 1 3 333393330
;423616 2010-06-01T00:06:00 27.55 -92.49 67.0 270 10.0 2 3 333393330
;423616 2010-06-01T00:06:00 27.55 -92.49 83.0 204 4.9 3 3 333393330
;$ tail -3 /tmp/testadcp.txt
;423616 2010-06-27T18:16:00 27.55 -92.49 771.0 300 4.0 46 3 333393333
;423616 2010-06-27T18:16:00 27.55 -92.49 787.0 333 1.1 47 3 333393333
;423616 2010-06-27T18:16:00 27.55 -92.49 803.0 45 2.1 48 3 333393333
;012345678901234567890123456789012345678901234567890123456789012345678
; 1 2 3 4 5 6
;

; a = (/ (/1.1,1.2,1.3/), (/2.1,2.2,2.3/), (/3.1,3.2,3.3/) /)
; a!0 = "test0"
; a!1 = "test1"
; a@units = "Degrees"
; a@long_name = "A"

$

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Mar 30 23:12:25 2011

This archive was generated by hypermail 2.1.8 : Tue Apr 05 2011 - 09:01:21 MDT