Hi Wei,
Your second script is excellent, Thanks very much!
Two more questions related to the script and data file:
(1) if there is several extra rows (following) without any data, just
accidentally added into the data fileļ¼ so the script would run with error (see
the attached example). Are there any trick to avoid this error, probably by
judging, then stopping pick-up of data?
(2) if I want one variable's value (e.g. eee1) always equal another variable's
value (e.g. aaa1, ignoring the original value of "eee1") (the order always
change, one judge which variables just by its name), can you give any
suggestion? (i.e. how to identify order by variable's name)
value name1 name2
20.0 aaa1
80.7 bbb1 bbb2
45.5 ccc1 ccc2
60.3 ddd1
100.6 eee1
Mark
----- Original Message ----
From: Wei Huang <huangwei_at_ucar.edu>
To: Mark Chan <cym263_at_yahoo.com>
Cc: "ncl-talk_at_ucar.edu forum" <ncl-talk_at_ucar.edu>
Sent: Sun, March 20, 2011 11:48:44 AM
Subject: Re: string plus string
Mark,
You can change your script to:
x = asciiread("mydata.dat",-1,"string")
data = stringtofloat(str_get_field(x(1:),1," "))
nx = dimsizes(x) - 1
ss = new(nx, "string", "No_FillValue")
do n=0,nx-1
nf = str_fields_count(x(n+1), " ")
if (nf.eq.2) then
aa1 = str_split(x(n+1)," ")
ss(n) = aa1(1)
else
aa2 = str_split(x(n+1)," ")
ss(n) = aa2(1)+" "+aa2(2)
end if
end do
print(ss)
print(data)
Here is the output:
Copyright (C) 1995-2011 - All Rights Reserved
University Corporation for Atmospheric Research
NCAR Command Language Version 6.0.0
The use of this software is governed by a License Agreement.
See http://www.ncl.ucar.edu/ for more details.
Variable: ss
Type: string
Total Size: 20 bytes
5 values
Number of Dimensions: 1
Dimensions and sizes: [5]
Coordinates:
(0) aaa1
(1) bbb1 bbb2
(2) ccc1 ccc2
(3) ddd1
(4) eee1
Variable: data
Type: float
Total Size: 20 bytes
5 values
Number of Dimensions: 1
Dimensions and sizes: [5]
Coordinates:
(0) 20
(1) 80.7
(2) 45.5
(3) 60.3
(4) 100.6
Or use this script:
;read the data
x = asciiread("mydata.dat",-1,"string")
;skip the header
y = x(1:)
;get number of lines
ny = dimsizes(y)
;define a float and string array
vals = new(ny, float, "No_FillValue")
strs = new(ny, string, "No_FillValue")
do n=0,ny - 1
;split each string
tmpstr = str_split(y(n), " ")
;convert first column to float
vals(n) = tofloat(tmpstr(0))
;join the rest to a new string
strs(n) = str_join(tmpstr(1:), " ")
;delete tmpsr for reuse
delete(tmpstr)
end do
print(vals)
print(strs)
Here is the output from the above:
Copyright (C) 1995-2011 - All Rights Reserved
University Corporation for Atmospheric Research
NCAR Command Language Version 6.0.0
The use of this software is governed by a License Agreement.
See http://www.ncl.ucar.edu/ for more details.
Variable: vals
Type: float
Total Size: 20 bytes
5 values
Number of Dimensions: 1
Dimensions and sizes: [5]
Coordinates:
(0) 20
(1) 80.7
(2) 45.5
(3) 60.3
(4) 100.6
Variable: strs
Type: string
Total Size: 20 bytes
5 values
Number of Dimensions: 1
Dimensions and sizes: [5]
Coordinates:
(0) aaa1
(1) bbb1 bbb2
(2) ccc1 ccc2
(3) ddd1
(4) eee1
Wei Huang
huangwei_at_ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924
On Mar 19, 2011, at 10:19 PM, Mark Chan wrote:
> Dear Dennis,
>
> Thank you very much! Actually my data is exactly like this (also see the
> attached files)
>
> value name1 name2
> 20.0 aaa1
> 80.7 bbb1 bbb2
> 45.5 ccc1 ccc2
> 60.3 ddd1
> 100.6 eee1
>
> I modify your script as below:
>
> begin
> x = asciiread("mydata.dat",-1,"string")
> data = stringtofloat(str_get_field(x(1:),1," "))
> nx = dimsizes(x)
> ss = new(nx, "string", "No_FillValue")
> do n=0,nx-1
> nf = str_fields_count(x(n), " ")
> if (nf.eq.2) then
> aa1 = str_split(x(n)," ")
> ss(n) = aa1(1)
> else
> aa2 = str_split(x(n)," ")
> ss(n) = aa2(1)+" "+aa2(2)
> end if
> end do
> print(ss)
> print(data)
> end
>
>
> My confusion is as:
>
> (1) I want to skip the first row but "ss"always keep "name1" as its values
> (2) dimsizes(x)=dimsizes(data)+1, so "x" didn't skip the first row. why is
>this?
> (3) It seems the first value (20.0) was given to "name1" instead of "aaa1",
> which actually is wrong, since I would like the "ss(0)=aaa1" to correspond to
> "data(0)=20"
>
> I may confuse you here, since I was totally confused.
> Thanks again for further suggestion.
>
> Mark
>
>
>
>
> ----- Original Message ----
> From: Dennis Shea <shea_at_ucar.edu>
> To: Mark Chan <cym263_at_yahoo.com>
> Cc: ncl-talk_at_ucar.edu
> Sent: Sat, March 19, 2011 10:29:03 AM
> Subject: Re: string plus string
>
> x = asciiread("....", -1, "string")
>
> nx = dimsizes(x)
> do n=0,nx-1
> nf = str_fields_count(x(n), " ")
> if (nf.eq.1) then
> ss = str_split(x(n)," ")
> else
> aa = str_split(x(n)," ")
> ss = aa(0)+" "+aa(1)
> end if
> print(""+ss)
> end do
>
> or
>
> ss = new(nx, "string", "No_FillValue")
> do n=0,nx-1
> nf = str_fields_count(x(n), " ")
> if (nf.eq.1) then
> ss(n) = str_split(x(n)," ")
> else
> aa = str_split(x(n)," ")
> ss(n) = aa(0)+" "+aa(1)
> end if
> end do
>
> print(""+ss)
>
>
> On 3/18/11 4:59 PM, Mark Chan wrote:
>> NCL users,
>>
>>
>> If "x" is a string array like this:
>>
>> aaa1 aaa2
>> bbb1
>> ccc1 ccc2
>> ddd1
>> eee1
>>
>> I want to make a NCL script as:
>>
>> s1 = str_get_field(x(1:),1," ")
>> s2 = str_get_field(x(1:),2," ")
>> if (s2.ne."null") then ;------------------how to judge "s2" here
>> ss =s1+" "+s2
>> else
>> ss=s1
>> end if
>>
>> The aim is plus "s1" and "s2" with a space between only if "s2" has value.
>> Otherwise the string "ss" would have extra space (e.g. ss=s1+" "), which
>> damage
>> the variable name.
>> Can any one help!
>>
>> Thanks very much.
>>
>> Mark
>>
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
>
> <mydata.dat><My_script.ncl>_______________________________________________
> 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
This archive was generated by hypermail 2.1.8 : Mon Mar 28 2011 - 08:51:50 MDT