! compute cruiseDensity

! create a coarse resolution XYT grid that can be used to anticipate the
! volume of data in an arbitrary XYT region
 
SET MEM/SIZ=300

DEFINE AXIS/X=-179.5:179.5:1/UNITS=degrees_east/MODULO xlon
DEFINE AXIS/Y=-89.5:89.5:1/UNITS=degrees_north ylat

LET dx = `0.5* xbox[gx=xlon,i=1]`
LET dy = `0.5* ybox[gy=ylat,j=1]`

!Data contains lat,lon,date,data_id,cruise_id,cruise_no,fco2_rec
! USE "/home/data/socat/SOCAT_triples_fco2.nc"

 !USE "/home/data/socat/SOCAT_triples_AD_1.3_fco2.nc"

use  "/home/data/socat/SOCAT_triples_averaged_8_30_2011_AD_1.3_fco2.nc"

!SET AXIS/STRIDE=100 `lon,return=xaxis`

! Triples have time with origin at start of 1970.
LET offset1970 = DAYS1900(1970,1,1)
LET month = MOD(ii-1,12)+1
LET year = 1970 + INT((ii-1)/12)

! for entire time period of SOCAT
LET ii = i[i=1:480]

! Repeat and do the calculation for 5-year intervals.
LET i1 = 1
LET i2 = 61
LET del = 60

let imax = 457  ! through the end of 2007

DEFINE SYMBOL file_qual = CLOBBER  ! First time CLOBBBER, afterwards APPEND to the file.
DEFINE SYMBOL gridded_outfile = /home/data/socat/SOCAT_tracks_gridded_stats_AD.nc
DEFINE SYMBOL gridded_outfile = /home/data/socat/SOCAT_tracks_gridded_monthly.SOCAT_triples_averaged_8_30_2011_AD_1.3_fco2.nc

repeat/range=1:10:1 ( \
 LET ii = i[i=`i1`:`i2`]; \
 GO fco2_xyt_stats_year.jnl; \
 LET i1 = `i2`; \
 LET i2 = `i2+del`; \
 IF `i2 GT imax` THEN LET i2 = `imax`; \
 IF `i1 GE imax` THEN exit/loop; \
 )


! Add global attributes

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

