<div dir="ltr"><div dir="ltr">Hi Mary<br><br>I was trying your suggested method--which is below<br><br>;-<span style="background-color:rgb(255,255,0)">--Read WRF character Times array off the files<br>  wrf_times = f[:]->Times    ; character array, ntime x character_length                                                                       <br>;---Convert WRF string times to numeric times                                           <br>  times  = wrf_times_c(wrf_times,0)<br>  printVarSummary(times)<br><br>;---Convert numeric times to N x 6 array (0=year,1=month,...,5=seconds)                   <br>  ymdhms = cd_calendar(times,0)<br>  years  = ymdhms(:,0)<br>  months = ymdhms(:,1)<br>  days   = ymdhms(:,2)<br>  hours  = ymdhms(:,3)<br>Now you can grab all the hours between 16 and 21 and use this to subset your array:<br>  ihour = ind(hours.ge.16.and.hours.le.21)<br>and then use the "ihour" index array to subscript the variables:<br> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(ihour,:,:,:),0)<br>  do nv=1,nvars-1<br>     Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(ihour,:,:,:),0)<br>  end do  </span><br><br>Kim<br>when i do this, it says, <span style="background-color:rgb(0,255,255)">fatal:Undefined identifier: (wrf_times_c) is undefined, can't continue</span><span style="background-color:rgb(243,243,243)">, why is it? Any suggestion to correct.</span></div><div dir="ltr"><br><br>On Fri, Nov 30, 2018 at 12:58 AM Mary Haley <<a href="mailto:haley@ucar.edu">haley@ucar.edu</a>> wrote:<br>><br>><br>> Hi Komal,<br>><br>> Glad it worked.  We ask that people post all follow-up questions to ncl-talk, so I've CC'ed ncl-talk here.<br>><br>> As for your certain hour window question, this depends on what your "time" array looks like. Are the files hourly, daily, 6-hourly, something else?<br>><br>> If your time array was length 24, where each index represented an hour:<br>><br>> index 0 --> hour 0<br>> index 1 --> hour 1<br>> . . .<br>> index 16 --> hour 16<br>> ...<br>> index 21 --> hour 21<br>><br>><br>> then you could do something like this:<br>><br>> ;---Average the first one and store to variable<br>> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(16:21,:,:,:),0)<br>><br>> ;---Loop thru rest of vars and sum their averages<br>> do nv=1,nvars-1<br>>    Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(16:21,:,:,:),0)<br>> end do  <br>><br>> Note that I'm assuming your array is four-dimensional. If it is three-dimensional, then you need to remove one of the colons:<br>><br>>   Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(16:21,:,:),0)<br>><br>> However, in general, it's not a good idea to hard code indexes in this way. <br>><br>> Instead, I recommend using wrf_times_c to convert your WRF Times character array to numeric times, and then cd_calendar to convert from the numeric times to an ntime x 6 array, where the 6 represents year, month, day, hour, minute, second.<br>><br>> For example:<br>><br>> ;---Read WRF character Times array off the files<br>>   wrf_times = f[:]->Times    ; character array, ntime x character_length                                                                       <br>> ;---Convert WRF string times to numeric times                                           <br>>   times  = wrf_times_c(wrf_times,0)<br>>   printVarSummary(times)<br>><br>> ;---Convert numeric times to N x 6 array (0=year,1=month,...,5=seconds)                   <br>>   ymdhms = cd_calendar(times,0)<br>>   years  = ymdhms(:,0)<br>>   months = ymdhms(:,1)<br>>   days   = ymdhms(:,2)<br>>   hours  = ymdhms(:,3)<br>><br>> Now you can grab all the hours between 16 and 21 and use this to subset your array:<br>><br>>   ihour = ind(hours.ge.16.and.hours.le.21)<br>><br>> and then use the "ihour" index array to subscript the variables:<br>><br>>   Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(ihour,:,:,:),0)<br>>   do nv=1,nvars-1<br>>      Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(ihour,:,:,:),0)<br>>   end do  <br>><br>> --Mary<br>><br>> On Thu, Nov 29, 2018 at 4:34 AM Komal Shukla <<a href="mailto:komalshukla1992@gmail.com">komalshukla1992@gmail.com</a>> wrote:<br>>><br>>> Mary,<br>>><br>>> This worked just brilliant without killing the process(your modified script below). What changed to be made in it, if we just need average of a certain hour window (for example 16:00 -21:00 hours, considering 0 to be first) instead of 24 hours?<br>>><br>>> vars_to_avg = (/"no","no2","oli","csl","hc3","ket"/)<br>>> nvars = dimsizes(vars_to_avg)<br>>><br>>> ;---Average the first one and store to variable<br>>> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$,0)<br>>><br>>> ;---Loop thru rest of vars and sum their averages<br>>> do nv=1,nvars-1<br>>>   Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$,0)<br>>> end do  <br>>> Mpan_avg=Mpan_avg*1000<br>>> Mpan_avg2D=Mpan_avg(0,:,:)<br>>><br>>><br>>><br>>> On Thu, Nov 29, 2018 at 4:34 PM Komal Shukla <<a href="mailto:komalshukla1992@gmail.com">komalshukla1992@gmail.com</a>> wrote:<br>>>><br>>>> Mary,<br>>>> It works just the right,4th option is most optimised. thanks! <br>>>><br>>>><br>>>><br>>>><br>>>> On Wed, Nov 28, 2018 at 8:49 PM Mary Haley <<a href="mailto:haley@ucar.edu">haley@ucar.edu</a>> wrote:<br>>>>><br>>>>> Hi Kim,<br>>>>><br>>>>> You could be correct that this is a memory issue. Are you getting any errors from NCL before it quits?<br>>>>><br>>>>> You can save on memory by not creating so many local variables. Below are four possible scenarios you can try:<br>>>>><br>>>>> [1] You can delete a variable after you no longer need it, which will free up some memory. <br>>>>><br>>>>> no = aa[:]->no<br>>>>> no_avg=dim_avg_n(no,0)<br>>>>> delete(no)<br>>>>> no2 = aa[:]->no2<br>>>>> no2_avg=dim_avg_n(no2,0)<br>>>>> delete(no2)<br>>>>> oli = aa[:]->oli<br>>>>> oli_avg=dim_avg_n(oli,0)<br>>>>> delete(oli)<br>>>>><br>>>>> [2] You can use [/.../] to delete several variables at once, if you don't need to free them individually:<br>>>>><br>>>>> no = aa[:]->no<br>>>>> no_avg=dim_avg_n(no,0)<br>>>>> no2 = aa[:]->no2<br>>>>> no2_avg=dim_avg_n(no2,0)<br>>>>> oli = aa[:]->oli<br>>>>> oli_avg=dim_avg_n(oli,0)<br>>>>> delete([/no,no2,oli/])<br>>>>><br>>>>> [3] Even better, if there's no reason that variables like "no", "no2", and "oli" need to exist, then there's no need to save them to a local variable before taking the average:<br>>>>><br>>>>> no_avg  = dim_avg_n(aa[:]->no,0)<br>>>>> no2_avg= dim_avg_n(aa[:]->no2,0)<br>>>>> oli_avg  = dim_avg_n(aa[:]->oli,0)<br>>>>><br>>>>> [4] Since you are doing the same operation repeatedly, you can use a do loop and save yourself some more memory.<br>>>>><br>>>>> vars_to_avg = (/"no","no2","oli","csl","olt","xyl","ald","hc8",\<br>>>>>                          "hcho","ol2","tol","hc5","hc3","ket"/)<br>>>>> nvars = dimsizes(vars_to_avg)<br>>>>><br>>>>> ;---Average the first one and store to variable<br>>>>> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$,0)<br>>>>><br>>>>> ;---Loop thru rest of vars and sum their averages<br>>>>> do nv=1,nvars-1<br>>>>>   Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$,0)<br>>>>> end do  <br>>>>> Mpan_avg=Mpan_avg*1000<br>>>>> Mpan_avg2D=Mpan_avg(0,:,:)<br>>>>> . . .<br>>>>><br>>>>> The above is not tested, so hopefully there are no syntax errors in it.<br>>>>><br>>>>> --Mary<br>>>>><br>>>>><br>>>>> On Wed, Nov 28, 2018 at 2:49 AM Komal Shukla <<a href="mailto:komalshukla1992@gmail.com">komalshukla1992@gmail.com</a>> wrote:<br>>>>>><br>>>>>> Hi<br>>>>>><br>>>>>> I am trying to add a variable from netcdf files, and make an average plot. My scripts is mentioned below, but the process gets killed. Maybe because of memory. <br>>>>>><br>>>>>> Can you please suggest a "efficient" way of writing this? Please help.<br>>>>>><br>>>>>> Script ----------<br>>>>>> begin<br>>>>>> a = addfile("../wrfout_d01_2012-05-01_00:00:<a href="http://00.nc">00.nc</a>","r")<br>>>>>><br>>>>>>  it        = 0     ; first time step<br>>>>>>   hgt       = wrf_user_getvar(a,"HGT_M",it)    ; Terrain elevation<br>>>>>>   hgt@lat2d = wrf_user_getvar(a,"XLAT",it)   ; latitude/longitude<br>>>>>>   hgt@lon2d = wrf_user_getvar(a,"XLONG",it)  ; required for plotting<br>>>>>><br>>>>>>   wks = gsn_open_wks("png","try1")<br>>>>>><br>>>>>>     FILES = systemfunc ("ls ../wrfout_d01_2012-05*") ; file paths<br>>>>>><br>>>>>>  numFILES = dimsizes(FILES)<br>>>>>>  aa    = addfiles (FILES+".nc", "r")     ;add multiple files<br>>>>>> ;;;;;;;;;;;;<br>>>>>><br>>>>>> no = aa[:]->no<br>>>>>> no_avg=dim_avg_n(no,0)<br>>>>>> no2 = aa[:]->no2<br>>>>>> no2_avg=dim_avg_n(no2,0)<br>>>>>> oli = aa[:]->oli<br>>>>>> oli_avg=dim_avg_n(oli,0)<br>>>>>> csl = aa[:]->csl<br>>>>>> csl_avg=dim_avg_n(csl,0)<br>>>>>> olt = aa[:]->olt<br>>>>>> olt_avg=dim_avg_n(olt,0)<br>>>>>> xyl = aa[:]->xyl<br>>>>>> xyl_avg=dim_avg_n(xyl,0)<br>>>>>> ald = aa[:]->ald<br>>>>>> ald_avg=dim_avg_n(ald,0)<br>>>>>> hc8 = aa[:]->hc8<br>>>>>> hc8_avg=dim_avg_n(hc8,0)<br>>>>>> hcho = aa[:]->hcho<br>>>>>> hcho_avg=dim_avg_n(hcho,0)<br>>>>>> ol2 = aa[:]->ol2<br>>>>>> ol2_avg=dim_avg_n(ol2,0)<br>>>>>> tol = aa[:]->tol<br>>>>>> tol_avg=dim_avg_n(tol,0)<br>>>>>> hc5= aa[:]->hc5<br>>>>>> hc5_avg=dim_avg_n(hc5,0)<br>>>>>> hc3= aa[:]->hc3<br>>>>>> hc3_avg=dim_avg_n(hc3,0)<br>>>>>> ket= aa[:]->ket<br>>>>>> ket_avg=dim_avg_n(ket,0)<br>>>>>><br>>>>>> Mpan_avg=no_avg+no2_avg+oli_avg+csl_avg+olt_avg+xyl_avg+ald_avg+hc8_avg+hcho_avg+ol2_avg+tol_avg+hc5_avg+hc3_avg+ket_avg<br>>>>>> Mpan_avg=Mpan_avg*1000<br>>>>>> Mpan_avg2D=Mpan_avg(0,:,:)<br>>>>>> Mpan_avg2D@lat2d = wrf_user_getvar(a,"XLAT",it)   ; latitude/longitude<br>>>>>> Mpan_avg2D@lon2d = wrf_user_getvar(a,"XLONG",it)  ; required for plotting<br>>>>>> --some plotting options here--<br>>>>>> contour = gsn_csm_contour_map(wks,Mpan_avg2D,res)<br>>>>>> draw(contour)<br>>>>>>   frame(wks)<br>>>>>>  <br>>>>>> Thanks<br>>>>>> Kim<br>>>>>> _______________________________________________<br>>>>>> ncl-talk mailing list<br>>>>>> <a href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a><br>>>>>> List instructions, subscriber options, unsubscribe:<br>>>>>> <a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a></div></div>