-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialise.m
executable file
·223 lines (222 loc) · 8.58 KB
/
initialise.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Created: 10-Oct-2013 16:53:06
% Computer: GLNX86
% Matlab: 7.9
% Author: NK
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% dataToScan - either raw,CUTS,CONTS etc
% this function is an integral part of the whole EDAT algorithm.
% Its output "DD" appears throughout the algorithm. Think of DD as storage
% place for all meta-info. user-given paramters, directory tree of all
% files etc.. (see ..dataXXX/DD.mat )
% It also rescans all data and checks relevant data for eg missing files.
function DD = initialise(dataToScan)
%% basic settings
preInits;
%% user input
DD = getUserInput;
%% append time info
DD.time = catstruct(DD.time, timestuff(DD.time));
%% scan for files and append
DD.path = catstruct(DD.path,findfiles(DD));
%% scan data 2 be checked
if nargin>0
DD = checkData(DD,dataToScan);
end
%% load workers
DD.threads.num = init_threads(DD.threads.num);
%% show some info
dispFileStatus(DD.path)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function preInits
addpath(genpath('./'));
addpath(genpath('~/matlabFunctions'));
rehash;
clc;
format shortg;
dbstop if error;
set(0,'DefaultTextInterpreter', 'LaTeX');
warning('off','parallel:convenience:RunningJobsExist')
warning('off','parallel:job:DestructionOfUnavailableJob')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function DD = checkData(DD,toCheck)
%% init
DD.time = initChecks(DD,toCheck);
%% check for each needed file
DD.time.passed = checkForFiles(DD.time);
% TODO throw warning about missing files and that delT will be altered
% accordingly!
%% calc new del_t's in accord with missing files
checks.del_t_full = buildNewDt(DD.time);
%% append info
checks.passedTotal = sum(DD.time.passed);
checks.passed(checks.passedTotal) = struct;
temp = num2cell(DD.time.timesteps.n(DD.time.passed))';
[checks.passed.daynums] = deal(temp{:});
%% find corresponding filenames
[checks.passed] = getFnames(DD,checks,toCheck);
%% disp found files
filedisps(checks);
%% append
DD.checks = checks;
DD.checks.del_t = [nan; checks.del_t_full(~isnan(checks.del_t_full))];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function TT=initChecks(DD,toCheck)
checkNoDataCase(DD.path.(toCheck));
%% get filenames
TT = DD.time;
TT.existant.filesall = extractfield(DD.path.(toCheck).files,'name');
%% cat numbers in filenames (for speed)
dateOnly.s = regexp(cat(2,TT.existant.filesall{:}),'\d{8}','match');
dateOnly.n = cellfun(@(c) datenum(c,'yyyymmdd'),dateOnly.s);
TT.existant.fcats = cell2mat(cellfun(@(c) ['-' c],dateOnly.s,'uniformoutput',false));
%% correct start date if not exactly on existing time step or not within range
[~,ii] = min(abs(dateOnly.n-TT.from.num));
offset = dateOnly.n(ii) - TT.from.num ;
%% shift ".from"
TT.from.num = TT.from.num + offset;
TT.from.str = datestr(TT.from.num,'yyyymmdd');
%% shift ".till" also to keep const time span
TT.till.num = TT.till.num + offset;
TT.till.str = datestr(TT.till.num,'yyyymmdd');
%% correct end date if not exactly on existing time step or not within range
[~,ii] = min(abs(dateOnly.n-TT.till.num));
offset = dateOnly.n(ii) - TT.till.num ;
TT.till.num = TT.till.num + offset;
TT.till.str = datestr(TT.till.num,'yyyymmdd');
TT.span = TT.till.num - TT.from.num + 1;
disp(['corrected date range to ' datestr(TT.from.num) ' - ' datestr(TT.till.num)])
%% init time-steps vector
TT.timesteps.n = TT.from.num:TT.delta_t:TT.till.num;
TT.timesteps.s = datestr(TT.timesteps.n,'yyyymmdd');
%% init new delta t and "existing" vector (.passed)
TT.passed = false(numel(TT.timesteps.n),1);
TT.del_t = nan(size(TT.passed));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function checkNoDataCase(toCheck)
if isempty(toCheck.files)
error('err:noFiles',['no files found in ' toCheck.name])
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function filedisps(checks)
disp(['found '])
for ff=1:numel(checks.passed)
disp([checks.passed(ff).filenames])
end
disp(['total of ' num2str(numel(checks.passed))])
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pass = checkForFiles(TT)
pass = TT.passed;
for tt = 1:numel(TT.passed);
%% look for date string in cat'ed string of all filenames
if ~isempty(strfind(TT.existant.fcats, TT.timesteps.s(tt,:)))
pass(tt)=true;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function del_t = buildNewDt(TT)
del_t = nan(TT.span,1);
tempdelt = TT.delta_t; % regular timestep
for tt = 2:numel(TT.passed);
if ~TT.passed(tt)
%% flag del_t where not applicable
del_t(tt)=nan;
%% increase current del_t by one increment
tempdelt=tempdelt + TT.delta_t;
else
%% if file exists, use accumulated delt_t
del_t(tt) = tempdelt;
%% reset tempdelt
tempdelt = TT.delta_t;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function passed = getFnames(DD,checks,toCheck)
passed = checks.passed;
path = DD.path.(toCheck);
pattern = DD.pattern.fname;
timestr = cellfun(@(x) datestr(x,'yyyymmdd'),{checks.passed.daynums}','uniformoutput',false); % only passed ones
for cc = 1:numel(timestr)
ts = timestr{cc};
if strcmp(toCheck,'raw') % raw filenames relevant
passed(cc).filenames = [path.name, strrep(DD.map.in.fname, 'yyyymmdd',ts)];
else % build new filenames
geo = DD.map.in;
file.out = strrep(strrep(pattern, 'yyyymmdd',ts),'CUT',DD.pattern.prefix.(toCheck));
passed(cc).filenames = NSWE2nums(path.name,file.out,geo,ts);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function T = timestuff(T)
T.from.num = datenum(T.from.str,'yyyymmdd');
T.till.num = datenum(T.till.str,'yyyymmdd');
T.span = T.till.num-T.from.num+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function PATH = findfiles(DD)
%%
PATH = DD.path;
PATH.base = fileparts(pwd);
PATH.root = sprintf('%s/data%s/',PATH.base,PATH.OutDirBaseName);
PATH.code = [PATH.root, 'code/'];
PATH.codesubs = [PATH.root, 'code/SUBS/'];
PATH.cuts.name = [PATH.root, 'CUTS/'];
PATH.conts.name = [PATH.root, 'CONTS/'];
PATH.eddies.name = [PATH.root,'EDDYS/'];
PATH.tracks.name = [PATH.root,'TRACKS/'];
PATH.Rossby.name = [PATH.root,'ROSSBY/'];
PATH.analyzed.name = [PATH.root,'ANALYZED/'];
PATH.Rossby.Nfile = [PATH.Rossby.name,'N.cdf']; % TODO ?
%% create nonexistant dirs
mkDirs(PATH)
%%
[~,~,ext.raw] = fileparts(DD.map.in.fname);
patt = strsplit(DD.map.in.fname,'yyyymmdd');
PATH.raw.files = dir2([PATH.raw.name,patt{1},'*']);
PATH.protoMaps.file = [PATH.root, 'protoMaps.mat'];
PATH.meanSsh.file = [PATH.root, 'meanSSH.mat'];
PATH.cuts.files = dir2([PATH.cuts.name,'*.mat']);
PATH.conts.files = dir2([PATH.conts.name,'*.mat']);
PATH.eddies.files = dir2([PATH.eddies.name,'*.mat']);
PATH.tracks.files = dir2([PATH.tracks.name,'*.mat']);
PATH.analyzed.files = dir2([PATH.analyzed.name,'*.mat']);
PATH.Rossby.files = dir2([PATH.Rossby.name,'*.mat']);
%%
PATH.windowFile = [PATH.root 'window.mat'];
PATH.coriolisFile = [PATH.root 'coriolis.mat'];
end
% TODO save all as tempfiles eg CUTS etc too
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mkDirs(path)
%%
mkdirp(path.root);
mkdirp(path.code);
mkdirp(path.codesubs);
subdirs = {'cuts','conts','eddies','tracks','Rossby','analyzed'};
for ss=1:numel(subdirs)
mkdirp(path.(subdirs{ss}).name);
mkdirp([path.(subdirs{ss}).name,'tmp']);
end
%%
system(['cp ./*.m ' path.code]);
system(['cp ./SUBS/*.m ' path.codesubs]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dispFileStatus(p)
FN = fieldnames(p)';
for ii = 1:numel(FN);fn = FN{ii};
if isfield(p.(fn),'files') && isfield(p.(fn),'name')
disp(['found ' num2str(numel(p.(fn).files)) ' files in ' p.(fn).name]);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%