You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.7 KiB
Matlab

% Written by Joshua Simmons 11/02/2015 and updated on 11/01/2016
% to convert from fielddata storage struct to SBEACH compatible format.
clear
clc
%% Settings
%all the initial files location
%specify folder with Mitch's bathymetry and field data setup for xbeach.
setupoutputpath = 'D:\7 Erosion Models\a_nb_model_comparison\sbeach\model_setup\setup_folders\';
setupbasepath = 'D:\7 Erosion Models\a_nb_model_comparison\sbeach\model_setup\';
basebatchrunpath = 'D:\7 Erosion Models\a_nb_model_comparison\sbeach\model_results\src\';
% meta
site = 'deewhy';
% stormid = '2007';
% stormid = '2011';
% stormid = '2014';
stormid = '2015';
%profile info - CHECK
pfno = 5;
% model parameters
%parameter vals - leave as '' if params to be generated
paramload = 'D:\7 Erosion Models\a_nb_model_comparison\sbeach\params\bigparams_K_EPS_BMAX.mat';
% paramload = '';
%or specify stats
paramsname = {'K','EPS','BMAX'}; %K divided by 3600 below to bring into seconds, Cs for R2;
% paramsrange = {[0.5e-6 2.5e-6],[0.001 0.003],[15 30]}; % give range as [min max] then fed into rand()
paramsrange = {[1.75e-6 1.75e-6],[0.002 0.002],[28.5 28.5]}; % give range as [min max] then fed into rand()
nruns = 10000; %number of parameter conmbinations
nrunst = 1;
nrunend = 10000;
%% Code - format the data
%generate param combos
if isempty(paramload)
for aa = 1:length(paramsname)
tmpmin = paramsrange{aa}(1);
tmpmax = paramsrange{aa}(2);
params(:,aa) = (tmpmax-tmpmin).*rand(nruns,1) + tmpmin;
end
bigparams.name = paramsname;
bigparams.value = params;
else
bigparams = load(paramload);
fnsparams = fieldnames(bigparams);
bigparams = bigparams.(fnsparams{1,1});
params = bigparams.value;
if sum(strcmp(bigparams.name,paramsname)) ~= length(paramsname)
error('Parameter names in loaded file do not match those specified')
end
end
%load pf info
profileinfo = load([setupbasepath 'setup_info\profileinfo_' site '.mat']);
fns2 = fieldnames(profileinfo);
profileinfo = profileinfo.(fns2{1});
%load cfg info
cfginfo = load([setupbasepath 'setup_info\cfginfo_' site '_' stormid '_PF' num2str(pfno) '.mat']);
fns3 = fieldnames(cfginfo);
cfginfo = cfginfo.(fns3{1});
%get the names of the files copying across
dns1 = dir([setupoutputpath 'sb_' site '_' stormid '_PF' num2str(pfno) '\']);
dns1dirs = {dns1.name}; % Get a list of the subdirectories
dns1ind2 = ~ismember(dns1dirs,{'.','..'}); % Find index of subdirectories
dns1 = dns1(dns1ind2);
allfilelocs = cell(nruns,1);
% not done VVVV
%do the modelling
for ii = nrunst:nrunend
CFGname = ['PF' num2str(pfno) '_Run_' num2str(ii) '.CFG'];
tmpprintdir = [basebatchrunpath site '_' stormid '_PF' num2str(pfno) '\PF' num2str(pfno) '_Run_' num2str(ii) '\'];
if ~exist(tmpprintdir,'dir')
mkdir(tmpprintdir)
end
%now copy the setup files across
copyfile([setupoutputpath 'sb_' site '_' stormid '_PF' num2str(pfno) '\*'],tmpprintdir);
% now change the contents of CFG file
fin = fopen([setupbasepath 'base.CFG']) ;
fout = fopen([tmpprintdir 'temp.CFG'], 'w');
tline = fgetl(fin);
while ischar(tline)
%replace:
%filename
if (~isempty(strfind(tline, '%RPLCNAME%')))
tline = strrep(tline, '%RPLCNAME%', ['_Run_' num2str(ii)]);
end
%profile ID
if (~isempty(strfind(tline, '%RPLCPFID%')))
tline = strrep(tline, '%RPLCPFID%', ['PF' num2str(pfno)]);
end
%run number
if (~isempty(strfind(tline, '%RPLCRNUM%')))
tline = strrep(tline, '%RPLCRNUM%', num2str(ii));
end
%calculation cells
%number
if (~isempty(strfind(tline, '%RPLCNDX%')))
tline = strrep(tline, '%RPLCNDX%', num2str(cfginfo.NDX));
end
%landward boundary
if (~isempty(strfind(tline, '%RPLCXSTART%')))
tline = strrep(tline, '%RPLCXSTART%', num2str(cfginfo.XSTART));
end
%spacing between
if (~isempty(strfind(tline, '%RPLCDXC%')))
tline = strrep(tline, '%RPLCDXC%', num2str(cfginfo.DXC));
end
%calculation time
%number
if (~isempty(strfind(tline, '%RPLCNDT%')))
tline = strrep(tline, '%RPLCNDT%', num2str(cfginfo.NDT));
end
%steps
if (~isempty(strfind(tline, '%RPLCDT%')))
tline = strrep(tline, '%RPLCDT%', num2str(cfginfo.DT));
end
%steps
if (~isempty(strfind(tline, '%RPLCD50%')))
tline = strrep(tline, '%RPLCD50%', num2str(profileinfo.data(pfno).d50*1000));
end
%check for params
for aa = 1:length(paramsname)
if (~isempty(strfind(tline, ['%RPLC' paramsname{aa} '%'])))
tline = strrep(tline, ['%RPLC' paramsname{aa} '%'], num2str(params(ii,aa)));
end
end
%now print the final string
fprintf(fout,'%s\r\n',tline);
tline = fgetl(fin);
end
fclose(fin);
fclose(fout);
%rename CFG to original name
try
movefile([tmpprintdir 'temp.CFG'],[tmpprintdir CFGname])
catch
fileattrib([tmpprintdir 'temp.CFG'],'+w')
movefile([tmpprintdir 'temp.CFG'],[tmpprintdir CFGname])
end
%change file name
for jj = 1:length(dns1)
movefile([tmpprintdir dns1(jj).name],[tmpprintdir 'PF' num2str(pfno) '_Run_' num2str(ii) dns1(jj).name(end-3:end)])
end
%save location of file
allfilelocs{ii,1} = ['PF' num2str(pfno) '_Run_' num2str(ii) '\PF' num2str(pfno) '_Run_' num2str(ii)];
end
%write allfilelocs to .txt
fid = fopen([basebatchrunpath site '_' stormid '_PF' num2str(pfno) '\SBEACHrun_loc.txt'],'w');
for kk = 1:length(allfilelocs)
fprintf(fid,'%s\r\n',allfilelocs{kk});
end
fclose(fid);