Re: stringtochar function

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Mon Jul 09 2012 - 20:42:47 MDT

Xin and Mary,

I wrote the following NCL function to remove the extra null character
when converting strings to characters. It also preserves dim names
and coordinates. I have found it quite useful for constructing Netcdf
files with arrays of strings cleanly represented in standard character
variables.

http://www.esrl.noaa.gov/psd/people/dave.allured/data/ncl/lib/stringtochar2.ncl

ncl 0> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
ncl 1> load "stringtochar2.ncl"
ncl 2> s = (/"123","ABC"/)
ncl 3> c = stringtochar2 (s)
ncl 4> print (c)

Variable: c
Type: character
Total Size: 6 bytes
            6 values
Number of Dimensions: 2
Dimensions and sizes: [2] x [len3 | 3]
Coordinates:
(0,0) 1
(0,1) 2
(0,2) 3
(1,0) A
(1,1) B
(1,2) C

If Xin's application is working with arrays of strings, then this may
be the right function. However, my version includes an extra
dimension on the left in the case of a scalar input string, so this is
not exactly what Xin asked for, below. I did it this way for best fit
for generic handling of arrays of strings.

I would also like to request that this function be included for the
NCL function library. Thanks for your consideration.

--Dave A.
NOAA/PSD/CIRES
Boulder, Colorado

On Mon, Jul 9, 2012 at 10:19 AM, Mary Haley <haley@ucar.edu> wrote:
> I think Dave will have to explain why the NULL character is necessary.
>
> If you just want the "1", "2", "3", then you will have to truncate the array yourself:
>
>
> ncl 0> s = "123"
> ncl 1> c = tochar(s)
> ncl 2> print(c)
>
> Variable: c
> Type: character
> Total Size: 4 bytes
> 4 values
> Number of Dimensions: 1
> Dimensions and sizes: [4]
> Coordinates:
> (0) 1
> (1) 2
> (2) 3
> (3) 0x00
> ncl 3> cnew = c(:dimsizes(c)-2)
> ncl 4> print(cnew)
>
>
> Variable: cnew
> Type: character
> Total Size: 3 bytes
> 3 values
> Number of Dimensions: 1
> Dimensions and sizes: [3]
> Coordinates:
> (0) 1
> (1) 2
> (2) 3
> ncl 5>
>
> On Jul 9, 2012, at 10:11 AM, Xin Xi wrote:
>
>> Mary,
>> I am not trying to get the length in this case. I am trying to get the "correct" char array converted from the given string, e.g., s="123", I want get c=(/"1", "2", "3"/) without the NULL char. And I still didnot get it why there should be a NULL in the end. Thanks.
>>
>> On Mon, Jul 9, 2012 at 11:45 AM, Mary Haley <haley@ucar.edu> wrote:
>> The character you're seeing is a NULL character. This will always be appended when you call stringtochar.
>>
>> I believe the reason for this NULL character is for the case where you give it an array of strings. Since each string may be a different length, it becomes
>> necessary to have something that denotes the actual end of each string
>>
>> To correctly get the string length, you need to subtract 1:
>>
>> s = "123456789"
>> c = stringtochar(s)
>> clen = dimsizes(c) -1
>> print("length of c = " + clen)
>>
>>
>> (0) length of c = 9
>>
>>
>> --Mary
>>
>> On Jul 8, 2012, at 11:27 PM, Xin Xi wrote:
>>
>> > Hi there,
>> > when I use the stringtochar function, the resulting char array has an ending element of "0x00". This cause the unmatched dimension size error while I tried to assign the char array to a variable (with a dimension of the string length). I donot understand how the "0x00" relates to the missing value of original string (as detailed on the ncl webpage of the stringtochar function). And how can I avoid it? Can someone clarify for me. Thanks!
>> > Xin
>> > _______________________________________________
>> > ncl-talk mailing list
>> > List instructions, subscriber options, unsubscribe:
>> > http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Jul 9 20:42:56 2012

This archive was generated by hypermail 2.1.8 : Thu Jul 12 2012 - 10:16:50 MDT