This repository was archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessedData.m
More file actions
2143 lines (1729 loc) · 101 KB
/
Copy pathProcessedData.m
File metadata and controls
2143 lines (1729 loc) · 101 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% PROCESSEDDATA is a class that contains the data for AC Data
% Processing.
%
%
% Other m-files required: spectral_unsmooth, ResidTempScatCorr,
% AttTempCorr, getPsiT
% Other files required: 'Sullivan_etal_2006_instrumentspecific.xls'
% Subfunctions: spectral_unsmooth, ResidTempScatCorr, AttTempCorr, getPsiT
% MAT-files required: none
%
% See also: spectral_unsmooth, ResidTempScatCorr, AttTempCorr, getPsiT
% Author: Wendy Neary
% MISCLab, University of Maine
% email address: wendy.neary@maine.edu
% Website: http://misclab.umeoce.maine.edu/index.php
% May 2015; Last revision: 13-08-15
% July 2016 - Major revision
% ------------- BEGIN CODE --------------
classdef ProcessedData < handle
properties (Access = private)
numWavelengths; %# number of wavelengths for this ac meter
end % end private properties
properties
var %# all processing variables
meta %# processing metadata
levelsMap %# a map of the level names and numbers
levelsFlags %# a set of flags showing which levels are in use
cExists %# boolean
aExists %# boolean
L %# logger
end % end public properties
methods
%% ----------------------------------------------------------------
% Constructor
% -----------------------------------------------------------------
%% ProcessedData
function obj = ProcessedData( DeviceFileIn, IngestParamsIn )
%#ProcessedData is the constructor for the object
%#
%# SYNOPSIS obj = ProcessedData( DeviceFileIn, IngestParamsIn )
%# INPUT DeviceFileIn - a ACDeviceFile object
%# IngestParamsIn - the params struct from readIngestParameters
%# OUTPUT obj - the object
%#
%%% Pre-initialization %%%
% Any code not using output argument (obj)
%%% Object Initialization %%%
% Call superclass constructor before accessing object
% This statement cannot be conditionalized
%%% Post-initialization %%%
% Any code, including access to the object
obj.L = log4m.getLogger();
obj.L.debug('ProcessedData.ProcessedData()','Created object');
if nargin > 0
% set device file
if isa( DeviceFileIn, 'ACDeviceFile' )
obj.meta.DeviceFile = DeviceFileIn;
else
error('Supply DeviceFile object')
end
if isa( IngestParamsIn, 'struct' )
obj.meta.Params = IngestParamsIn;
else
error('Supply Ingest Params')
end
end % if nargin > 0
% set number of wavelengths from device file
obj.meta.numWavelengths = str2num(obj.meta.DeviceFile.NumberWavelengths);
keySet = {'raw', ... %L1
'preprocessed', ...%L2
'binned', ... %L3
'filtered', ... %L4
'particulate', ... %L5
'matchedWL', ... %L6
'corrected', ... %L7
'unsmoothed', ... %L8
'flagsApplied'}; %L9 MAYBE
valueSet = {'L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9'};
obj.levelsMap = containers.Map(keySet, valueSet);
end %#ProcessedData
%% ----------------------------------------------------------------
% Getter and Setter for Variables
% -----------------------------------------------------------------
%% setVar
function obj = setVar( obj, varNameIn, levelNameIn, dataNameIn, dataIn )
%#setVar sets any name/data pair into the right level in .var
%#
%# SYNOPSIS obj = setVar( obj, varNameIn, levelNameIn, dataNameIn, dataIn )
%# INPUT obj - the object
%# varNameIn - the var of the name/data pair being set, i.e. "ap"
%# levelNameIn - the name of the level to set the data in, i.e. "corrected"
%# dataNameIn - the name of the actual data, i.e. "timestamps"
%# dataIn - the actual data
%# OUTPUT obj - the object
%#
% debug statements in this code are printing regardless?
% obj.L.debug('ProcessedData.setVar()', 'in method');
level = obj.levelsMap(levelNameIn);
obj.var.(varNameIn).(level).(dataNameIn) = dataIn;
%find index number for this level
levelInt = level(:,2); % get just the number off
levelInt = str2num(levelInt);
if isempty(obj.levelsFlags)
% if no flags exist at all yet -- create flags and index
% obj.L.debug('ProcessedData.setVar()', 'have no flags at all');
levelFlags = 1:9;
levelIndex = zeros(size(levelFlags));
levelIndex = logical(levelIndex);
obj.levelsFlags.(varNameIn).levelFlags = levelFlags;
obj.levelsFlags.(varNameIn).levelIndex = levelIndex;
obj.levelsFlags.(varNameIn).levelIndex(levelInt) = true;
else
% obj.L.debug('ProcessedData.setVar()','we do have flags');
if ~isfield(obj.levelsFlags, varNameIn )
% it's not empty - but need to check if we have this level
% obj.L.debug('ProcessedData.setVar()','have flags but not for this data');
levelFlags = 1:9;
levelIndex = zeros(size(levelFlags));
levelIndex = logical(levelIndex);
obj.levelsFlags.(varNameIn).levelFlags = levelFlags;
obj.levelsFlags.(varNameIn).levelIndex = levelIndex;
obj.levelsFlags.(varNameIn).levelIndex(levelInt) = true;
else
% just update correct level
% obj.L.debug('ProcessedData.setVar()','have flags for this data');
obj.levelsFlags.(varNameIn).levelIndex(levelInt) = true;
end;
end
end; %#setVar
%% getVar
function [varOut] = getVar( obj, varargin )
%#getVar gets any data out of the right level in .var
%#
%# SYNOPSIS [varOut] = getVar( obj, varargin )
%# INPUT obj - the object
%# varNameIn - the var of the name/data pair being set, i.e. "ap"
%# levelNameIn - the name of the level to set the data in, i.e. "corrected"
%# dataNameIn - the name of the actual data, i.e. "timestamps"
%# OUTPUT varOut - the data
%#
% debug statements in this code are printing regardless?
% obj.L.debug('ProcessedData.getVar()', 'in method');
% check inputs
% check varName is one that exists. Create if it doesn't exist?
varNameIn = '';
levelNameIn = '';
dataNameIn = '';
% loop through name/value pairs
if (~isempty(varargin))
iArg = 1;
while iArg < nargin
if strcmpi(varargin{iArg}, 'name')
varNameIn = varargin{iArg+1};
elseif strcmpi(varargin{iArg}, 'level')
levelNameIn = varargin{iArg+1};
elseif strcmpi(varargin{iArg}, 'data')
dataNameIn = varargin{iArg+1};
else
obj.L.error('ProcessedData.getVar', 'invalid argument');
end;
iArg = iArg + 2;
end; % while loop
else
obj.L.error('ProcessedData.getVar', 'no argument');
end;
% get variable - by name, level and datatype
if ~isempty(varNameIn) && ~isempty(levelNameIn) && ~isempty(dataNameIn)
% lookup level
level = obj.levelsMap(levelNameIn);
% check this data field exists for this level of data
if isfield(obj.var.(varNameIn).(level), dataNameIn )
varOut = obj.var.(varNameIn).(level).(dataNameIn);
% obj.L.debug('ProcessedData.getVar()', ...
% sprintf('level: %s', level));
else
obj.L.error('ProcessedData.getVar', 'invalid data name');
end;
elseif ~isempty(varNameIn) && ~isempty(levelNameIn) && isempty(dataNameIn)
% obj.L.debug('ProcessedData.getVar()', ...
% 'have both name and level, don''t need data -- getting specific level');
% lookup level
level = obj.levelsMap(levelNameIn);
if isfield(obj.var.(varNameIn), level )
varOut = obj.var.(varNameIn).(level);
else
obj.L.error('ProcessedData.getVar', 'invalid level');
end;
elseif ~isempty(varNameIn) && isempty(levelNameIn) && isempty(dataNameIn)
% obj.L.debug('ProcessedData.getVar()', ...
% 'have name, don''t need data -- need to find most recent level');
% find most recent level
idx = obj.levelsFlags.(varNameIn).levelIndex;
thisLevels = obj.levelsFlags.(varNameIn).levelFlags(idx);
% if we have data for this var:
if ~isempty(thisLevels)
maxLevel = max(thisLevels);
level = sprintf('L%u', maxLevel);
varOut = obj.var.(varNameIn).(level);
else
obj.L.error('ProcessedData.getVar()', 'no data for this variable');
end;
elseif ~isempty(varNameIn) && isempty(levelNameIn) && ~isempty(dataNameIn)
% if we have a variable name and a data field name, but no
% specific level, get the highest level we have for this
% data field for this variable
idx = obj.levelsFlags.(varNameIn).levelIndex;
thisLevels = obj.levelsFlags.(varNameIn).levelFlags(idx);
% if we have data for this var:
if ~isempty(thisLevels)
minLevel = min(thisLevels);
maxLevel = max(thisLevels);
for iLevel = minLevel:maxLevel
level = sprintf('L%u', iLevel);
levelExists = isfield(obj.var.(varNameIn), level);
if levelExists
dataExists = isfield(obj.var.(varNameIn).(level), dataNameIn );
if dataExists % at this level
varOut = obj.var.(varNameIn).(level).(dataNameIn);
% obj.L.debug('ProcessedData.getVar()', ...
% sprintf('setting data to level: %s', level));
else
% obj.L.debug('ProcessedData.getVar()', ...
% 'data doesn''t exist at this level');
end;
else
% obj.L.debug('ProcessedData.getVar()', ...
% 'level doesn''t exist');
end;
end; % for loop through levels
else
obj.L.error('ProcessedData.getVar()', 'no data for this variable');
end; %isempty(thisLevels)
else
obj.L.error('ProcessedData.getVar', 'problem');
end; %~isempty(varNameIn) && ~isempty(levelNameIn) && ~isempty(dataNameIn)
end; %#getVar
%% --------------------------------------------------------------------
% Processing methods
% ---------------------------------------------------------------------
%% processBins
function obj = processBins(obj, varargin)
%#processBins - Take L2 "preprocessed" data and bin it -> L3 "binned".
%#If no argsin are provided, it will process both a/c and TSW/FSW
%# - calls processFSW()
%# SYNOPSIS obj = processBins(obj, varargin)
%# INPUT obj - the object
%# dataType1 - the primary type of the data being binned, i.e. "a" or "c"
%# dataType2 - the secondary type of the data being binned, i.e. TSW or FSW
%# OUTPUT obj - the object
%#
obj.L.info('ProcessBins','Start of Method');
% check varargin
dataType1 = '';
dataType2 = '';
% parse optional input parameters
if (~isempty(varargin))
for iArgs = 1:length(varargin)
switch varargin{iArgs}
case {'a'}
dataType1 = {'a'};
case {'c'}
dataType1 = {'c'};
case {'TSW'}
dataType2 = {'TSW'};
case {'FSW'}
dataType2 = {'FSW'};
otherwise
obj.L.error('ProcessedData.processBins', 'Invalid argument');
end % switch
end % for
else
obj.L.debug('ProcessedData.processBins', 'no args in');
dataType1 = {'a','c'};
dataType2 = {'TSW','FSW'};
end; % if varargin is empty
for iType1 = 1:length(dataType1)
for iType2 = 1:length(dataType2)
obj.L.info('ProcessedData.processBins', sprintf('type: %s', dataType1{iType1}));
obj.L.info('ProcessedData.processBins', sprintf('type: %s', dataType2{iType2}));
thisType = sprintf('%s%s', dataType1{iType1}, dataType2{iType2});
[binnedTime, numberBins, binIndexNumbers, all_sample_size, ....
all_mean, all_median, all_std, all_variance, ...
all_variability, all_variability2,...
sample_size, ...
mean, median, std, variance, variability, variability2] = ...
processFSWTSW( obj, obj.getVar('name', thisType), ...
obj.meta.Params.PROCESS.BIN_SIZE );
if all(isnan(std))
obj.L.debug('ProcessBins', 'all std nan');
else
obj.L.debug('ProcessBins', 'all std not nan');
end;
obj.setVar( thisType, 'binned', 'binnedTime', binnedTime);
obj.setVar( thisType, 'binned', 'numberBins', numberBins);
obj.setVar( thisType, 'binned', 'binIndexNumbers', binIndexNumbers);
% these are the 'despiked' data
obj.setVar( thisType, 'binned', 'sample_size', sample_size);
obj.setVar( thisType, 'binned', 'mean', mean);
obj.setVar( thisType, 'binned', 'median', median);
obj.setVar( thisType, 'binned', 'std', std);
obj.setVar( thisType, 'binned', 'variance', variance);
obj.setVar( thisType, 'binned', 'variability', variability);
obj.setVar( thisType, 'binned', 'variability2', variability2);
obj.setVar( thisType, 'binned', 'all_sample_size', all_sample_size);
obj.setVar( thisType, 'binned', 'all_mean', all_mean);
obj.setVar( thisType, 'binned', 'all_median', all_median);
obj.setVar( thisType, 'binned', 'all_std', all_std);
obj.setVar( thisType, 'binned', 'all_variance', all_variance);
obj.setVar( thisType, 'binned', 'all_variability', all_variability);
obj.setVar( thisType, 'binned', 'all_variability2', all_variability2);
end %for iType2
end %for iType1
obj.L.info('ProcessedData.ProcessBins:','End of Method');
end %#processBins
%% processFSWTSW
function [binned_time, numberBins, binIndexNumbers, ...
all_sample_size, ...
all_mean, all_median, all_std, all_variance, ...
all_variability, all_variability2, ...
despiked_sample_size, ...
despiked_mean, despiked_median, despiked_std, ...
despiked_variance, ...
despiked_variability, despiked_variability2] ...
= processFSWTSW(obj, dataIn, binSizeIn )
%#processFSW: actually processes BOTH FSW and TSW for a AND c
%#If no argsin are provided, it will process both a/c and TSW/FSW
%#
%# SYNOPSIS obj = processBins(obj, varargin)
%# INPUT obj - the object
%# dataIn - the actual data to bin
%# binSizeIn - a datenum bin size to use,
%# i.e. datenum(0,0,0,0,1,0) for 1 minute
%# OUTPUT
%# binned_time - the binned timestamps
%# numberBins - the number of bins created
%# binIndexNumbers - a vector of index numbers for the bins for the raw data
obj.L.info('ProcessedData.processFSW','Start of Method');
data = dataIn.data;
time = dataIn.timestamps;
obj.L.debug('ProcessedData.processFSW', sprintf('First timestamp to be binned: %s', datestr(time(1))));
obj.L.debug('ProcessedData.processFSW', sprintf('Last timestamp to be binned: %s', datestr(time(end))));
binSize = binSizeIn;
UPPER_PERCENTILE = obj.meta.Params.PROCESS.UPPER_PERCENTILE;
LOWER_PERCENTILE = obj.meta.Params.PROCESS.LOWER_PERCENTILE;
% CHANGED FOR NAAMES -- MISMATCH BETWEEN DEVICE FILE AND DATA
[rows,cols] = size(data);
obj.L.debug('ProcessedData.processFSW',sprintf('size of data: %u x %u', rows, cols));
numberWavelengths = cols;
obj.L.debug('ProcessedData.processFSW',sprintf('numberWavelengths: %u', numberWavelengths));
% END CHANGE FOR NAAMES
% Binned time stamps
% convert time datenums to datevec type
time_vec = datevec(time);
% create a datevec of the first time stamp
start_time_vec = time_vec(1,:);
% set the seconds to zero (just have minute)
start_time_vec(6) = 0;
% convert it back to a datenum
start_time = datenum(start_time_vec);
% create a datevec of the last timestamp
end_time_vec = time_vec(end,:);
% set the seconds to zero (just have minutes)
end_time_vec(6) = 0;
% convert it back to da datenum
end_time = datenum(end_time_vec);
% create bins of minutes, starting at start time, ending at end time, with
% one minute intervals
binned_time = start_time:binSize:end_time;
numberBins = numel(binned_time);
obj.L.debug('ProcessedData.processFSW', sprintf('Number of bins: %u', numberBins));
obj.L.debug('ProcessedData.processFSW', sprintf('First bin: %s', datestr(binned_time(1))));
obj.L.debug('ProcessedData.processFSW', sprintf('Last bin: %s', datestr(binned_time(end))));
% create an index of the bins
binIndexNumbers = zeros(size(time));
binIndexNumbers(:,:) = NaN;
% sample_size = number of timestamps in Bin
all_sample_size = zeros(numberBins, numberWavelengths);
all_sample_size(:,:) = NaN;
% set up matrices for statistics:
% one set for all data, one set for despiked
all_mean = zeros(numberBins, numberWavelengths);
all_mean(:,:) = NaN;
all_median = zeros(numberBins, numberWavelengths);
all_median(:,:) = NaN;
all_std = zeros(numberBins, numberWavelengths);
all_std(:,:) = NaN;
all_variance = zeros(numberBins, numberWavelengths);
all_variance(:,:) = NaN;
all_variability = zeros(numberBins, numberWavelengths);
all_variability(:,:) = NaN;
all_variability2 = zeros(numberBins, numberWavelengths);
all_variability2(:,:) = NaN;
% sample_size = number of timestamps in Bin
despiked_sample_size = zeros(numberBins, numberWavelengths);
despiked_sample_size(:,:) = NaN;
despiked_mean = zeros(numberBins, numberWavelengths);
despiked_mean(:,:) = NaN;
despiked_median = zeros(numberBins, numberWavelengths);
despiked_median(:,:) = NaN;
despiked_std = zeros(numberBins, numberWavelengths);
despiked_std(:,:) = NaN;
despiked_variance = zeros(numberBins, numberWavelengths);
despiked_variance(:,:) = NaN;
despiked_variability = zeros(numberBins, numberWavelengths);
despiked_variability(:,:) = NaN;
despiked_variability2 = zeros(numberBins, numberWavelengths);
despiked_variability2(:,:) = NaN;
% loop through each bin (currently minute)
for iBin=1:numel(binned_time)
% get an index of which timestamps belong in this bin
if iBin < numel(binned_time)
% its any bin other than the last one
thisBinTimestampIndex = ( time >= binned_time(iBin) ) & ( time < binned_time(iBin+1) );
else
% its the last timestamp
thisBinTimestampIndex = ( time >= binned_time(iBin) );
end
% Fill in the index number of this bin for future processing
binIndexNumbers(thisBinTimestampIndex) = iBin;
% Gather the data for this bin
thisBinData = data(thisBinTimestampIndex,:);
[numRows, numCols] = size(thisBinData);
% all_sample_size(iBin,:) = numRows;
%sample size is all the rows (,2) in this matrix that don't
%have all nans
all_sample_size(iBin,:) = sum(~all(isnan(thisBinData),2));
% moved up here 6/7/16
all_mean(iBin,:) = nanmean(thisBinData, 1);
all_median(iBin,:) = nanmedian(thisBinData, 1);
all_std(iBin,:) = nanstd(thisBinData, 1);
all_variance(iBin,:) = nanvar(thisBinData, 1);
% LOWER_PERCENTILE is 2.5% default
% UPPER_PERCENTILE is 97.5% default
all_variability(iBin,:) = ...
( prctile(thisBinData, UPPER_PERCENTILE) - prctile(thisBinData,LOWER_PERCENTILE) )/2;
all_variability2(iBin,:) = ...
( prctile(thisBinData,84) - prctile(thisBinData,16) )/2;
% check not ALL of these values are NaN
if ~all(all(isnan(thisBinData)))
% 1. FIND PERCENTILES
% Find data in between 2.5% and 97.5% in this bin
% prctile will return a matrix - one column per column of data
% first row for LOWER_PERCENTILE value
% second row for UPPER_PERCENTILE value
thisBinPercentiles = prctile( thisBinData, [LOWER_PERCENTILE UPPER_PERCENTILE], 1);
% set up an index of good data to use
thisBinPercCheckIndex = zeros( numRows, numCols);
thisBinPercCheckIndex(:,:) = NaN;
% for each column of data (for each wavelength)
% create an index of data to use in further calculations
% if it passes the check, assign a 1 to the index
for iCol = 1:numCols
lowerPercentile = thisBinPercentiles(1, iCol);
upperPercentile = thisBinPercentiles(2, iCol);
thisBinPercCheckIndex(:,iCol) = ( thisBinData(:,iCol) >= lowerPercentile) & ...
( thisBinData(:, iCol) <= upperPercentile );
end;
thisBinPercCheckIndex = logical(thisBinPercCheckIndex);
%create a matrix, same dimensions as data, to copy data with index
%applies into.
thisBinPercCheckGood = thisBinData;
thisBinPercCheckGood(:,:) = NaN;
% for each location, copy in the good data
thisBinPercCheckGood(thisBinPercCheckIndex) = thisBinData(thisBinPercCheckIndex);
despiked_sample_size(iBin,:) = sum(~all(isnan(thisBinPercCheckGood),2));
% 2. STATISTICS
% use nan(STAT), i.e. nanmean: "For matrices X, nanmean(X) is a row vector of
% column means, once NaN values are removed."
% thisBinData is N number of timestamps, with 83 columns.
% We want column means -- one for each WL
% Assign to row "i" of bin_data_mean
% Only use data that met percentile criteria above
% (thisBinPercCheckGood)
despiked_mean(iBin,:) = nanmean(thisBinPercCheckGood, 1);
despiked_median(iBin,:) = nanmedian(thisBinPercCheckGood, 1);
despiked_std(iBin,:) = nanstd(thisBinPercCheckGood, 1);
despiked_variance(iBin,:) = nanvar(thisBinPercCheckGood,1);
thisBinFinalPercentiles = prctile( thisBinPercCheckGood, [LOWER_PERCENTILE UPPER_PERCENTILE], 1);
for iCol = 1:numCols
lowerPercentile = thisBinFinalPercentiles(1, iCol);
upperPercentile = thisBinFinalPercentiles(2, iCol);
% (97.%-2.5%)/2
despiked_variability(iBin, iCol) = (upperPercentile-lowerPercentile)/2;
end;
despiked_variability2(iBin,:) = ...
( prctile(thisBinPercCheckGood,84) - prctile(thisBinPercCheckGood,16) )/2;
else % ~all(all(isnan(thisBinData)))
obj.L.debug('ACData.processFSW', sprintf('Bin %u has no valid data', iBin));
end % end check for valid data
end; %for each bin
obj.L.info('processFSW','End of Method');
end %#processFSW
%% findFSWBinMedians
function obj = findFSWBinMedians(obj, varargin)
%#findFSWBinMedians - Interpolate Filtered Data for a and c: L3->L4
%# - calls obj.findBinMeds
%#
%# SYNOPSIS obj = findFSWBinMedians(obj, varargin)
%# INPUT obj : the object
%# dataType1 : the primary type of the data being binned, i.e. "a" or "c"
%# OUTPUT obj: the object
% check varargin
dataType1 = '';
% parse optional input parameters
if (~isempty(varargin))
for iArgs = 1:length(varargin)
switch varargin{iArgs}
case {'a'}
dataType1 = {'a'};
case {'c'}
dataType1 = {'c'};
otherwise
obj.L.error('ProcessedData.findFSWBinMedian', 'Invalid argument');
end % switch
end % for
else
obj.L.debug('ProcessedData.findFSWBinMedian', 'no args in');
dataType1 = {'a','c'};
end; % if varargin is empty
for iType1 = 1:length(dataType1)
obj.L.debug('ProcessedData.findFSWBinMedian', sprintf('type: %s', dataType1{iType1}));
thisType = sprintf('%sFSW', dataType1{iType1});
binMethod = obj.meta.Params.PROCESS.STAT_FOR_BINNING;
% get most recent data of the type given
filtered_bins = obj.getVar('name', thisType, 'level', 'binned', 'data', binMethod); %1502x83
obj.L.debug('ProcessedData.findFSWBinMedian',...
sprintf('size filtered bins: %u x %u', size(filtered_bins)));
% run method
[binMedians] = obj.findBinMeds(filtered_bins);
%set variables
obj.setVar( thisType, 'filtered', 'interpolatedSectionMedians', binMedians);
end; %# for iType1 = 1:length(dataType1)
obj.L.info('ProcessedData.findFSWBinMedian','End of Method');
end; % #findFSWBinMedian(obj, varargin)
%% findBinMeds
function [binMediansOut] = findBinMeds(obj, filteredBinsIn)
%#findBinMeds - find the median of each section of FSW bins,
%#and assign the median to the middle timestamp
%#
%# SYNOPSIS [binMediansOut] = findBinMeds(obj, filteredBinsIn)
%# INPUT obj: the object
%# filteredBinsIn: FSW data that has been binned
%# OUTPUT binMediansOut: data including one bin for each section of
%# bins, where value = median of that section
%#
% create index to bins which are all NaNs: i.e. the TSW bins
nanDataBinIndex = all(isnan(filteredBinsIn),2); %1502x83
% create an index of which bins have filtered data
filtBinIndex = ~nanDataBinIndex;
% calc size of data
[numBins,numCols] = size(filteredBinsIn);
% create new matrix of medians, same size as original data
interpolatedSectionMedians = zeros(numBins, numCols);
interpolatedSectionMedians(:,:) = NaN;
% find where sections of FSW/TSW switch:
offsetfiltBinIndex = zeros(size(filtBinIndex));
offsetfiltBinIndex(1,:) = NaN;
offsetfiltBinIndex(2:end,:) = filtBinIndex(1:end-1);
transitionIndex = filtBinIndex - offsetfiltBinIndex;
startSectionIndex = find(transitionIndex == 1);
endSectionIndex = find(transitionIndex == -1);
% decrease section by one
endSectionIndex = endSectionIndex -1;
numSections = size(startSectionIndex);
if length(startSectionIndex) > length(endSectionIndex)
%ending in middle of FSW
newEndSectionIndex = zeros(length(endSectionIndex)+1,1);
newEndSectionIndex(1:end-1) = endSectionIndex(:,:);
newEndSectionIndex(end) = length(filtBinIndex);
endSectionIndex = newEndSectionIndex;
end;
% for each section
for iSection = 1:numSections;
% create a temporary section the size of the current
% section of interpolated data
endSectionIndex(iSection);
startSectionIndex(iSection);
currSection = zeros( endSectionIndex(iSection) - startSectionIndex(iSection) + 1, numCols);
currSection(:,:) = NaN;
% copy in the data from the original data
istart = startSectionIndex(iSection);
iend = endSectionIndex(iSection);
% find size of currSection
currSection(:,:) = filteredBinsIn( istart:iend, :) ;
[numBinsInSection,~] = size(currSection);
halfwayBin = startSectionIndex(iSection) + floor(numBinsInSection/2);
obj.L.debug('ProcessedData.calculateInterpolatedBinUncertainty',...
sprintf('Size: %u; Halfway bin: %u', numBinsInSection, halfwayBin));
obj.L.debug('ProcessedData.calculateInterpolatedBinUncertainty',...
sprintf('Section Number: %u; start: %u; end %u', ...
iSection, startSectionIndex(iSection), endSectionIndex(iSection)));
medianCurrSection = nanmedian(currSection(:,:),1);
obj.L.debug('ProcessedData.calculateInterpolatedBinUncertainty',...
sprintf('Section Meidan: %u; median size: %u x %u',medianCurrSection, size(medianCurrSection)));
for iBin = startSectionIndex(iSection):endSectionIndex(iSection)
for iCol = 1:numCols
currMax = max(currSection(:,iCol));
currMin = min(currSection(:,iCol));
% check interpolated bins
if abs(currMax-currMin) > 0.005
obj.L.error('ProcessedData.calculateInterpolatedBinUncertainty',...
sprintf('max-min > 0.005 = %u -- HIGH VAR in FSW Sec %u -- MAKE INTERVAL SMALLER?', ...
(abs(currMax-currMin) > 0.005), iBin));
end;
% copy in median to middle bin
if iBin == halfwayBin
interpolatedSectionMedians(iBin,iCol) = medianCurrSection(:,iCol);
end
end %# for iCol = 1:numCols
end %# for iBin = startSectionIndex(iSection):endSectionIndex(iSection)
end
binMediansOut = interpolatedSectionMedians;
end %# findFilteredBinMedian
%% interpolateFiltered
function obj = interpolateFiltered(obj, varargin)
%#interpolateFiltered - Interpolate Filtered Data for a and c: L3->L4
%# - calls obj.interpolateFSW
%# - calls obj.calculateInterpolatedBinUncertainty
%#
%# SYNOPSIS obj = interpolateFiltered(obj, varargin)
%# INPUT obj: the object
%# dataType1 - the primary type of the data being binned, i.e. "a" or "c"
%# dataType2 - the secondary type of the data being binned, i.e. TSW or FSW
%# OUTPUT obj: the object
%#
obj.L.info('ProcessedData.interpolateFiltered','Start of Method');
% check varargin
dataType1 = '';
% only interpolate filtered data
dataType2 = {'FSW'};
% parse optional input parameters
if (~isempty(varargin))
for iArgs = 1:length(varargin)
switch varargin{iArgs}
case {'a'}
dataType1 = {'a'};
case {'c'}
dataType1 = {'c'};
otherwise
obj.L.error('ProcessedData.interpolateFiltered', 'Invalid argument');
end % switch
end % for
else
obj.L.debug('ProcessedData.interpolateFiltered', 'no args in');
dataType1 = {'a','c'};
end; % if varargin is empty
for iType1 = 1:length(dataType1)
for iType2 = 1:length(dataType2)
obj.L.debug('ProcessedData.interpolateFiltered', sprintf('type: %s', dataType1{iType1}));
obj.L.debug('ProcessedData.interpolateFiltered', sprintf('type: %s', dataType2{iType2}));
thisType = sprintf('%s%s', dataType1{iType1}, dataType2{iType2});
% get most recent data of the type given
filtered_bins = obj.getVar('name', thisType, 'level', ...
'filtered', 'data', 'interpolatedSectionMedians'); %1502x83
time_bins = obj.getVar('name', thisType, 'data', 'binnedTime'); %1x1502
obj.L.debug('ProcessedData.interpolateFiltered',...
sprintf('size filtered bins: %u x %u', size(filtered_bins)));
obj.L.debug('ProcessedData.interpolateFiltered',...
sprintf('size time bins: %u x %u', size(time_bins)));
time_bins = time_bins';
% run method
[interpolatedFSWData, interpolatedBinIndex] = ...
obj.interpolateFSW(time_bins, filtered_bins, 'extrap');
%set variables
obj.setVar( thisType, 'filtered', 'interpolatedData', interpolatedFSWData);
obj.setVar( thisType, 'filtered', 'interpolatedBinIndex', interpolatedBinIndex);
obj.setVar( thisType, 'filtered', 'binnedTime', time_bins);
% calculate bin variability
[uncertainty, ~ ] = obj.calculateInterpolatedBinUncertainty( ...
interpolatedFSWData, interpolatedBinIndex);
% set variability
obj.setVar( thisType, 'filtered', 'interpolatedUncertainty', uncertainty);
end;
end;
obj.L.info('ProcessedData.interpolateFiltered','End of Method');
end %#interpolateFiltered
%% calcTSWUncertainty
function obj = calcTSWUncertainty(obj, varargin)
obj.L.info('ProcessedData.calcTSWUncertainty','Start of Method');
% check varargin
dataType1 = '';
% only interpolate filtered data
dataType2 = {'TSW'};
% parse optional input parameters
if (~isempty(varargin))
for iArgs = 1:length(varargin)
switch varargin{iArgs}
case {'a'}
dataType1 = {'a'};
case {'c'}
dataType1 = {'c'};
otherwise
obj.L.error('ProcessedData.calcTSWUncertainty', 'Invalid argument');
end % switch
end % for
else
obj.L.debug('ProcessedData.calcTSWUncertainty', 'no args in');
dataType1 = {'a','c'};
end; % if varargin is empty
for iType1 = 1:length(dataType1)
for iType2 = 1:length(dataType2)
obj.L.debug('ProcessedData.calcTSWUncertainty', sprintf('type: %s', dataType1{iType1}));
obj.L.debug('ProcessedData.calcTSWUncertainty', sprintf('type: %s', dataType2{iType2}));
thisType = sprintf('%s%s', dataType1{iType1}, dataType2{iType2});
% get most recent data of the type given
TSW_STD = obj.getVar('name', thisType, 'level', ...
'binned', 'data', 'std'); %1502x83
TSW_Var = obj.getVar('name', thisType, 'level', ...
'binned', 'data', 'variability'); %1502x83
if strcmpi(obj.meta.Params.PROCESS.BIN_METHOD, 'mean')
binUncertainty = TSW_STD./sqrt(obj.meta.Params.PROCESS.UNCERTAINTY_N);
elseif strcmpi(obj.meta.Params.PROCESS.BIN_METHOD, 'median')
binUncertainty = TSW_Var./sqrt(obj.meta.Params.PROCESS.UNCERTAINTY_N);
else
obj.L.error('ProcessingManager', 'unexpected bin method');
end;
obj.setVar( thisType, 'binned', 'uncertainty', binUncertainty);
end; %for iType2
end; %for iType1
end; %calcTSWUncertainty
%% interpolateFSW
function [interpolatedFSWData, interpolatedBinIndex] = interpolateFSW(obj, binTimestampsIn, binnedFSWDataIn, extrapIn)
%#interpolateFSW - use INTERP1 to create interpolated data between FSW
%#
%# SYNOPSIS [interpolatedFSWData, interpolatedBinIndex] = interpolateFSW(obj, binTimestampsIn, binnedFSWDataIn)
%# INPUT obj: the object
%# binTimestampsIn
%# binnedFSWDataIn
%# OUTPUT interpolatedFSWData
%# interpolatedBinIndex
%#
obj.L.info('ProcessedData.interpolateFSW','Start of Method');
% STEP 1:
% When interpolating, the array that interp1 uses to find the values can't
% have any NaNs so create a copy of your filtered array to fill in the blanks.
% make copy of binned data passed in, NaNs will be blanked out of this
FSWBinsNoNans = binnedFSWDataIn; %1502x83
% make copy of binned time passed in, NaNs will be blanked out of this
binTimestampsNoNans = binTimestampsIn;
% make another copy of binned timestamps, to use for
% interpolation
binTimestamps = binTimestampsIn;
% create index to bins which are all NaNs: i.e. the TSW bins
nanDataBinIndex = all(isnan(FSWBinsNoNans),2); %1502x83
% what is this for?
filtBinIndex = ~nanDataBinIndex;
% blank any bins that are totally nan
FSWBinsNoNans(nanDataBinIndex, :) = []; % new array with just data
binTimestampsNoNans(nanDataBinIndex, :) = []; % corresponding time_stamps
% STEP 2:
% make copy of filtered_data for interpolated data to go INTO
% this is copy of original so WILL contain NaNs.
% new_filtered_data = filtered_bins;
if strcmp(extrapIn, 'extrap')
interpolatedFSWData = interp1(binTimestampsNoNans, FSWBinsNoNans, binTimestamps, ...
'linear', 'extrap');
else
interpolatedFSWData = interp1(binTimestampsNoNans, FSWBinsNoNans, binTimestamps, ...
'linear');
end;
newDataIndex = ~all(isnan(interpolatedFSWData),2);
% an index to the interpolated bins will be the OPPOSITE of:
% bins that were Nan before Interp AND bins that are nan after
% Interpolation
interpolatedBinIndex = newDataIndex & ~filtBinIndex;
obj.L.info('ProcessedData.interpolateFSW','End of Method');
end %end interpolateFSW
%% calculateInterpolatedBinUncertainty
function [interpolatedBinUncertaintyOut, interpolatedBinUncertainty2Out] ...
= calculateInterpolatedBinUncertainty( ...
obj, interpolatedFSWDataIn, interpolatedBinIndexIn )
%#calculateInterpolatedBinUncertainty - calculates interpolated bin variability as max-min / 2
%#
%# SYNOPSIS [interpolatedBinUncertaintyOut, interpolatedBinUncertainty2Out ] = ...
%# calculateInterpolatedBinUncertainty(obj, interpolatedFSWDataIn, interpolatedBinIndexIn )
%# INPUT obj: the object
%# interpolatedFSWDataIn -
%# interpolatedBinIndexIn -
%# OUTPUT interpolatedBinUncertaintyOut
%# interpolatedBinUncertainty2Out
%#
obj.L.info('ProcessedData.calculateInterpolatedBinUncertainty','Start of Method');
interpolatedFSWData = interpolatedFSWDataIn;
interpBinIndex = interpolatedBinIndexIn;
[~,numCols] = size(interpolatedFSWData);
[numBins] = length(interpolatedFSWData);
interpolatedBinUncertainty = zeros(numBins, numCols);
interpolatedBinUncertainty(:,:) = NaN;
interpolatedBinUncertainty2 = zeros(numBins, numCols);
interpolatedBinUncertainty2(:,:) = NaN;
% calculate variability for each interpolated bin.
% variability is max of bins in that section - min of bins in
% that section/2
offsetInterpBinIndex = zeros(size(interpBinIndex));
offsetInterpBinIndex(1,:) = NaN;
offsetInterpBinIndex(2:end,:) = interpBinIndex(1:end-1);
transitionIndex = interpBinIndex - offsetInterpBinIndex;
%switched
% find where the transitionIndex is 1, this will be where to
% start a new seciton.
startSectionIndex = find(transitionIndex == 1);
%find where transitinIndex is -1, this will mark end of
%section