Re: array of strings as an attribute

From: Saji Njarackalazhikam Hameed <saji_at_nyahnyahspammersnyahnyah>
Date: Tue, 13 Nov 2007 09:10:03 +0900

Hi Ozan,

The way I go about this is to convert the array of strings into
a string. For example

(/"a","b","c","d"/) => "a b c d"

and when I need to read back, I convert the string into an array.
I am attaching two functions that provide ways to convert
strings to array and back. In the string_to_array, you can set
an optional "separator" attribute to decide how you want the
strings to be delimited. In the array_to_string, you can
set an option "join" attribute to decide what delimiter to
use while joining the array.

Hope it helps,

saji

function array_to_string(var_arr)
begin
  dims=dimsizes(var_arr)
  rank=dimsizes(dims)
  if rank.gt.1
    print("This function is for 1-D arrays")
    exit
  end if
  var_str=""
  if isatt(var_arr,"join")
    spc=var_arr_at_join
  else
    spc=""
  end if

  do i = 0,dims-2
    var_str=var_str+var_arr(i)+spc
  end do
  var_str=var_str+var_arr(dims-1)
  return(var_str)
end

function string_to_array(str)
begin
  if .not.isatt(str,"separator")
    str_at_separator=","
    cmd="echo "+str+" | tr [:blank:] ',' "
    str=systemfunc(cmd)
  end if
  nstr=systemfunc("echo "+str+" | tr "+str_at_separator+" \\n | wc -l")
  istr=stringtoint(nstr)
  str_arr=new((/istr/),"string")

  do i = 0, istr-1
    CMD="echo "+str+" | awk -F"
    CMD=CMD+str_at_separator+" '{print $"+(i+1)+"}'"
    str_arr(i)=systemfunc(CMD)
  end do
  return(str_arr)
end

* ozan mert gokturk <zanmerto_at_yahoo.com> [2007-11-12 08:22:16 -0800]:

> Dear NCL Community,
>
> I would like to assign an array of strings to a
> variable as an attribute, and then output this
> variable as netCDF. The assignment of the attribute is
> successful, but when I output the variable as netCDF,
> only the first string in the array is written as the
> attribute!
>
> Here is my sample code:
>
> names = (/"a","b","c","d"/)
> x_at_names_of_stations = names
>
> When I print "x_at_names_of_stations" at this stage, it
> is fine. But when I write
>
> fout = addfile("mydata.nc","c")
> fout->data = x
>
> and then read this netCDF file, "mydata.nc", I see
> that only "a" is written as the "names_of_stations"
> attribute of "data".
>
> Do you have any idea to solve this problem?
>
> Thanks in advance
>
> Ozan
>
>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> ncl-talk mailing list
> ncl-talk_at_ucar.edu
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-- 
Saji N. Hameed
APEC Climate Center          				+82 51 668 7470
National Pension Corporation Busan Building 12F         
Yeonsan 2-dong, Yeonje-gu, BUSAN 611705			saji_at_apcc21.net
KOREA
_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Nov 12 2007 - 17:10:03 MST

This archive was generated by hypermail 2.2.0 : Wed Nov 14 2007 - 10:07:00 MST