[ncl-talk] ncl plot getting killed

Mary Haley haley at ucar.edu
Thu Nov 29 12:28:18 MST 2018


Hi Komal,

Glad it worked.  We ask that people post all follow-up questions to
ncl-talk, so I've CC'ed ncl-talk here.

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?

If your time array was length 24, where each index represented an hour:

index 0 --> hour 0
index 1 --> hour 1
. . .
index 16 --> hour 16
...
index 21 --> hour 21


then you could do something like this:

;---Average the first one and store to variable
Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(*16:21*,:,:,:),0)

;---Loop thru rest of vars and sum their averages
do nv=1,nvars-1
   Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(*16:21*
,:,:,:),0)
end do

Note that I'm assuming your array is four-dimensional. If it is
three-dimensional, then you need to remove one of the colons:

  Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$*(16:21,:,:)*,0)

However, in general, it's not a good idea to hard code indexes in this way.

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.

For example:

;---Read WRF character Times array off the files
  wrf_times = f[:]->Times    ; character array, ntime x character_length

;---Convert WRF string times to numeric times

  times  = wrf_times_c(wrf_times,0)
  printVarSummary(times)

;---Convert numeric times to N x 6 array (0=year,1=month,...,5=seconds)

  ymdhms = cd_calendar(times,0)
  years  = ymdhms(:,0)
  months = ymdhms(:,1)
  days   = ymdhms(:,2)
  hours  = ymdhms(:,3)

Now you can grab all the hours between 16 and 21 and use this to subset
your array:

  *ihour* = ind(hours.ge.16.and.hours.le.21)

and then use the "ihour" index array to subscript the variables:

  Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(*ihour*,:,:,:),0)
  do nv=1,nvars-1
     Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(*ihour*
,:,:,:),0)
  end do

--Mary

On Thu, Nov 29, 2018 at 4:34 AM Komal Shukla <komalshukla1992 at gmail.com>
wrote:

