! bn_regrid_to_dsg_traj.jnl

! Create gridded data file: values are based on latitude/longitudes
! test regridding to a DSG trajectory grid
! Change def of gridded data for more variation

define axis/t=17-may-2019:20-may-2019:1/units=hours tfake
define axis/x=-168:-165:0.2/units=degrees_east xfake
define axis/y=54:57:0.2/units=degrees_north yfake
define grid/x=xfake/y=yfake/t=tfake gfake
let/title="Gridded Temperature"/Units="Degrees_C" temp = 2*y[g=gfake] + 0.8*x[g=gfake] + 0*t[g=gfake]
save/clobber/file=fake_model_temp.nc/outtype=float temp
can var/all

use sail_2019.nc
use fake_model_temp.nc

set v ul
plot/thick temp[d=2,g=temp_ctd_rbr_mean[d=1]]
LET/title="Temp regridded from gridded to DSG" temp_from_grid = temp[d=2,g=temp_ctd_rbr_mean[d=1]]

set v ur
plot/thick temp_from_grid

! test modulo in X
define axis/t=17-may-2019:20-may-2019:1/units=hours tfake
define axis/x=192:195:0.2/units=degrees_east xfake
define axis/y=54:57:0.2/units=degrees_north yfake
define grid/x=xfake/y=yfake/t=tfake gfake
let/title="Gridded Temperature"/Units="Degrees_C" temp = 2*y[g=gfake] + 0.8*x[g=gfake] + 0*t[g=gfake]
save/clobber/file=fake_model_temp.nc/outtype=float temp
can var/all

cancel data/all
! make sure to use the new one
use sail_2019.nc
use fake_model_temp.nc

say `temp,r=size`

set v ll
plot/thick/title="Modulo Longitudes" temp[d=2,g=temp_ctd_rbr_mean[d=1]]

set v lr
LET/title="Regrid to DSG w/ moduloing" temp_from_grid = temp[d=2,g=temp_ctd_rbr_mean[d=1]]
plot/thick temp_from_grid
frame/file=regrid_to_dsg_traj.gif

! finer grid, do we get scatter/gather?
can dat/all
define symbol save_mem = ($ferret_memory)
set mem/siz=2mb

use sail_2019.nc 

define axis/t=17-may-2019:20-may-2019:1/units=minutes tbig

define axis/x=0:359:.5/unit=degrees/modulo xbig
define axis/y=54:57:0.2/units=degrees_north ybig
define grid/x=xbig/y=ybig/t=tbig gbig
let big_temp = 2*y[g=gbig] + 0*(x[g=gbig]+t[g=gbig])

say `big_temp,r=size`

load big_temp[g=temp_ctd_rbr_mean[d=1]]
show mem/diag

set mem/siz=($save_mem)


! Finally, trajectory datasets can have depths. 
! Synthesize a depth variable in a trajectory dataset.
sp cp data/sail_2019.nc sail_copy.nc
use sail_2019.nc

! Good test of a dsg-variable made up of several expressions
let/units=meters/title="Depth synthesized" depth = 1 + 3*(if temp_ctd_rbr_mean then temp_ctd_rbr_mean-6 else .2)
define attribute/output depth.axis = "Z"
set attribute/output depth.axis
save/append/file=sail_copy.nc depth

can dat/all; use sail_copy
show dat
show att depth


! make a 4D grid on the region.
define axis/t=17-may-2019:20-may-2019:1/units=hours tfake
define axis/x=-168:-165:0.2/units=degrees_east xfake
define axis/y=54:57:0.2/units=degrees_north yfake
define axis/y=54:57:0.2/units=degrees_north yfake
define grid/x=xfake/y=yfake/t=tfake gfake
define axis/z=1:5:.5/units=meters zfake
define grid/x=xfake/y=yfake/t=tfake/z=zfake gfake
let/title="Gridded Temperature"/Units="Degrees_C" temp = 2*y[g=gfake] + 0.8*x[g=gfake] + 0.2*z[gz=zfake] + 0*t[g=gfake]
save/clobber/file=fake_model_temp.nc/outtype=float temp
can var/all

use sail_copy.nc

use fake_model_temp
sh dat
LET/title="Temp regridded from gridded to DSG" temp_from_grid = temp[d=2,g=temp_ctd_rbr_mean[d=1]]
save/clobber/file=sample_xyzt_to_traj.nc temp_from_grid


sp rm sail_copy.nc
