%% RUNSCRIPT [matlab version: 7.6.0 (R2008a)]
% --------------------------------------------------
% input data
% - see below
%
% output data
% - ascii file of the same format
% - figure with the converted parameters [png format]
% ---------------------------------------------------

%% parameters
clear all; close all; clc;
addpath('functions')

XY = load('ldbs\offshoreboundary.txt');

for ii=1:size(XY,1)
    s.StartDate         = datenum(2010,06,01);
    s.EndDate           = datenum(2013,12,31);
    s.Loc.X             = XY(ii,1); %102908;             % x-coordinates RD >> make sure it is in the model domain
    s.Loc.Y             = XY(ii,2); %533840;             % y-coordinates RD >> make sure it is in the model domain
    s.OutputFile        = ['tst',num2str(ii,'%02.0f'),'.asc'];          % name of outputfile
    Transform(s);
end

%% Convert the result into a wavecond file
for ii=1:size(XY,1)
    fid = fopen(['tst',num2str(ii,'%02.0f'),'.asc'],'rt');
    INH = fread(fid);
    IDline = find(INH==10);
    INHtxt = char(INH(IDline(9)+1:end)');
    INHnum = str2num(INHtxt);
    ID = find(~isnan(INHnum(:,3)));
    INHnum2 = INHnum(ID,:);
    INHdatnum = datenum([num2str(INHnum2(:,1),'%08.0f'),num2str(INHnum2(:,2),'%06.0f')],'yyyymmddHHMMSS');
    WVCdata.time      = (INHdatnum-INHdatnum(1))*24*60;
    WVCdata.hs        = INHnum2(:,3);
    WVCdata.xdir      = INHnum2(:,4);
    WVCdata.tp        = INHnum2(:,5);
    WVCdata.dirspr    = repmat(4,size(WVCdata.time));
    WVCdata.h0        = repmat(0,size(WVCdata.time));
    WVCdata.windspeed = repmat(0,size(WVCdata.time));
    WVCdata.winddir   = repmat(0,size(WVCdata.time));
    % WVCdata.hs,WVCdata.tp,WVCdata.xdir,WVCdata.dirspr,WVCdata.h0,WVCdata.windspeed,WVCdata.winddir
    WVCfilename       = ['wavecon.r',num2str(ii,'%02.0f')];
    MDVWACfilename    = ['mdvwac.r',num2str(ii,'%02.0f')];
    writeWAVECON(WVCfilename,WVCdata);
    writeMDVWAC(MDVWACfilename,WVCdata);
end

