# [ncl-talk] removing ensemble mean from original data

Sri Nandini bax8609 at uni-hamburg.de
Mon Jun 15 05:16:05 MDT 2020

```Hello dear ncl-users,

I would like to know how to remove the ensemble mean from the
corresponding time of each ensemble member, the method i used is not
correct as it performs time average and i want to do ensemble average.
The idea is to calculate ensemble mean each year (this also removes the
seasonal cycle).

my original data is x=  time | 240] x [ens | 100] x [lat | 45] x [lon | 90]

;First method

dimx = dimsizes(x)
ntim = dimx(0)          ; 240
nens = dimx(1)          ; 100
nlat = dimx(2)          ; 45
mlon = dimx(3)          ; 90

x at _FillValue = -9.96921e+36
ens_mean1=dim_avg_n_Wrap(x,1)
printVarSummary(ens_mean1)        ;[time | 240]  x [lat | 45] x [lon
| 90]

x1 = x
do n=0,nens-1
hist_anom1(:,n,:,:) = hist_anom(:,n,:,:) - ens_mean1
end do

;=second
method=================================================================
; Compute  monthly anomalies from the mean climatology to remove
seasonal cycle or to use
; rmMonAnnCycTLL (x) Removes the annual cycle from monthly data and
subtracts the long term means from each month.
;==================================================================

xClm  = clmMonTLLL(x)                                ; climatologies
for each ensemble member
printVarSummary(xClm)                                ; [month | 12]
x [ens | 100] x [lat | 45] x [lon | 90]
printMinMax(xClm,0)                                  ;   :
min=-226.887   max=161.211cm
print("============")

xAnom = calcMonAnomTLLL(x,xClm)                     ; anomalies from
each ensemble member climatology
printVarSummary(xAnom)                              ; [time | 240] x
[ens | 100] x [lat | 45] x [lon | 90]
printMinMax(xAnom,0)                                ;
min=-0.737722   max=0.83556
print("============")

;3rd method
;==================================================================
xClmMonEns = new((/nmos,nlat,mlon/), typeof(x), x at _FillValue)
do nmo=0,nmos-1
work := reshape(x(nmo::nmos,:,:,:) ,(/nyrs*nens,nlat,mlon/))
xStat = dim_stat4_n(work,0 )       ; (4,nlat,mlon)
xClmMonEns(nmo,:,:) = xStat(0,:,:)
end do
; meta data
xClmMonEns at long_name = "Monthly Climatology"
xClmMonEns at LONG_NAME = "Monthly Climatology over all Ensemble Members"
xClmMonEns at units     = x at units
copy_VarCoords(x(0,0,:,:), xClmMonEns(0,:,:))
xClmMonEns!0 = "month"
printVarSummary(xClmMonEns)
print("-----")
printMinMax(xClmMonEns,0)

;==================================================================
;  Remove the ensemble monthly climatology from each ensemble member
;==================================================================
printVarSummary(x)
printMinMax(x,0) print("===")

xAnom = x
do nt=0,ntim-1
nmo = nt%12
do ne=0,nens-1
xAnom(nt,ne,:,:) = (/ x(nt,ne,:,:) - xClmMonEns(nmo,:,:) /) ;
anomaly array
end do
end do
xAnom at long_name = "Anomalies from Climatology"

printVarSummary(xAnom)
printMinMax(xAnom,0)
print("===")

On 6/12/2020 2:06 PM, Rashed Mahmood wrote:
>
> In that case, my suggestion in previous message should work.
>
> Rashe
> On Fri, Jun 12, 2020 at 1:55 PM Sri nandini <bax8609 at uni-hamburg.de
> <mailto:bax8609 at uni-hamburg.de>> wrote:
>
>     Thank you.
>
>     I wish to remove the ensemble mean from each corresponding time of
>     each ensemble.
>
>     Sri
>
>     On 12.06.20 13:48, Rashed Mahmood wrote:
>>     If you just want to remove ensemble mean then you could do the
>>     following:
>>
>>     x_new      = x
>>     ens_mean = dim_avg_n_Wrap(x,1)
>>     do n=0,nens-1
>>        x_new(:,n,:,:) = x(:,n,:,:) - ens_mean
>>     end do
>>
>>     However, please make sure that this is what you want!, it is up
>>     to you to decide and know what you are trying to accomplish here.
>>     Your variable names suggest that there could be some confusion...
>>
>>     Cheers,
>>     Rashed
>>
>>
>>
>>
>>
>>     On Fri, Jun 12, 2020 at 12:50 PM Sri nandini via ncl-talk
>>     <ncl-talk at mailman.ucar.edu <mailto:ncl-talk at mailman.ucar.edu>> wrote:
>>
>>         Hello dear ncl-users,
>>
>>         I want to know the best way to remove the ensemble mean from the
>>         corresponding time of each ensemble member. Can someone tell
>>         me if the
>>         following code is correct? i do not get any error but i
>>         wanted to be
>>         sure of the method.
>>
>>
>>         Sri
>>
>>         ;==================================================================
>>         ;  Remove the ensemble mean from each ensemble member
>>         ;==================================================================
>>             dimx = dimsizes(x)
>>             ntim = dimx(0)          ; 240
>>             nens = dimx(1)          ; 100
>>             nlat = dimx(2)          ; 45
>>             mlon = dimx(3)          ; 90
>>
>>             nmos = 12
>>             nyrs = ntim/nmos         ; 21
>>
>>             printVarSummary(x)       ;[time | 240] x [ens | 100] x
>>         [lat | 45] x [lon | 90]
>>             ens_mean=dim_avg_n_Wrap(x,1)
>>             printVarSummary(ens_mean);[time | 240]  x [lat | 45] x
>>         [lon | 90]
>>
>>              detrended_x = x
>>              do nt=0,ntim-1
>>                nmo = nt%12
>>               do ne=0,nens-1
>>                  detrended_x(nt,ne,:,:) = (/ x(nt,ne,:,:) -
>>         ens_mean(nmo,:,:) /) ; detrended array
>>               end do
>>             end do
>>             detrended_x at long_name = “Ensemble mean removed”
>>
>>             printVarSummary(detrended_x)
>>            [time | 240] x [ens | 100] x [lat | 45] x [lon | 90]
>>
>>         _______________________________________________
>>         ncl-talk mailing list
>>         ncl-talk at mailman.ucar.edu <mailto:ncl-talk at mailman.ucar.edu>
>>         List instructions, subscriber options, unsubscribe:
>>         https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20200615/9a17d882/attachment.html>
```