forked from gremau/NMEG_FluxProc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUNM_RemoveBadData.m
2356 lines (2104 loc) · 95.8 KB
/
UNM_RemoveBadData.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
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
function [] = UNM_RemoveBadData( sitecode, year, varargin )
% UNM_REMOVEBADDATA - remove bogus observations from UNM flux data and write
% filtered data to delimited ASCII files FOR SITE-YEARS 2012 AND LATER.
%
% This program was created by Krista Anderson Teixeira in July 2007
% Modified by John DeLong 2008 through 2009.
% Modifed by Timothy W. Hilton, 2011 through 2013
%
% The program reads site_fluxall_year delimited text files and pulls in a
% combination of matlab processed ts data and data logged average 30-min flux
% data. It then flags values based on a variety of criteria and writes out new
% files that do not have the identified bad values. It writes out a
% site_flux_all_qc file and a site_flux_all_for_gap_filling file to send to
% REddyProc for gapfilling and flux partitioning. It can be adjusted to make
% other subsetted files too.
%
% USAGE
% UNM_RemoveBadData( sitecode, year )
% UNM_RemoveBadData( sitecode, year, 'iteration', iteration )
% UNM_RemoveBadData( sitecode, year, ..., 'write_QC', write_QC )
% UNM_RemoveBadData( sitecode, year, ..., 'write_GF', write_GF )
% UNM_RemoveBadData( sitecode, year, ..., 'draw_plots', draw_plots )
%
% INPUTS
% sitecode: UNM_sites object or integer; specifies site to process
% year: integer; year to process
% PARAMETER-VALUE PAIRS
% iteration: integer 1-6 (Default is 6); defines which set of bad data tasks
% to perform (see code for details)
% write_QC: {true}|false; if true, writes flux_all_qc file
% write_GF: {true}|false; if true, writes flux_all_for_gapfilling file
% draw_plots: 0|{1}|2|3; determines extent of plotting. larger values
% cause more plots to be drawn.
% 0: draw no plots
% 1 (default): plot only NEE time series with NEE filter results
% 2: all plots from 1, plus:
% - PAR normalization results
% - radiation timing correction results
% - six-panel plot showing "fingerprints" for incoming shortwave
% (Rg), relative humidity (RH), air temperature (T), net ecosystem
% exchange (NEE), latent heat (LE), and sensible heat (H),
% 3: all plots from 1 and 2, plus:
% - latent heat diagnostic plot showing LE and PAR time series
% with the results of various filters (see plot legend)
% - four-panel plot showing time series for NEE, T, carbon
% dioxide concentration ([CO2]), and pcp
% - NEE vs wind speed scatter plot
% - NEE vs wind direction scatter plot (split by day/night)
% - NEE vs friction velocity (ustar) scatter plot
% - [CO2] time series, with results of various filters (see plot
% legend)
% - Burba cold temperature correction results
%
% OUTPUTS:
% This function has no outputs
%
% SEE ALSO
% UNM_RemoveBadData_pre2012
%
% author: Timothy W. Hilton, UNM, 2012-2013
%clear all
%close all
[ this_year, ~, ~ ] = datevec( now );
% -----
% define optional inputs, with defaults and typechecking
% -----
args = inputParser;
args.addRequired( 'sitecode', @(x) ( isintval( x ) | isa( x, 'UNM_sites' ) ) );
args.addRequired( 'year', ...
@(x) ( isintval( x ) & ( x >= 2006 ) & ( x <= this_year ) ) );
args.addParamValue( 'iteration', 6, ...
@(x) ( isintval( x ) & ( x >= 1 ) & ( x <= 6 ) ) );
args.addParamValue( 'write_QC', true, @islogical );
args.addParamValue( 'write_GF', true, @islogical );
args.addParamValue( 'old_fluxall', false, @islogical );
args.addParamValue( 'xls_fluxall', false, @islogical );
args.addParamValue( 'draw_plots', 1, ...
@(x) ( isnumeric( x ) & ismember( x, [ 0, 1, 2, 3 ] ) ) );
% parse optional inputs
args.parse( sitecode, year, varargin{ : } );
% place user arguments into variables
sitecode = args.Results.sitecode;
year_arg = args.Results.year;
site = char( sitecode );
iteration = int8( args.Results.iteration );
%true to write "[sitename].._qc", -- file with all variables & bad data removed
write_complete_out_file = args.Results.write_QC;
%true to write file for Reichstein's online gap-filling. SET U* LIM (including
%site- specific ones--comment out) TO 0!!!!!!!!!!
write_gap_filling_out_file = args.Results.write_GF;
% Parameters used to select between versions of fluxall files
use_old_fluxall = args.Results.old_fluxall;
use_xls_fluxall = args.Results.xls_fluxall;
draw_plots = args.Results.draw_plots;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% USTAR filter switch
% If this is set to false, the ustar filter for fluxes is turned off
% (this is desireable for making ameriflux files)
% See line 1272 to see where this takes affect (its pretty hacky)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ustar_filter_switch = false;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_for_analyses = 0; %1 to output file with data sorted for specific analyses
ET_gap_filler = 0; %run ET gap-filler program
winter_co2_min = -100; %initialization -- will be set for specific sites later
obs_per_day = 48; % half-hourly observations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Specify some details about sites and years
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if sitecode == UNM_sites.GLand; % grassland
ustar_lim = 0.06;
co2_min_by_month = -15; co2_max_by_month = 7.25;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1, 3 ];
sd_filter_thresh = 3.5;
wind_min = 330; wind_max = 30; % these are given a sonic_orient = 180;
Tdry_min = 240; Tdry_max = 320;
HS_min = -100; HS_max = 450;
HSmass_min = -100; HSmass_max = 450;
LH_min = -150; LH_max = 450;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
elseif sitecode == UNM_sites.SLand; % shrubland
ustar_lim = 0.08;
co2_min_by_month = -15; co2_max_by_month = 6;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1, 3 ];
sd_filter_thresh = 3.5;
wind_min = 330; wind_max = 30; % these are given a sonic_orient = 180;
Tdry_min = 240; Tdry_max = 320;
HS_min = -100; HS_max = 450;
HSmass_min = -100; HSmass_max = 450;
LH_min = -150; LH_max = 450;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
elseif sitecode == UNM_sites.JSav; % Juniper savanna
ustar_lim = 0.11;
co2_min_by_month = -11;
co2_max_by_month = [ repmat( 5, 1, 6 ), repmat( 10, 1, 6 ) ];
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1, 3, 5 ];
sd_filter_thresh = 3;
wind_min = 15; wind_max = 75; % these are given a sonic_orient = 225;
Tdry_min = 240; Tdry_max = 320;
HS_min = -100; HS_max = 550;
HSmass_min = -100; HSmass_max = 550;
LH_min = -150; LH_max = 450;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
press_min = 70; press_max = 130;
elseif sitecode == UNM_sites.PJ | sitecode == UNM_sites.TestSite; % Pinyon Juniper
ustar_lim = 0.22;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
wind_min = 15; wind_max = 75; % these are given a sonic_orient = 225;
sd_filter_windows = [ 1.5, 3, 5 ];
sd_filter_thresh = 3;
co2_min_by_month = -10;
co2_max_by_month = 6;
Tdry_min = 240; Tdry_max = 310;
HS_min = -100; HS_max = 640;
HSmass_min = -100; HSmass_max = 640;
LH_min = -150; LH_max = 450;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
press_min = 70; press_max = 130;
co2_max_by_month = [ 2.5, 2.5, 2.5, 3.5, 4, 5, 6, repmat( 6, 1, 5 ) ];
elseif sitecode == UNM_sites.PJ_girdle; % Pinyon Juniper girdle
ustar_lim = 0.16;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1.5, 3, 5 ];
sd_filter_thresh = 3;
wind_min = 15; wind_max = 75; % these are given a sonic_orient = 225;
Tdry_min = 240; Tdry_max = 310;
HS_min = -100; HS_max = 640;
HSmass_min = -100; HSmass_max = 640;
LH_min = -150; LH_max = 450;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
press_min = 70; press_max = 130;
co2_min_by_month = -10;
co2_max_by_month = [ 2.5, 2.5, 2.5, 3.5, 4, 5, 6, 6, 6, 4, 4, 4 ];
%co2_max_by_month = [ 1.5, 1.5, 2, 2, 3, 4, 4, repmat( 6, 1, 5 ) ];
elseif sitecode == UNM_sites.New_GLand; % new Grassland
ustar_lim = 0.06;
n_SDs_filter_hi = 4.5; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1, 3 ];
sd_filter_thresh = 4;
co2_min_by_month = -15; co2_max_by_month = 7.25;
wind_min = 330; wind_max = 30; % these are given a sonic_orient = 180;
Tdry_min = 240; Tdry_max = 320;
HS_min = -100; HS_max = 450;
HSmass_min = -100; HSmass_max = 450;
LH_min = -150; LH_max = 450;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
elseif sitecode == UNM_sites.PPine; % Ponderosa Pine
% site default values
%co2_min_by_month = [-6 -6 -15 -15 -15 -15 -15 -15 -15 -15 -15 -5];
%co2_max_by_month = [4 4 4 5 30 30 30 30 30 30 5 4];
co2_min_by_month = [-8 -8 -21 -21 -21 -21 -21 -21 -21 -21 -17 -10];
% Note that the sketchy NEE normalization that occurs relies on this:
co2_max_by_month = 30;
ustar_lim = 0.2;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 2, 4, 7 ];
sd_filter_thresh = 3;
wind_min = 119; wind_max = 179; % these are given a sonic_orient = 329;
Tdry_min = 240; Tdry_max = 310;
HS_min = -200; HS_max = 800;
HSmass_min = -200; HSmass_max = 800;
LH_min = -50; LH_max = 550;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
elseif sitecode == UNM_sites.MCon; % Mixed conifer
co2_min_by_month = [ -2.5, -2.5, repmat( -16, 1, 9 ), -2.5 ];%[ -1.5, -1.5, repmat( -12, 1, 9 ), -1.5 ];
co2_max_by_month = 6;
n_SDs_filter_hi = 2.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1, 3, 6 ];
sd_filter_thresh = 3;
wind_min = 153; wind_max = 213; % these are given a sonic_orient = 333;
Tdry_min = 250; Tdry_max = 300;
HS_min = -200; HS_max = 800;
HSmass_min = -200; HSmass_max = 800;
LH_min = -50; LH_max = 550;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
ustar_lim = 0.2;
elseif sitecode == UNM_sites.MCon_SS; % New Mixed Conifer
warning('Filters copied from MCon, adjust for NMCon ');
co2_min_by_month = [ -6.5, -6.5, repmat( -16, 1, 9 ), -.5 ];%[ -1.5, -1.5, repmat( -12, 1, 9 ), -1.5 ];
co2_max_by_month = 6;
n_SDs_filter_hi = 2.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
sd_filter_windows = [ 1, 3, 6 ];
sd_filter_thresh = 3;
% Note that these are copied from MCon (which may itself be off)
% There are two things to consider - distortion from wind behind the
% anemometer and from the tower - these will be in two different
% directions since the flux instruments are on a boom positioned a
% meter or so to the east of the tower and sonic is not facing straight
% out from tower.
wind_min = 153; wind_max = 213; % these are given a sonic_orient = 333;
Tdry_min = 250; Tdry_max = 300;
HS_min = -200; HS_max = 800;
HSmass_min = -200; HSmass_max = 800;
LH_min = -50; LH_max = 550;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
ustar_lim = 0.2;
elseif sitecode == UNM_sites.TX;
ustar_lim = 0.11;
co2_min_by_month = -26;
switch args.Results.year
case 2011
co2_max_by_month = [ 4.0, 4, 4, 4, 9, 10, ...
10, 4, 4, 4, 4, 4.0 ];
case 2012
co2_max_by_month = [ 4.9, 6, 7, 8, 9, 12, ...
12, 12, 9, 6, 6, 4.9 ];
end
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
wind_min = 296; wind_max = 356; % these are given a sonic_orient = 146;
Tdry_min = 265; Tdry_max = 315;
HS_min = -200; HS_max = 800;
HSmass_min = -200; HSmass_max = 800;
LH_min = -150; LH_max = 550;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
press_min = 70; press_max = 130;
elseif sitecode == UNM_sites.TX_forest;
ustar_lim = 0.11;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
co2_min_by_month = -26; co2_max_by_month = 12;
wind_min = 300; wind_max = 360; % these are given a sonic_orient = ;
Tdry_min = 265; Tdry_max = 315;
HS_min = -200; HS_max = 800;
HSmass_min = -200; HSmass_max = 800;
LH_min = -150; LH_max = 550;
rH_min = 0; rH_max = 1;
h2o_max = 30; h2o_min = 0;
press_min = 70; press_max = 130;
elseif sitecode == UNM_sites.TX_grass;
ustar_lim = 0.11;
n_SDs_filter_hi = 3.0; % how many std devs above the mean NEE to allow
n_SDs_filter_lo = 3.0; % how many std devs below the mean NEE to allow
co2_min_by_month = -26; co2_max_by_month = 12;
wind_min = 300; wind_max = 360; % these are given a sonic_orient = ;
Tdry_min = 265; Tdry_max = 315;
HS_min = -200; HS_max = 800;
HSmass_min = -200; HSmass_max = 800;
LH_min = -150; LH_max = 550;
rH_min = 0; rH_max = 1;
h2o_max = 35; h2o_min = 0;
press_min = 70; press_max = 130;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read the fluxall file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if use_old_fluxall
pathname = fullfile( get_site_directory( sitecode ), ...
'old_fluxall');
fname = sprintf( '%s_FLUX_all_%d.txt', get_site_name( sitecode ), ...
year_arg );
else
pathname = fullfile( get_site_directory( sitecode ));
fname = sprintf( '%s_%d_fluxall.txt', get_site_name( sitecode ), ...
year_arg );
end
if use_xls_fluxall
fname = sprintf( '%s_FLUX_all_%d.xls', get_site_name( sitecode ), ...
year_arg );
data = UNM_parse_fluxall_xls_file( sitecode, year_arg, ...
'file', fullfile( pathname, fname ));
else
data = UNM_parse_fluxall_txt_file( sitecode, year_arg, ...
'file', fullfile( pathname, fname ));
data_orig = parse_fluxall_txt_file( sitecode, year_arg, 'file', ...
fullfile( pathname, fname ));
% In 2007 the existing GLand and SLand sites and were acquired by
% Marcy. The sites were then revamped (new programs and data handling)
% in late May/early June 2007. Data from before the revamp is present
% in old fluxall files, but I am unsure what had to be done to get this
% data into the old files. For now merge the old 2007 fluxall
% files into the current dataset and proceed with the QC process.
if (sitecode == 1 || sitecode == 2 ) && year_arg == 2007
warning('Dataset to table conversions! FIX THESE!');
data = merge_2007_fluxall_files( dataset2table(data), sitecode );
data = table2dataset(data);
% These old data have duplicated SW_IN measurements
data = replacedata( data, ...
interp_duplicated_radiation( double( data ), ...
data.Properties.VarNames, data.timestamp ) );
end
% Fill fluxes with 30 minute data?
disp( 'WARNING: Filling fluxes with 30min data');
data = fill_30min_flux_spooler( data, sitecode, year_arg );
end
outfolder = fullfile( get_site_directory( sitecode ), ...
'processed_flux' );
headertext = data.Properties.VarNames;
timestamp = data.timestamp;
[year,month,day,hour,minute,second] = datevec( data.timestamp );
ncol = size( data, 2 );
filelength_n = size( data, 1 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% some siteyears have periods where the observed radition does not line
% up with sunrise. Fix this here so that the matched time/radiation
% propagates through the rest of the calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = UNM_fix_datalogger_timestamps( sitecode, year_arg, ...
dataset2table( data ),...
'debug', args.Results.draw_plots > 1 );
data = table2dataset( data );
% data.timestamp = [];
% This was an issue with old MCon fluxall files where SW_IN appears to be
% filled with hourly data from off-site (which is mistakely duplicated
% each half hour instead of averaged)
if ( sitecode == UNM_sites.MCon )
% if ( year == 2007 | year == 2008)
% data = replacedata( data, ...
% revise_MCon_duplicated_Rg( double( data ), ...
% headertext, ...
% timestamp ) );
% end
end
shift_t_str = 'shifted';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read in Matlab processed ts data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
jday = data.jday;
iok = data.iok ;
Tdry = data.tdry ;
%wnd_dir_compass = data.wind_direction ;
wnd_spd = data.speed ;
%u_star = data.ustar ;
%CO2_mean = data.CO2_mean ;
%CO2_std = data.CO2_std ;
%H2O_mean = data.H2O_mean ;
%H2O_std = data.H2O_std ;
%u_mean = data.u_mean_unrot ;
t_mean = data.temp_mean;
t_meanK = t_mean + 273.15;
fc_raw = data.Fc_raw ;
fc_raw_massman = data.Fc_raw_massman ;
fc_water_term = data.Fc_water_term ;
fc_heat_term_massman = data.Fc_heat_term_massman ;
%fc_raw_massman_wpl = data.Fc_raw_massman_ourwpl;
E_raw = data.E_raw ;
E_raw_massman = data.E_raw_massman ;
E_water_term = data.E_water_term;
E_heat_term_massman = data.E_heat_term_massman;
E_wpl_massman = data.E_wpl_massman;
%HSdry = data.SensibleHeat_dry ;
%HSdry_massman = data.HSdry_massman;
%HL_raw = data.LatentHeat_raw;
%HL_wpl_massman = data.LatentHeat_raw_massman ; % Is this correct?
%HL_wpl_massman_un = repmat( NaN, size( data, 1 ), 1 );
%rhoa_dry = data.rhoa_dry_air_molar_density;
decimal_day = ( datenum( year, month, day, hour, minute, second ) - ...
datenum( year, 1, 1 ) + 1 );
year_arg = year(2);
%initialize some variables to a NaN array
dummy = repmat( NaN, size( data, 1), 1 );
rH = dummy;
precip = dummy;
% Some sites are missing radiation measurements
sw_incoming = dummy;
lw_incoming = dummy;
sw_outgoing = dummy;
lw_outgoing = dummy;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read in 30-min data, variable order and names in flux_all files are not
% consistent so match headertext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% convert CNR1 temperature from centrigrade to Kelvins
% FIXME - this needs to handle different header names from early years
CNR1TK = [];
CNR1_var = regexp_header_vars( data, 'CNR1*|Temp_C_Avg' );
if ~isempty(CNR1_var)
CNR1TK = data.( CNR1_var{ 1 } ) + 273.15;
end
h2oflag_1 = 0;
data = double( data );
for i=1:numel( headertext );
if strcmp('agc_Avg',headertext{i}) == 1
agc_Avg = data(:,i);
elseif strcmp('h2o_hmp_Avg', headertext{i}) == 1 || ...
strcmp('h2o_hmp_mean', headertext{i}) == 1 || ...
strcmp('h2o_hmp_mean_Avg', headertext{i}) == 1
h2o_hmp = data( :, i );
elseif strcmp('wind_direction', headertext{i}) == 1 || ...
strcmp('windDirection_theta', headertext{i}) == 1
wnd_dir_compass = data( :, i );
elseif strcmp('ustar', headertext{i}) == 1 || ...
strcmp('ustar_frictionVelocity_M_s', headertext{i}) == 1
u_star = data( :, i );
% filter out absurd u_star values
u_star( u_star > 50 ) = NaN;
elseif strcmp('u_mean_unrot', headertext{i}) == 1 || ...
strcmp('u_mean', headertext{i}) == 1
u_mean = data( :, i );
elseif strcmp('CO2_mean', headertext{i}) == 1 || ...
strcmp('CO2_mean_umol_molDryAir', headertext{i}) == 1
CO2_mean = data( :, i );
elseif strcmp('CO2_std', headertext{i}) == 1 || ...
strcmp('CO2_std_umol_molDryAir', headertext{i}) == 1
CO2_std = data( :, i );
% This should come in from 10hz data in g/m3, but some fluxall
% have it in mmol/mol, so convert it below
elseif strcmp('H2O_mean', headertext{i}) == 1
H2O_mean = data( :, i );
elseif strcmp('H2O_mean_mmol_molDryAir', headertext{i}) == 1
H2O_mean = data( :, i );
h2oflag_1 = 1;
elseif strcmp('H2O_std', headertext{i}) == 1 || ...
strcmp('H2O_std_mmol_molDryAir', headertext{i}) == 1
H2O_std = data( :, i );
elseif strcmp('Fc_raw_massman_ourwpl', headertext{i}) == 1 || ...
strcmp('Fc_raw_massman_wpl', headertext{i}) == 1
fc_raw_massman_wpl = data( :, i );
elseif strcmp('SensibleHeat_dry', headertext{i}) == 1 || ...
strcmp('HSdry_WM2', headertext{i}) == 1
HSdry = data( :, i );
elseif strcmp('HSdry_massman', headertext{i}) == 1 || ...
strcmp('HSdry_massman_WM2', headertext{i}) == 1
HSdry_massman = data( :, i );
elseif strcmp('LatentHeat_raw', headertext{i}) == 1 || ...
strcmp('HL_raw_WM2', headertext{i}) == 1
HL_raw = data( :, i );
elseif strcmp('LatentHeat_raw_massman', headertext{i}) == 1 || ...
strcmp('HL_raw_massman_WM2', headertext{i}) == 1
HL_wpl_massman = data( :, i );% Is this correct? Its raw, not wpl
HL_wpl_massman_un = repmat( NaN, size( data, 1 ), 1 );
% Half hourly data filler only produces uncorrected HL_wpl_massman,
% but use these where available as very similar values
HL_wpl_massman( isnan( HL_wpl_massman ) & ...
~isnan( HL_wpl_massman_un ) ) = ...
HL_wpl_massman_un( isnan( HL_wpl_massman ) & ...
~isnan( HL_wpl_massman_un ) );
elseif strcmp('rhoa_dry_air_molar_density', headertext{i}) == 1 || ...
strcmp('rhoa_dryAirMolarDensity_mols_m3MoistAir', headertext{i}) == 1 || ...
strcmp('rhoa_dryAirMolarDensity_g_m3MoistAir', headertext{i}) == 1% Is this correct? Convert?
rhoa_dry = data( :, i );
% Input all the different relative humidity variables and change to 0-1.
elseif strcmp('rH', headertext{i}) == 1 || ...
strcmp('RH_Avg', headertext{i}) == 1 || ...
strcmp('RH_4p5_Avg',headertext{i}) == 1 || ...
strcmp('RH_4p5',headertext{i}) == 1 || ...
strcmp('rh_hmp', headertext{i}) == 1 || ...
strcmp('rh_hmp_4_Avg', headertext{i}) == 1 || ...
strcmp('RH',headertext{i}) == 1 || ...
strcmp('RH_2_Avg',headertext{i}) == 1 || ...
strcmp('RH_10_Avg',headertext{i}) == 1 || ...
strcmp('RH_6p85_Avg', headertext{i})==1 || ...
strcmp('RH_24_Avg', headertext{i})==1
rH = data(:,i);
%strcmp('RH_3p7_Avg',headertext{i}) == 1 || ...
% strcmp('RH_2',headertext{i}) == 1 || ...
%Fixed scaling the rH now on a per rH value to account for
%the scale changes associated with program changes in the
%fluxall.
scale = find(rH > 1.1);
rH(scale) = rH(scale) ./ 100;
% Now convert back to percent
%rH = rH * 100;
elseif strcmp('Ts_mean', headertext{i}) == 1 || ...
strcmp('Ts_Avg', headertext{i}) == 1
Tair_TOA5 = data(:,i);
elseif strcmp('5point_precip', headertext{i}) == 1 || ...
strcmp('rain_Tot', headertext{i}) == 1 || ...
strcmp('precip', headertext{i}) == 1 || ...
strcmp('precip(in)', headertext{i}) == 1 || ...
strcmp('ppt', headertext{i}) == 1 || ...
strcmp('Precipitation', headertext{i}) == 1
precip = data(:,i);
elseif strcmp( 'press_mean', headertext{i}) == 1 || ...
strcmp('press_Avg', headertext{i}) == 1 || ...
strcmp('press_a', headertext{i}) == 1
atm_press = data(:,i);
elseif strcmp('par_correct_Avg', headertext{i}) == 1 || ...
strcmp('par_Avg(1)', headertext{i}) == 1 || ...
strcmp('par_Avg_1', headertext{i}) == 1 || ...
strcmp('par_Avg', headertext{i}) == 1 || ...
strcmp('par_licor', headertext{i}) == 1 || ...
strcmp('par_up_Avg', headertext{i}) == 1 || ...
strcmp('par_face_up_Avg', headertext{i}) == 1 || ...
strcmp('par_faceup_Avg', headertext{i}) == 1 || ...
strcmp('par_incoming_Avg', headertext{i}) == 1 || ...
strcmp('par_lite_Avg', headertext{i}) == 1
Par_Avg = data(:,i);
elseif strcmp('t_hmp_mean', headertext{i})==1 || ...
strcmp('AirTC_Avg', headertext{i})==1 || ...
strcmp('AirTC_2_Avg', headertext{i})==1 || ...
strcmp('AirTC_10_Avg', headertext{i})==1 || ...
strcmp('AirTC_4p5_Avg', headertext{i})==1 || ...
strcmp('t_hmp_3_Avg', headertext{i})==1 || ...
strcmp('pnl_tmp_a', headertext{i})==1 || ...
strcmp('t_hmp_Avg', headertext{i})==1 || ...
strcmp('t_hmp_4_Avg', headertext{i})==1 || ...
strcmp('t_hmp_top_Avg', headertext{i})==1| ...
strcmp('AirTC_6p85_Avg', headertext{i})==1| ...
strcmp('AirTC_24_Avg', headertext{i})==1
air_temp_hmp = data(:,i);
elseif strcmp('Tsoil',headertext{i}) == 1 || ...
strcmp('Tsoil_avg',headertext{i}) == 1 || ...
strcmp('soilT_Avg(1)',headertext{i}) == 1
Tsoil = data(:,i);
elseif strcmp('Rn_correct_Avg',headertext{i})==1 || ...
strcmp('NR_surf_AVG', headertext{i})==1 || ...
strcmp('NetTot_Avg_corrected', headertext{i})==1 || ...
strcmp('NetTot_Avg', headertext{i})==1 || ...
strcmp('Rn_Avg',headertext{i})==1 || ...
strcmp('Rn_total_Avg',headertext{i})==1
NR_tot = data(:,i);
elseif strcmp('Rad_short_Up_Avg', headertext{i}) || ...
strcmp('pyrr_incoming_Avg', headertext{i})
sw_incoming = data(:,i);
elseif strcmp('Rad_short_Dn_Avg', headertext{i})==1 || ...
strcmp('pyrr_outgoing_Avg', headertext{i})==1
sw_outgoing = data(:,i);
elseif strcmp('Rad_long_Up_Avg', headertext{i}) == 1 || ...
strcmp('Rad_long_Up__Avg', headertext{i}) == 1
lw_incoming = data(:,i);
elseif strcmp('CG3UpCo_Avg', headertext{i})==1 || ...
strcmp('Rad_long_Up_TCor_Avg', headertext{i})==1
lw_incoming_Co = data(:, i);
elseif strcmp('Rad_long_Dn_Avg', headertext{i})==1 || ...
strcmp('Rad_long_Dn__Avg', headertext{i})==1
lw_outgoing = data(:,i);
elseif strcmp('CG3DnCo_Avg', headertext{i})==1 || ...
strcmp('Rad_long_Dn_TCor_Avg', headertext{i})==1
lw_outgoing_Co = data(:, i);
elseif strcmp('VW_Avg', headertext{i})==1 || ...
strcmp('SWC_Avg_1', headertext{i})==1 || ...
strcmp('SWC_P1_5_Avg', headertext{i})==1
VWC = data(:,i);
elseif strcmp('shf_Avg(1)', headertext{i})==1 || ...
strcmp('shf_Avg_1', headertext{i})==1 || ...
strcmp('shf_pinon_1_Avg', headertext{i})==1
soil_heat_flux_1 = data(:,i);
disp('FOUND shf_pinon_1_Avg');
elseif any( strcmp( headertext{i}, ...
{ 'hfp_grass_1_Avg', 'hfp01_grass_Avg' } ) )
soil_heat_flux_1 = data(:,i);
disp('FOUND hfp_grass_1_Avg');
elseif any( strcmp( headertext( i ), ...
{ 'hfp_grass_2_Avg', 'hft3_grass_Avg' } ) )
soil_heat_flux_2 = data(:,i);
disp('FOUND hfp_grass_2_Avg');
elseif strcmp('shf_Avg(2)', headertext{i})==1 || ...
strcmp('shf_Avg_2', headertext{i})==1 || ...
strcmp('shf_jun_1_Avg', headertext{i})==1
soil_heat_flux_2 = data(:,i);
elseif strcmp('hfpopen_1_Avg', headertext{i})==1 % only for TX
soil_heat_flux_open = data(:,i);
elseif strcmp('hfpmescan_1_Avg', headertext{i})==1 % only for TX
soil_heat_flux_mescan = data(:,i);
elseif strcmp('hfpjuncan_1_Avg', headertext{i})==1 % only for TX
soil_heat_flux_juncan = data(:,i);
%Shrubland flux plates 2009 onwards
elseif strcmp('hfp01_1_Avg', headertext{i})==1
soil_heat_flux_1 = data(:,i);
elseif strcmp('hfp01_2_Avg', headertext{i})==1
soil_heat_flux_2 = data(:,i);
elseif strcmp('hfp01_3_Avg', headertext{i})==1
soil_heat_flux_3 = data(:,i);
elseif strcmp('hfp01_4_Avg', headertext{i})==1
soil_heat_flux_4 = data(:,i);
elseif strcmp('hfp01_5_Avg', headertext{i})==1
soil_heat_flux_5 = data(:,i);
elseif strcmp('hfp01_6_Avg', headertext{i})==1
soil_heat_flux_6 = data(:,i);
elseif strcmp('shf_Avg(3)', headertext{i})==1 |...
strcmp('shf_Avg_3', headertext{i})==1
soil_heat_flux_3 = data(:,i);
elseif strcmp('shf_Avg(4)', headertext{i})==1 |...
strcmp('shf_Avg_4', headertext{i})==1
soil_heat_flux_4 = data(:,i);
end
end
% Fix selected atmospheric water content measuremnts
if h2oflag_1
H2O_mean = H2O_mean .* ( 18 * ( 1 ./ ...
( 8.3143e-3 .* ( t_meanK ./ atm_press ) ) ) ./ 1000);
end
% remove absurd precipitation measurements
precip( precip > 1000 ) = NaN;
% In 2009 at GLand and SLand, LiCor PAR sensors (Par_Avg) are scaled to
% Kipp & Zonen sensors (Par_lite). Par_lite favored thereafter.
if ismember( sitecode, [ 1, 2 ] ) & year_arg == 2009
Par_Avg = combine_PARavg_PARlite( headertext, data );
end
% PJ girdle, calculate relative humidity from hmp obs using helper
% function. Not needed starting 1/10/2014
if sitecode == 10
rH = thmp_and_h2ohmp_2_rhhmp( air_temp_hmp, h2o_hmp ) ./ 100.0;
end
% Calculate VPD in millibars (hPa) using Teten's equation - GEM 5/2015
% see here:
% https://en.wikipedia.org/wiki/Clausius-Clapeyron_relation#Meteorology_and_climatology
% or here:
% http://andrewsforest.oregonstate.edu/data/studies/ms01/dewpt_vpd_calculations.pdf
tair_temp = Tdry - 273.15;
rH_100 = rH * 100;
es = 6.1078 .* exp( (17.269 .* tair_temp )./(237.3 + tair_temp) );
vpd = es - ( rH_100 .* es ./ 100 );
% vpd = 6.1078 * (1 - rH) .* exp(17.08085*tair_temp./(234.175+tair_temp));
%es = 0.6108*exp(17.27*air_temp_hmp./(air_temp_hmp+237.3));
%ea = rH .* es ;
%vpd2 = ea - es;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Site-specific steps for soil temperature
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if sitecode == 1 %GLand added TWH, 27 Oct 2011
for i=1:ncol;
if strcmp('TCAV_grass_Avg',headertext(i)) == 1
Tsoil = data(:,i-1);
end
end
% find soil heat flux plate measurements
SHF_idx = find( cellfun( @(x) ~isempty(x), ...
regexp( headertext, 'hfp.*[Aa]vg' ) ) );
if numel( SHF_idx ) ~= 2
%error( 'could not find two soil heat flux observations' );
end
soil_heat_flux = data( :, SHF_idx );
SHF_labels = headertext( SHF_idx );
SHF_labels = regexprep( SHF_labels, 'hfp01_(.*)', 'SHF_$1');
elseif sitecode == 2 %SLand changed TWH, 30 Oct 2012
% regular expression to identify SHF variables
re_SHF = '.*([Ss][Hh][Ff]).*|.*([Hh][Ff][Pp]).*';
SHF_idx = find( ~cellfun( @isempty, regexp( headertext, re_SHF ) ) );
SHF_labels = headertext( SHF_idx );
soil_heat_flux = data( :, SHF_idx );
elseif sitecode == 3 %JSav added TWH, 7 May 2012
SHF_cols = find( ~cellfun( @isempty, regexp( headertext, 'shf_Avg.*' ) ) );
soil_heat_flux = data( :, SHF_cols - 1 );
if isempty( soil_heat_flux )
soil_heat_flux = repmat( NaN, size( data, 1 ), 4 );
soil_heat_flux_1 = soil_heat_flux( :, 1 );
soil_heat_flux_2 = soil_heat_flux( :, 2 );
soil_heat_flux_3 = soil_heat_flux( :, 3 );
soil_heat_flux_4 = soil_heat_flux( :, 4 );
end
SHF_labels = { 'SHF_1', 'SHF_2', 'SHF_3', 'SHF_4' };
elseif sitecode == 4 | sitecode == 14 %PJ/TestSite
for i=1:ncol;
if strcmp('tcav_pinon_1_Avg',headertext(i)) == 1
Tsoil1 = data(:,i-1);
elseif strcmp('tcav_jun_1_Avg',headertext(i)) == 1
Tsoil2 = data(:,i-1);
end
end
if exist( 'Tsoil1' ) == 1 & exist( 'Tsoil2' ) == 1
Tsoil = (Tsoil1 + Tsoil2) ./ 2;
else
Tsoil = repmat( NaN, size( data, 1 ), 1 );
end
soil_heat_flux_1 = repmat( NaN, size( data, 1 ), 1 );
soil_heat_flux_2 = repmat( NaN, size( data, 1 ), 1 );
SHF_labels = { 'soil_heat_flux_1', 'soil_heat_flux_2' };
soil_heat_flux = [ soil_heat_flux_1, soil_heat_flux_2 ];
% related lines 678-682: corrections for site 4 (PJ) soil_heat_flux_1 and soil_heat_flux_2
%Tsoil=sw_incoming .* NaN; %MF: note, this converts all values in Tsoil to NaN. Not sure if this was intended.
elseif ismember( sitecode, [ UNM_sites.PPine, UNM_sites.MCon ] )
SHF_labels = { 'soil_heat_flux_1', 'soil_heat_flux_2', 'soil_heat_flux_3' };
soil_heat_flux = repmat( NaN, size( data, 1 ), 3 );
Tsoil = repmat( NaN, size( data, 1 ), 1 );
elseif ismember( sitecode, [ UNM_sites.MCon_SS ] )
SHF_labels = { 'shf_pit_1_Avg', 'shf_pit_2_Avg', 'shf_pit_3_Avg' };
soil_heat_flux = repmat( NaN, size( data, 1 ), 3 );
Tsoil = repmat( NaN, size( data, 1 ), 1 );
elseif sitecode == 7 % Texas Freeman
for i=1:ncol;
if strcmp('Tsoil_Avg_2',headertext(i)) == 1
open_5cm = data(:,i-1);
elseif strcmp('Tsoil_Avg_3',headertext(i)) == 1
open_10cm = data(:,i-1);
elseif strcmp('Tsoil_Avg_5',headertext(i)) == 1
Mesquite_5cm = data(:,i-1);
elseif strcmp('Tsoil_Avg_6',headertext(i)) == 1
Mesquite_10cm = data(:,i-1);
elseif strcmp('Tsoil_Avg_8',headertext(i)) == 1
Juniper_5cm = data(:,i-1);
elseif strcmp('Tsoil_Avg_9',headertext(i)) == 1
Juniper_10cm = data(:,i-1);
end
end
if year_arg == 2005 % juniper probes on-line after 5/19/05
% before 5/19
canopy_5cm = Mesquite_5cm(find(decimal_day < 139.61));
canopy_10cm = Mesquite_10cm(find(decimal_day < 139.61));
% after 5/19
canopy_5cm(find(decimal_day >= 139.61)) = ...
(Mesquite_5cm(find(decimal_day >= 139.61)) + ...
Juniper_5cm(find(decimal_day >= 139.61))) ./ 2;
canopy_10cm(find(decimal_day >= 139.61)) = ...
(Mesquite_10cm(find(decimal_day >= 139.61)) + ...
Juniper_10cm(find(decimal_day >= 139.61))) ./ 2;
% clean strange 0 values
canopy_5cm(find(canopy_5cm == 0)) = NaN;
canopy_10cm(find(canopy_10cm == 0)) = NaN;
Tsoil = (open_5cm + canopy_5cm) ./ 2;
else
canopy_5cm = (Mesquite_5cm + Juniper_5cm) ./ 2;
canopy_10cm = (Mesquite_10cm + Juniper_10cm) ./ 2;
Tsoil = (open_5cm + canopy_5cm) ./ 2;
end
elseif sitecode == 10 || sitecode == 11
Tsoil=sw_incoming .* NaN;
soil_heat_flux_1 =sw_incoming .* NaN;
soil_heat_flux_2 =sw_incoming .* NaN;
SHF_labels = { 'soil_heat_flux_1', 'soil_heat_flux_2' };
soil_heat_flux = [ soil_heat_flux_1, soil_heat_flux_2 ];
end
% Juniper S heat flux plates need multiplying by calibration factors
if sitecode == 3
soil_heat_flux_1 = soil_heat_flux_1 .* 32.27;
soil_heat_flux_2 = soil_heat_flux_2 .* 33.00;
soil_heat_flux_3 = soil_heat_flux_3 .* 31.60;
soil_heat_flux_4 = soil_heat_flux_4 .* 32.20;
end
% Pinon Juniper heat flux plates need multiplying by calibration factors
if sitecode == 4
soil_heat_flux_1 = soil_heat_flux_1 .* 35.2;
soil_heat_flux_2 = soil_heat_flux_2 .* 32.1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% data processing and fixing datalogger & instrument errors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fix incorrect precipitation values
precip_uncorr= precip;
precip = fix_incorrect_precip_factors( sitecode, year_arg, ...
timestamp, precip );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Radiation corrections
% RJL added the lw_incoming and lw_outgoing variables to output on 01172014
% Variables lw_incoming, lw_outgoing, NR_sw, NR_lw, and NR_tot are now
% corrected for temperature. New data logger programs starting 2014
% will have lw_incomingCo and lw_outgoingCo corrected variables.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% In 2007 & 2010 the thermocouple in the CNR1 at GLand failed. Use air temp.
if sitecode == 1 && year_arg==2007;
CNR1TempK = CNR1TK;
idx1 = find(decimal_day > 156.71 & decimal_day < 162.52 )
CNR1TempK( idx1 ) = air_temp_hmp( idx1 ) + 273.15;
elseif sitecode == 1 && year_arg==2010;
CNR1TempK = air_temp_hmp + 273.15;
% Same at shrub in 2007
elseif sitecode == 2 && year_arg==2007;
CNR1TempK = CNR1TK;
idx1 = find(decimal_day >= 150.75 & decimal_day < 162.44);
CNR1TempK( idx1 ) = air_temp_hmp( idx1 ) + 273.15;
% In 2007 the thermocouple in the CNR1 at PPine failed for a while in
% the spring. Use air temp.
elseif sitecode == 5 && year_arg==2007;
CNR1TempK = CNR1TK;
idx = DOYidx( 155.5 ):DOYidx( 198.55 );
CNR1TempK( idx ) = air_temp_hmp( idx ) + 273.15;
elseif sitecode == 5 & year_arg==2013;
CNR1TempK = CNR1TK;
idx = DOYidx( 353.18 ):17520;
CNR1TempK( idx ) = air_temp_hmp( idx ) + 273.15;
elseif sitecode == 5 & year_arg==2014;
CNR1TempK = CNR1TK;
idx = [ DOYidx( 1 ):DOYidx( 3.6 ), DOYidx( 83.81 ):DOYidx( 106.36 ) ] ;
CNR1TempK( idx ) = air_temp_hmp( idx ) + 273.15;
% In 2014 the thermocouple in the CNR1 at NewGLand failed for a while in
% the spring. Use air temp.
elseif sitecode == 11 & year_arg==2014;
CNR1TempK = CNR1TK;
idx = DOYidx( 17.645 ):DOYidx( 63.335 );
CNR1TempK( idx ) = air_temp_hmp( idx ) + 273.15;
% In 2013 the thermocouple in the CNR1 at PJ failed for a while in
% the fall. Use air temp.
elseif sitecode == 4 & year_arg==2013;
CNR1TempK = CNR1TK;
idx = DOYidx( 240.53 ):DOYidx( 282.53 );
CNR1TempK( idx ) = air_temp_hmp( idx ) + 273.15;
else
CNR1TempK = CNR1TK;
end
[ sw_incoming, sw_outgoing, ...
lw_incoming, lw_outgoing, Par_Avg ] = ...
UNM_RBD_apply_radiation_calibration_factors( sitecode, ...
year_arg, ...
decimal_day, ...
sw_incoming, ...
sw_outgoing, ...
lw_incoming, ...
lw_outgoing, ...
Par_Avg, ...
NR_tot, ...
wnd_spd, ...
CNR1TempK );
% These next 2 removals were formerly in
% UNM_RBD_apply_radiation_calibration_factors
% Applies to all sites and all years
% remove negative Rg_out values
sw_outgoing( sw_outgoing < -50 ) = NaN;
isnight = ( Par_Avg < 20.0 ) | ( sw_incoming < 20 );
%remove nighttime Rg and RgOut values outside of [ -5, 5 ]
% added 13 May 2013 in response to problems noted by Bai Yang
sw_incoming( isnight & ( abs( sw_incoming ) > 5 ) ) = NaN;
sw_outgoing( isnight & ( abs( sw_outgoing ) > 5 ) ) = NaN;
[ NR_sw, NR_lw, NR_tot ] = ...
UNM_RBD_calculate_net_radiation( sitecode, year_arg, ...
sw_incoming, sw_outgoing, ...
lw_incoming, lw_outgoing, ...
NR_tot, wnd_spd, decimal_day );
% normalize PAR to account for calibration problems at some sites
Par_Avg = normalize_PAR_wrapper( sitecode, year_arg, decimal_day, Par_Avg, ...
draw_plots >= 0 );
% Diagnostic plot for checking corrected radiation data
plot_qc_radiation( sitecode, ...
year_arg, ...
timestamp, ...
sw_incoming, ...
sw_outgoing, ...
lw_incoming, ...
lw_outgoing, ...
Par_Avg, ...
NR_tot );
% Diagnostic plot for checking corrected Met data
plot_qc_meteorology( sitecode, ...
year_arg, ...
timestamp, ...
air_temp_hmp, ...
rH_100, ...
vpd, ...
Tdry, ...
H2O_mean, ...
precip );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Apply Burba 2008 correction for sensible heat conducted from 7500
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define some constants
R = 8.3143e-3; % universal gas constant [J / umol / K ]
MWd = 28.97; % dry air molecular weight [g / mol]
Cp = 1004.67; % specific heat capacity of dry air @ constant pres [J / kg / K]
% This factor for conversion from mumol/mol to mg/m3 for CO2
% Essentially works as: ppm CO2 * ( MolecularWt CO2 / Vol of 1mol Air )
hh = 44.01 .* (1 ./ ( R .* ( t_meanK ./ atm_press ) .* 1000 ) );
CO2_mg = CO2_mean .* hh;
% This is the conversion from mmol/mol to g/m3 for H2O
gg = 18 * ( 1 ./ ...
( R .* ( t_meanK ./ atm_press ) ) ) ./ 1000;
% I don't think it is necessary to convert this (see notes above)
H2O_g = H2O_mean;% .* gg;
% Convert dry air density from mol/m3 to kg/m3
rhoa_dry_kg = ( rhoa_dry .* MWd ) ./ 1000; %