DEFINE AXIS/T0="1-jan-1970"/EDGES/UNITS=days tmnth = DAYS1900(year,month,1) - offset1970

! Count of cruises with some data in each XYT cell
   ! Send a scalar as argument 1 to just count the cruises
   LET var = 1
   LET/UNITS="count"/TITLE="Number of cruises" \
     count_ncruise = TRACKS2GRID_AVE_XYT(var,LON,LAT,DATE,CRUISE_NO,X[GX=xlon],Y[GY=ylat],T[GT=tmnth])
   
   SAVE/($file_qual)/FILE="($gridded_outfile)" count_ncruise
   sh axis tmnth
   DEFINE SYMBOL file_qual = APPEND

! Now count all of observations in each XYT cell
   LET var = fco2_rec
   LET/Units="count"/TITLE="Number of obs" \
     count_nobs = SCAT2GRID_NBIN_XYT(LON,LAT,DATE,var,X[GX=xlon],Y[GY=ylat],T[GT=tmnth])
   
   SAVE/APPEND/FILE="($gridded_outfile)" count_nobs
   sh axis tmnth

! Mean by cruise of Fco2_rec, with some data in each XYT cell
   LET var = fco2_rec
   LET/UNITS="umol"/TITLE="fco2 mean - per cruise weighted" \
    FCO2_AVE_WEIGHTED = TRACKS2GRID_AVE_XYT(VAR,LON,LAT,DATE,CRUISE_NO,X[GX=xlon],Y[GY=ylat],T[GT=tmnth])
   
   SAVE/APPEND/FILE="($gridded_outfile)" fco2_ave_weighted
   sh axis tmnth


! Mean fco2_rec, all observations in each XYT cell
   LET/UNITS="umol"/TITLE="fco2 mean - unweighted all obs" \
     FCO2_AVE_UNWTD = SCAT2GRID_BIN_XYT(LON,LAT,DATE,fco2_rec,X[GX=xlon],Y[GY=ylat],T[GT=tmnth])
   
   SAVE/APPEND/FILE="($gridded_outfile)" fco2_ave_unwtd
   sh axis tmnth

! Min and max fco2.  These are computed from one function with min at k=1, max at k=2.
! Use the Z=1:1@ave to remove the Z axis from the grid of the variable on output.
   LET minmax = SCAT2GRID_MINMAX_XYT(lon, lat, date, fco2_rec, x[gx=xlon], y[gy=ylat], t[gt=tmnth])
   LOAD minmax

   LET/UNITS=umol/TITLE="fco2 min" fco2_min_unwtd = minmax[z=1:1@ave]
   SAVE/APPEND/FILE="($gridded_outfile)" fco2_min_unwtd
   sh axis tmnth

   LET/UNITS=umol/TITLE="fco2 max" fco2_max_unwtd = minmax[z=2:2@ave]
   SAVE/APPEND/FILE="($gridded_outfile)" fco2_max_unwtd

   sh axis tmnth

! Variance by cruise of Fco2_rec, with some data in each XYT cell
   LET fvar = fco2_rec

   LET/UNITS="umol"/TITLE="fco2 std dev - per cruise weighted" \
    fco2_std_weighted = tracks2grid_std_xyt(fvar,lon,lat,\
    date,cruise_no,x[gx=xlon],y[gy=ylat],t[gt=tmnth])

   SAVE/APPEND/FILE="($gridded_outfile)" fco2_std_weighted
   sh axis tmnth
   

! unweighted std dev fco2
   LET/UNITS=umol/TITLE="fco2 std dev - unweighted all obs" \
     fco2_std_unwtd = SCAT2GRID_STD_XYT(lon, lat, date, fco2_rec, x[gx=xlon], y[gy=ylat], t[gt=tmnth])
   SAVE/APPEND/FILE="($gridded_outfile)" fco2_std_unwtd
   sh axis tmnth


! Mean delta-latitude of all observations in each XYT cell
   LET nearest_lat = IF lat GE 0 THEN (INT(lat) + 0.5) ELSE (INT(lat) - 0.5)
   LET y_from_lat = (lat - nearest_lat)
   LET/UNITS="Deg N"/TITLE="Latitude average offset from cell center" \
     lat_offset_unwtd = SCAT2GRID_BIN_XYT(LON,LAT,DATE,y_from_lat,X[GX=xlon],Y[GY=ylat],T[GT=tmnth])
   
   SAVE/APPEND/FILE="($gridded_outfile)" lat_offset_unwtd

! Mean delta-longitude of all observations in each XYT cell
   LET nearest_lon = IF lon GE 0 THEN (INT(lon) + 0.5) ELSE (INT(lon) - 0.5)
   LET x_from_lon = (lon - nearest_lon) 
   LET/UNITS="Deg E"/TITLE="Longitude average offset from cell center" \
     lon_offset_unwtd = SCAT2GRID_BIN_XYT(LON,LAT,DATE,x_from_lon,X[GX=xlon],Y[GY=ylat],T[GT=tmnth])
   
   SAVE/APPEND/FILE="($gridded_outfile)" lon_offset_unwtd
   sh axis tmnth
