<div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Hi Komal,</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Glad it worked. We ask that people post all follow-up questions to ncl-talk, so I've CC'ed ncl-talk here.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">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?</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">If your time array was length 24, where each index represented an hour:</div><div class="gmail_default" style="font-size:small"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_default" style="font-size:small">index 0 --> hour 0</div><div class="gmail_default" style="font-size:small">index 1 --> hour 1</div><div class="gmail_default" style="font-size:small">. . .</div><div class="gmail_default" style="font-size:small">index 16 --> hour 16</div><div class="gmail_default" style="font-size:small">...</div><div class="gmail_default" style="font-size:small">index 21 --> hour 21</div></blockquote><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">then you could do something like this:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"><div class="gmail_default"><font face="monospace, monospace">;---Average the first one and store to variable</font></div><div class="gmail_default"><font face="monospace, monospace">Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(<b>16:21</b>,:,:,:),0)</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="monospace, monospace">;---Loop thru rest of vars and sum their averages</font></div><div class="gmail_default"><font face="monospace, monospace">do nv=1,nvars-1</font></div><div class="gmail_default"><font face="monospace, monospace"> Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(<b>16:21</b>,:,:,:),0)</font></div><div class="gmail_default"><font face="monospace, monospace">end do </font></div></div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Note that I'm assuming your array is four-dimensional. If it is three-dimensional, then you need to remove one of the colons:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default"><div class="gmail_default" style="font-size:small"><font face="monospace, monospace"> Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$<b>(16:21,:,:)</b>,0)</font><br></div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">However, in general, it's not a good idea to hard code indexes in this way. </div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">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.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">For example:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"><span style="font-family:monospace,monospace">;---Read WRF character Times array off the files</span><br></div><div class="gmail_default"><div class="gmail_default"><font face="monospace, monospace"> wrf_times = f[:]->Times ; character array, ntime x character_length </font></div><div class="gmail_default"><span style="font-family:monospace,monospace">;---Convert WRF string times to numeric times </span><span style="font-family:monospace,monospace"> </span></div><div class="gmail_default"><font face="monospace, monospace"> times = wrf_times_c(wrf_times,0)</font></div><div class="gmail_default"><font face="monospace, monospace"> printVarSummary(times)</font></div><div class="gmail_default"><font face="monospace, monospace"><br></font></div><div class="gmail_default"><font face="monospace, monospace">;---Convert numeric times to N x 6 array (0=year,1=month,...,5=seconds) </font></div><div class="gmail_default"><font face="monospace, monospace"> ymdhms = cd_calendar(times,0)</font></div><div class="gmail_default"><font face="monospace, monospace"> years = ymdhms(:,0)</font></div><div class="gmail_default"><font face="monospace, monospace"> months = ymdhms(:,1)</font></div><div class="gmail_default"><font face="monospace, monospace"> days = ymdhms(:,2)</font></div><div class="gmail_default"><font face="monospace, monospace"> hours = ymdhms(:,3)</font></div><div style="font-size:small"><br></div><div style="font-size:small">Now you can grab all the hours between 16 and 21 and use this to subset your array:</div><div style="font-size:small"><br></div><span style="font-family:monospace,monospace"> </span><b style="font-family:monospace,monospace">ihour</b><span style="font-family:monospace,monospace"> = ind(hours.ge.16.and.hours.le.</span><span style="font-family:monospace,monospace">21)</span><br style="font-family:monospace,monospace"></div><div class="gmail_default"><span style="font-family:monospace,monospace"><br></span></div><div class="gmail_default" style="font-size:small">and then use the "ihour" index array to subscript the variables:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default"><div class="gmail_default"><span style="font-family:monospace,monospace"> Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$(<b>ihour</b>,:,:,:),0)</span><br></div><div class="gmail_default"><span style="font-family:monospace,monospace"> do nv=1,nvars-1</span><br></div><div class="gmail_default"><font face="monospace, monospace"> Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$(</font><span style="font-family:monospace,monospace"><b>ihour</b></span><span style="font-family:monospace,monospace">,:,:,:),0)</span></div><div class="gmail_default"><font face="monospace, monospace"> end do </font></div></div></div><br>--Mary <br></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Nov 29, 2018 at 4:34 AM Komal Shukla <<a href="mailto:komalshukla1992@gmail.com" target="_blank">komalshukla1992@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default">Mary,</div><div class="gmail_default"><br></div><div class="gmail_default">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?</div><div class="gmail_default"><br></div><div class="gmail_default">vars_to_avg = (/"no","no2","oli","csl","hc3","ket"/)</div><div class="gmail_default">nvars = dimsizes(vars_to_avg)</div><div class="gmail_default"><br></div><div class="gmail_default">;---Average the first one and store to variable</div><div class="gmail_default">Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$,0)</div><div class="gmail_default"><br></div><div class="gmail_default">;---Loop thru rest of vars and sum their averages</div><div class="gmail_default">do nv=1,nvars-1</div><div class="gmail_default"> Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$,0)</div><div class="gmail_default">end do </div><div class="gmail_default">Mpan_avg=Mpan_avg*1000</div><div class="gmail_default">Mpan_avg2D=Mpan_avg(0,:,:)</div><div class="gmail_default"><br></div><div><div dir="ltr" class="m_-6622181972300533619m_7404432217518881119m_-7360352464085629663gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Nov 29, 2018 at 4:34 PM Komal Shukla <<a href="mailto:komalshukla1992@gmail.com" target="_blank">komalshukla1992@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Mary,<div>It works just the right,4th option is most optimised. thanks! <br clear="all"><div><div dir="ltr" class="m_-6622181972300533619m_7404432217518881119m_-7360352464085629663m_-2241818979720693385gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><br><div><br></div></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Nov 28, 2018 at 8:49 PM Mary Haley <<a href="mailto:haley@ucar.edu" target="_blank">haley@ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="font-size:small">Hi Kim,</div><div style="font-size:small"><br></div><div style="font-size:small">You could be correct that this is a memory issue. Are you getting any errors from NCL before it quits?</div><div style="font-size:small"><br></div><div style="font-size:small">You can save on memory by not creating so many local variables. Below are four possible scenarios you can try:</div><div style="font-size:small"><br></div><div style="font-size:small">[1] You can delete a variable after you no longer need it, which will free up some memory. </div><div style="font-size:small"><br></div><div style="font-size:small"><div>no = aa[:]->no</div><div>no_avg=dim_avg_n(no,0)</div><div>delete(no)</div><div>no2 = aa[:]->no2</div><div>no2_avg=dim_avg_n(no2,0)</div><div>delete(no2)</div><div>oli = aa[:]->oli</div><div>oli_avg=dim_avg_n(oli,0)</div><div>delete(oli)</div><div><br></div><div>[2] You can use [/.../] to delete several variables at once, if you don't need to free them individually:</div><div><br></div><div><div>no = aa[:]->no</div><div>no_avg=dim_avg_n(no,0)</div><div>no2 = aa[:]->no2</div><div>no2_avg=dim_avg_n(no2,0)</div><div>oli = aa[:]->oli</div><div>oli_avg=dim_avg_n(oli,0)</div><div>delete([/no,no2,oli/])</div><div><br></div><div>[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:</div><div><br></div><div><div>no_avg = dim_avg_n(aa[:]->no,0)<br></div><div>no2_avg= dim_avg_n(aa[:]->no2,0)<br></div><div>oli_avg = dim_avg_n(aa[:]->oli,0)<br></div><div><br>[4] Since you are doing the same operation repeatedly, you can use a do loop and save yourself some more memory.</div></div></div></div><div style="font-size:small"><br></div><div><div>vars_to_avg = (/"no","no2","oli","csl","olt","xyl","ald","hc8",\</div><div> "hcho","ol2","tol","hc5","hc3","ket"/)</div><div>nvars = dimsizes(vars_to_avg)</div><div><br></div><div>;---Average the first one and store to variable</div><div>Mpan_avg = dim_avg_n(aa[:]->$vars_to_avg(0)$,0)</div><div><br></div><div>;---Loop thru rest of vars and sum their averages</div><div>do nv=1,nvars-1</div><div> Mpan_avg = Mpan_avg + dim_avg_n(aa[:]->$vars_to_avg(nv)$,0)</div><div>end do </div><div>Mpan_avg=Mpan_avg*1000</div><div>Mpan_avg2D=Mpan_avg(0,:,:)</div><div>. . .</div><div><br></div><div>The above is not tested, so hopefully there are no syntax errors in it.</div><div><br></div><div>--Mary</div><div><br></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Nov 28, 2018 at 2:49 AM Komal Shukla <<a href="mailto:komalshukla1992@gmail.com" target="_blank">komalshukla1992@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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" target="_blank">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<div><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> <div>Thanks<div>Kim</div></div></div></div>
_______________________________________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br>
</blockquote></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div>