% function compareNEW_DF_with_OLD
%
% Compares two Dune Foot NetCDF files
%
%

%% clean
clear
clc

%% settings
% general settings
OLDfile   = 'http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/DuneFoot/DF.nc';
NEWfile   = 'DF.nc';
% OLDfile   = 'DF.nc';
% NEWfile   = 'DF_RPN.nc';

%% Helperfunction
elements = @(in,varargin) in(varargin{:});

%% Variables and Dimensions
% actually should get these from old file
variables = {
'epsg'
'id'
'areacode'
'areaname'
'alongshore'
'cross_shore'
'time'
'time_bounds'
};
gridnames = {
'dune_foot_upperMKL'
'dune_foot_upperMKL_cross'
'dune_foot_upperMKL_x'
'dune_foot_upperMKL_y'
'dune_foot_upperMKL_lat'
'dune_foot_upperMKL_lon'
'dune_foot_threeNAP'
'dune_foot_threeNAP_cross'
'dune_foot_threeNAP_x'
'dune_foot_threeNAP_y'
'dune_foot_threeNAP_lat'
'dune_foot_threeNAP_lon'
};
variables = [variables; gridnames];
nvar = length(variables);
for ivar = nvar:-1:1
    disp(variables{ivar})
    old.(regexprep(variables{ivar},'([^a-zA-Z_0-9])','_')) = nc_varget(OLDfile,variables{ivar});
    new.(regexprep(variables{ivar},'([^a-zA-Z_0-9])','_')) = nc_varget(NEWfile,variables{ivar});
end

%% check on size first !!!

%% check epsg
if ~(old.epsg==new.epsg); fprintf(2,'Difference found, not all old "epsg" is present in new\n'); return; end

%% Check time axis, POS_TIME gives position of old value in new array.
[OldIsPresent, POS_TIME] = ismember(old.time,new.time);
if ~all(OldIsPresent); fprintf(2,'Difference found, not all old "time" is present in new\n'); return; end
if ~all(new.time_bounds(POS_TIME,:)==old.time_bounds); fprintf(2,'Difference found, not all old "time_bounds" is present in new\n'); return; end

%% Check id
[OldIspresent,POS_ID]= ismember(old.id, new.id);
if ~all(OldIspresent)
    fprintf(2,'Difference found, not all old "id" is present in new\n'); 
    return; 
end
% Check areacode
if ~all(new.areacode(POS_ID)==old.areacode)
    fprintf(2,'Difference found, not all old "areacode" is present in new\n'); 
    return; 
end
% Check alongshore
if ~all(new.alongshore(POS_ID)==old.alongshore)
    fprintf(2,'Difference found, not all old "alongshore" is present in new\n'); 
    return; 
end
% Check areaname
if ~all(elements(new.areaname(POS_ID,1:size(old.areaname,2))==old.areaname,':'))
    fprintf(2,'Difference found, not all old "areaname" is present in new\n'); 
    OLDnames = old.areaname;
    NEWnames = new.areaname(POS_ID,1:size(old.areaname,2));
    index = find(any( OLDnames~=NEWnames,2 ));
    for n = unique(round(linspace(1,length(index),8)))
        fprintf(2,'\nOLD:"%s"\nOLD:"%s"  ',sprintf(' %c ',OLDnames(index(n),:)),sprintf('%02X ',OLDnames(index(n),:)));
        fprintf(2,'\nNEW:"%s"\nNEW:"%s"\n',sprintf('%02X ',NEWnames(index(n),:)),sprintf(' %c ',NEWnames(index(n),:)));
    end
end

%% Check cross_shore
[OldIspresent,POS_CrossShore]= ismember(old.cross_shore, new.cross_shore);
if ~all(OldIspresent); fprintf(2,'Difference found, not all old "cross_shore" is present in new\n'); return; end

%% CHECK GRIDS
for igrid = 1:length(gridnames)
    if ~isequaln( new.(gridnames{igrid})(POS_TIME, POS_ID), old.(gridnames{igrid}) )
        newG = new.(gridnames{igrid})(POS_TIME, POS_ID);
        oldG = old.(gridnames{igrid});
        [iTime,iID] = find(~arrayfun(@isequaln,newG,oldG));
        fprintf(2,'\nDifference found in "%s"\n NEW-OLD ranges from %g to %g\n',gridnames{igrid},min(newG(:)-oldG(:)),max(newG(:)-oldG(:))); 
        fprintf(2,'Difference found in [transect] [year]\n%s',sprintf('%d - %04d-%02d-%02d\n',[old.id(iID), elements(datevec(old.time(iTime)+datenum([1970 1 1])),':',1:3)]'));
    end
end

%%
fprintf(1,'\n"%s" is done :)\n',mfilename);

