%build_nc.m

clear;
dd = '2020Oct12';
sn = 'BOG2'
mpath = sprintf( '../../%s/%s/parsed',sn,dd);

fpcdf = sprintf('%s_%s.nc',sn,dd);
if exist(fpcdf, 'file')==2
  delete(fpcdf);
end

%load profile data
fpin = sprintf('%s/CPF%s_PROF.mat',mpath,dd);
load(fpin);

[CTDtime,GlobalMinTime,GlobalMaxTime]=mat2posix(CTDtime);
FLBBtime=mat2posix(FLBBtime);
O2time=mat2posix(O2time);


FLBBpressure = -FLBBpressure;
FLBBpressure(find(FLBBpressure==0))=NaN;
O2pressure = -O2pressure;
O2pressure(find(O2pressure==0))=NaN;
pressure(find(pressure==0))=NaN;

FLBB(find(FLBB==0))=NaN;
O2(find(O2==0))=NaN;
PSAL(find(PSAL==0))=NaN;
TEMP(find(TEMP==0))=NaN;


%load sediment trap data
L = readtable('BOG2 Sed trap summary.csv');

SedSampleNumber = L.Var1';

for i=1:1:size(L,1)
    tmp = sprintf('%sT%s',L.Var2(i), L.Var3(i));
    SedSampleTime(i) = datenum(tmp,'mm/dd/yyyyTHH:MM:SS');
    
    h = sscanf(string(L.Var5(i,:)),'%d %fN %d %fW');
    SedSampleBegLat(i) = h(1) + h(2)/60;
    SedSampleBegLon(i) = h(3) + h(4)/60;
    
    h = sscanf(string(L.Var6(i,:)),'%d %fN %d %fW');
    SedSampleEndLat(i) = h(1) + h(2)/60;
    SedSampleEndLon(i) = h(3) + h(4)/60;
end
SedSampleTime=mat2posix(SedSampleTime);

GlobalMaxLat = max([max(SedSampleBegLat) max(SedSampleEndLat)]);
GlobalMinLat = min([min(SedSampleBegLat) min(SedSampleEndLat)]);
GlobalMaxLon = max([max(SedSampleBegLon) max(SedSampleEndLon)]);
GlobalMinLon = min([min(SedSampleBegLon) min(SedSampleEndLon)]);
GlobalMinPressure = min(min(pressure));
GlobalMaxPressure = max(max(pressure));

SedSamplePressure = L.Var4';

x = size(CTDtime,1); y = size(CTDtime,2);
nccreate(fpcdf,'CTDtime','Dimensions', {'CTDx',x,'CTDy',y})
fileattrib(fpcdf,'+w');
ncwriteatt(fpcdf,'CTDtime','standard_name','time')
ncwriteatt(fpcdf,'CTDtime','units','seconds since 1970-01-01 00:00:00' );

nccreate(fpcdf,'PSAL','Dimensions', {'CTDx',x,'CTDy',y});
ncwriteatt(fpcdf,'PSAL','standard_name','sea_water_salinity') ;
ncwriteatt(fpcdf,'PSAL','units', 'psu');
        
nccreate(fpcdf,'TEMP','Dimensions', {'CTDx',x,'CTDy',y})
ncwriteatt(fpcdf,'TEMP','standard_name','sea_water_temperature') ;
ncwriteatt(fpcdf,'TEMP','units', 'degC');

x = size(FLBB,1); y = size(FLBB,2);
nccreate(fpcdf,'FLBB','Dimensions',{'FLBBx',x,'FLBBy',y})
ncwriteatt(fpcdf,'FLBB','standard_name','mass_concentration_of_chlorophyll_in_sea_water') ;
ncwriteatt(fpcdf,'FLBB','units', 'count');

nccreate(fpcdf,'FLBBtime','Dimensions',{'FLBBx',x,'FLBBy',y})
ncwriteatt(fpcdf,'FLBBtime','standard_name','time')
ncwriteatt(fpcdf,'FLBBtime','units','seconds since 1970-01-01 00:00:00' );

nccreate(fpcdf,'FLBBpressure','Dimensions',{'FLBBx',x,'FLBBy',y})
%ncwriteatt(fpcdf,'FLBBpressure','standard_name','mass_concentration_of_chlorophyll_in_sea_water') ;
ncwriteatt(fpcdf,'FLBBpressure','units', 'dBar');

x = size(O2,1); y = size(O2,2);
nccreate(fpcdf,'O2','Dimensions',{'O2x',x,'O2y',y})
ncwriteatt(fpcdf,'O2','long_name','oxygen, Optdode') ;
ncwriteatt(fpcdf,'O2','units', 'count');

nccreate(fpcdf,'O2time','Dimensions',{'O2x',x,'O2y',y})
ncwriteatt(fpcdf,'O2time','standard_name','time')
ncwriteatt(fpcdf,'O2time','units','seconds since 1970-01-01 00:00:00' );

