[ncl-talk] Error with loop

Alan Brammer abrammer at albany.edu
Mon Jul 28 17:19:40 MDT 2014


“Error” is a very vague term.  It helps if you narrow down what you mean by that. 
	— Is the script returning an error, if so copy and paste that error so everyone can get a better idea as to the problem. 
	— Is the code just not doing what you want? If so try and be explicit about what it is you want returned from the section code and include data if possible.


There are a number of issues on the loop. 
 	—1.   I’d recommend running that loop and printing the i, j k values with each iteration to make sure the logic is what you expect.  It’s not intuitive to me. 
	—2.   da is an array of 5 numbers.  You’re just overwriting all 5 numbers with the scalar return with every iteration so it’s pointless.
	—3.   This syntax “ ya(k:i) “  says ya only has 1 dimension but you’re trying to average over the the 2nd dimension. Thats going to give you an error message.


How I would do this "I am writing a loop to calculate mean every 10 indices “.  Which is calculating the mean of 0-9, 10-19, 20-29 etc. If that’s not it, be more explicit in your reply to the list. 

retval = new( dimsizes(ya)/10, float)  ;;; assumes ya%10 is 0.  if not end indices will be ignored.
ct = 0
do i=0, dimsizes(ya)-10, 10  ;  loop from 0 to max length of array and step by 10
retval(ct) = avg( ya(i:i+9) )
ct = ct+1
end do



Good luck, 
		Alan. 


##############################
Alan Brammer,
PhD Student,

Department of Atmospheric and Environmental Sciences,
University at Albany, State University of New York, Albany, NY, 12222
abrammer at albany.edu
##############################

On Jul 28, 2014, at 4:55 PM, Ipshita Majhi <ipmajhi at alaska.edu> wrote:

> I am writing a loop to calculate mean every 10 indices and there is an error towards the end of the loop. It would be great if you could help me sort it out
> 
> ;This is to create decadal mean
> 
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> 
> ncol = 12
> nrows=134
> 
> air=readAsciiTable("~/Documents/PhD_June_2015/Data_AIR/air.txt", ncol, "integer",2)
> 
> ya = dim_avg_n(air,1)   ;yearly average 
> 
> i=0
> j=0
> k=0
> 
> da=new(5,"float")
> 
> do while (i.le.133)
> It would 
> if (i/1.eq.1+j) then
> 
> da=dim_avg_n(ya(k:i),1)
> 
> k=k+10
> 
> j=j+1
> continue
> end if
> 
> 
> i=i+1
> 
> end do
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk



More information about the ncl-talk mailing list