Re: if ----thank you very much for your reply

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Sat Nov 05 2011 - 09:57:00 MDT

First, you want to move the asciiwrite command *outside* of the do
loop. What's happening is that you are writing "str" to the same file
for every iteration of the do loop.
asciiwrite affectively removes the file and completely rewrites it
every time.

So, instead of this:

   strs(nt) = vals
   asciiwrite("nlcheng55chuance_test.txt",strs)
end do

you want this:

   strs(nt) = vals
end do
asciiwrite("nlcheng55chuance_test.txt",strs)

Secondly, two of your "if" statements inside the do loop doesn't make
sense:

    if ((hh(nt) .lt. 8) .and. (hh(nt) .gt. 0 ) .and. (hh(nt) .eq.
0 ).and. (dd(nt) .ne. 1)) then

This "if" statement will never be True, because hh can't both be
greater than 0 and equal to 0 at the same time.

Perhaps you meant for the middle ".and." to be a ".or"?

Also, you don't need the "do" loop at all. You can use the "where" or
"ind" functions to clean this code up.

Finally, instead of using sprintf, you should use sprinti for
formatting strings. Use "%0.2i" to force a leading "0" for single-
digit numbers.

Here's what your code would look like with these changes, but you need
to look at the logic inside the "ind" statements, because I'm not sure
what you wanted here.

;************************************************
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;************************************************
begin

fname = "nlcheng44chuance_test.txt"
data = asciiread(fname,(/6336,4/),"integer")

mm = data(:,1)
yy = data(:,0)
mm = data(:,1)
dd = data(:,2)
hh = data(:,3)

hh = where(hh.ge.8,hh-8,hh)

ii = ind((hh .lt. 8 .and. hh .gt. 0).or.(hh .eq. 0 .and. dd .ne. 1))
if(.not.any(ismissing(ii))) then
   hh(ii) = hh(ii) + 16
   dd(ii) = dd(ii) -1
end if
delete(ii)

ii = ind( (hh .lt. 8 .and. hh .gt. 0 ) .or. (hh .eq. 0 .and. dd .eq. 1))
if(.not.any(ismissing(ii))) then
   hh(ii) = hh(ii)+16
   dd(ii) = 31
   mm(ii) = mm(ii)-1
end if

strs = sprinti("%0.4i",yy) + " "+ sprinti("%0.2i",mm) + " " + \
         sprinti("%0.2i",dd) + " " + sprinti("%0.2i",hh)

asciiwrite("nlcheng55chuance_test.txt",strs)

end

On Nov 5, 2011, at 5:43 AM, nlcheng wrote:

> I want to convert the time of beijing to global time ,like this
> yymmddhh: 2011 03 18 07 to 2011 03 17 23
>
>
> I have upload my script and data .You can put them in one directory
> and ncl read44-55write55.ncl
>
> part of my script is below ,but it put out the same data ,like this
> 2011 03 18 07 to 2011 3 18 7
> just the same and there is no error.hope for your help! thank you
> very much
>
>
> ;************************************************
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "/usr/local/ncl/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"
> load "/usr/local/ncl/lib/ncarg/nclscripts/csm/contributed.ncl"
> ;************************************************
> begin
> fname = "nlcheng44chuance_test.txt"
> strs =new(6336,"string")
>
> data = asciiread(fname,(/6336,4/),"integer")
> mmm = data(:,1)
> nts = dimsizes(mmm)
> yy=new(nts,"integer")
> mm=new(nts,"integer")
> dd=new(nts,"integer")
> hh=new(nts,"integer")
> do nt=0,nts-1
> yy(nt) = data(nt,0)
> mm(nt) = data(nt,1)
> dd(nt) = data(nt,2)
>
> hh(nt) = data(nt,3)
>
> if ((hh(nt) .gt. 8) .and. (hh(nt) .eq. 8 ) ) then
>
> hh(nt) = hh(nt) -8
>
> if ((hh(nt) .lt. 8) .and. (hh(nt) .gt. 0 ) .and. (hh(nt) .eq.
> 0 ).and. (dd(nt) .ne. 1)) then
> hh(nt) = hh(nt) +16
> dd(nt) = dd(nt) -1
>
> if ((hh(nt) .lt. 8 ) .and. (hh(nt) .gt. 0 ) .and.( hh(nt) .eq.
> 0 ) .and. (dd(nt) .eq. 1)) then
> hh(nt) = hh(nt) +16
> dd(nt) = 31
> mm(nt) = mm(nt) -1
>
> end if
> end if
> end if
>
> vals = sprintf("%4.0f",yy(nt)) + " "+ sprintf("%2.0f",mm(nt)) +
> " "+sprintf("%2.0f",dd(nt)) + " " + sprintf("%2.0f",hh(nt))
> strs(nt) = vals
> asciiwrite("nlcheng55chuance_test.txt",strs)
>
> end do
> end
>
>
>
>
>
> <
> read44
> -55write55
> .ncl
> >
> <
> nlcheng44chuance_test
> .txt>_______________________________________________
> 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 Sat Nov 5 09:57:09 2011

This archive was generated by hypermail 2.1.8 : Tue Nov 08 2011 - 15:54:50 MST