% BW2nc
%
% Generates Beach width in NetCDF ( strandbreedte.nc ) using
% http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/MHW_MLW/MHW_MLW.nc
% and
% http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/DuneFoot/DF.nc
% 
% Beach width is defined at mean high water and at mean low water with respect to +3 m NAP along the Dutch coast since 1843
% 
% NB. 
%
% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords$ 
%
%%

%% read files
% general settings
OPT.fillvalue    = NaN;
OPT.refdatenum   = datenum(1970,1,1);
OPT.outputfile   = 'BW.nc';

% jarkus database for info about RSP, latlon, RDNew
OPT.ncpath1      = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/';
OPT.Filename1    = 'transect.nc';
OPT.ncpath2      = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/DuneFoot/';
OPT.Filename2    = 'DF.nc';
OPT.ncpath3      = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/MHW_MLW/';
OPT.Filename3    = 'MHW_MLW.nc';

% read jarkus information
id       = ncread([OPT.ncpath1 OPT.Filename1],'id');
time     = ncread([OPT.ncpath1 OPT.Filename1],'time');
rsp_lat  = ncread([OPT.ncpath1 OPT.Filename1],'rsp_lat');
rsp_lon  = ncread([OPT.ncpath1 OPT.Filename1],'rsp_lon');
cross    = ncread([OPT.ncpath1 OPT.Filename1],'cross_shore');
along    = ncread([OPT.ncpath1 OPT.Filename1],'alongshore');
areaname = (ncread([OPT.ncpath1 OPT.Filename1],'areaname'))';
areacode = ncread([OPT.ncpath1 OPT.Filename1],'areacode');

time        = ncread([OPT.ncpath2 OPT.Filename2],'time');
time_bounds = ncread([OPT.ncpath2 OPT.Filename2],'time_bounds');

dune_foot_threeNAP_cross = ncread([OPT.ncpath2 OPT.Filename2],'dune_foot_threeNAP_cross');
mean_high_water_cross    = ncread([OPT.ncpath3 OPT.Filename3],'mean_high_water_cross');
mean_low_water_cross     = ncread([OPT.ncpath3 OPT.Filename3],'mean_low_water_cross');

time_year = str2num(datestr(time+OPT.refdatenum,'yyyy')); %#ok<ST2NM>
transectid = id - areacode*1e6;

% calculate difference
beach_width_at_mean_high_water = mean_high_water_cross - dune_foot_threeNAP_cross;
beach_width_at_mean_low_water  = mean_low_water_cross - dune_foot_threeNAP_cross;

%% assembly variables from strandlijnen.nc and .mat files all together
s.beach_width_at_mean_high_water = beach_width_at_mean_high_water;
s.beach_width_at_mean_low_water  = beach_width_at_mean_low_water;
% include relevant information from Jarkus
s.id   = id;   
s.cross = cross; 
s.along = along;
s.time        = time;
s.time_bounds = time_bounds;
s.areaname = areaname'; 
s.areacode = areacode;
% add for convenience
s.jarkusyear = time_year; 

bw2netcdf(OPT.outputfile, s,'processing_level', 'final');
