[ncl-talk] Need help in Do looping

Trisanu Banik baniktrisanu at gmail.com
Tue Sep 24 08:52:59 MDT 2019


Dear All

I need one help related to Do loop in NCL.

I have attached a code, the target of the code is to take data for few lat,
long points represented by Do loops of j and k. Collected data values for
the desired lat long need to put in the variable temp_file. Therefore, what
I expect temp_file should be populated with all the data values(from spd
variable) corresponding to the lat long in 'loc' variable. Here the loop of
j and k completes when j and k run from loc(1,0) to loc(1,1) and loc(0,0)
to loc(0,1). Once the data has been collected in temp_file, next target is
to take average of all the data and put it in the variable final_spd. Here
average value is put in the variable temp_avg. The same iteration needs to
do for 48 different times.

Here what I noticed the loop of j and k each time filled the temp_file with
a single value upto its dimension 30 and each iteration of j and k did the
same task. Therefore, instead of taking average over various data value
corresponding to different lat long it takes average on a single value that
arises during the last iteration and put it in the final_spd variable.

What I want is to take all the data corresponds to the j and k loop
corresponding to a particular time (represented by i loop) and take the
average over those data and put it in the final_spd variable and need to do
the same task for next 47 hours and put the average in final_spd variable.
So what I expect the final_spd variable should have 1 column and 48
different row.

Please suggest me how to do the same.


Thanks & Regards
Trisanu

*Trisanu Banik,PhD*

*Research Scientist*

*North Eastern Space Applications Centre (NESAC)*
*Government of India*
*Department of Space*

*Umiam-793103, Meghalaya*
*Mobile-9774837581*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190924/f7d3c67c/attachment.html>
-------------- next part --------------
 a = addfile("wrfout_d02_2019-09-04_00:00:00","r")
 

  minlat = 24.16            ; 21    ;24.13
  maxlat =24.29            ;23     ;24.27
  minlon =75.58             ; 70     ;75.53
  maxlon =75.68            ; 73    ;75.64

  opt = True
  loc  = wrf_user_ll_to_ij(a,(/minlon,maxlon/),(/minlat,maxlat/),opt)

  latlon = wrf_user_ij_to_ll(a,loc(0,:),loc(1,:),opt)
  point_spd = new((/49,1/),"float")
  temp_file = new ((/30,1/),"float","No_FillValue")
  temp_avg = new ((/1,1/),"float")
  final_spd = new((/49,1/),"float")



 time = wrf_user_getvar(a,"times",-1) ; get all times in the file

 
   u=wrf_user_getvar(a, "U", -1)
   ua = wrf_user_unstagger(u,u at stagger)
 
   v=wrf_user_getvar(a,"V",-1)
   va = wrf_user_unstagger(v,v at stagger)
;  W = wrf_user_getvar(a, "W", it)

  XLAT= wrf_user_getvar(a,"XLAT",20);
 ; XLAT= wrf_user_getvar(a,"lat",it);
  XLONG = wrf_user_getvar(a,"XLONG",20);
; XLONG = wrf_user_getvar(a,"lon",it);
  z = wrf_user_getvar(a, "z", -1);
 ; z=z1(:,:,y,x)
height = (/500./)
; height = height_levels(level)

 u_plane = wrf_user_intrp3d( ua,z,"h",height,0.,False)
 v_plane = wrf_user_intrp3d( va,z,"h",height,0.,False)

 spd = (u_plane*u_plane + v_plane*v_plane)^(0.5) ; m/sec
 spd at description = "Wind Speed"
 spd at units = "m/s"
; p=1

 ntimes =dimsizes(time)
 do i=2, ntimes-1,1
 do j= loc(1,0),loc(1,1)  ; 65,67,1
 do k= loc(0,0),loc(0,1)                ; 65,67,1


; if (spd(i,j,k).ne. 9.96921e+36) then
  if(.not.all(ismissing(spd(i,j,k)))) then
; if (spd(i,j,k).ne.spd at _FillValue)
 temp_file=spd(i,j,k)
; p=p+1
 else
 end if
 end do
 end do
; asciiwrite("bb.txt",temp_file)
; temp_file(:,1)=spd(i,108:111,139:143)
 if(.not.all(ismissing(temp_file)))
 temp_avg=dim_avg_n(temp_file,0)
 final_spd(i,:)=temp_avg
;asciiwrite("bb.txt",temp_file)
;asciiwrite("cc.txt",temp_avg)
; point_spd(i,:)=spd(i,60,56)
 delete(temp_file)
 delete(temp_avg)
 else
 end if
 end do
; asciiwrite("AA.txt",point_spd)
 asciiwrite("AA.txt",final_spd)


; lats =(/22.005,24.26,24.25,24.19,24.13,24.16,24.27,24.16,24.23,24.22,24.21/)
; lons =(/71.48,75.55,75.55,75.55,75.6,75.63,75.63,75.53,75.64,75.64,75.64/)
; lats=(/22.005,24.26/)
; lons=(/71.48,75.55/)



More information about the ncl-talk mailing list