 ! Compute monthly climatologies of SOCAT gridded data.
 ! sum the counts
 ! average the fco2 averages
 ! min and max the fco2 min and max.

use "/home/data/socat/SOCAT_tracks_gridded_monthly.nc"


define symbol outfile = /home/data/socat/SOCAT_tracks_gridded_month_clim.nc
sp rm -f outfile

define symbol invar = count_ncruise  ! count_ncruise or ($invar)
define sym op = SUM

define symbol title_out=`($invar)[d=1],return=title`
define symbol units_out=`($invar)[d=1],return=units`

use climatological_axes
can data 2

let LL = l[gt=($invar)[d=1]]

if `tax_month(LL[L=1], LL) NE 1` THEN 
   say 'original time axis does not start in January'
   exit/program
endif
 
! For plotting the climatologies
define sym levsym=(v,0min)
keymark,1
set win/siz=0.5

! Sum for each month, adding to the dataset
repeat/range=1:12/name=m (let imon = `m`; go clim_month)
pause

! Now the same for count_nobs
define symbol invar = count_nobs  ! count_ncruise or count_nobs
define symbol op = SUM

define symbol title_out=`($invar)[d=1],return=title`
define symbol units_out=`($invar)[d=1],return=units`

! All months
repeat/range=1:12/name=m (let imon = `m`; go clim_month)
pause

! For FCO2 variables, average.
define symbol invar = fco2_ave_weighted
define symbol op = AVE

define symbol title_out=`($invar)[d=1],return=title`
define symbol units_out=`($invar)[d=1],return=units`

! All months
repeat/range=1:12/name=m (let imon = `m`; go clim_month)
pause

! For FCO2 variables, average.
define symbol invar = fco2_ave_unwtd
define symbol op = AVE

define symbol title_out=`($invar)[d=1],return=title`
define symbol units_out=`($invar)[d=1],return=units`

! All months
repeat/range=1:12/name=m (let imon = `m`; go clim_month)
pause

! For FCO2 min and max, use the min and max.
define symbol invar = fco2_min_unwtd
define symbol op = MIN

define symbol title_out=`($invar)[d=1],return=title`
define symbol units_out=`($invar)[d=1],return=units`

! All months
repeat/range=1:12/name=m (let imon = `m`; go clim_month)
pause

! For FCO2 min and max, use the min and max.
define symbol invar = fco2_max_unwtd
define symbol op = MAX

define symbol title_out=`($invar)[d=1],return=title`
define symbol units_out=`($invar)[d=1],return=units`

! All months
repeat/range=1:12/name=m (let imon = `m`; go clim_month)


! Add global attributes

let status = nco_attr("($outfile)", "global", "title", "c", "a",\
"SOCAT gridded v1.4 May 2011, QC-Flags A-D, WOCE-Flag 2 Monthly Climatology" )
load status