> Mary,
>
> 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?
>
> vars_to_avg = (/"no","no2","oli","csl","hc3","ket"/)
> nvars = dimsizes(vars_to_avg)
>
> ;---Average the first one and store to variable
> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$,0)
>
> ;---Loop thru rest of vars and sum their averages
> do nv=1,nvars-1
>   Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$,0)
> end do
> Mpan_avg=Mpan_avg*1000
> Mpan_avg2D=Mpan_avg(0,:,:)
>
>
>
> On Thu, Nov 29, 2018 at 4:34 PM Komal Shukla <komalshukla1992 at gmail.com>
> wrote:
>
>> Mary,
>> It works just the right,4th option is most optimised. thanks!
>>
>>
>>
>>
>> On Wed, Nov 28, 2018 at 8:49 PM Mary Haley <haley at ucar.edu> wrote:
>>
>>> Hi Kim,
>>>
>>> You could be correct that this is a memory issue. Are you getting any
>>> errors from NCL before it quits?
>>>
>>> You can save on memory by not creating so many local variables. Below
>>> are four possible scenarios you can try:
>>>
>>> [1] You can delete a variable after you no longer need it, which will
>>> free up some memory.
>>>
>>> no = aa[:]->no
>>> no_avg=dim_avg_n(no,0)
>>> delete(no)
>>> no2 = aa[:]->no2
>>> no2_avg=dim_avg_n(no2,0)
>>> delete(no2)
>>> oli = aa[:]->oli
>>> oli_avg=dim_avg_n(oli,0)
>>> delete(oli)
>>>
>>> [2] You can use [/.../] to delete several variables at once, if you
>>> don't need to free them individually:
>>>
>>> no = aa[:]->no
>>> no_avg=dim_avg_n(no,0)
>>> no2 = aa[:]->no2
>>> no2_avg=dim_avg_n(no2,0)
>>> oli = aa[:]->oli
>>> oli_avg=dim_avg_n(oli,0)
>>> delete([/no,no2,oli/])
>>>
>>> [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:
>>>
>>> no_avg  = dim_avg_n(aa[:]->no,0)
>>> no2_avg= dim_avg_n(aa[:]->no2,0)
>>> oli_avg  = dim_avg_n(aa[:]->oli,0)
>>>
>>> [4] Since you are doing the same operation repeatedly, you can use a do
>>> loop and save yourself some more memory.
>>>
>>> vars_to_avg = (/"no","no2","oli","csl","olt","xyl","ald","hc8",\
>>>                          "hcho","ol2","tol","hc5","hc3","ket"/)
>>> nvars = dimsizes(vars_to_avg)
>>>
>>> ;---Average the first one and store to variable
>>> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$,0)
>>>
>>> ;---Loop thru rest of vars and sum their averages
>>> do nv=1,nvars-1
>>>   Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$,0)
>>> end do
>>> Mpan_avg=Mpan_avg*1000
>>> Mpan_avg2D=Mpan_avg(0,:,:)
>>> . . .
>>>
>>> The above is not tested, so hopefully there are no syntax errors in it.
>>>
>>> --Mary
>>>
>>>
>>> On Wed, Nov 28, 2018 at 2:49 AM Komal Shukla <komalshukla1992 at gmail.com>
>>> wrote:
>>>
>>>> Hi
>>>>
>>>> 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.
>>>>
>>>> Can you please suggest a "efficient" way of writing this? Please help.
>>>>
>>>> Script ----------
>>>> begin
>>>> a = addfile("../wrfout_d01_2012-05-01_00:00:00.nc","r")
>>>>
>>>>  it        = 0     ; first time step
>>>>   hgt       = wrf_user_getvar(a,"HGT_M",it)    ; Terrain elevation
>>>>   hgt at lat2d = wrf_user_getvar(a,"XLAT",it)   ; latitude/longitude
>>>>   hgt at lon2d = wrf_user_getvar(a,"XLONG",it)  ; required for plotting
>>>>
>>>>   wks = gsn_open_wks("png","try1")
>>>>
>>>>     FILES = systemfunc ("ls ../wrfout_d01_2012-05*") ; file paths
>>>>
>>>>  numFILES = dimsizes(FILES)
>>>>  aa    = addfiles (FILES+".nc", "r")     ;add multiple files
>>>> ;;;;;;;;;;;;
>>>>
>>>> no = aa[:]->no
>>>> no_avg=dim_avg_n(no,0)
>>>> no2 = aa[:]->no2
>>>> no2_avg=dim_avg_n(no2,0)
>>>> oli = aa[:]->oli
>>>> oli_avg=dim_avg_n(oli,0)
>>>> csl = aa[:]->csl
>>>> csl_avg=dim_avg_n(csl,0)
>>>> olt = aa[:]->olt
>>>> olt_avg=dim_avg_n(olt,0)
>>>> xyl = aa[:]->xyl
>>>> xyl_avg=dim_avg_n(xyl,0)
>>>> ald = aa[:]->ald
>>>> ald_avg=dim_avg_n(ald,0)
>>>> hc8 = aa[:]->hc8
>>>> hc8_avg=dim_avg_n(hc8,0)
>>>> hcho = aa[:]->hcho
>>>> hcho_avg=dim_avg_n(hcho,0)
>>>> ol2 = aa[:]->ol2
>>>> ol2_avg=dim_avg_n(ol2,0)
>>>> tol = aa[:]->tol
>>>> tol_avg=dim_avg_n(tol,0)
>>>> hc5= aa[:]->hc5
>>>> hc5_avg=dim_avg_n(hc5,0)
>>>> hc3= aa[:]->hc3
>>>> hc3_avg=dim_avg_n(hc3,0)
>>>> ket= aa[:]->ket
>>>> ket_avg=dim_avg_n(ket,0)
>>>>
>>>>
>>>> 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
>>>> Mpan_avg=Mpan_avg*1000
>>>> Mpan_avg2D=Mpan_avg(0,:,:)
>>>> Mpan_avg2D at lat2d = wrf_user_getvar(a,"XLAT",it)   ; latitude/longitude
>>>> Mpan_avg2D at lon2d = wrf_user_getvar(a,"XLONG",it)  ; required for
>>>> plotting
>>>> --some plotting options here--
>>>> contour = gsn_csm_contour_map(wks,Mpan_avg2D,res)
>>>> draw(contour)
>>>>   frame(wks)
>>>>
>>>> Thanks
>>>> Kim
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> ncl-talk at ucar.edu
>>>> List instructions, subscriber options, unsubscribe:
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181129/543da0dd/attachment.html>


More information about the ncl-talk mailing list