Skip to content

Commit 88f64b9

Browse files
committed
Added functions to do all the work for the user
1 parent 7a7b8c8 commit 88f64b9

File tree

4 files changed

+444
-18
lines changed

4 files changed

+444
-18
lines changed

complete_run.m

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
function complete_run(ssh_path, eddies_save_path, tracks_save_path, fake_eddies, viewer_data_save_path, viewer_path, varargin)
2+
% NOTE: Please provide full path names for all paths.
3+
%
4+
% The purpose of this function is to automate the entire gauntlet of
5+
% running all of the code to go from having only MATLAB SSH data to getting
6+
% the data set up to be viewed on the tracks viewer. This function should
7+
% automate the following steps:
8+
% 1. Scan SSH data for eddies
9+
% 2. Save detected eddies
10+
% 3. Build LNN tracks for these eddies (Possibly with fake eddies, if the
11+
% user of the script wants fake eddies)
12+
% 4. Format and process saved data so that it can be viewed in Hung's
13+
% viewer that we include
14+
%
15+
% NOTE: The process of turning NetCDF file data into MATLAB data is
16+
% partially left up to the user to do. A function will be provided that
17+
% will accomplish this for the user, but the user will have to provide the
18+
% variable names for what the SSH data is called, what the Latitude data is
19+
% called, and what the Longitude data is called. Also, the user will either
20+
% have to have this function generate a dates array (that is, what day each
21+
% day of SSH data relates to) for it, or they will have to generate their
22+
% own. Code will be provided in the NetCDf to MATLAB data function to
23+
% assist with generating this dates array.
24+
% Also, this script will not open the viewer for the user, it will leave
25+
% that action up to the user. To open the viewer, all the user has to do is
26+
% run the start_track_viewer script inside the tracks_viewer directory.
27+
28+
p = inputParser;
29+
defaultLat = NaN;
30+
defaultLon = NaN;
31+
defaultDates = NaN;
32+
defaultAreaMap = NaN;
33+
defaultScanType = 'v2';
34+
defaultMinPixelSize = 9;
35+
defaultThresholdStep = 0.05;
36+
defaultSSHUnits = 'centimeters';
37+
defaultPaddingFlag = true;
38+
39+
addRequired(p, 'ssh_dir');
40+
addRequired(p, 'eddies_save_path');
41+
addRequired(p, 'fake_eddies');
42+
43+
addParameter(p, 'lat', defaultLat);
44+
addParameter(p, 'lon', defaultLon);
45+
addParameter(p, 'dates', defaultDates);
46+
addParameter(p, 'area_map', defaultAreaMap);
47+
addParameter(p, 'scan_type', defaultScanType);
48+
addParameter(p, 'minimumArea', defaultMinPixelSize, @isnumeric);
49+
addParameter(p, 'thresholdStep', defaultThresholdStep, @isnumeric);
50+
addParameter(p, 'isPadding', defaultPaddingFlag);
51+
addParameter(p, 'sshUnits', defaultSSHUnits);
52+
53+
parse(p, ssh_path, eddies_save_path, fake_eddies, varargin{:});
54+
55+
lat = p.Results.lat;
56+
lon = p.Results.lon;
57+
dates = p.Results.dates;
58+
area_map = p.Results.area_map;
59+
scan_type = p.Results.scan_type;
60+
minimumArea = p.Results.minimumArea;
61+
thresholdStep = p.Results.thresholdStep;
62+
isPadding = p.Results.isPadding;
63+
SSH_Units = p.Results.sshUnits;
64+
if ~strcmp(ssh_path(end), '/')
65+
ssh_path = strcat(ssh_path, '/');
66+
end
67+
if ~strcmp(eddies_save_path(end), '/')
68+
eddies_save_path = strcat(eddies_save_path, '/');
69+
end
70+
if ~strcmp(tracks_save_path(end), '/')
71+
tracks_save_path = strcat(tracks_save_path, '/');
72+
end
73+
if ~strcmp(viewer_data_save_path(end), '/')
74+
viewer_data_save_path = strcat(viewer_data_save_path, '/');
75+
end
76+
ssh_names = get_ssh_names(ssh_path);
77+
if isnan(lat)
78+
vars = load([ssh_path, 'lat.mat']);
79+
names = fieldnames(vars);
80+
lat = vars.(names{1});
81+
end
82+
if isnan(lon)
83+
vars = load([ssh_path, 'lon.mat']);
84+
names = fieldnames(vars);
85+
lon = vars.(names{1});
86+
end
87+
if isnan(dates)
88+
vars = load([ssh_path, 'dates.mat']);
89+
names = fieldnames(vars);
90+
dates = vars.(names{1});
91+
end
92+
if isnan(area_map)
93+
vars = load([ssh_path, 'area_map.mat']);
94+
names = fieldnames(vars);
95+
area_map = vars.(names{1});
96+
end
97+
if ~exist(eddies_save_path, 'dir')
98+
mkdir(eddies_save_path);
99+
end
100+
old_path = cd('eddyscan/');
101+
disp('Scanning SSH data for eddies');
102+
for i = 1:length(ssh_names)
103+
disp(['Iteration ', num2str(i)]);
104+
vars = load(ssh_names{i});
105+
names = fieldnames(vars);
106+
ssh_data = vars.(names{1});
107+
ant_eddies = scan_single(ssh_data, lat, lon, dates(i), 'anticyc', scan_type, area_map, 'sshUnits', SSH_Units,...
108+
'thresholdStep', thresholdStep, 'minimumArea', minimumArea, 'isPadding', isPadding);
109+
par_save([eddies_save_path, 'anticyc_', num2str(dates(i)), '.mat'], ant_eddies);
110+
cyc_eddies = scan_single(ssh_data, lat, lon, dates(i), 'cyclonic', scan_type, area_map, 'sshUnits', SSH_Units,...
111+
'thresholdStep', thresholdStep, 'minimumArea', minimumArea, 'isPadding', isPadding);
112+
par_save([eddies_save_path, 'cyclonic_', num2str(dates(i)), '.mat'], cyc_eddies);
113+
end
114+
cd(old_path);
115+
% Track eddies and save tracks
116+
if ~exist(tracks_save_path, 'dir')
117+
mkdir(tracks_save_path);
118+
end
119+
if ~exist(viewer_data_save_path, 'dir')
120+
mkdir(viewer_data_save_path);
121+
end
122+
old_path = cd('track_lnn/');
123+
disp('Tracking eddies');
124+
if strcmp(fake_eddies, 'yes')
125+
anticyclonic_tracks = tolerance_track_lnn(eddies_save_path, 'anticyc', 1, 1, minimumArea);
126+
cyclonic_tracks = tolerance_track_lnn(eddies_save_path, 'cyclonic', 1, 1, minimumArea);
127+
modified_eddies_path = [viewer_data_save_path, 'modified_eddies/'];
128+
modified_tracks_path = [viewer_data_save_path, 'modified_tracks/'];
129+
if ~exist(modified_eddies_path, 'dir')
130+
mkdir(modified_eddies_path);
131+
end
132+
if ~exist(modified_tracks_path, 'dir')
133+
mkdir(modified_tracks_path);
134+
end
135+
disp('Modifying eddy data to include fake eddies (for the sake of the tracks)');
136+
process_eddies_and_tracks_tolerance('anticyc', dates, eddies_save_path, anticyclonic_tracks, modified_eddies_path, modified_tracks_path);
137+
process_eddies_and_tracks_tolerance('cyclonic', dates, eddies_save_path, cyclonic_tracks, modified_eddies_path, modified_tracks_path);
138+
vars = load([modified_tracks_path, 'anticyc_tracks_processed.mat']);
139+
s = fieldnames(vars);
140+
anticyclonic_tracks = vars.(s{1});
141+
vars = load([modified_tracks_path, 'cyclonic_tracks_processed.mat']);
142+
s = fieldnames(vars);
143+
cyclonic_tracks = vars.(s{1});
144+
eddies_save_path = modified_eddies_path;
145+
else
146+
anticyclonic_tracks = track_lnn(eddies_save_path, 'anticyc', 1); % Change this so it can be an optional argument in input parser
147+
cyclonic_tracks = track_lnn(eddies_save_path, 'cyclonic', 1); % Same as above
148+
end
149+
save([tracks_save_path, 'anticyclonic_tracks.mat'], 'anticyclonic_tracks');
150+
save([tracks_save_path, 'cyclonic_tracks.mat'], 'cyclonic_tracks');
151+
% Set up data for viewer
152+
disp('Preparing data for viewer');
153+
prepare_eddy_data_for_viewer(viewer_data_save_path, dates, eddies_save_path, cyclonic_tracks, anticyclonic_tracks, ssh_path);
154+
cd(old_path);
155+
% Deleting old viewer files
156+
files = dir([viewer_path, 'SSH/']);
157+
old_path = cd([viewer_path, 'SSH/']);
158+
for i = 1:length(files)
159+
file = files(i).name;
160+
if ~strcmp(file, '.') && ~strcmp(file, '..')
161+
if files(i).isdir
162+
rmdir(file, 's');
163+
else
164+
delete(file);
165+
end
166+
end
167+
end
168+
files = dir([viewer_path, 'track_data/']);
169+
cd([viewer_path, 'track_data/']);
170+
for i = 1:length(files)
171+
file = files(i).name;
172+
if ~strcmp(file, '.') && ~strcmp(file, '..')
173+
if files(i).isdir
174+
rmdir(file, 's');
175+
else
176+
delete(file);
177+
end
178+
end
179+
end
180+
files = dir([viewer_path, 'attributes/']);
181+
cd([viewer_path, 'attributes/']);
182+
for i = 1:length(files)
183+
file = files(i).name;
184+
if ~strcmp(file, '.') && ~strcmp(file, '..')
185+
if files(i).isdir
186+
rmdir(file, 's');
187+
else
188+
delete(file);
189+
end
190+
end
191+
end
192+
files = dir([viewer_path, 'pixels_data/']);
193+
cd([viewer_path, 'pixels_data/']);
194+
for i = 1:length(files)
195+
file = files(i).name;
196+
if ~strcmp(file, '.') && ~strcmp(file, '..')
197+
if files(i).isdir
198+
rmdir(file, 's');
199+
else
200+
delete(file);
201+
end
202+
end
203+
end
204+
cd(old_path);
205+
% Copying new viewer files into place
206+
files = dir(ssh_path);
207+
for i = 1:length(files)
208+
file = files(i).name;
209+
if ~strcmp(file, '.') && ~strcmp(file, '..')
210+
copyfile([ssh_path, file], [viewer_path, 'SSH/', file]);
211+
end
212+
end
213+
files = dir(tracks_save_path);
214+
for i = 1:length(files)
215+
file = files(i).name;
216+
if ~strcmp(file, '.') && ~strcmp(file, '..')
217+
copyfile([tracks_save_path, file], [viewer_path, 'track_data/', file]);
218+
end
219+
end
220+
files = dir(viewer_data_save_path);
221+
for i = 1:length(files)
222+
file = files(i).name;
223+
if ~strcmp(file, '.') && ~strcmp(file, '..')
224+
if ~files(i).isdir
225+
if strcmp(file, 'anticyclonic_tags.mat') || strcmp(file, 'cyclonic_tags.mat')
226+
copyfile([viewer_data_save_path, file], [viewer_path, 'track_data/', file]);
227+
else
228+
copyfile([viewer_data_save_path, file], [viewer_path, 'attributes/', file]);
229+
end
230+
end
231+
end
232+
end
233+
files = dir([viewer_data_save_path, 'pixel_data/']);
234+
for i = 1:length(files)
235+
file = files(i).name;
236+
if ~strcmp(file, '.') && ~strcmp(file, '..')
237+
copyfile([viewer_data_save_path, 'pixel_data/', file], [viewer_path, 'pixels_data/', file]);
238+
end
239+
end
240+
241+
end
242+
243+
function par_save(path, eddies)%#ok
244+
save(path, 'eddies');
245+
end
246+
247+
function [ssh_names] = get_ssh_names(path)
248+
% path is the path to the eddies directory
249+
% type is anticyclonic or cyclonic
250+
if ~strcmp(path(end), '/')
251+
path = strcat(path, '/');
252+
end
253+
files = dir(path);
254+
x = 0;
255+
for i = 1:length(files)
256+
if ~isempty(strfind(files(i).name, 'ssh_'))
257+
x = x + 1;
258+
end
259+
end
260+
ssh_names = cell(x, 1);
261+
x = 1;
262+
for i = 1:length(files)
263+
if files(i).isdir && ~isequal(files(i).name, '.') && ~isequal(files(i).name, '..')
264+
rec_names = get_ssh_names([path, files(i).name, '/']);
265+
for j = 1:length(rec_names)
266+
ssh_names{x} = rec_names{j};
267+
x = x + 1;
268+
end
269+
continue;
270+
end
271+
file = files(i).name;
272+
[~, name, ext] = fileparts([path, file]);
273+
if ~isempty(strfind(name, 'ssh_')) && strcmp(ext, '.mat')
274+
ssh_names{x} = [path, file];
275+
x = x + 1;
276+
end
277+
end
278+
end

0 commit comments

Comments
 (0)