%%% 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!

% 25-11-2009 Sierd introduced valid RD_coordinates
% 26-01-2010 Sierd added survey paths to include XYZ type output


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

% clear all
clc
%% Initiate



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 addded the scripts folder to the path
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
STRINGSIZE = 10;
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 !')
fprintf('\n')



%% Load all data
% load survey lines
load Surveylines

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

for i = data.alongshore;
    data.dist_alongshore(i) = Surveylines{i}.along_shore;
    data.xi(i,:) = Surveylines{i}.xi;
    data.yi(i,:) = Surveylines{i}.yi;
end


% 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
        %only include surveys with NetCDF = 1
        fid = fopen(['walking/raw/' w_list(i_list,1).name '\products\XYZ_RDNAP.txt']);
        go = 1; while go
            tline = fgetl(fid);
            temp=strfind(tline,'Use');
            if temp
                A=strread(tline, '%s', 'delimiter', ' ');
                if str2double(A{6,1});
                    ind=[ind; i_list];
                end
                go = 0;
            end
            temp=strfind(tline,'X_rd');
            if temp
                go = 0;
            end
        end
        fclose(fid);
    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=[];
data.path_walk=[];
data.path_walk=-99*ones(ndates,15000,3);

% Now go through all surveys
for idate=1:ndates
    XYZ=Read_XYZ( ['walking/raw/' list(idate,1).name '\products\XYZ_RDNAP.txt']);
    fprintf('\n')
    disp(['Processing survey ' cell2mat(XYZ.surveydate)]);
    data.time_walk = ([data.time_walk ; XYZ.surveydate]);
    data.path_walk(idate,1:length(XYZ.data),:) =  XYZ.data ;

    % 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 ' num2str(itransect) ' 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
        %only include surveys with NetCDF = 1
        fid = fopen(['jetski/raw/' j_list(i_list,1).name '\products\XYZ_jetski_RD_NAP.txt']);
        go = 1; 
        while go
            tline = fgetl(fid);
            temp=strfind(tline,'Use');
            if temp
                A=strread(tline, '%s', 'delimiter', ' ');
                if str2double(A{6,1});
                    ind=[ind; i_list];
                end
                go = 0;
            end
            temp=strfind(tline,'-');
            if ~isempty(temp) && temp(1)==1
                go = 0;
            end
        end
        fclose(fid);
    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=[];
data.path_jet=-99*ones(ndates,15000,3);


%% Now go through all surveys
for idate=1:ndates
    XYZ=Read_XYZ( ['jetski/raw/' list(idate,1).name '\products\XYZ_jetski_RD_NAP.txt']);
    fprintf('\n')
    disp(['Processing survey ' cell2mat(XYZ.surveydate)])
    data.time_jet = ([data.time_jet ; XYZ.surveydate]);
    data.path_jet(idate,1:length(XYZ.data),:) =  XYZ.data ;
    % 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 ' num2str(itransect) ' extracted'])
    end
end


% combine both data series
data.time=[data.time_walk ; data.time_jet ];
data.series=[data.series_walk ; data.series_jet];
data.path=[data.path_walk ; data.path_jet];
data.type=[repmat({'Walking   '},size(data.time_walk)) ;  repmat({'Jetski    '},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),1))
nc_add_dimension(NetCDFfile, 'stringsize' , STRINGSIZE)
nc_add_dimension(NetCDFfile, 'survey_path' ,  size(data.path,2))
nc_add_dimension(NetCDFfile, 'survey_path_dim' ,  size(data.path,3))


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

s.Name      = 'dist_alongshore';
s.Nctype    = nc_double;
s.Dimension = {'alongshore'};
s.Attribute = struct('Name', {'long_name', 'units', 'comment'}, ...
    'Value', {'alongshore', 'm', 'Alongshore distance starting from the SW'});
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 = {'alongshore' ,'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 = {'alongshore' ,'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','stringsize'};
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 or Walking' , -99});
nc_addvar(NetCDFfile, s);

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

s.Name      = 'survey_path';
s.Nctype    = nc_double;
s.Dimension = {'time','survey_path','survey_path_dim'};
s.Attribute = struct('Name', {'standard_name', 'units', 'comment', 'coordinates', 'survey_type' ,'_FillValue'}, ...
    'Value', {'survey_path', 'm', 'XYZ coordinates of survey path', 'Dutch Coordinate system RDNAP', 'Jetski or Walking' , -99});
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, 'survey_type', cell2mat(data.type))
nc_varput(NetCDFfile, 'X_RD', data.xi)
nc_varput(NetCDFfile, 'Y_RD', data.yi)
nc_varput(NetCDFfile, 'alongshore', data.alongshore)
nc_varput(NetCDFfile, 'dist_alongshore', data.dist_alongshore)
nc_varput(NetCDFfile, 'cross_shore', data.distance)
nc_varput(NetCDFfile, 'altitude', data.series)
nc_varput(NetCDFfile, 'survey_path', data.path)


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

