Re: problem with yyyyddd_to_yyyymmdd

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Mon Jan 13 2014 - 11:49:24 MST

Wenshan,

I would look at the various conversion functions found at:

http://www.ncl.ucar.edu/Document/Functions/type_convert.shtml

The “toxxxx” functions are the ones I would look at. However, in any programming language, you have to be careful about going from a float/double to an integer because of truncation issues.

Going from a string to a numeric value is also not always well defined.

I’m not sure why you needed to convert a float to an integer and then to a string and then back to an integer again.

It’s best to stay with numeric calculations if possible, and keep strings out of there. “sprinti” and “sprintf” should only be used if you need to “pretty print” integers and floats for use in labels, or maybe for writing output ASCII files.

To see some sample scripts that do conversions, go to our “tips” page:

http://www.ncl.ucar.edu/Applications/concepts_list.shtml

and use the browser search window to search for “convert”. You will see several topics on this kind of subject, along with a list of scripts that do some kind of conversion.

—Mary

On Jan 13, 2014, at 11:06 AM, Wenshan Wang <wenshanw@uci.edu> wrote:

> Got it! Thank you very much!
>
> Can I ask one more question?
> Is there a way to find out problems like this "add a float[ dd(j,1) ] and a string and then convert the result to an integer" one?
> Since the output looks like what I want and there is no error message about this.
> I guess I used the bash script idea here.
>
> Thank you very much! I really appreciate it!
>
> Best,
> Wenshan
> ------------------------------------------------------------------------------
> Graduate Student Researcher
> Earth System Science
> University of California, Irvine
>
>
> On Mon, Jan 13, 2014 at 9:52 AM, Dennis Shea <shea@ucar.edu> wrote:
> Upon further examination .... Your statement below is wrong
>
> sprinti("%0.3i",toint(dd(j,2))) return a values of type string!!!
>
> print(""+yyyyddd_to_yyyymmdd(tointeger(dd(j,1)+sprinti("%0.3i",toint(dd(j,2))))))
> ;tointeger(float + string)
>
> You can not add a float[ dd(j,1) ] and a string and then convert the
> result to an integer. Well, you can BUT the answer is not what you
> expect. Further, the _FillValue associated with this bogus result is 9.
>
> This bogus _FillValue was propagated thru some internal calculations and ultimately caused an error on an internal to 'yyyyddd_to_yyyymmdd'
> 'if' statement.
>
>
>
> print(""+yyyyddd_to_yyyymmdd(tointeger(dd(j,1))*100 + toint(dd(j,2)))
> ;---------- one integer --------------
>
> Also, far more efficient to use arrays
>
> YYYYMMDD = yyyyddd_to_yyyymmdd(toint(dd(:,1))*100 + toint(dd(:,2)) )
>
>
> On 1/11/14, 10:34 AM, Dennis Shea wrote:
> Sorry for the delay.
>
> The following works
>
> fn = "WenshanWang.test.txt"
> ncol = numAsciiCol(fn)
> opt = 10
>
> dd = readAsciiTable(fn, ncol, "float", opt)
> yyyymmdd = yyyyddd_to_yyyymmdd(toint(dd(:,1))*100 + toint(dd(:,2)) )
> print(yyyymmdd)
>
> Processing the each row (line) one at a time like you do fails.
> Not sure why. A bug report will be filed.
>
> Good luck
>
>
>
>
> On 1/10/14, 10:00 PM, Wenshan Wang wrote:
> Sorry to bother but It's so unusual to receive no reply in a week.
>
> On Fri, Jan 3, 2014 at 5:17 PM, Wenshan Wang <wenshanw@uci.edu> wrote:
>
> Dear all,
>
> Happy New Year!
>
> I had a problem using the function 'yyyyddd_to_yyyymmdd'.
> I read a ascii file of radiation data.
> For every year, it works fine from Jan 1st to 8th, but fails on 9th:
> ==============================================================
> fatal:The result of the conditional expression yields a missing
> value. NCL
> can not determine branch, see ismissing function
>
> fatal:["Execute.c":8128]:Execute: Error occurred at or near line 4670 in
> file $NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl
>
> fatal:["Execute.c":8128]:Execute: Error occurred at or near line 30 in
> file yyyyddd_to_yyyymmdd_check.ncl
> ===============================================================
>
> The ascii files look normal. They are downloaded from GC-NET.
> I print all the inputs. They look fine too.
> I tried to debug contributed.ncl. There are no missing values in the
> conditional statement, where the script breaks.
> I have workarounds but really want to know why this happens.
>
> The following is my data and script.
> I attached them and put them on to 'ftp.cgd.ucar.edu:/incoming' as well.
> NCL version: 6.1.2
> System: Linux grele 3.5.0-44-generic #67~precise1-Ubuntu SMP Wed Nov 13
> 16:16:57 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
>
> Thank you very much!
>
> DATA (test.txt)
> =================================================
> Data avaiable from 1996 1.0000 to 2013 123.5000
> 1 Station Number A 01 .. 18
> 2 Year B 1995 - 1999
> 3 Julian Decimal Time C 0.0000 - 365.9583
> 4 SW_down [W m-2]
> 5 SW_up [W m-2]
> 6 Net Radiation F [W m-2]
> 7 Albedo
> 8 Zenith Angle [deg]
>
> 1 2011 8.7500 1.20 0.58 -33.12 0.85 95.97
> 1 2011 8.7917 0.20 0.14 -31.44 0.85 99.50
> 1 2011 8.8333 0.40 0.07 -30.40 0.85 103.83
> 1 2011 8.8750 0.20 0.00 -30.72 0.85 108.72
> 1 2011 8.9167 0.20 0.00 -31.84 0.85 113.91
> 1 2011 8.9583 0.20 0.00 -31.84 0.85 119.07
> 1 2011 9.0000 0.20 0.00 -32.80 0.85 123.91
> 1 2011 9.0417 0.40 0.00 -33.04 0.85 128.02
> 1 2011 9.0833 0.20 0.00 -31.12 0.85 131.00
> 1 2011 9.1250 0.20 0.07 -31.60 0.85 132.51
> 1 2011 9.1667 0.20 0.07 -32.48 0.85 132.34
> 1 2011 9.2083 0.20 0.14 -30.88 0.85 130.51
> 1 2011 9.2500 0.20 0.00 -32.24 0.85 127.26
> 1 2011 9.2917 0.20 0.00 -32.96 0.85 122.97
> 1 2011 9.3333 0.20 0.07 -29.36 0.85 118.04
> 1 2011 9.3750 0.20 0.00 -31.60 0.85 112.83
> 1 2011 9.4167 0.20 0.17 -31.36 0.85 107.66
> 1 2011 9.4583 0.20 0.14 -30.08 0.85 102.86
> 1 2011 9.5000 0.00 0.00 -24.08 0.85 98.65
> 1 2011 9.5417 0.00 0.00 -13.52 0.85 95.28
> 1 2011 9.5833 0.40 0.34 -28.24 0.85 92.96
> 1 2011 9.6250 2.00 1.70 -35.76 0.85 91.82
> 1 2011 9.6667 3.20 2.37 -36.96 0.85 91.93
> 1 2011 9.7083 2.20 1.58 -37.60 0.85 93.29
> 1 2011 9.7500 0.80 0.43 -36.72 0.85 95.81
> 1 2011 9.7917 0.40 0.07 -36.40 0.85 99.34
> 1 2011 9.8333 0.20 0.07 -37.20 0.85 103.67
> 1 2011 9.8750 0.20 0.07 -37.12 0.85 108.56
> 1 2011 9.9167 0.40 0.00 -37.12 0.85 113.74
> 1 2011 9.9583 0.40 0.00 -37.20 0.85 118.91
> 1 2011 10.0000 0.20 0.00 -37.52 0.85 123.74
>
> ==================================================
>
>
> SCRIPT (yyyyddd_to_yyyymmdd_check.ncl)
> ==================================================
> ; Complied by Wang Wenshan 2014-01-03 Friday 14:10:49
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> ;*************************************************************
> ; txt --> nc
> ; GC-NET
> ; txt: each line:
> ; 0 1 2 3 4 5 6 7
> ; stn year day SW-down SW-up net albedo zenith
> ; nc: 16stn * 91day * 24 hr, 5 var
> ;************************************************************
> begin
> ; read in txt
> ;------------------------------------------------------------
> fn = "test.txt"
>
> ncol = numAsciiCol(fn)
> opt = 10
>
> dd = readAsciiTable(fn, ncol, "float", opt)
> printVarSummary(dd)
> dim_dd = dimsizes(dd)
> nrow = dim_dd(0)
>
> do j = 0, nrow - 1 ; loop: rows
> print(dd(j,0)+" "+dd(j,1)+" "+dd(j,2)+" "+dd(j,3)+" "+dd(j,4)+"
> "+dd(j,5)+" "+dd(j,6)+" "+dd(j,7))
> print(sprinti("%0.3i",toint(dd(j,2))))
> print(""+dd(j,1)+sprinti("%0.3i",toint(dd(j,2))))
>
> print(""+yyyyddd_to_yyyymmdd(tointeger(dd(j,1)+sprinti("%0.3i",toint(dd(j,2))))))
>
> print("--------------")
> end do
>
> print("done!")
> end
>
>
> Best,
> Wenshan
>
> ------------------------------------------------------------------------------
>
> Graduate Student Researcher
> Earth System Science
> University of California, Irvine
>
>
>
>
> _______________________________________________
> 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 Jan 13 11:49:43 2014

This archive was generated by hypermail 2.1.8 : Sun Jan 19 2014 - 21:56:35 MST