-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChlFnew.m
296 lines (236 loc) · 10 KB
/
ChlFnew.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
%% Calculate SIF at 760nm
% This is the new version that automatically finds the radiance
% measurement 10 minutes after the irradiance measurements
% Calculates SIF using both SFM (Meroni et al., 2009)
% and SVD (Guanter et al., 2012)
clear variables
clc
tic
coeff = importdata('/Volumes/XiYangBackUp/Projects/9.Fluorescence/1.coefficients/coeff.txt');
% Coeff of radiance is modified by Xi Yang on Mar 8, 2017, I multiplied by
% 3.14 as it seems was missing in the last round
% coeff(:,1) = smooth(coeff(:,1),0.1,'rloess');
%coeff(:,2) = smooth(coeff(:,2),0.1,'rloess')*3.14;
datapath = '/Volumes/XiYangBackUp/Data/13.Fluorescence/2013/measurements/';
rad_path = dir(fullfile(strcat(datapath,'rad/*.txt')));
irrad_path = dir(fullfile(strcat(datapath,'irrad/*.txt')));
file_no = size(rad_path);
time = NaN(file_no);
f760 = NaN(file_no);
rsq = NaN(file_no);
rmse = NaN(file_no);
ref = NaN(file_no(1),2048); % 2048 sampling intervals
sif_svd = NaN(file_no);
sif_error = NaN(file_no);
f760_error = NaN(file_no);
rad755 = NaN(file_no);
irrad755 = NaN(file_no);
irrad_time = NaN(file_no);
rad_time = NaN(file_no);
% %
% %Store the time of each measurement
% for i = 1:file_no(1)
% fname = fullfile(strcat(datapath,'rad/',rad_path(i).name));
% fname0 = fullfile(strcat(datapath,'irrad/',irrad_path(i).name));
%
% raw_time = strread(rad_path(i).name,'%s','delimiter','_');
% raw_time_irrad = strread(irrad_path(i).name,'%s','delimiter','_');
%
% rad_time(i) = datenum([str2double(raw_time{2}) str2double(raw_time{3})...
% str2double(raw_time{4}) str2double(raw_time{5}) str2double(raw_time{6}) 0]) - datenum( [2013 1 1 0 0 0]) + 1;
% irrad_time(i) = datenum([str2double(raw_time_irrad{2}) str2double(raw_time_irrad{3})...
% str2double(raw_time_irrad{4}) str2double(raw_time_irrad{5}) str2double(raw_time_irrad{6}) 0]) - datenum( [2013 1 1 0 0 0]) + 1;
% end
%
% save('hf_2013.mat','rad_time','irrad_time');
% 4422-4559 are DOY 175
load('/Volumes/XiYangBackUp/Projects/9.Fluorescence/11.Matlab_data/hf_2013.mat','rad_time','irrad_time')
date0 = datenum(2013,1,1);
time1.year = 2013;
%4461 = DOY 175 around 13:30
%4430 = DOY 175 around 11:00
%4794
irrad_all = nan(1,2048);
rad_all = nan(1,2048);
for j=1:file_no(1) %1:file_no(1) %1:file_no(1) %1:file_no(1) %1:file_no(1) %1:file_no(1) 4430:4430
tenmin = datenum([2013 1 1 0 10 0]) - datenum([2013 1 1 0 0 0]);
irrad_time_tmp = irrad_time(j);
rad_time_sub = find((rad_time - irrad_time_tmp)>0 & (rad_time - irrad_time_tmp)<tenmin);
% if the number of subscript that fit the above criteria is less than
% one, then quit the loop
if (numel(rad_time_sub) == 0)
continue
end
% open the irradiance file
fname_irrad = fullfile(strcat(datapath,'irrad/',irrad_path(j).name));
temp_irrad = dlmread(fname_irrad, ' ',3, 0);
% open the radiance file
fname_rad = fullfile(strcat(datapath,'rad/',rad_path(rad_time_sub(1)).name));
temp_rad = dlmread(fname_rad, ' ',3, 0);
irrad_all(j+1,:) = temp_irrad(:,3).*coeff(:,1);
rad_all(j+1,:) = temp_rad(:,3).*coeff(:,2);
time_all(j+1) = irrad_time_tmp;
wl = temp_rad(:,5);
% for 2012 data, shift irrad to the right by 3
% temp_irrad(:,3) = circshift(temp_irrad(:,3),3);
% SFM method =============
ref(j,:) = (temp_rad(:,3).*coeff(:,2))./((temp_irrad(:,3).*coeff(:,1))./pi());
wl = temp_rad(:,5);
% Depth of O2A band
% irrad_o2a(j) = mean(temp_irrad(wl>=758 & wl<=759,3).*coeff(wl>=758 & wl<=759,1)) - mean(temp_irrad(wl>=761 & wl<=762,3).*coeff(wl>=761 & wl<=762,1));
% rad_o2a(j) = mean(temp_rad(wl>=758 & wl<=759,3).*coeff(wl>=758 & wl<=759,2)) - mean(temp_rad(wl>=761 & wl<=762,3).*coeff(wl>=761 & wl<=762,2));
%
% irrad_758(j) = mean(temp_irrad(wl>=758 & wl<=759,3).*coeff(wl>=758 & wl<=759,1));
% rad_758(j) = mean(temp_rad(wl>=758 & wl<=759,3).*coeff(wl>=758 & wl<=759,2));
rad755(j,1) = mean(temp_rad(wl>=755 & wl<=756,3).*coeff(wl>=755 & wl<=756,2));
irrad755(j,1) = mean(temp_irrad(wl>=755 & wl<=756,3).*coeff(wl>=755 & wl<=756,1));
roi = temp_rad(:,5)>759.00 & temp_rad(:,5)<767.76;
% For O2B: 682.00-692.00
% roi = temp_rad(:,5)>682.00 & temp_rad(:,5)<692.00;
%ref = rad'./irrad';
% =====================================================================
% =====================================================================
% Calculate SZA at the time of measurement
dstr = datestr(irrad_time_tmp+date0-1,29);
time1.month = str2double(dstr(6:7));
time1.day = str2double(dstr(9:10));
time1.min = 0;
time1.sec = 0;
time1.UTC = -5;
location.latitude = 42.5;
location.longitude = -72.2;
location.altitude = 100;
time1.hour = 24.0*(irrad_time_tmp - fix(irrad_time_tmp));
sun_pos = sun_position(time1,location);
sun_zenith = sun_pos.zenith;
phase_angle(j) = acos(cos(sun_zenith*pi()/180)*cos(60*pi()/180)+sin(sun_zenith*pi()/180)*sin(60*pi()/180)*cos(sun_pos.azimuth*pi()/180));
% =====================================================================
% =====================================================================
wavelength = temp_rad(roi,5);
rad = temp_rad(roi,3).*coeff(roi,2);
% Here irradiance is multiplied by cos(SZA) cos(sun_zenith*pi()/180)*
irrad = temp_irrad(roi,3).*coeff(roi,1);
xdata = [wavelength irrad]';
ydata = rad';
sza(j) = sun_zenith;
% SFM fitting
x0 = [0.1,0.0005,1,-0.001];
lb = [-inf,0,-inf,-inf];
ub = [inf,inf,inf,0];
options = optimset('TolX',1e-10,'TolFun',1e-10,'Display','off');
[x,SSresid] = lsqcurvefit(@radtrans,x0,xdata,ydata,lb,ub,options);
SStotal = (length(ydata)-1) * var(ydata);
rsq(j) = 1 - SSresid/SStotal;
rmse(j) = (SSresid/(length(ydata)))^(0.5);
% For O2A: center 760
f760(j) = 760.0*x(4)+x(3);
[beta,resid,J,Sigma] = nlinfit(xdata,ydata,@radtrans,x0);
%[ci,se] = nlparci(beta,resid,'covar',Sigma); % need to modify nlparci to include se
%f760_error(j) = sqrt((760*se(4))^2+se(3)^2); %1-sigma error of SIF
% title(num2str(f760(j),'%f5.2'))
% yyaxis left
% plot(wavelength,irrad,'r-','LineWidth',2)
%
% yyaxis right
% plot(wavelength,rad,'b-','LineWidth',2)
%f760_error(j) = sqrt((760*se(4))^2+se(3)^2); %1-sigma error of SIF
% For O2B: center: 687
% f687(j) = 687.0*x(5)+x(6);
%
% [beta,resid,J,Sigma] = nlinfit(xdata,ydata,@radtrans,x0);
% [ci,se] = nlparci(beta,resid,'covar',Sigma);
% f687_error(j) = sqrt((687*se(4))^2+se(3)^2); %1-sigma error of SIF
%
% % %SVD method=================
% % % temp_irrad(:,3)'
% % spectra = struct('irrad', temp_final,...
% % 'rad', temp_rad(:,3)',...
% % 'ircoeff', coeff(:,1),...
% % 'rcoeff', coeff(:,2),...
% % 'wl', temp_rad(:,5));
% % WL_range = [745.00,780.00]; % Including O2A: 717.00 780.00; 745.00 780.00
% % SIF_result = SIF_SVD(spectra,WL_range,2,1,0.02,6);
% % sif_svd(j) = SIF_result.SIF;
% % sif_error(j) = SIF_result.SIF_error;
% % % sif_day = [time_day,sif_svd(rad_idx)];
% %
% %
% % record the time -- if the run went through
time(j) = irrad_time(j);
%
end
%save('/Volumes/XiYangBackUp/src/HF_Fluo_data/spectra_samples.mat','irrad_all','rad_all','time_all','wl');
%clear variables
% Store the reflectance
ref_final = [time, ref];
ref_final_time = sortrows(ref_final,1);
raw_ref_result = ref_final_time;
% Store SIF results
final_result = [time, f760, rsq, rmse, f760_error, rad755, irrad755];
% Time selection
final_result_time = sortrows(final_result,1);
raw_final_result = final_result_time;
save('/Volumes/XiYangBackUp/Projects/9.Fluorescence/11.Matlab_data/SIF760_result_2013_with750nm_Apr12-2017_ModifiedCoeff.mat');
% final_sfm = [time,f760,rsq,rmse];
% final_sfm = sortrows(final_sfm,1);
% final_svd = [time,sif_svd,sif_error];
% final_svd = sortrows(final_svd,1);
% save('hf_2013_svd.mat','rad_time','irrad_time','final_svd','final_sfm');
% SVD method
% for kk = 175:175
%
% irrad_idx = find(irrad_time >= kk+0.3555 & irrad_time <= kk+0.3565);
% temp_irrad_day = NaN(length(irrad_idx),2048);
%
% if isempty(irrad_idx)
% continue
% else
% for irrad_ii = 1:length(irrad_idx)
% fname_irrad = fullfile(strcat(datapath,'irrad/',irrad_path(irrad_idx(irrad_ii)).name));
% temp_irrad = dlmread(fname_irrad, ' ',3, 0);
% temp_irrad_day(irrad_ii,:) = temp_irrad(:,3);
% end
% end
%
% rad_idx = find(rad_time >= kk+0.3555 & rad_time <= kk+0.3565);
% temp_rad_day = NaN(length(rad_idx),2048);
%
% if isempty(rad_idx)
% continue
% else
% for rad_ii = 1:length(rad_idx)
% fname_rad = fullfile(strcat(datapath,'rad/',rad_path(rad_idx(rad_ii)).name));
% temp_rad = dlmread(fname_rad, ' ',3, 0);
% temp_rad_day(rad_ii,:) = temp_rad(:,3);
% end
% end
%
% time_day = rad_time(rad_idx);
%
% spectra = struct('irrad', temp_irrad_day,...
% 'rad', temp_rad_day,...
% 'ircoeff', coeff(:,1),...
% 'rcoeff', coeff(:,2),...
% 'wl', temp_rad(:,5));
% WL_range = [745.00,775.00];
% SIF = SIF_SVD(spectra,WL_range);
% sif_svd(rad_idx) = SIF.SIF;
% sif_error(rad_idx) = SIF.SIF_error;
% sif_day = [time_day,sif_svd(rad_idx)];
%
% end
% % Store the reflectance
% ref_final = [time, ref];
% ref_final_time = sortrows(ref_final,1);
% raw_ref_result = ref_final_time;
%
% % Store SIF results
% final_result = [time, f760, rsq, rmse];
%
% % Time selection
% final_result_time = sortrows(final_result,1);
%
% raw_final_result = final_result_time;
%
% %save('SIF760_2014_result.mat');
toc