# [ncl-talk] Calculate Average value for a number of files

Jack Ogaja jack.ogaja at gmail.com
Tue Nov 24 08:35:33 MST 2015

```Dear Ioannis,

You can still use “dim_avg_n” function, you just need to reorganise your code, and I don’t think another loop is necessary.

My suggestions:

[1] Retrieve the variables from all the files at once(hoping you won’t experience any memory problem).
;****
it = -1 ;<== this retrieves the time dimension as well
dust1  = wrf_user_getvar(fS,"DUST_1",it)
dust2  = wrf_user_getvar(fS,"DUST_2",it)
dust3  = wrf_user_getvar(fS,"DUST_3",it)
dust4  = wrf_user_getvar(fS,"DUST_4",it)
dust5  = wrf_user_getvar(fS,"DUST_5",it)
;****

[2] Do most data manipulations outside the loops.
;****
dust_J   = dim_sum_n((/dust1,dust2,dust3,dust4,dust5/),0)*density
dust_tot = dust_J(:,level,:,:)
;****

[3] Calculate the files averages(hoping I understood what you want to do)
;****
n1=0  ;<=== Set to suit your needs
n2=2
do ifil = 0,numFILES-1

if (ifil.eq.n2) then
dust_avg = dim_avg_n(dust_J(n1:n2,level,:,:),0)
print(" average of file|" + n1 + "| and file|" + n2 + "| calculated...")
n1=n1+2
n2=n2+2
end if
;****
;….and then write to the output file(whichever way you wish)

;****
data(npt) = data(npt) + sprintf("%7.3f ", dust_tot(ifil,ml,nl))
if (ifil.eq.n2-2) then ;<== write the averages after every other column
data(npt) = data(npt) + sprintf("%7.3f ", dust_avg(ml,nl))
end if

end do
;****

See the attached script. Test it with your data files. If it doesn’t solve the problem as always write again to ncl-talk.

Jack

> On 24 Nov 2015, at 12:31, Ioannis Koletsis <koletsis at meteo.noa.gr> wrote:
>
> Dear Jack,
>
> thanks again for your attention….
>
> I have already made a script which the dust_tot for the zero level (level=0) was estimated and wrote down the lat, lon and the dust_tot for each grid point (see attached files at the previous email readwrite_dust_all.ncl, with the output file All_Dust.txt (https://www.dropbox.com/s/g8yscu2qvj3muuk/All_Dust.txt?dl=0 <https://www.dropbox.com/s/g8yscu2qvj3muuk/All_Dust.txt?dl=0> ))….
>
> I have to clarify that I want the average value of the produced columns of each file that is calculated from your latest version and the script readwrite_dust_all.ncl of mine….
>
> So in order to be clear, the revised script calculates the dust_tot value for each grid point and for the first file (wrfoutput) which is written into the 4th column.
> The next dust_tot value for the next file is written into the 5th column, and etc
>
> I would like to calculate the average value of the 4th and 5th columns (for 2 wrfoutput files) for each grid point….
> The problem is that I cannot use the dim_avg_n because the dust_tot variable is 2d, one value for lat, lon of the dimensions….
> May be a new do loop or the modification of the existence loop is the solution….
>
> I hope to clarify the aim of the desired script…
>
> I apologise for the inconvenience and the possible waste of your time….
>
> Best regards
> Ioannis
>
>
>
> From: ncl-talk-bounces at ucar.edu <mailto:ncl-talk-bounces at ucar.edu> [mailto:ncl-talk-bounces at ucar.edu <mailto:ncl-talk-bounces at ucar.edu>] On Behalf Of Jack Ogaja
> Sent: Monday, November 23, 2015 8:07 PM
> To: Ioannis Koletsis
> Cc: ncl-talk [ncl-talk]
> Subject: Re: [ncl-talk] Calculate Average value for a number of files
>
> Ioannis,
>
> Sorry, I oversaw one point in your script, the "density weighted sum”. However,
>> I would like to calculate the average value for big number of files wrfoutput which it seems to be infeasible with the revised version of the script (readwrite_dust_all_2_Jack….
> for which dimension do you want to calculate the average? time? level? It was not clear in your first email and unfortunately it is still not clear in your recent email what you mean by “average”. If the rest of your files have been written at each time step as it seems to be with the two files you uploaded, then the only possible average will be in “levels” if you want the output as indicated in the last email.
>
> Sure, you wouldn’t use “readwrite_dust_all_2_Jack…” script to read multiple files. In this case you need the “addfiles” function. Please, find attached a modification of your latest script. Unlike the earlier script, I have stayed as close as possible to the original coding and I have only added “addfiles” and “dim_sum_n” functions to your script. You can read more about the functions in the documentation.
>
> Assuming you mean to calculate average in the vertical direction, just switch between
> ;==
>    dust_tot = dust_J(level,:,:)
> ;==
> and
> ;==
>     ;+dust_tot = dim_avg_n(dust_J,0)
> ;==
> in lines 54 and 56 respectively, otherwise for the two data files you uploaded, the modified script should give you the exact numbers as your original script(you should confirm it). If you add more files, the modified script should print to the output file multiple columns representing values from each file.
>
> If you need further clarification/help, don’t hesitate to write back to the ncl-talk.
>
> Jack
>
>
>
>  <https://www.avast.com/antivirus>
> Αυτό το e-mail ελέγχθηκε για ιούς από το πρόγραμμα Avast antivirus.
> www.avast.com <https://www.avast.com/antivirus>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151124/f30bab84/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Type: application/octet-stream
Size: 2894 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151124/f30bab84/attachment.obj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151124/f30bab84/attachment-0001.html
```