nccreate(fpcdf,'O2pressure','Dimensions',{'O2x',x,'O2y',y})
ncwriteatt(fpcdf,'O2pressure','units', 'dBar');

x = size(SedSampleBegLat,1); y = size(SedSampleBegLat,2);
nccreate(fpcdf,'SedSampleBegLat','Dimensions',{'SEDx',x,'SEDy',y})
nccreate(fpcdf,'SedSampleBegLon','Dimensions',{'SEDx',x,'SEDy',y})
nccreate(fpcdf,'SedSampleEndLat','Dimensions',{'SEDx',x,'SEDy',y})
nccreate(fpcdf,'SedSampleEndLon','Dimensions',{'SEDx',x,'SEDy',y})
nccreate(fpcdf,'SedSampleNumber','Dimensions',{'SEDx',x,'SEDy',y})
nccreate(fpcdf,'SedSamplePressure','Dimensions',{'SEDx',x,'SEDy',y})
nccreate(fpcdf,'SedSampleTime','Dimensions',{'SEDx',x,'SEDy',y});

ncwrite(fpcdf,'CTDtime',CTDtime)
ncwrite(fpcdf,'FLBB',FLBB)
ncwrite(fpcdf,'FLBBtime',FLBBtime)
ncwrite(fpcdf,'FLBBpressure',FLBBpressure)
ncwrite(fpcdf,'O2',O2)
ncwrite(fpcdf,'O2time',O2time)
ncwrite(fpcdf,'O2pressure',O2pressure)
ncwrite(fpcdf,'PSAL',PSAL)
ncwrite(fpcdf,'TEMP',TEMP)

ncwrite(fpcdf,'SedSampleBegLat',SedSampleBegLat)
ncwriteatt(fpcdf,'SedSampleBegLat','long_name','LATITUDE') ;
ncwriteatt(fpcdf,'SedSampleBegLat','standard_name','latitude') ;
ncwriteatt(fpcdf,'SedSampleBegLat','units','degree_north');

ncwrite(fpcdf,'SedSampleBegLon',SedSampleBegLon)
ncwriteatt(fpcdf,'SedSampleBegLon','long_name','LONGITUDE') ;
ncwriteatt(fpcdf,'SedSampleBegLon','standard_name','longitude') ;
ncwriteatt(fpcdf,'SedSampleBegLon','units','degree_east');

ncwrite(fpcdf,'SedSampleEndLat',SedSampleEndLat)
ncwriteatt(fpcdf,'SedSampleEndLat','long_name','LATITUDE') ;
ncwriteatt(fpcdf,'SedSampleEndLat','standard_name','latitude') ;
ncwriteatt(fpcdf,'SedSampleEndLat','units','degree_north');

ncwrite(fpcdf,'SedSampleEndLon',SedSampleEndLon)
ncwrite(fpcdf,'SedSampleBegLon',SedSampleBegLon)
ncwriteatt(fpcdf,'SedSampleBegLon','long_name','LONGITUDE') ;
ncwriteatt(fpcdf,'SedSampleBegLon','standard_name','longitude') ;
ncwriteatt(fpcdf,'SedSampleBegLon','units','degree_east');

ncwrite(fpcdf,'SedSampleNumber',SedSampleNumber)

ncwrite(fpcdf,'SedSamplePressure',SedSamplePressure)
ncwriteatt(fpcdf,'SedSamplePressure','units', 'dBar');

ncwrite(fpcdf,'SedSampleTime',SedSampleTime)
ncwriteatt(fpcdf,'SedSampleTime','standard_name','time')
ncwriteatt(fpcdf,'SedSampleTime','units','seconds since 1970-01-01 00:00:00' );

creat = sprintf('%s',datetime('now','Format','yyyy-MM-dd''T''HH:mm:ss'));
%fileattrib(fpcdf,'+w');
ncwriteatt(fpcdf,'/','title','Profile CTD, O2, FLBB and sed trap data from CPF');
ncwriteatt(fpcdf,'/','date_created',creat);
ncwriteatt(fpcdf,'/','geospatial_lat_min',GlobalMinLat) ;
ncwriteatt(fpcdf,'/','geospatial_lat_max',GlobalMaxLat) ;
ncwriteatt(fpcdf,'/','geospatial_lon_min',GlobalMinLon) ;
ncwriteatt(fpcdf,'/','geospatial_lon_max',GlobalMaxLon) ;
ncwriteatt(fpcdf,'/','geospatial_vertical_min',GlobalMinPressure);
ncwriteatt(fpcdf,'/','geospatial_vertical_max',GlobalMaxPressure);
ncwriteatt(fpcdf,'/','geospatial_vertical_units','m') ;
ncwriteatt(fpcdf,'/','geospatial_vertical_positive','down') ;
ncwriteatt(fpcdf,'/','time_coverage_start',GlobalMinTime) ;
ncwriteatt(fpcdf,'/','time_coverage_end',GlobalMaxTime) ;
ncwriteatt(fpcdf,'/','useconst','Not intended for legal use. Data may contain inaccuracies.') ;

