%%% Make/update netCDF file with transects
%
% syntax: Transect2Netcdf
%
% Run OETsettings first! (F:\check_outs\matlab on Matthieu's pc)
% add scripts folder (and subfolders) from tudelft/vlugtenburg to path
%
%
%
%M.A. De Schipper 2009 Garantie tot de deur!


% TO D0
%
% reduce gridsize Surveylines
% % fix the time in NetCDF
% Check if it is possible to add to an existing netcdf file


%% Initiate

clear all
clc

curdir=pwd; %% save current path
% check if you are in the right folder
if ~strcmp( curdir(end-10:end), 'vlugtenburg')
    error (' Wrong Directory.. Please run this script in main Vlugtenburg directory')
end

% check if you have ran oetsettings.m
if exist ('nc_create_empty.m') ~= 2
    error (' run OETsettings first... DUMBASS!')
end

% check if you have ran oetsettings.m
if exist ('surveylines.mat') ~= 2
    error ('add whole scripts folder to path')
end

NetCDFfile = 'Vlugtenburg.nc';


%%%%%%%%%%%
%%% Load old NetCDF file
%%%%%%%%%%%
% TO DO
% Make the possibility to overwrite old file, or maybe ask the user first
% if he wants to.
% NC_getall('Vlugtenburg.nc');


%%%%%%%%%%%
%%% Make new NetCDF file
%%%%%%%%%%%
reply = input('Are you sure you want to overwrite vlugtenburg.nc? y/n : ', 's');
if isempty(reply) | reply~='y'
    error('Transect2Netcdf.m stopped')
end

nc_create_empty(NetCDFfile);  % Create a new empty netcdf file.
disp('old vlugtenburg.nc deleted, start making a new one')
disp('get a cup of coffee... this is going to take +/- 10 minutes !')

% load survey lines
load Surveylines

ntransects=length(Surveylines);
data.alongshore=1:ntransects;
data.distance = (Surveylines{1}.cross_shore)';       % XYZ Surveydata is interpolated to crosshore locations as defined in the surveylines


%% load WALKING data 
% make a list of all available post processed surveys
w_list=dir('walking/raw/20*'); % list of walking survey directories 
ind=[];
for i_list=1:length(w_list)    % this loop is to exclude surveys that have not been processed (yet)
    if exist(['walking/raw/' w_list(i_list,1).name '\products\XYZ_RDNAP.txt']) == 2
        ind=[ind; i_list];
    end
end
list=w_list(ind,1);  % final list with directories containing a processed survey

ndates=length(list);
data.series_walk = -99*ones(ndates,ntransects,length(data.distance)); % depth values zi. This is an array with zi(time,alongshore,cross_shore)
data.time_walk=[];

% Now go through all surveys
for idate=1:ndates
    XYZ=read_XYZ( ['walking/raw/' list(idate,1).name '\products\XYZ_RDNAP.txt']);
    disp(['processing survey' XYZ.surveydate])
    data.time_walk = ([data.time_walk ; XYZ.surveydate]);

    % Now extract all surveylines
    for itransect=1:ntransects
        transect= extract_line_nr(XYZ,Surveylines{itransect});  %% do for all transects
        data.series_walk (idate,itransect,:)=transect.zi;
        disp(['line ' ' extracted'])
    end
end

%% load JETSKI data 

% make a list of all available post processed surveys
j_list=dir('jetski/raw/20*');; % list of walking survey directories 
ind=[];
for i_list=1:length(j_list)    % this loop is to exclude surveys that have not been processed (yet)
    if exist(['jetski/raw/' j_list(i_list,1).name '\products\XYZ_jetski_RD_NAP.txt']) == 2
        ind=[ind; i_list];
    end
end
list=j_list(ind,1);  % final list with directories containing a processed survey

ndates=length(list);
data.series_jet = -99*ones(ndates,ntransects,length(data.distance)); % depth values zi. This is an array with zi(time,alongshore,cross_shore)
data.time_jet=[];

% Now go through all surveys
for idate=1:ndates
    XYZ=read_XYZ( ['jetski/raw/' list(idate,1).name '\products\XYZ_jetski_RD_NAP.txt']);
    disp(['processing survey' XYZ.surveydate])
    data.time_jet = ([data.time_jet ; XYZ.surveydate]);

    % Now extract all surveylines
    for itransect=1:ntransects
        transect= extract_line_nr(XYZ,Surveylines{itransect});  %% do for all transects
        data.series_jet (idate,itransect,:)=transect.zi;
        disp(['line ' ' extracted'])
    end
end


%% combine both data series
data.time=[data.time_walk ; data.time_jet ];
data.series=[data.series_walk ; data.series_jet];
data.type=[repmat({'w'},size(data.time_walk)) ;  repmat({'j'},size(data.time_jet)) ] % structure containing information on the survey type
% note: voorlopig deze gammele benaming.. namen 'w' and 'j' moeten evenveel karakters hebben!

%% build NetCDF file
%%% Add dimensions
nc_add_dimension(NetCDFfile, 'alongshore', size(data.series,2))
nc_add_dimension(NetCDFfile, 'cross_shore', size(data.series,3))
nc_add_dimension(NetCDFfile, 'time', size(cell2mat(data.time)))
%nc_add_dimension(NetCDFfile, 'surveytype', size(cell2mat(data.type)))
nc_add_dimension(NetCDFfile, 'altitude', size(data.series))

%%% Add variables
s.Name      = 'alongshore';
s.Nctype    = nc_double;
s.Dimension = {'alongshore'};
s.Attribute = struct('Name', {'long_name', 'units', 'comment'}, ...
    'Value', {'alongshore', 'm', 'Alongshore transect ID starting from the North'});
nc_addvar(NetCDFfile, s);

s.Name      = 'cross_shore';
s.Nctype    = nc_double;
s.Dimension = {'cross_shore'};
s.Attribute = struct('Name', {'long_name', 'units', 'comment'}, ...
    'Value', {'cross-shore coordinate', 'm', 'cross-shore coordinate relative to reference line through beachpoles'});
nc_addvar(NetCDFfile, s);

s.Name      = 'X_RD';
s.Nctype    = nc_double;
s.Dimension = {'cross_shore'};  %  We have a X (and Y) coordinates for each cross-shore coordinate. Therefore the dimension is cross_shore.
s.Attribute = struct('Name', {'standard_name', 'units', 'axis'}, 'Value', {'X coordinate RD', 'm', 'X'});
nc_addvar(NetCDFfile, s);

s.Name      = 'Y_RD';
s.Nctype    = nc_double;
s.Dimension = {'cross_shore'};
s.Attribute = struct('Name', {'standard_name', 'units', 'axis'}, 'Value', {'y coordinate RD', 'm', 'Y'});
nc_addvar(NetCDFfile, s);

s.Name      = 'time';
s.Nctype    = nc_char;
s.Dimension = {'time'};
s.Attribute = struct('Name', {'standard_name', 'units', 'comment', 'axis'}, 'Value', {'time', 'year', 'survey date', 'dd-mm-yyyy'});
nc_addvar(NetCDFfile, s);

s.Name      = 'altitude'; %% this is the depth array!
s.Nctype    = nc_double;
s.Dimension = {'time', 'alongshore' ,'cross_shore'};
s.Attribute = struct('Name', {'standard_name', 'units', 'comment', 'coordinates', 'survey_type' ,'_FillValue'}, ...
    'Value', {'surface_altitude', 'm', 'altitude above geoid (NAP)', 'Dutch Coordinate system RD', 'Jetski' , -99});
nc_addvar(NetCDFfile, s);

% s.Name      = 'surveytype';
% s.Nctype    = nc_char;
% s.Dimension = {'time'};
% s.Attribute = struct('Name', {'Comment'}, 'Value', {'jetski or walking'});
% nc_addvar(NetCDFfile, s);

%%%%%%%%%%%
%%% Possibly Compare old and new file and if user agrees add new info and overwrite.
%%%%%%%%%%%



% write values
nc_varput(NetCDFfile, 'time', cell2mat(data.time))   %%% This has to be altered... Time is not well written in variable (voorlopig wordt de string uitgesmeerd )
%nc_varput(NetCDFfile, 'surveytype', cell2mat(data.type))
nc_varput(NetCDFfile, 'alongshore', data.alongshore)
nc_varput(NetCDFfile, 'cross_shore', data.distance)
nc_varput(NetCDFfile, 'altitude', data.series)


disp('data written to vlugtenburg.nc file')

