[ncl-talk] how to Calculate Seasonal average for a variable for different years

Will Hobbs will.hobbs at utas.edu.au
Fri Sep 13 10:13:14 MDT 2019


Adriana

There’s a number of ways of doing this that are probably more efficient. You can preselect the files that you’re opening, and then use cd_calendar() to select the days for each year’s seasonal average. (Note that this approach would need some alteration for ‘DJF’ or ‘NDJ’, where the 3 months are across different years

year1 = 2005
year2 = 2007
mois = (/6,7,8/)    ;You only want JJA right – you could edit this to deal with different seasons

file1 := systemfunc(" ls /PROJETS/MOUSSON-NEW/MAM7_CMIP6_CAMS/MAM7_test10_MERRA2.cam.h0.????-{"+str_join(mois,”,”)+"}-*.nc")   ;only selects files for the appropriate months

fi = addfile(file1,”r”)  ;default ListSetType is “cat” ; don’t need to alter this

time = cd_calendar(fi[:]->time, 0)   ; creates array split into (year, month, day,…etc)

data = fi[:]->$dataName$   ; I don’t know what varaibles you’re reading., you didn’t explain in your email. I’m assuming it’s dimensioned (time, lev, lat ,lon) though

year = ispan(year1,year2,1)
nyr  = dimsizes(year)
dataSeas = data(:nyr-1,:,:,:)    ; new array for seasonal means
dataSeas&time := year

do yy = year1, year2

  ii = ind(time(:,0).eq.yy.and.any(time(:.1).eq.mois))   ;selects time indices of desired month/year
  dataSeas({yy},:,:,:) = dim_avg_n(data(ii,:,:,:), 0)         ;average of those time indices

end do




From: ncl-talk <ncl-talk-bounces at ucar.edu> on behalf of Adriana via ncl-talk <ncl-talk at ucar.edu>
Reply-To: "adriana.bossolasco at cnrs-orleans.fr" <adriana.bossolasco at cnrs-orleans.fr>
Date: Friday, 13 September 2019 at 7:47 AM
To: "ncl-talk at ucar.edu" <ncl-talk at ucar.edu>
Subject: [ncl-talk] how to Calculate Seasonal average for a variable for different years

Hi everyone

I have different .nc daily files and I want to calculate the seasonal
(JJA) for several year and save in one variable that contain all the
years for this three months. I did one code ncl that works for one year
but after I don't realize how to continue. I think the problem is
because I add time dimension after using dim_avg_n and the index doesn't
change. I will put part of my code to be clear


year1=2005
year2=2007
nyear=year2-year1

month1=6
month2=8
nmonths=month2-month1+1

do i = 1,nyear
y= i+year1-1
do j = 1,nmonths
print(nmonths)
if (month1+j-1 .ge. 10) then
mois=10
else
mois="0"+(j-1+month1) ; ver aqui
end if
printVarSummary(mois)


file1 := systemfunc(" ls
/PROJETS/MOUSSON-NEW/MAM7_CMIP6_CAMS/MAM7_test10_MERRA2.cam.h0."+y+"-"+mois+"-*.nc")

print(file1)
setfileoption("nc","SuppressClose", False)
f = addfiles(file1,"r")

ListSetType (f, "cat")


After I read different variables and for calculate the seasonal average
I do the next:

;------------------
if (j .eq. 1) then ;for JUN
CO_press_JUN = new ((/60*nyear,nlev,nlat,nlon/),"float",CO_2 at _FillValue)
CO_press_JUN(60*(i-1):60*(i)-1,:,:,:) = CO_2
CO_JUN_avg = CO_press_JUN(0:0,:,:,:)
CO_JUN_avg(0,:,:,:) = (/ dim_avg_n(CO_press_JUN,0)/)
printMinMax(CO_JUN_avg, True)
printVarSummary(CO_JUN_avg)


if (j .eq. 2) then ; for JUL
CO_press_JUL = new ((/62*nyear,nlev,nlat,nlon/),"float",CO_2 at _FillValue)
CO_press_JUL(62*(i-1):62*(i)-1,:,:,:) = CO_2
CO_JUL_avg = CO_press_JUL(0:0,:,:,:)
CO_JUL_avg(0,:,:,:) = (/dim_avg_n(CO_press_JUL,0)/)
printMinMax(CO_JUL_avg, True)
printVarSummary(CO_JUL_avg)

if (j .eq. 3) then ; For AUG
CO_press_AUG = new ((/62*nyear,nlev,nlat,nlon/),"float",CO_2 at _FillValue)
CO_press_AUG(62*(i-1):62*(i)-1,:,:,:) = CO_2
CO_AUG_avg = CO_press_AUG(0:0,:,:,:)
CO_AUG_avg(0,:,:,:) = (/dim_avg_n(CO_press_AUG,0)/)
printMinMax(CO_AUG_avg, True)
printVarSummary(CO_AUG_avg)

end do ; boucle mois
;---------

Now I want to create a variable
CO_JJA = new((/nyear,nlev,nlat,nlon/),"float")
which contain the number of year and the values CO_JUN_avg, CO_JUL_avg,
CO_AUG_avg of each year
However I didn't arrive to this because I think that the problem is due
to this CO_AUG_avg = CO_press_AUG(0:0,:,:,:) and the index is always 0.

I'm stuck with this! Can anyone give me any advice or help?

I really appreciate

Thank you in advance

Adriana
--
Adriana Bossolasco
LPC2E UMR 7328 (CNRS, Université d'Orléans)
3A avenue de la Recherche Scientifique
45071 Orléans Cedex 2 - FRANCE
Tel +33 (0)2 38 25 52 85
e-mail: adriana.bossolasco at cnrs-orleans.fr
_______________________________________________
ncl-talk mailing list
ncl-talk at ucar.edu
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk<http://mailman.ucar.edu/mailman/listinfo/ncl-talk>


University of Tasmania Electronic Communications Policy (December, 2014).
This email is confidential, and is for the intended recipient only. Access, disclosure, copying, distribution, or reliance on any of it by anyone outside the intended recipient organisation is prohibited and may be a criminal offence. Please delete if obtained in error and email confirmation to the sender. The views expressed in this email are not necessarily the views of the University of Tasmania, unless clearly intended otherwise.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20190913/03cbbcf5/attachment.html>


More information about the ncl-talk mailing list