-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEgg_vertical_concentration.m
265 lines (240 loc) · 11 KB
/
Egg_vertical_concentration.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
%==============================================================================
% FluEgg -Fluvial Egg Drift Simulator
%==============================================================================
% Copyright (c) 2013 Tatiana Garcia
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as published by
% the Free Software Foundation (currently at http://www.gnu.org/licenses/agpl.html)
% with a permitted obligation to maintain Appropriate Legal Notices.
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%%:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::%
%% Egg vertical concentration distribution %
%:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::%
%-------------------------------------------------------------------------%
% This function is part of the post-processing tools included in FluEgg. %
% This tool is used to generate a vertical concentration distribution of %
% eggs at a given downstream distance %
%-------------------------------------------------------------------------%
% %
%-------------------------------------------------------------------------%
% Created by : Tatiana Garcia %
% Last Modified : May 31, 2016 %
%-------------------------------------------------------------------------%
%
%:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::%
function varargout = Egg_vertical_concentration(varargin)
% EGG_VERTICAL_CONCENTRATION MATLAB code for Egg_vertical_concentration.fig
%
% Last Modified by GUIDE v2.5 05-Aug-2013 14:19:10
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Egg_vertical_concentration_OpeningFcn, ...
'gui_OutputFcn', @Egg_vertical_concentration_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
%% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function Egg_vertical_concentration_OpeningFcn(hObject, ~, handles, varargin)
diary('./results/FluEgg_LogFile.txt')
axes(handles.bottom); imshow('asiancarp.png');
%% Loads results
handleResults = getappdata(0,'handleResults');
ResultsSim = getappdata(handleResults,'ResultsSim');
CumlDistance = ResultsSim.CumlDistance;
Depth = ResultsSim.Depth;
% populate default parameters
set(handles.X_editBox,'String',round(CumlDistance(end)/2));
set(handles.Nlayers_editBox,'String',7);
%% Get user input data from current GUI
Dist_X = str2double(get(handles.X_editBox,'String'))*1000;% It is in km and then we convert it to m
Nlayers = str2double(get(handles.Nlayers_editBox,'String'));
cell = find((CumlDistance)*1000>=Dist_X);cell=cell(1);
%
H = Depth(cell);
Nodes = 0:-H/(Nlayers):-H;
Nodes = Nodes';
set(handles.LayerNodes_table,'Data',[Nodes (Nodes+H)/H]);%Nodes of the layers in model coordinates
%%
handles.output = hObject;
handles.update = 0;
guidata(hObject, handles);
function Egg_Vertical_Concentration_Distribution(hObject, eventdata, handles)
%% This function divides the water depth into layers and gets the nodes table
%% From Results Gui
handleResults = getappdata(0,'handleResults');
ResultsSim = getappdata(handleResults,'ResultsSim');
CumlDistance = ResultsSim.CumlDistance;
Depth = ResultsSim.Depth;
Nlayers = str2double(get(handles.Nlayers_editBox,'String'));
%% From Current Gui
Dist_X = str2double(get(handles.X_editBox,'String'))*1000;% It is in km and then we convert it to m
% display error if distance is outside of domain
if Dist_X>CumlDistance(end)*1000
ed = errordlg('The longitudinal distance you input is out of the domain','Error');
set(ed, 'WindowStyle', 'modal');
uiwait(ed);
end
% Find cell corresponding to selected distance
cell = find((CumlDistance)*1000>=Dist_X);cell=cell(1);
H = Depth(cell);
%% If user wants to add additional nodes to layers.
if handles.update==0
if handles.eddit==1 %if we need to add an extra node
Nodes = get(handles.LayerNodes_table,'Data');Nodes=Nodes(:,1);
extra = str2double(get(handles.ExtraNode_editBox,'String'));
Nodes = [extra; Nodes];
else
Nodes = 0:-H/(Nlayers):-H;
Nodes = Nodes';
end
else %handles.update==1 Ïf the table is eddited please update the Nodes
Nodes = get(handles.LayerNodes_table,'Data');Nodes=Nodes(:,1);
end
Nodes = sort(Nodes,'ascend');
sortedNodes = sort(Nodes,'descend');
set(handles.LayerNodes_table,'Data',[sortedNodes (sortedNodes+H)/H]);%Nodes of the layers in model coordinates
Nodes = get(handles.LayerNodes_table,'Data');
function Plot_Vertical_Distribution(hObject, eventdata, handles)
% This function generates the plot of the vertical concentration
% distribution
%% Error checking==========================================================
if isempty(str2double(get(handles.X_editBox,'String')))||isempty(str2double(get(handles.Nlayers_editBox,'String')))||isnan(str2double(get(handles.X_editBox,'String')))||isnan(str2double(get(handles.Nlayers_editBox,'String')))
msgbox('Empty input field. Please make sure all required fields are filled out correctly ','FluEgg Error: Empty fields','error');
return
end
if str2double(get(handles.X_editBox,'String'))<=0||str2double(get(handles.Nlayers_editBox,'String'))<=0
msgbox('Incorrect negative or zero value. Please make sure all required fields are filled out correctly','FluEgg Error: Incorrect negative or zero value','error');
return
end
%==========================================================================
%% From results GUI
handleResults = getappdata(0,'handleResults');
ResultsSim = getappdata(handleResults,'ResultsSim');
CumlDistance = ResultsSim.CumlDistance;
Depth = ResultsSim.Depth;
X = ResultsSim.X;
Z = ResultsSim.Z;
%% Input data from current GUI
Nodes = get(handles.LayerNodes_table,'Data');
Dist_X = str2double(get(handles.X_editBox,'String'))*1000;
cell = find((CumlDistance)*1000>=Dist_X);cell=cell(1);
H = Depth(cell);
%%
c = 0;
[~,b] = size(X);
check = 0;
%% Have all the eggs arrive to distance X???
for i=1:b
tind=find(X(:,i)>=Dist_X);
if length(tind)>=1
tind = tind(1);
c = c+1;
Z_Dist_X(c) = Z(tind,i);
check = check+1;
end
end
%% Display error if not all the eggs arrived to distance selected by user
if check==0
ed = errordlg('No eggs have arrived downstream distance X, please check the egg vertical distribution closer to the spawning location or run the model for a longer time','Error');
set(ed, 'WindowStyle', 'modal');
uiwait(ed);
else
z_over_H = Nodes;
z_over_H = z_over_H(:,2);
z_over_H = sort(z_over_H,'ascend');
Nodes = sort(Nodes(:,1),'ascend');
z_o_H_midd = z_over_H(1:end-1)+(diff(z_over_H)/2);%in dimensionless form z/H middle points
hightLayers = [abs(diff(Nodes))];%in m
N = histc(Z_Dist_X,Nodes);
N = N(1:end-1)';
%%
Cy = (N./hightLayers)*100;
Cavg = sum(N)/H;
CyCavg = Cy./Cavg;
Vertdist = [z_o_H_midd CyCavg];
%% Saving results as .mat
handleResults = getappdata(0,'handleResults');
fullpath_result = getappdata(handleResults,'fullpath_result');
save(fullpath_result,'Vertdist','-mat','-append');
%% Saving results as text file
handleResults = getappdata(0,'handleResults');
pathname = getappdata(handleResults,'pathname');
hdr = {'Z/H','Log Cz/Cavg'};
dlmwrite([pathname,'VertDist' '.txt'],[sprintf('%s\t',hdr{:}) ''],'');
dlmwrite([pathname,'VertDist' '.txt'],Vertdist,'-append','delimiter','\t','precision', 6);
%%
%if Batch==0 % in case of batch processing
set(0,'Units','pixels') ;
scnsize = get(0,'ScreenSize');
figure('Name','Egg vertical concentration distribution','Color',[1 1 1],...
'position',[8 scnsize(4)/2.6 scnsize(3)/2.1333 scnsize(4)/2]);
subaxis(1,1,1,'MR',0.035,'ML',0.09,'MB',0.13,'MT',0.075);
semilogx(CyCavg,z_o_H_midd,'MarkerFaceColor',[0 0 0],'MarkerSize',5,...
'Marker','square','LineWidth',2,'LineStyle','--','Color',[0 0 0]);
xlim([1 500])
ylim([0 1])
set(gca,'XTick',[0 10 50 100 500],'XTickLabel',[0 10 50 100 500]);
xlabel('log C_z/C [%]');
ylabel('z/h+1');
legend('FluEgg simulation','Location','NorthWest')
%% Grid ON-OFF
gridon = get(handles.Grid_on_checkbox,'Value'); %Need to comment this for now
if gridon==1
grid on
else
grid off
end
%% Message
h = msgbox([num2str(sum(N)) ' eggs passed by ' num2str(Dist_X/1000) ' km downstream from the virtual spawning location during the simulation time'],'FluEgg Message');
diary off
end
close(handles.figure1);
function varargout = Egg_vertical_concentration_OutputFcn(~, ~, handles)
diary off
varargout{1} = handles.output;
function X_editBox_Callback(hObject,eventdata, handles)
handles.eddit = 0;
Egg_Vertical_Concentration_Distribution(hObject, eventdata, handles);
function X_editBox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Nlayers_editBox_Callback(hObject, eventdata, handles)
handles.eddit = 0;
Egg_Vertical_Concentration_Distribution(hObject, eventdata, handles);
guidata(hObject, handles);
function Nlayers_editBox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ExtraNode_editBox_Callback(hObject, eventdata, handles)
function ExtraNode_editBox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Plot_button_Callback(hObject, eventdata, handles)
Plot_Vertical_Distribution(hObject, eventdata, handles)
function Grid_on_checkbox_Callback(hObject, eventdata, handles)
%If user eddits the table, for example add an additional node
function AddNode_button_Callback(hObject, eventdata, handles)
handles.eddit = 1;
Egg_Vertical_Concentration_Distribution(hObject, eventdata, handles);
guidata(hObject, handles);
function LayerNodes_table_CellEditCallback(hObject, eventdata, handles)
handles.update = 1;
Egg_Vertical_Concentration_Distribution(hObject, eventdata, handles);