[ncl-talk] NaN string in array
Adam Phillips
asphilli at ucar.edu
Tue May 2 09:56:44 MDT 2017
Hi Xiao,
I would suggest reading in the text file as a string using asciiread, then
switch the NaN values to 0, then convert to float. Note that there are
numerous string functions in NCL (many of them start with "str").
An example:
; a = asciiread("file.txt",(/-1/),"string")
a = "-0.1 20 -999999 NaN 10.4 NaN" ; set up test data
b = str_sub_str(a,"NaN","0") ; convert NaN to 0's
c = str_split(b," ") ; split all values into individual strings
d = tofloat(c)
print(d)
Variable: d
Type: float
Total Size: 24 bytes
6 values
Number of Dimensions: 1
Dimensions and sizes: [6]
Coordinates:
(0) -0.1
(1) 20
(2) -999999
(3) 0
(4) 10.4
(5) 0
The above call to str_split assumes that your values are separated by
spaces, and thus you might need to change this if your values are separated
by commas or something else.
Hope that helps! If not, or if you have further questions please post to
the ncl-talk email list.
Adam
On Tue, May 2, 2017 at 9:00 AM, Zhang, Xiao <xiao.zhang at pnnl.gov> wrote:
> Hello,
>
>
>
> I have a big text input file, most values are numbers but it has string
> “NaN” in it. After reading in I tried to replace it with 0 using: (where
> a.eq.”NaN”,0,a), but it didn’t work. Anyone knows how to fix it? Thank you!
>
>
>
> Best,
>
> Xiao
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
--
Adam Phillips
Associate Scientist, Climate and Global Dynamics Laboratory, NCAR
www.cgd.ucar.edu/staff/asphilli/ 303-497-1726
<http://www.cgd.ucar.edu/staff/asphilli>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170502/144305fd/attachment.html
More information about the ncl-talk
mailing list