Re: strings questions

From: David Ian Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Wed, 20 Sep 2006 16:43:40 -0600

On Sep 20, 2006, at 2:52 PM, Adam Phillips wrote:

> Hi Kate,
>
> I think I have answers to your first 2 questions:
>
> 1) Try adding the the following code to your .nc write out section:
>
> spherical!0 = "spherical"
> spherical&spherical = spherical
>
> That should result in the .nc file looking like this:
>
> variables:
> char spherical(spherical) ;
> spherical:long_name = "Grid type logical switch"
>

I think Kate's first question is about creating a NetCDF scalar
variable, which you can do
one of two ways -- both of which are documented (perhaps too obscurely)
in the example
section of the 'filevardef'. Either way you use the special pre-defined
dimension name "ncl_scalar".

Here is one way:

spherical = new(1,"character")
spherical!0 = "ncl_scalar"
spherical = integertocharacter(1) ; 1 for True -- 0 for False
spherical_at_long_name = "Grid type logical switch"
ncid->spherical = spherical

The other way is to use 'filevardef' to create the scalar variable
directly in the file.

filevardef(ncid, "spherical","character","ncl_scalar")
ncld->spherical = integertocharacter(1)
ncid->spherical_at_long_name = "Grid type logical switch"

Note that if you look at this file in NCL it will appear as if there is
a dimension named "ncl_scalar",
but in an ncdump of the file the dimension will not appear, so the
variable *is* a true NetCDF scalar.

> 2) The NCL developers can correct me if I wrong, but I do believe that
> the netCDF3 conventions do not allow any other dimension other than
> time to be unlimited.

NetCDF3 doesn't care what the unlimited dimension is called or what it
represents. The limitations for
unlimited dimensions are (1) that there can only be one of them and (2)
that it is the leftmost (slowest changing)
dimension for every variable that uses it.

The error you are getting has nothing to do with the unlimited
dimension. The line it is complaining
about is the assignment to "str", which is a result of NCL's fussy
assignment constraints. "str" has been defined
to be 80 characters but the character array assigned is shorter than 80
characters.
One way around this would be to modify the code as follows:

filedimdef(ncid, "bath", -1, True)
filedimdef(ncid, "stringlen", 80, False)
dims = (/ "bath", "stringlen" /)
filevardef(ncid, "bath_history", "character", dims)
str = "Flat bottom"
len = dimsizes(stringtochar(str))
ncid->bath_history(0,0:len-1) = stringtochar(str)
fileAtt = True
fileAtt_at_bath_history_0 = "Flat bottom"
fileattdef(ncid, fileAtt)

Note: the new release of NCL has a strlen function which will simplify
the above code to this:

str = "Flat bottom"
ncid->bath_history(0,0:strlen(str)) = stringtochar(str)

You will notice another small wrinkle: the variable 'len' includes the
Null terminating character of the string,
whereas strlen does not.

>
> Good luck,
> Adam

What is your application for the inverse map projection? You are
correct that there is no interface to a grid based
inverse map projection in NCL although you can do it point by point.
  -dave
>
> Kate Hedstrom wrote:
>> I have an NCL program to convert a grid created by gridgen
>> (http://www.marine.csiro.au/~sakov/) into a format that ROMS can use.
>> There are a few nagging things about string handling that I'm not
>> 100% happy with. First of all, we want to have:
>> char spherical ;
>> spherical:long_name = "Grid type logical switch" ;
>> and instead I have:
>> ncl10 = 2 ;
>> char spherical(ncl10) ;
>> spherical:long_name = "Grid type logical switch" ;
>> Is there any way to have a scalar character?
>> Second, I was hoping to have a string (or character array) that is
>> dimensioned (/ bath, stringlen /) with bath being an unlimited
>> dimension. I'm using the unlimited dimension to contain working
>> bathymetries and would like a description of each one. Here is what
>> did and didn't work:
>> filedimdef(ncid, "bath", -1, True)
>> ; delete(dims)
>> ; filedimdef(ncid, "stringlen", 80, False)
>> ; dims = (/ "bath", "stringlen" /)
>> ; filevardef(ncid, "bath_history", "character", dims)
>> ; str = new((/ 80 /), "character")
>> ; str = stringtochar("Flat bottom")
>> ; ncid->bath_history(0,:) = str
>> fileAtt = True
>> fileAtt_at_bath_history_0 = "Flat bottom"
>> fileattdef(ncid, fileAtt)
>> The commented out code complained about a mismatch in array lengths.
>> Third, the next step is an inverse map projection. NCAR graphics
>> provides access to mapinv but I'm not finding anything similar in
>> NCL. I was hoping to avoid using WRAPIT and requiring extra packages
>> if at all possible, plus I want the double precision of NCL.
>> Thanks!
>> Kate
>
> --
> --------------------------------------------------------------
> Adam Phillips asphilli_at_ucar.edu
> National Center for Atmospheric Research tel: (303) 497-1726
> ESSL/CGD/CAS fax: (303) 497-1333
> P.O. Box 3000
> Boulder, CO 80307-3000 http://www.cgd.ucar.edu/cas/asphilli
> _______________________________________________
> 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 Wed Sep 20 2006 - 16:43:40 MDT

This archive was generated by hypermail 2.2.0 : Mon Sep 25 2006 - 11:45:07 MDT