-
Notifications
You must be signed in to change notification settings - Fork 1
/
TreatBrewerData.R
2088 lines (1805 loc) · 88.1 KB
/
TreatBrewerData.R
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
#############################################################################
# #
# #
# TOTAL OZONE RECALCULATION #
# #
# #
# Module Name : TreatBrewerData.R #
# #
# #
# Creation Date : 21.04.2016 #
# #
# Last Modifications : #
# 06.10.2021 #
# 21.05.2021 #
# 06.01.2021 #
# 28.12.2020 #
# 22.07.2020 #
# 19.07.2020 #
# 18.01.2019 #
# 05.01.2018 #
# 23.03.2017 #
# 19.03.2017 #
# 17.03.2017 #
# 09.02.2017 #
# #
# #
# Author : SCHILL Herbert Meteoswiss #
# Modifications by : SCHILL Herbert PMOD/WRC #
# #
# Developing System : R 4.1.1 (2021) #
# #
# Short Description : #
# #
# 'TreatBrewerData.R' contains functions for the reading, writing, #
# recalculating and flagging of total ozone (direct sun) and #
# Standard Lamp tests values of Brewer measurements from/to #
# different data formats. #
# #
# #
# List of functions: #
# #
# - CalcNewDS #
# - CalcNewSL #
# - DataReduction #
# - ManuFlagDS #
# - ReadBrewerB #
# - ReadBrewerDS #
# - ReduceDS #
# - SetFlagsDS #
# - WriteBrewerDS #
# - WriteBrewerSL #
# - WriteDayozDS #
# - WriteAvgSL #
# - zenith_angle #
# #
# #
# Modification history: #
# #
# - 08.02.2017 by Herbert Schill: #
# #
# - addition of a number of new functions for recalculating #
# DS-measurements #
# #
# #
# - 17.03.2017 by Herbert Schill: #
# #
# - addition of function 'ManuFlagDS' for flagging/unflagging #
# DS-measurements of a day accordingly to 'FlagLst' #
# #
# - remove bug in 'SetFlagsDS' for proper unflagging of all #
# DS-measurements of a day #
# #
# #
# - 19.03.2017 by Herbert Schill: #
# #
# - allow use of original AbsCoeffs and ETCs from DS-file to #
# "recalculate" DS-file in 'CalcNewDS' #
# #
# - addition of function 'WriteDayozDS' for writing data on #
# a daily statistics file #
# #
# #
# - 23.03.2017 by Herbert Schill: #
# #
# - extend 'ReadBrewerDS' for reading DS-files of PMOD origin. #
# #
# #
# - 05.01.2018 by Herbert Schill: #
# #
# - remove bug in 'ReadBrewerDS' (reading StaLong) #
# #
# - remove bug in 'WriteBrewerDS' (data format of ETC's) #
# #
# - adapt calendar format to array instead of list #
# #
# #
# - 15.05.2018 by Herbert Schill: #
# #
# - 'print'-commands replaced by 'cat' #
# #
# #
# - 18.01.2019 by Herbert Schill: #
# #
# - remove bug in 'ReadBrewerB' (double info line) #
# #
# #
# - 19.07.2020 by Herbert Schill: #
# #
# - adaption in 'ReadBrewerDS' (Header line 5) #
# #
# #
# - 22.07.2020 by Herbert Schill: #
# #
# - modify procedure 'CalcNewDS' for calculations with different #
# O3 absorption coefficient sets, based or not based on the #
# daily effective stratosphere temperature #
# #
# #
# - 18.12.2020 by Herbert Schill: #
# #
# - remove bug in 'ReadBrewerDS': skip measurements with irregular #
# raw data sections #
# #
# - 'ReadBrewerB': check on valid filter pos #
# #
# - 'DataReduction': check on hard-coded boundaries for O3 and SO2 #
# #
# #
# - 28.12.2020 by Herbert Schill: #
# #
# - add procedures for recalculation of Standard Lamp tests from #
# raw data: 'CalcNewSL', 'WriteBrewerSL', 'WriteAvgSL' #
# #
# - modify 'DataReduction', 'ReadBrewerB' for treatment of #
# Standard Lamp tests from raw data #
# #
# #
# - 06.01.2021 by Herbert Schill: #
# #
# - 'CalcNewDS', refine calculations for undefined ETC(SO2) #
# #
# #
# - 21.05.2021 by Herbert Schill: #
# #
# - 'ReadBrewerB', read also station name from B-file #
# #
# #
# - 06.10.2021 by Herbert Schill: #
# #
# - 'DataReduction', catch erronous raw count values #
# #
#############################################################################
CalcNewDS <- function (BrewerStr, CalStr, cDate, rcType, bdata, ICFType,
ICFvalues, StationPara, tempACdataSet, A1cor)
#############################################################################
# #
# 'CalcNewDS' recalculates O3 and SO2 values from the raw data or #
# from the summary data read from the 'Bjjjyy.iii'-file (single #
# day file), or from the 'DSjjjyy.iii'-file (single day file). #
# #
# Function call: #
# #
# CalcNewDS (BrewerStr, CalStr, cDate, rcType, bdata, ICFType, #
# ICFvalues, StationPara, tempACdataSet, A1cor) #
# #
# Input: #
# #
# BrewerStr Brewer identifier as string (e.g. "040") #
# CalStr Brewer calibration as string (e.g. "Cal16a") #
# cDate date array, format={day, mon, year, jdn, leap} #
# #
# rcType recalculation type: #
# #
# BRaw recalculation from raw counts of B-file #
# BSum recalculation from summary data of B-file #
# DS recalculation from existing O3-resp. SO2-values #
# of the DS-file #
# #
# bdata structured Brewer direct sun raw and summary data #
# (if rcType="BRaw" or "Bsum") resp. calculated #
# direct sun data (if rcType="DS"). #
# #
# ICFType Instrument constants will be read from one of the #
# following sources: #
# #
# 0 constants from 'inst'-values from B-file (for #
# rcType=BRaw/BSum) resp. DS-file (for rcType=DS) #
# 1 constants from 'ICFjjjyy.iii'-file #
# 2 period constants from 'ICFCalxxx.iii'-file #
# #
# ICFvalues contents the instrument constants from the #
# 'ICFjjjyy.iii'-file (ICFType=1)resp. from the #
# 'ICFCalxxx.iii'-file (ICFType=2) resp. the #
# SO2 standard absorption coefficient (ICFType=0) #
# #
# StationPara station parameters #
# #
# tempACdataSet Flag for calculating O3-values with absorption #
# coefficients (A1cor) based on the effective #
# stratospheric temperature (Teff)of the day #
# #
# A1cor Teff-corrected absorption coefficient #
# #
# #
# Output: #
# #
# List (data header, single values, day statistics) #
# #
# #
# Some parts of this function were created by: #
# #
# Florian Pantillon (paf) at MeteoSuisse Payerne, 26.06.2008 #
# #
#############################################################################
{
# Choose source for instrument constants
if (ICFType==0)
{
if (rcType!="DS") # use instrument constants from B-file
{
PZ <- bdata[[1]][[1]] # standard station pressure [hPa]
TC <- bdata[[1]][[4]] # temperature coefficients
A1 <- bdata[[1]][[5]] # absorption coefficient O3
A2 <- bdata[[1]][[6]] # absorption coefficient SO2
A3 <- bdata[[1]][[7]] # absorption coefficient O3/SO2
B1 <- bdata[[1]][[8]] # extraterrestrial constant O3
B2 <- bdata[[1]][[9]] # extraterrestrial constant SO2
T1 <- bdata[[1]][[10]] # dead time [sec]
AF <- bdata[[1]][[11]] # filter attenuation values
#PZ;TC;A1;A2;A3;B1;B2;T1;AF
} else
{ A2 <- ICFvalues }
} else # use instrument constants from 'ICFjjjyy.iii'- resp. 'ICFCalxxx.iii'-file
{
PZ <- StationPara[[3]][[4]] # standard station pressure [hPa]
A1 <- as.double(ICFvalues[[8]]) # absorption coefficient O3
A2 <- as.double(ICFvalues[[9]]) # absorption coefficient SO2
A3 <- as.double(ICFvalues[[10]]) # absorption coefficient O3/SO2
B1 <- as.double(ICFvalues[[11]]) # extraterrestrial constant O3
B2 <- as.double(ICFvalues[[12]]) # extraterrestrial constant SO2
T1 <- as.double(ICFvalues[[13]]) # dead time [sec]
AF = array()
TC = array()
for (i in 1:5) (TC[i] <- as.double(ICFvalues[[i+1]])) # temperature coefficients
for (i in 1:6) (AF[i] <- as.double(ICFvalues[[i+16]])) # filter attenuation values
#PZ;TC;A1;A2;A3;B1;B2;T1;AF
}
# Choose source of data and type of recalculation
if (rcType!="DS")
{
Flag = array()
t0 = array()
strTime <- bdata[[3]][[1]] # time-strings [UTC]
AirM <- bdata[[3]][[3]] # airmass
NDF <- bdata[[3]][[4]] # neutral density filter pos
Tmp <- bdata[[3]][[5]] # instrument temperature
NMeas = length (bdata[[3]][[1]]) # total number of measurements
for (i in 1:NMeas)
{
t0[i] = StringToTime(strTime[i]) # UTC time [hrs]
Flag[i]=0
}
# if (rcType=="BRaw") # recalculation from raw counts
if (length(grep("BRaw",rcType,ignore.case=FALSE))>0)
{
ZA <- bdata[[2]][[2]] # solar zenith angle
p <- bdata[[2]][[3]] # neutral density filter pos
TE <- bdata[[2]][[4]] # instrument temperature
index <- bdata[[2]][[5]] # raw data index
F <- bdata[[2]][[6]] # raw photon counts
recalc <- DataReduction(PZ,TC,A1,A2,A3,B1,B2,T1,AF,ZA,p,TE,index,F,StationPara,0)
# DataReduction <- function(PZ,TC,A1,A2,A3,B1,B2,T1,AF,ZA,p,TE,index,F,StationPara,0)
# recalculated:
MS9 <- recalc[[1]][[1]] # double ratio Oz3
MS8 <- recalc[[1]][[2]] # double ratio SO2
Oz3 <- recalc[[1]][[3]] # Oz3
SO2 <- recalc[[1]][[5]] # SO2
SdevOz3 <- recalc[[1]][[4]] # staDev Oz3
SdevSO2 <- recalc[[1]][[6]] # staDev SO2
# check wether all indices of the raw data were used for the data reduction,
# otherwise adapt AirM, Flag, NDF, strTime, t0, Tmp vectors
# (modif. 08.10.2020 by Herbert Schill)
nnOrg=length(AirM)
nnNew=length(Oz3)
if (nnOrg!=nnNew)
{
AirM = array()
Flag = array()
NDF = array()
strTime = array()
tt0 = array()
Tmp = array()
k=0
for (i in 1:nnOrg)
{
if (length(grep(i,index))>0)
{
k=k+1
strTime[k] = bdata[[3]][[1]][[i]]
AirM[k] = bdata[[3]][[3]][[i]]
NDF[k] = bdata[[3]][[4]][[i]]
Tmp[k] = bdata[[3]][[5]][[i]]
Flag[k]=0
tt0[k] = t0[i]
} # end if
} # next i
t0 = array()
t0 <- tt0
NMeas = k
} # end if nnOrg!=nnNew
} else # rcType=="BSum": recalculation from original summary data
{
MS8 <- bdata[[3]][[10]] # double ratio MS8
MS9 <- bdata[[3]][[11]] # double ratio MS9
SdevOz3 <- bdata[[3]][[13]] # original staDev O3
SdevSO2 <- bdata[[3]][[15]] # original staDev SO2
Oz3 <- ((MS9-B1)/(A1*AirM))/10 # MS11
SO2 <- ((MS8-B2)/(A2*A3*AirM)-Oz3*10/A2)/10 # MS10
}
} else # rcType="DS": recalculation from the 'DSjjjyy.iii'-file data
{
NMeas <- bdata[[1]][[1]] # total number of measurements
A1org <- bdata[[1]][[10]] # original absorption coefficient O3
A2org <- A2 # absorption coefficient SO2
A3org <- bdata[[1]][[11]] # original absorption coefficient O3/SO2
B1org <- bdata[[1]][[8]] # original extraterrestrial constant O3
B2org <- bdata[[1]][[9]] # original extraterrestrial constant SO2
strTime <- bdata[[2]][[1]] # time-strings [UTC]
t0 <- bdata[[2]][[2]] # UTC time [hrs]
NDF <- bdata[[2]][[3]] # neutral density filter positions
Tmp <- bdata[[2]][[4]] # instrument temperatures
AirM <- bdata[[2]][[5]] # airmasses
Oz3org <- bdata[[2]][[6]] # original Oz3 values
SdevOz3 <- bdata[[2]][[7]] # original staDev O3
SO2org <- bdata[[2]][[8]] # original SO2 values
SdevSO2 <- bdata[[2]][[9]] # original staDev SO2
Flag <- bdata[[2]][[10]] # flag
# If a temperature-dependent temporal absorption ceofficient series, based
# on the stratospheric temperature of Payerne of the day, or an O3-absorption
# ceofficient different from BassPaurOp, is desired ,the proper absorption
# ceofficient 'A1cor' is used for the O3-recalculation, otherwise original
# AbsCoeffs and ETCs from the input file are used
if (ICFType==0)
{
A1 = A1cor
A2 = A2org
A3 = A3org
B1 = B1org
B2 = B2org
}
# backwards calculation of double ratios MS8 and MS9 from
# original Oz3 and SO2, then recalculation of Oz3 and SO2
# with new (or old, if ICFType=0, tempACdataSet=0)
# AbsCoeffs and ETCs
MS9 <- 10*Oz3org*A1org*AirM+B1org
Oz3 <- ((MS9-B1)/(A1*AirM))/10 # MS11
if (B2<9000)
{
MS8 <- 10*(SO2org*A2org+Oz3org)*A3org*AirM+B2org
SO2 <- ((MS8-B2)/(A2*A3*AirM)-Oz3*10/A2)/10 # MS10
} else # ETC(SO2) not defined
{
SO2 = array()
for (i in 1:length(SO2org)) SO2[i] <- 9.9
SdevSO2 <- SO2
MS8 <- SO2*1000
}
#A1;A1org;A2;A2org;A3;A3org;B1;B1org;B2;B2org
#Oz3org;Oz3;SO2org;SO2
} # end if rcType:"DS"
# Daily summary statistics: filtering depends of data source:
#
# if data source = "DS", filtering is done by flag,
# if data source = "B", filtering is done by AirM and SdevOz3
S1<-S2<-S3<-S5<-S6<-S7<-S8<-Z1<-Z2<-Z3<-Z4<-Z5<-Z6<-Z7<-Z8<-Z9<-0
if (rcType!="DS")
{
AirR <- AirM[AirM<=3.2 & SdevOz3<=2.5]
Oz3R <- Oz3[AirM<=3.2 & SdevOz3<=2.5]
SO2R <- SO2[AirM<=3.2 & SdevOz3<=2.5]
MS9R <- MS9[AirM<=3.2 & SdevOz3<=2.5]
MS8R <- MS8[AirM<=3.2 & SdevOz3<=2.5]
TER <- Tmp[AirM<=3.2 & SdevOz3<=2.5]
} else
{
AirR <- AirM[Flag<1]
Oz3R <- Oz3[Flag<1]
SO2R <- SO2[Flag<1]
MS9R <- MS9[Flag<1]
MS8R <- MS8[Flag<1]
TER <- Tmp[Flag<1]
}
S1 = length(AirR) # number of valid measurements
if (S1>0)
{
S2 <- sum(Oz3R)
S3 <- sum(Oz3R*Oz3R)
S5 <- sum(SO2R)
S6 <- sum(SO2R*SO2R)
S7 <- sum(TER)
Z1 <- S1
Z2 <- sum(1/AirR)
Z3 <- sum(1/(AirR*AirR))
Z5 <- sum(MS9R/AirR)
Z4 <- sum(MS9R/(AirR*AirR))
Z8 <- sum(MS8R/AirR)
Z7 <- sum(MS8R/(AirR*AirR))
#Z1;Z2;Z3;Z4;Z5;Z7;Z8
if (S1==1)
{
S3=0
S6=0
Z6=0
Z9=0
} else
{
S8 = (S1*S3-S2*S2)/S1/(S1-1)
if (S8>=0) {S3=sqrt(S8)} else {S3=0} # staDev Oz3
Z6 = (Z1*Z4-Z2*Z5)/(Z1*Z3-Z2*Z2) # ETC Oz3 today (ETD)
if (B2<9000)
{
S8 = (S1*S6-S5*S5)/S1/(S1-1)
if (S8>=0) {S6=sqrt(S8)} else {S6=0} # staDev SO2
Z9 = (Z1*Z7-Z2*Z8)/(Z1*Z3-Z2*Z2) # ETC SO2 today (ETD)
} else # ETC(SO2) not defined
{
S6 = 9.9
Z9 = 9999.9
}
}
S2 = S2/S1 # mean Oz3
S5 = S5/S1 # mean SO2
S7 = S7/S1 # mean instrument temperature
Z2 = Z1/Z2 # mean airmass
}
#S1;S2;S3;S5;S6;S7
#Z1;Z6;DSdata[[3]][[9]];Z9;DSdata[[3]][[10]]
# Write recalculated data in DS-data structure
HeaderValues = list(NMeas,cDate[1],cDate[2],cDate[3],cDate[4],BrewerStr,CalStr,B1,B2,A1,A3)
names(HeaderValues) = c("NMeas","day","mon","year","jdn","Brewer","Calib","EtcOz3","EtcSO2","AbsCoeffOz3","AbsCoeffSO2")
Measures = list(strTime, t0, NDF, Tmp, AirM, Oz3, SdevOz3, SO2, SdevSO2, Flag)
names(Measures) = c("TimeStr","MeasTime","NDF","InstrTemp","Airmass","Oz3","ErrOz3","SO2","ErrSO2","Flag")
DayStatis = list(S1, NMeas, S7, Z2, S2, S5, S3, S6, Z6, Z9)
names(DayStatis) = c("NbOzVal","NbOzTot","AvgTemp","AvgAirm","AvgOzon","AvgSO2","sdvOz3","sdvSO2","EtdOz3","EtdSO2")
return(list(HeaderData=HeaderValues,Measurements=Measures,DayStatistics=DayStatis))
} # end of function 'CalcNewDS'
CalcNewSL <- function (BrewerStr, CalStr, cDate, bdata, ICFType, ICFvalues,
StationPara)
#############################################################################
# #
# 'CalcNewSL' recalculates SL-test values from the raw data from #
# the 'Bjjjyy.iii'-file (single day file) #
# #
# Function call: #
# #
# CalcNewSL (BrewerStr, CalStr, cDate, bdata, ICFType, ICFvalues, #
# StationPara) #
# #
# Input: #
# #
# BrewerStr Brewer identifier as string (e.g. "040") #
# CalStr Brewer calibration as string (e.g. "Cal16a") #
# cDate date array, format={day, mon, year, jdn, leap} #
# #
# bdata structured Brewer SL-tests raw and summary data #
# #
# ICFType Instrument constants will be read from one of the #
# following sources: #
# #
# 0 constants from 'inst'-values from B-file #
# 1 constants from 'ICFjjjyy.iii'-file #
# 2 period constants from 'ICFCalxxx.iii'-file #
# #
# ICFvalues contents the instrument constants from the #
# 'ICFjjjyy.iii'-file (ICFType=1)resp. from the #
# 'ICFCalxxx.iii'-file (ICFType=2) resp. the #
# SO2 standard absorption coefficient (ICFType=0) #
# #
# StationPara station parameters #
# #
# #
# Output: #
# #
# List (data header, single values, day statistics) #
# #
# #
# Some parts of this function were created by: #
# #
# Florian Pantillon (paf) at MeteoSuisse Payerne, 26.06.2008 #
# #
#############################################################################
{
# Choose source for instrument constants
if (ICFType==0)
{
PZ <- bdata[[1]][[1]] # standard station pressure [hPa]
TC <- bdata[[1]][[4]] # temperature coefficients
A1 <- bdata[[1]][[5]] # absorption coefficient O3
A2 <- bdata[[1]][[6]] # absorption coefficient SO2
A3 <- bdata[[1]][[7]] # absorption coefficient O3/SO2
B1 <- bdata[[1]][[8]] # extraterrestrial constant O3
B2 <- bdata[[1]][[9]] # extraterrestrial constant SO2
T1 <- bdata[[1]][[10]] # dead time [sec]
AF <- bdata[[1]][[11]] # filter attenuation values
#PZ;TC;A1;A2;A3;B1;B2;T1;AF
} else # use instrument constants from 'ICFjjjyy.iii'- resp. 'ICFCalxxx.iii'-file
{
PZ <- StationPara[[3]][[4]] # standard station pressure [hPa]
A1 <- as.double(ICFvalues[[8]]) # absorption coefficient O3
A2 <- as.double(ICFvalues[[9]]) # absorption coefficient SO2
A3 <- as.double(ICFvalues[[10]]) # absorption coefficient O3/SO2
B1 <- as.double(ICFvalues[[11]]) # extraterrestrial constant O3
B2 <- as.double(ICFvalues[[12]]) # extraterrestrial constant SO2
T1 <- as.double(ICFvalues[[13]]) # dead time [sec]
AF = array()
TC = array()
for (i in 1:5) (TC[i] <- as.double(ICFvalues[[i+1]])) # temperature coefficients
for (i in 1:6) (AF[i] <- as.double(ICFvalues[[i+16]])) # filter attenuation values
#PZ;TC;A1;A2;A3;B1;B2;T1;AF
}
# Prepare and perform recalculation
strTime <- bdata[[3]][[1]] # time-strings [UTC]
NDF <- bdata[[3]][[4]] # neutral density filter pos
Tmp <- bdata[[3]][[5]] # instrument temperature
NMeas = length (bdata[[3]][[1]]) # total number of measurements
ZA <- bdata[[2]][[2]] # solar zenith angle
p <- bdata[[2]][[3]] # neutral density filter pos
TE <- bdata[[2]][[4]] # instrument temperature
index <- bdata[[2]][[5]] # raw data index
F <- bdata[[2]][[6]] # raw photon counts
recalc <- DataReduction(PZ,TC,A1,A2,A3,B1,B2,T1,AF,ZA,p,TE,index,F,StationPara,1)
# DataReduction <- function(PZ,TC,A1,A2,A3,B1,B2,T1,AF,ZA,p,TE,index,F,StationPara,isSL)
# check wether all indices of the raw data were used for the data reduction,
# otherwise adapt NDF, strTime, Tmp vectors
valSL <- recalc[[1]]
nnOrg=length(Tmp)
nnNew=length(recalc[[1]][[6]])
if (nnOrg!=nnNew)
{
NDF = array()
strTime = array()
Tmp = array()
k=0
for (i in 1:nnOrg)
{
if (length(grep(i,index))>0)
{
k=k+1
strTime[k] = bdata[[3]][[1]][[i]]
NDF[k] = bdata[[3]][[4]][[i]]
Tmp[k] = bdata[[3]][[5]][[i]]
} # end if
} # next i
NMeas = k
} # end if nnOrg!=nnNew
# Daily summary statistics
if (NMeas>0)
{
avgSL = array()
redSL = array()
kk=0
for (k in 1:NMeas)
{
if ((valSL[[9]][[k]]<50) & (valSL[[8]][[k]]<100))
{
for (i in 1:7)
{
kk=kk+1
redSL[kk] = valSL[[i]][[k]]
}
}
}
NAvg=kk/7
dim(redSL) <- c(7,NAvg) # reshape array
if (NAvg>0)
{
for (i in 1:7)
{
avgSL[i] = mean(redSL[i,])
if (NAvg>1) avgSL[i+7]=sd(redSL[i,]) else avgSL[i+7]=0
}
}
} # end if NMeas>0
# Write recalculated data in SL-data structure
HeaderValues = list(NMeas,cDate[1],cDate[2],cDate[3],cDate[4],BrewerStr,CalStr," ")
names(HeaderValues) = c("NMeas","day","mon","year","jdn","Brewer","Calib","TimeStamp")
Measures = list(strTime, NDF, Tmp, valSL)
names(Measures) = c("TimeStr","NDF","InstrTemp","valuesSL")
DayStatis = list(NAvg, min(Tmp), max(Tmp), avgSL)
names(DayStatis) = c("NAvg", "TLo", "THi", "AvgData")
return(list(HeaderData=HeaderValues,Measurements=Measures,DayStatistics=DayStatis))
} # end of function 'CalcNewSL'
DataReduction <- function(PZ,TC,A1,A2,A3,B1,B2,T1,AF,ZA,p,TE,index,F,
StationPara,isSL)
#############################################################################
# #
# FUNCTION DataReduction(PZ,TC,A1,A2,A3,B1,B2,T1,AF,ZA,P,TE,INDEX,F) #
# #
# INPUT: MEAN PRESSURE, TEMPERATURE COEFF, 3 ABSORPTION COEFFS, #
# 2 EXTRATERRESTRIAL CONSTANTS, DEAD TIME, ABSORPTION OF FILTERS, #
# VECTOR OF ZENITH ANGLES [°], VECTOR OF FILTER POSITIONS, #
# VECTOR OF TEMPERATURES [°C], VECTOR OF MESUREMENT INDEXES, #
# MATRIX OF RAW PHOTON COUNTS, DataType isSL [DS=0, SL=1] #
# #
# OUTPUT: LIST(INSTRUMENT CORRECTED PHOTON RATES, #
# COMPUTED TOTAL OZONE & STANDARD DEVIATION [DU], #
# COMPUTED TOTAL SULPHUR DIOXIDE & STANDARD DEVIATION [DU]) #
# #
# ALL INPUT VECTORS MUST HAVE SAME LENGTH N #
# INPUT MATRIX F MUST HAVE 6 ROWS AND N COLUMNS #
# #
# This function was essentially created by: #
# #
# Florian Pantillon (paf) at MeteoSuisse Payerne, 26.06.2008 #
# #
# The extensions for treating SL-tests are added by Herbert Schill #
# #
#############################################################################
{
# Hard-coded parameters
CY <- 20 # Number of Cycles
IT <- 0.1147 # Time Factor
BE <- c(4870,4620,4410,4220,4040) # Rayleigh Coefficients
# Station parameters
HOZ = StationPara[[3]][[5]] # Barycentric height ozone layer [km]
HAT = StationPara[[3]][[6]] # Barycentric height atmosphere [km]
ERA = StationPara[[3]][[7]] # Earth radius [km]
PSL = StationPara[[3]][[8]] # Standard sea level pressure [hPa]
# Computing corrected photon rates
if (isSL==1) ctsw1 <- F[2,] # Photon raw counts wl 1
for (i in 2:6) F[i,] <- 2*(F[i,]-F[1,])/(CY*IT) # Photon raw counts to rates
for (i in 1:length(F)) if (F[i]<1) F[i] <- 1.e-4 # set negative/low values to 1E-04
F <- FF <- F[2:6,]
for (i in 1:9) F <- FF*exp(F*T1) # Deadtime correction
F <- log10(F)*10^4 # for integer arithmetic
for (i in 1:5) F[i,] <- F[i,]+TC[i]*TE # Instrument temperature correction
for (i in 1:5) F[i,] <- F[i,]+AF[p+1] # Neutral density filters attenuation
if (isSL==0)
{
AMA <- (1/cos(asin(ERA/(ERA+HAT)*sin(ZA*pi/180)))) # Airmass through atmosphere
for (i in 1:5) F[i,] <- F[i,]+BE[i]*AMA*PZ/PSL # Rayleigh scattering correction
}
# Computing ratios
R1 <- F[4,]-F[1,] # MS(4)
R2 <- F[4,]-F[2,] # MS(5)
R3 <- F[4,]-F[3,] # MS(6)
R4 <- F[5,]-F[4,] # MS(7)
R5 <- R1-3.2*R4 # MS(8)
R6 <- R2-0.5*R3-1.7*R4 # MS(9)
if (isSL==0) # Treat DS-measurements
{
# Determining total Ozone and total Sulphur Dioxide (DS)
AMO <- (1/cos(asin(ERA/(ERA+HOZ)*sin(ZA*pi/180)))) # airmass through ozone layer
ozone <- (R6-B1)/(A1*AMO) # MS(11)
sulph <- (R5-B2)/(A2*A3*AMO)-ozone/A2 # MS(10)
# Mean values by index (groups of 5 normally)
MS8 <- MS9 <- O3 <- dO3 <- SO2 <- dSO2 <- array()
i <- j_old <- 1
for (j in 1:length(ozone))
{
if (j==length(ozone) | index[j+1]!=index[j])
{
MS8[i] <- mean(R5[j_old:j]) # Double Ratio SO2
MS9[i] <- mean(R6[j_old:j]) # Double Ratio Oz3
O3[i] <- mean(ozone[j_old:j])/10 # Total O3 [DU]
SO2[i] <- mean(sulph[j_old:j])/10 # Total SO2 [DU]
if ((j-j_old)>0)
{
dO3[i] <- sd(ozone[j_old:j])/10 # StaDev on total O3 [DU]
dSO2[i] <- sd(sulph[j_old:j])/10 # StaDev on total SO2 [DU]
} else
{
dO3[i]=0
dSO2[i]=0
}
j_old <- j+1
i <- i+1
} # end if
} # next j
# Check on hard-coded boundaries for O3, SO2, dO3 and dSO2; write values in list
for (i in 1:length(O3))
{
if ((O3[i]<100) | (O3[i]>600)) dO3[i]=99.9
if (O3[i]<100) O3[i]=99.9
if (O3[i]>999) O3[i]=999.9
if ((SO2[i]<(-15)) | (SO2[i]>15)) dSO2[i]=99.9
if (SO2[i]<(-100)) SO2[i]=-99.9
if (SO2[i]>100) SO2[i]=99.9
if (dO3[i]>99.9) dO3[i]=99.9
if (dSO2[i]>99.9) dSO2[i]=99.9
}
computed <- list(MS8,MS9,O3,dO3,SO2,dSO2)
names(computed) <- c("DoubleRatioOz3","DoubleRatioSO2","TotalOz3","StaDevOz3","TotalSO2","StaDevSO2")
} else # Treat SL-measurements
{
# Mean values by index (groups of 7 normally)
MS4 <- MS5 <- MS6 <- MS7 <- MS8 <- MS9 <- Int1 <- sdR5 <- sdR6 <- sdInt <- array()
i <- j_old <- 1
for (j in 1:length(R6))
{
if ((j==length(R6)) | (index[j+1]!=index[j]))
{
MS4[i] <- mean(R1[j_old:j]) # Single Ratio R1
MS5[i] <- mean(R2[j_old:j]) # Single Ratio R2
MS6[i] <- mean(R3[j_old:j]) # Single Ratio R3
MS7[i] <- mean(R4[j_old:j]) # Single Ratio R4
MS8[i] <- mean(R5[j_old:j]) # Single Ratio R5
MS9[i] <- mean(R6[j_old:j]) # Single Ratio R6
Int1[i] <- mean(ctsw1[j_old:j]) # Mean of counts wl 1
if ((j-j_old)>0)
{
sdR5[i] <- sd(R5[j_old:j]) # StaDev on single Ratio R5
sdR6[i] <- sd(R6[j_old:j]) # StaDev on single Ratio R6
sdInt[i] <- sd(ctsw1[j_old:j]) # StaDev on counts wl 1
} else
{
sd5[i]=0
sd6[i]=0
sdInt[i]=0
}
# if ((sdR5[i]<50) & (sdR6[i]<100)) # same filter as GW-Basic 'SLSUM.RTN'
# {
# j_old <- j+1
i <- i+1
# }
j_old <- j+1
} # end if
} # next j
# Write values in list
computed <- list(MS4,MS5,MS6,MS7,MS8,MS9,Int1,sdR5,sdR6,sdInt)
names(computed) <- c("R1","R2","R3","R4","R5","R6","Int1","sdR5","sdR6","sdInt1")
} # end if isSL
return(list("Recalculated"=computed))
} # end of function 'DataReduction'
ManuFlagDS <- function (dataIn, f2ix, flagLst)
#############################################################################
# #
# Function 'ManuFlagDS' sets flags accordingly to 'FlagLst' to the #
# DS-data in dataset 'dataIn' #
# #
# #
# Function call: #
# #
# ManuFlagDS (dataIn, f2ix, flagLst) #
# #
# Input: #
# #
# dataIn Unflagged (or partially unflagged) DS dataset #
# flagLst Complete list of measurements to flag/unflag #
# f2ix Indices of flaglist for instrument and day #
# #
# Output: #
# #
# return Properly flagged (or unflagged) DS dataset. #
# #
#############################################################################
{
hms <- flagLst[6,f2ix]
for (i in 1:length(hms))
{
n=grep (hms[i],dataIn$Measurements$TimeStr,ignore.case=FALSE)
if (length(n)>0)
{
k=f2ix[i]
dataIn$Measurements$Flag[n] = as.integer(flagLst[14,k])
}
} # end for i=1:length(hms)
return(dataIn)
} # end of function 'ManuFlagDS'
ReadBrewerB <- function (jdn, day, mon, year, brewStr, bPath, inData)
#############################################################################
# #
# 'ReadBrewerB' reads the raw and the summary values of Brewer #
# measurements from the 'B' data format: #
# #
# - 'Bjjjyy.iii'-files (single day file) #
# #
# Function call: #
# #
# ReadbrewerB (jdn, day, mon, year, brewer, dspath) #
# #
# Input: Date: either jdn [jjj] and year [yyyy], #
# or day [dd], month [mm] and year [yyyy], #
# #
# Brewer identifier [bbb] #
# #
# Inputfile path (may contain 'bbb', 'Calxxx', 'yyyy') #
# #
# Type of data to read: direct sun meas. (inData='ds'), #
# or SL-tests (inData='sl') #
# #
# Output: List (Lists of instrument constants, raw and summary #
# values) #
# #
# #
# Some parts of this function were created by: #
# #
# Florian Pantillon (paf) at MeteoSuisse Payerne, 26.06.2008 #
# #
#############################################################################
{
# Replace "bbb", and "yyyy" in filepath 'dspath' by brewer and year,
# if occurring
bPath = ReplaceCalInstYear (bPath, "xxxxx", brewStr, year)
if (jdn==0) # calculate jdn, if necessary
{
cDate = array()
cDate[1] = day
cDate[2] = mon
cDate[3] = year
cDate[4] = 0
cDate[5] = 0
cDate = ConvertDate(cDate, 1)
jdn = cDate[4]
}
# Create filename, test on existence and open 'Bjjjyy.iii'-file
if (year>=2000) {sYear=year-2000} else {sYear=year-1900}
filename = sprintf("%s%03d%02d.%s", "B", jdn, sYear, brewStr)
fpathname = sprintf("%s%s", bPath, filename)
ex=file.exists(fpathname)
if (file.exists(fpathname))
{
# Open and read lines of input-file, close file
bf = file(fpathname,open="r")
inp = readLines(bf,n=-1,ok=TRUE)
close(bf)
# Find data header ("dh" line)
n=grep ("dh",inp,ignore.case=FALSE)
if (length(n)>0)
{
staName <- inp[n[1]+4]
latitude = as.double(inp[n[1]+5])
longitude = as.double(inp[n[1]+6])
PZ = as.integer(inp[n[1]+9]) # mean station pressure
}
# Find instrument constants ("inst" line)
n=grep ("inst",inp,ignore.case=FALSE)
if (length(n)>0)
{
TC = as.double(inp[n[1]+1:5]) # temperature coefficients
A1 = as.double(inp[n[1]+7]) # absorption coefficients
A2 = as.double(inp[n[1]+8])
A3 = as.double(inp[n[1]+9])
B1 = as.integer(inp[n[1]+10]) # extra-terrestrial constants
B2 = as.integer(inp[n[1]+11])
T1 = as.double(inp[n[1]+12]) # dead time
AF = as.integer(inp[n[1]+16:21]) # filter absorption values
pos = n[1]+22
}
# Iinitialization
i <- j <- k <- l <- m <- nrat <- i_old <- 0
p <- t_ds <- ZA_ds <- F <- TE <- index_ds <- array()
t_sumds <- ZA_sumds <- M2 <- SO2 <- O3 <- dSO2 <- dO3 <- array()
TT <- ND <- MS4 <- MS5 <- MS6 <- MS7 <- MS8 <- MS9 <- array()
if (length(grep("sl",inData,ignore.case=FALSE))>0) nSeq=7 else nSeq=5
# Find direct sun summary and raw data ("summary" resp. "ds" or "sl" lines)
n=grep ("summary",inp,ignore.case=FALSE)
n <- n[n>pos]
found=length(n)
if (found>0)
{
for (f in 1:found)
{
if (inp[n[f]+8]==inData) # summary contains ds or sl data
{
nrat=0
invalidInput=0
for (g in 1:nSeq)
{
r = n[f]-19*g
if (inp[r]==inData) # decode previous ds or sl raw data
{
nrat=nrat+1
i=i+1
p[i] <- as.integer(inp[r+2])/64 # Filter position
if (p[i]>5)
{
p[i]=0 # set invalid filter pos to zero
invalidInput=invalidInput+1
}
t_ds[i] <- as.double(inp[r+3]) # UTC time [min]
ZA_ds[i] <- zenith_angle(as.double(inp[r+3]),jdn,sYear,latitude,longitude) # compute SZA[°]
F[(6*i-5):(6*i)] <- as.integer(inp[(r+8):(r+13)]) # Raw photon counts
}
} # next g
# decode direct sun summary data, if raw data available
if (nrat>0)
{
l <- l+1
t_sumds[l] <- inp[n[f]+1] # UTC TIME [HH:MM:SS]
ZA_sumds[l] <- as.double(inp[n[f]+5]) # ZENITH ANGLE [°]