calculate temporal average of a variable field only for a certain value of other field
Added by joao ferreira about 5 years ago
Hi,
I have two variables merged in a file:
msl(time,latitude,longitude)
and
tipo(time)
I want to make the temporal average of msl, obtaining:
msl_avg(latitude,longitude) but only for the instants where tipo=5 (for example…).
How can I do this?
I'm sending part of the file…
Regards
Joao
Replies (3)
RE: calculate temporal average of a variable field only for a certain value of other field - Added by Karin Meier-Fleischer about 5 years ago
Hi Joao,
if I understand you correctly, this is what you want to do: select only the timesteps where tipo is equal 5
and then do a field average on msl only for that timestep. In my opinion there is no simple way but you can
write a short shell script.
Here is a Korn Shell example script which computes the fldmean for the three timesteps where tipo is equal 5,
writes each to a single file and merge all together at the end.
#!/bin/ksh input="merged.nc" rm -rf tmp_*.nc ntime=$(cdo -s ntime merged.nc) for ((i=1; i<=$ntime; i++)) ; do tipo=$(cdo -s -outputint -selname,tipo -seltimestep,${i} $input) if [[ $tipo -eq 5 ]]; then echo "-- ${i}: tipo == 5 fldmean" cdo -s -fldmean -selname,msl -seltimestep,${i} $input tmp_${i}.nc else echo "-- ${i}: tipo != 5" fi done cdo -O mergetime tmp_*.nc outfile.nc exit
-Karin
RE: calculate temporal average of a variable field only for a certain value of other field - Added by joao ferreira about 5 years ago
Hi!!
Thanks for your answer… Instead of field average I want a time average on all gridpoints but only for those timesteps where tipo=5
Meanwhile I found an elegant and compact way to do it:
cdo expr,"var=msl/(tipo==5)" input output -> it populates "var" with the original msl for the times where tipo=5 and NaN for tipo diferent from 5
cdo timmean output output_mean -> it calculates the time mean not taking into account the times where msl is NaN
Regards
Joao
RE: calculate temporal average of a variable field only for a certain value of other field - Added by Karin Meier-Fleischer about 5 years ago
Sorry, I missed that word 'temporal'. To make it perfect combine the two cdo calls.
cdo -timmean -expr,"var=msl/(tipo==5)" merged.nc outfile2.nc