! Transforms:
!  DSG_ERDDAP_Tao5daySst.nc  timeseries example
!  simple_traj_dsg.nc trajectories with a varialbe having gaps (can test timeseries ops too)
!  profile_dsg.nc has gaps in the variable pot_temp_degc


!!!!!!!!!!!!
! NGD, NBD  MIN, MAX

use DSG_ERDDAP_Tao5daySst  
list/m=1:5 rowsize, T_25[t=@ngd], T_25[t=@nbd], T_25[t=@min], T_25[t=@max]

let tmin = T_25[t=@min]
let tmax = T_25[t=@max]

list rowsize[e=@min], rowsize[e=@max]
list tmin[e=@min], tmax[e=@max]

! Which tseries have some bad data?

LET mask = if T_25[t=@nbd] GT 0 then 1
set data/fmask=mask DSG_ERDDAP_Tao5daySst

list station, rowsize, T_25[t=@ngd], T_25[t=@nbd]

can data/all
use simple_traj_dsg
list rowsize, sstgap[i=@ngd], sstgap[i=@nbd], sstgap[i=@min], sstgap[i=@max]

! good+bad is now less than rowsize - based on # in the subset

list/y=15:20 rowsize, sstgap[i=@ngd]+sstgap[i=@nbd], sstgap[i=@ngd], sstgap[i=@nbd], sstgap[i=@min], sstgap[i=@max]

can data/all
use WestCoastCarbon_9d9c_f562_77b5_5f8e_5d19_7b1d

list rowsize, pco2[z=@ngd], pco2[z=@nbd], pco2[z=@min], pco2[z=@max]


!!!!!!!!!!!!
! FILLERS 
! FLN, FAV, FNR
! Filling ends and restarts with each new feature

cancel data/all

! IN X
use simple_traj_dsg
list sstgap, sstgap[x=@fln], sstgap[x=@fav], sstgap[x=@fnr]

use/feature=none simple_traj_dsg
list sstgap, sstgap[x=@fln], sstgap[x=@fav], sstgap[x=@fnr]

! In T

use DSG_ERDDAP_Tao5daySst 

let miss = if t_25 gt 28.4 then t_25
list/m=2/t=15-jan-2017:15-mar-2017  t_25, miss, miss[t=@fln], miss[t=@fav], miss[t=@fnr]

! In Z
! Write computed var in DSG netCDF file

use profile_dsg
let/like=sal nobig = if sal le 35 then sal

save/clobber/file=a.nc sal
save/append/file=a.nc nobig
can data profile_dsg; cancel var/all

use a
list/m=3 nobig, nobig[z=@fln]
plot /thick/line/sym nobig
plot /thick/line/sym nobig[z=@fln]

use WestCoastCarbon_9d9c_f562_77b5_5f8e_5d19_7b1d
list/m=20:21 pco2, pco2[z=@fav], pco2[z=@fln], pco2[z=@fnr]


!!!!!!!!!!!!
! SMOOTHERS

use simple_traj_dsg
list sst, sst[x=@sbx], sst[x=@swl], sst[x=@shn], sst[x=@spz] 

use DSG_ERDDAP_Tao5daySst 
list/m=62  t_25, t_25[t=@sbx], t_25[t=@swl], t_25[t=@shn], t_25[t=@spz]


use DSG_ERDDAP_Tao5daySst
plot/m=2/thick t_25
plot/over/color=red/thick/m=2/nolab t_25[t=@sbx]
plot/over/color=blue/thick/m=2/nolab t_25[t=@swl]
plot/over/color=green/thick=3/m=2/nolab t_25[t=@shn]
plot/over/color=black/line/sym=19/siz=.12/m=2/nolab t_25[t=@spz]

use WestCoastCarbon_9d9c_f562_77b5_5f8e_5d19_7b1d
list/m=1 pco2, pco2[z=@sbx], pco2[z=@swl], pco2[z=@shn]


!!!!!!!!!!!!
! DIFFERENCES

use simple_traj_dsg
list sst, sst[x=@ddc], sst[x=@ddb], sst[x=@ddf]

use DSG_ERDDAP_Tao5daySst 
list/m=62  t_25, t_25[t=@ddc], t_25[t=@ddb], t_25[t=@ddf]


use WestCoastCarbon_9d9c_f562_77b5_5f8e_5d19_7b1d
list/m=3:4 pco2, pco2[z=@ddc], pco2[z=@ddb], pco2[z=@ddf]

cancel data/all


!!!!!!!!!!!!
! AVERAGES and  INTEGRALS

! trajectory
use simple_traj_dsg.nc

list sst[x=@ave]
list sst[x=@sum] , sst[x=@sum]/rowsize

list/m=3 sst[x=@iin], sst[x=@rsum]

list rowsize[e=@sum]

use profile_dsg
list sal[z=@ave]
list sal[z=@sum], sal[z=@sum]/rowsize
list sal[z=@din]
list sal, sal[z=@iin], sal[z=@rsum]


list sal, sal[z=@iin], sal[z=@rsum]

let s_ave = sal[z=@ave]
list s_ave[e=@sum]

use DSG_ERDDAP_Tao5daySst
sh dat
list/m=1:5 t_25[T=@ave]
list/m=1:5 t_25[T=@sum], t_25[T=@sum]/rowsize
list/m=1:5 t_25[T=@din]
list/m=30:31/t=1-jan-2017:15-feb-2017 t_25, t_25[t=@rsum], t_25[t=@iin]


cancel data/all


!!!!!!!!!!!!
! WEIGHTS
! @WGT weights for ave, integration

use simple_traj_dsg
list sst[x=@wgt], 1000* earth_distance(lons, lats)

list sst[x=@ave]

cancel data/all
! note variable "time" doesn't have a missing-data flag so the last column
! for some reason shows NaN at the end of this listing.
use DSG_ERDDAP_Tao5daySst 
list/m=62  t_25, t_25[t=@wgt], time[t=@ddf]

use WestCoastCarbon_9d9c_f562_77b5_5f8e_5d19_7b1d
list/m=3:4 pco2, pco2[z=@wgt], press_db[z=@ddf]

cancel data/all

!!!!!!!!!!!!
! EVENT MASK

use simple_traj_dsg
list sst, sst[x=@evn:20], sst[x=@evn:24]

use DSG_ERDDAP_Tao5daySst.nc
let/title="Where station is 5n155w, 8n95w"  mask = element_index_str (station, {"5n155w", "8n95w"})
set data/fmask=mask DSG_ERDDAP_Tao5daySst
list t_25, t_25[t=@evn:27.4]


use WestCoastCarbon_9d9c_f562_77b5_5f8e_5d19_7b1d
list/m=3:4 pco2, pco2[z=@evn:1000], pco2[z=@evn:500]


!!!!!!!!!!!!
! CIA, CIB, CDA, CDB not implemented at this time.
! issue a note and just do the operation on the full list

use simple_traj_dsg
list sstgap, sstgap[x=@cda],  sstgap[x=@cdb],  sstgap[x=@cia],  sstgap[x=@cib]

