Re: problem in looping

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Tue Sep 25 2012 - 07:34:03 MDT

Erika,

The do loop should be:

do i = 0, iLast - iStrt -1

Wei

huangwei@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 Sep 25, 2012, at 7:28 AM, Wei Huang wrote:

> Erika,
>
> Try change these two lines:
>
> SST = f->sst (iStrt:iLast,:,:)
> do i= iStrt,iLast
>
> To
>
> SST = f->sst (iStrt:iLast,:,:)
> do i= 0, iLast - iStrt
>
>
> The reason is that you have read SST from iStrt to iLast,
> so the new dimension for SST will be from 0 to iLast - iStrt - 1.
>
> You may try printVarSummary(SST) to verify this.
>
> Wei
>
> huangwei@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 Sep 25, 2012, at 1:29 AM, Erika Folova wrote:
>
>> HI NCL users,
>>
>>
>> Hi Kyle, i think its a bit miss understanding. Ok, the main problem is the looping not working correctly with my first loop command:
>>
>> SST = f->sst (iStrt:iLast,:,:)
>> do i= iStrt,iLast
>> wks = gsn_open_wks("pdf","SST"+time(i))
>> gsn_define_colormap(wks,"GreenYellow")
>> res@tiMainString = "SST Pacific, Time: "+time(i)+" "
>> plot = gsn_csm_contour_map(wks,SST(i, :, :),res)
>> delete(wks)
>> end do
>> end
>>
>> i got the error: fatal:Subscript out of range, error in subscript #0
>> That's why in the first email i said that i have set the loop quite well but still produce the error.
>> I really appreciate if somebody can figure out this problem. Thanks a lot.
>>
>> I explored why this error appear:
>>
>> f = addfile("./sst.nc", "r")
>> time = f->time
>> ymd = ut_calendar(time, -2)
>> ymdStrt = 20070601
>> ymdLast = 20070610
>> iStrt = ind(ymd.eq.ymdStrt) ; index start
>> iLast = ind(ymd.eq.ymdLast) ; index last
>> delete(ymd)
>> delete(time)
>> SST = f->sst(iStrt:iLast,:,:) ; read only specified time period
>>
>>
>> Variable:SST
>> Type: float
>> Total Size: 213120 bytes
>> 53280 values
>> Number of Dimensions: 3
>> Dimensions and sizes: [time | 10] x [lat | 37] x [lon | 144]
>>
>> print(iStrt)
>> (0) 10013
>>
>> print(iLast)
>> (0) 10022
>>
>> print(SST&time)
>> Variable: time (coordinate)
>> Type: double
>> Total Size: 80 bytes
>> 10 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [time | 10]
>> Coordinates:
>> Number Of Attributes: 6
>> units : hours since 0001-01-01 00:00:00
>> long_name : Time
>> axis : T
>> time_origin : 01-JAN-0001 00:00:00
>> beginning_date : 0000 UTC 1 Jan 1980
>> ending_date : 0000 UTC 31 Dec 2011
>> (0) 17587896
>> (1) 17587920
>> (2) 17587944
>> (3) 17587968
>> (4) 17587992
>> (5) 17588016
>> (6) 17588040
>> (7) 17588064
>> (8) 17588088
>> (9) 17588112
>>
>>
>> Cheers,
>>
>>
>>
>>
>>
>> On Tue, Sep 25, 2012 at 3:58 AM, Kyle Griffin <ksgriffin2@wisc.edu> wrote:
>> Hi Erika,
>>
>> In the two emails you've sent, both have errors. In the first (sent only to me), you loop over just i=0,9 which will not work with iStrt+1 as the time index. Your best bet is to loop over your original statement (the do i=iStrt,ilast statement) with the time(i) and SST(i,:,:) statements.
>>
>> Your second email is similarly close, but still slightly in error. If you correct the time(iStrt+1) to time(i) and change the gsn_csm_contour plotting to SST(i,:,:) instead of the SST(n,:,:) you have (I'm not even sure where n is defined), then you should get the script to work.
>>
>> The index i, when utilized in the time array, will directly correspond to the same index you are plotting from the SST variable, and the times will almost certainly agree.
>>
>> I'll apologize if I confused you with my comment about i as a counter confused you - that was merely an alternate solution that could be applied with my first statement, not in addition to.
>>
>> Hope this helps better,
>>
>>
>> Kyle
>> ----------------------------------------
>> Kyle S. Griffin
>> Department of Atmospheric and Oceanic Sciences
>> University of Wisconsin - Madison
>> 1225 W Dayton St, Madison, WI 53706
>> Room 1421 Email: ksgriffin2@wisc.edu
>>
>>
>>
>> On Mon, Sep 24, 2012 at 5:42 PM, Erika Folova <e.folova@gmail.com> wrote:
>> Hi,
>>
>> I did what Kyle suggested. I still get the same error: fatal:Subscript out of range, error in subscript #0 . Any suggestion? Thanks a lot.
>>
>> begin
>> f = addfile(./sst.nc", "r") ; ALL times on file
>>
>> time = f->time
>> ymd = cd_calendar(time, -2) ; convert
>> ymdStrt = 20070601 ; year-month start
>> ymdLast = 20070610 ; year-month last
>> iStrt = ind(ymd.eq.ymdStrt) ; index start
>> iLast = ind(ymd.eq.ymdLast) ; index last
>> delete(time)
>> delete(ymd)
>> ==================
>> SST = f->sst (iStrt:iLast,:,:) ; read only specified time period
>> SST@_FillValue= -999
>> ymd = cd_calendar(SST&time, -2) ; selected period
>>
>> [SNIP]
>> =======PLOT
>> do n= iStrt,iLast
>> wks = gsn_open_wks("pdf","SST"+time(iStrt+1))
>> gsn_define_colormap(wks,"GreenYellow")
>> res@tiMainString = "SST Pacific, Time: "+time(iStrt+1)+" "
>> plot = gsn_csm_contour_map(wks,SST(n, :, :),res)
>> delete(wks)
>> end do
>> end
>> ====
>>
>> Cheers,
>>
>>
>>
>>
>>
>>
>> On Mon, Sep 24, 2012 at 11:28 PM, Kyle Griffin <ksgriffin2@wisc.edu> wrote:
>> I haven't tested this, but the following line should work if you don't mind keeping the formatting of the dates as-is (e.g. "20060601" format):
>>
>> res@tiMainString = "SST Pacific, Time: "+time(i)+" "
>>
>> sted this, but the following line should work if you don't mind keeping the formatting of the dates as-is (e.g. "20060601" format):
>> The time coordinate variable is a 1-D array with the same size as the time dimension of your SST variable. As a result, the index you use for the time in your SST should work for time as well.
>> If 'i' were just a counter for the number of times you have gone through the loop, you could use time(iStrt+i) to achieve a similar result.
>>
>> If you wish to reformat the string, look into uses of the date functions found:
>>
>> http://www.ncl.ucar.edu/Document/Functions/date.shtml
>>
>> I find cd_string (or ut_string if you are in an older version) to be the most useful to format dates the way you like to see them when reading from a time coordinate variable, although others have varying preferences.
>>
>>
>> Kyle
>> ----------------------------------------
>> Kyle S. Griffin
>> Department of Atmospheric and Oceanic Sciences
>> University of Wisconsin - Madison
>> 1225 W Dayton St, Madison, WI 53706
>> Room 1421 Email: ksgriffin2@wisc.edu
>>
>>
>>
>> On Mon, Sep 24, 2012 at 4:17 PM, Erika Folova <e.folova@gmail.com> wrote:
>> Hi NCL users,
>>
>> I just wonder how to figure out this error: fatal:Subscript out of range, error in subscript #0
>> I think I've set everything quite well as follow:
>>
>> begin
>> f = addfile(./sst.nc", "r") ; ALL times on file
>>
>> time = f->time
>> ymd = cd_calendar(time, -2) ; convert
>> ymdStrt = 20070601 ; year-month start
>> ymdLast = 20070610 ; year-month last
>> iStrt = ind(ymd.eq.ymdStrt) ; index start
>> iLast = ind(ymd.eq.ymdLast) ; index last
>> delete(time)
>> delete(ymd)
>>
>> ==================
>> SST = f->sst (iStrt:iLast,:,:) ; read only specified time period
>> SST@_FillValue= -999
>>
>> [SNIP]
>> ==================
>> print("PLOT --->>>")
>>
>> do i=iStrt, iLast
>> wks = gsn_open_wks("pdf","SST"+i)
>> gsn_define_colormap(wks,"GreenYellow") ; choose color map
>> res@tiMainString = "SST Pacific, Time: "+i+" "
>> plot = gsn_csm_contour_map(wks,SST(i,:,:),res)
>> delete(wks)
>> end do
>>
>> end
>> ======
>>
>> I also just wonder how to reversed the index values back to the ymd values thus i can attach it on my res@tiMainString correctly.
>> Thanks a lot
>>
>> Cheers...
>>
>> _______________________________________________
>> 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
>
> _______________________________________________
> 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 Tue Sep 25 07:34:15 2012

This archive was generated by hypermail 2.1.8 : Wed Sep 26 2012 - 13:56:03 MDT