Re: could not convert date correctly

From: Dave Allured - NOAA Affiliate <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu Dec 27 2012 - 12:50:18 MST

Welcome to the wonderful wide world of roundoff error. If you prefer
to have units of "days since", then change line 1 to use double
precision math:

   time = todouble (ispan(0,24-1,1)) * 1/24.

The problem is that the expression ispan(0,24-1,1) * 1 / 24. is
evaluated with single precision math, so your variable "time" is
single precision floating point, rather than double. There is enough
single precision roundoff error in one part of this calculation
(dividing by 24), to result in non-zero fractional digits when
displayed with NCL print. If you change to double precision, then the
roundoff errors become so small that you will not normally see them.

This form is better because it eliminates the possibility of a subtle
problem in order of evaluation:

   time = todouble (ispan(0,24-1,1)) / 24.

Your other solution for "hours since" will also work correctly,
because that solution produces only whole numbers in the values for
"time". There is no divide by 24, therefore no roundoff error at all.

--Dave

On Thu, Dec 27, 2012 at 11:28 AM, huiping yan <yanhp2009@gmail.com> wrote:
> Hi there,
>
>
>
> I want to generate date for some variables but the results seems not
> correct. And wonder if anyone could help me.
>
> The scripts are:
>
> ;;;;;;;;;;;;;;;;;; generate time;;;;;;;;;;;;;;;;;;;;;;
>
>
>
> time=ispan(0,24-1,1)*1/24.
>
>
>
> time@units="days since 2011-05-01 00:00:0.0"
>
>
>
> time@long_name = "time" ;
>
>
>
> time@calendar = "noleap" ;
>
>
>
> time@bounds = "time_bnds"
>
>
>
> ut=cd_calendar(time,0) ;;; converts to time(size[TIME], 6) => YYYY, MM, DD,
> HH, MM, SS
>
>
>
> print(ut(:,0)+" "+ut(:,1)+" "+ut(:,2)+" "+ut(:,3)+" "+ut(:,4)+" "+ut(:,5))
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
>
>
> Then the output are “
>
>
>
> (0) 2011 5 1 0 0 0
>
>
>
> (1) 2011 5 1 1 0 0.000107288
>
>
>
> (2) 2011 5 1 2 0 0.000214577
>
>
>
> (3) 2011 5 1 3 0 0
>
>
>
> (4) 2011 5 1 4 0 0.000429153
>
>
>
> ……
>
> “
>
> The hours of the time should be 1,2,.. 23, but they could not be generated
> correctly.
>
> However when I changed the script:
>
> time=ispan(0,24-1,1).
>
>
>
> time@units="hours since 2011-05-01 00:00:0.0"
>
>
>
> time@long_name = "time" ;
>
>
>
> time@calendar = "noleap" ;
>
>
>
> time@bounds = "time_bnds"
>
>
>
> ut=cd_calendar(time,0) ;;; converts to time(size[TIME], 6) => YYYY, MM, DD,
> HH, MM, SS;
>
>
>
>
> Then the result is right. It seems weird. Does anyone have ideas?
>
>
> The ncl version I use is 6.0.0.
>
> The system is Linux (x86_64 GNU/Linux).
>
> Thanks,
>
> Huiping
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Dec 27 12:50:33 2012

This archive was generated by hypermail 2.1.8 : Fri Jan 04 2013 - 15:32:29 MST