function jarkus_convert(varargin)
%JARKUS_CONVERT convert raw jarkus data to netCDF-CF file
%
% This script can be used to convert jarkus transect (raai) files into
% netCDF files.
%
% A few entities are used here:
%
% jarkus transects: as read from the jarkus transect file (.jrk)
% transect:         assumed to be measurements along a seaward angle
% grid:             in this case a curvilinear structured grid 
% netCDF output:    a netCDF file following the cf convention where results are stored.
%
% Transect:
%
%  A transect is considered to be a set of measurements along a line. This
%  line has a local seaward coordinate 
%
% Jarkus transect:
%
%  A jarkus transect is measured by rijkswaterstaat. Starting from a pole in
%  the coast ("rijksstrandpaal") measurements are taken along a line seaward ("raai"), 
%  both on the coast and in the water. 
%  The data is related to areas, regions in the Netherlands
%  Each transect raai has a region specific distance within the region along the
%  coastline ("metrering").
%
% Grid:
%
%  The grid which contains the data for the transects is curvilinear
%  structured. Each point in the grid has a seperate geographic coordinate.
%  The grid is considered to be curvilinear, it follows the coastline. 
%  The grid is structured, all points are related to 2 dimensions (seaward,
%  along the coast)
%
% Netcdf output
%
%  The netcdf file is stored according to the CF convention
%
% Input:
%
%  Data is stored in 
%  https://svn.oss.deltares.nl/repos/openearthrawdata/trunk/rijkswaterstaat/jarkus/
%   ./raw/transects/total/jarkus_raaien.txt -> table with indexes of transects, contains index, relative coordinate, coordinate in EPSG:28992, angle
%   ./raw/transects/total/*.txt             -> all jarkus transect data
%
% See web : <a href="http://www.watermarkt.nl/kustenzeebodem/">www.watermarkt.nl/kustenzeebodem/</a>
% See also: JARKUS_TRANSECT2GRID  , JARKUS_NETCDF2GRID, JARKUS_UPDATEGRID, 
%           JARKUS_TRANSECT2NETCDF, JARKUS_GRID2NETCDF (OpenEarthTools)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$

%% Run oetsettings
OPT = struct(...
    'dataDir', '..',... % By default data is 1 directory up from the script.
    'processing_level', 'final',...
    'msg', '');
OPT = setproperty(OPT, varargin);

%% get list of files

rawFileDir   =     fullfile(OPT.dataDir, 'raw');
Dannual = dir2(fullfile(rawFileDir, 'annual'),...
    'no_dirs', 'true',...
    'depth', 1,...
    'file_incl', '\.jrk');

Dtotal = dir2(fullfile(rawFileDir, 'total'),...
    'no_dirs', 'true',...
    'file_incl', '\.txt');
rawFileArray = [Dtotal(:); Dannual(:)]; %dir(fullfile(rawFileDir, 'total', '*.txt'));       % lists all available area data files (15 areas with transect data)

% rawFileArray = ...
% ('Ameland.txt',...
%  'Delfland.txt',...
%  'Goeree.txt',...
%  'Maasvlakte.txt',...
%  'Noord-Beveland.txt',...
%  'Noord-Holland.txt',...
%  'Rijnland.txt',...
%  'Schiermonnikoog.txt',...
%  'Schouwen.txt',...
%  'Terschelling.txt',...
%  'Texel.txt',...
%  'Vlieland.txt',...
%  'Voorne.txt',...
%  'Walcheren.txt',...
%  'Zeeuws-Vlaanderen.txt')

%% get user info
if isunix
    [~, user_name] = system('whoami');
    user_name = strtrim(user_name);
    systeminfo = computer('arch');
else
    usernames = cellfun(@getenv, {'USER', 'USERNAME'}, 'UniformOutput', false);
    usernames = usernames(~cellfun(@isempty, usernames));
    if ~isempty(usernames)
        user_name = usernames{1};
    else
        user_name = 'undefined';
    end
    systeminfo = sprintf('%s %s\\%s', computer('arch'), getenv('USERDOMAIN'), getenv('COMPUTERNAME'));
end

% define publisher_name and publisher_email
publisher_struct = struct(...
    'undefined', {user_name, 'info@deltares.nl'},...
    'fuentes', {'Mario Fuentes Monjaraz', 'Mario.FuentesMonjaraz@deltares.nl'});
if all(cellfun(@isempty, strfind(fieldnames(publisher_struct), user_name)))
    [publisher_name, publisher_email] = deal(publisher_struct.undefined);
else
    [publisher_name, publisher_email] = deal(publisher_struct.(user_name));
end

% get svn revision information of raw files
datefmt = 'yyyy-mm-ddTHH:MMZ';
try
    % since the files are gathered from 2 directories, the rawFileDir is
    % taken as common base and relative paths are derived from there.
    relpaths = cellfun(@(p,f) strrep(abspath(fullfile(p,f)), abspath(rawFileDir), ''), {rawFileArray.pathname}, {rawFileArray.name},...
        'uniformoutput', false);
    rawsvninfo = jarkus_svnrev_transects('mainpath', rawFileDir, 'filenames', relpaths);
    oerd_svninfo = svn_info([mfilename('fullpath') '.m']);
    oerdpath = oerd_svninfo.working_copy_root_path;
    oet_svninfo = svn_info(which('oetsettings'));
    oetpath = oet_svninfo.working_copy_root_path;
    [~, svnver] = system(['svnversion "' mfilename('fullpath') '.m"']);
    processinginfo = sprintf('%s: NetCDF created by %s on %s computer %s\\%s with Matlab version %s and main function %s (revision %s)',...
        datestr(nowutc, datefmt), user_name, computer('arch'), getenv('USERDOMAIN'), getenv('COMPUTERNAME'), version, oerd_svninfo.url, strtrim(svnver));
    relfuns = [getCalls([mfilename('fullpath') '.m'], oerdpath, 'quiet'); getCalls([mfilename('fullpath') '.m'], oetpath, 'quiet')];
    svninfo = cellfun(@svn_info, relfuns);
    reposroots = unique({svninfo.working_copy_root_path});
    repossvn = cellfun(@svn_info, reposroots, 'UniformOutput', false);
    reposinfo = 'subversion repositories:';
    for i = 1:length(repossvn)
        [~, svnver] = system(['svnversion "' reposroots{i} '"']);
        reposinfo = sprintf('%s\n%s (revision %s)', reposinfo, repossvn{i}.url, strtrim(svnver));
    end
    majorfuninfo = 'primary related functions:';
    for i = 1:length(relfuns)
        if strcmpi(relfuns{i}, [mfilename('fullpath') '.m'])
            continue
        end
        [~, svnver] = system(['svnversion "' relfuns{i} '"']);
        majorfuninfo = sprintf('%s\n%s (revision %s)', majorfuninfo, svninfo(i).url, strtrim(svnver));
    end
    historyatt = sprintf('%s\n%s\n%s\n%s', processinginfo, reposinfo, majorfuninfo, rawsvninfo);
catch
    disp('Error: not able to retreive svn info of raw data');
    D = dir(rawFileDir);
    tzoffset = java.util.Date().getTimezoneOffset()/60/24; % time zone offset [days]
    rawsvninfo = sprintf('%s: Raw data received from Rijkswaterstaat', datestr(D(1).datenum+tzoffset, datefmt));
    processinginfo = sprintf('%s: NetCDF created by %s on %s with Matlab version %s and main function %s (revision %s) with %s',...
        datestr(nowutc, datefmt), user_name, systeminfo, version, '$HeadURL$', '$Revision$', var2evalstr(varargin));
    historyatt = sprintf('%s\n%s', processinginfo, rawsvninfo);
end

%% Define which data is converted to netcdf
% originselection = [ 1 2]; % topo only
% originselection = [ 4 5]; % bathy only
% originselection = [ 2 3 4]; % overlap only: topo - interpol - bathy 
originselection = [ 1 3 5]; % non-overlap: topo - interpol - bathy

outputfile      = sprintf('transect_r%s.nc', datestr(nowutc, 'yyyymmdd'));

%% Allocate structure for dataset. 
%  This might be faster with a preallocated size

transectStruct    = jarkus_createtransectstruct();
transectStruct(:) = [];
% rawFileArray      = rawFileArray(10:11); % for testing, makes life a lot more comfortable

%% Loop over all files (= areas)
disp('Listing files in repos');
for i = 1:length(rawFileArray)
   filename = fullfile(rawFileArray(i).pathname, rawFileArray(i).name);
   disp(['   reading ' filename])
   % transectStruct are jarkus specific Arrays. 
   % Concatenate them into a big transectStruct
   % structure Array.
   try
       transectStruct = [transectStruct jarkus_transect(filename, originselection)];
   catch 
       s = lasterror;
       % catch only my errors for which no identifiers are defined
       % native matlab errors will be rethrown
       if ~isempty(s.identifier)
          error(lasterror)
       end
   end
end

%% define a grid based on the transectStruct

disp('converting transects to grid');
grid = jarkus_transect2grid(transectStruct);% renamed from transect2grid 2009 june 19th
transectfiles = {fullfile(OPT.dataDir, 'raw', 'info', 'raaien.txt'),...
    fullfile(OPT.dataDir, 'raw', 'info', 'raaien2.txt'),...
    fullfile(OPT.dataDir, 'raw', 'info', 'raaien3.txt'),...
    fullfile(OPT.dataDir, 'raw', 'info', 'raaien4.txt'),...
    fullfile(OPT.dataDir, 'raw', 'info', 'raaien5.txt')};
[grid, msg, nometa_ids] = jarkus_updategrid(grid, transectfiles, ...
                               fullfile(OPT.dataDir, 'raw', 'info', 'TIDEINFO.txt')); % this file contains extra information
if ~isempty(nometa_ids)
    idx = ismember([transectStruct.id], nometa_ids);
    msg = sprintf('%s\nDue to incomplete metadata, the data of %i available measurements is skipped in years %sand transects %s', msg, sum(idx), sprintf('%i ', unique([transectStruct(idx).year])), sprintf('%i ', nometa_ids));
end
                           
%% merge messages if applicable
if ~isempty(OPT.msg)
    msg = sprintf('%s\n%s', msg, OPT.msg);
end

%% store grid and transects to a netcdf file
disp('Publish grid to NetCDF file');
jarkus_grid2netcdf(outputfile, grid,...
    'publisher_name', publisher_name,...
    'publisher_email', publisher_email,...
    'historyatt', historyatt,...
    'origins', originselection,...
    'msg', msg,...
    'processing_level', OPT.processing_level);

%% extract a curvilinear grid definition from the transectStruct x grid H(time, transect, length) where
%  * time is the year, 
%  * transect is the transect and 
%  * lenght is the difference along the line starting from the measurement pole.
disp('Publish transects to a NetCDF file');
jarkus_transect2netcdf(outputfile, transectStruct);

if nargin > 0
   delete('matlabruns.busy');
   exit;
end