-
Notifications
You must be signed in to change notification settings - Fork 1
/
NOX_P20_1907.R
1351 lines (1235 loc) · 79.1 KB
/
NOX_P20_1907.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
###########################################################################################################################
## Project: NEXTDATA ##
## Parameter: NOx ##
## Script first purpose: create a formatted Level-0 dataset (in EBAS format) from raw data ##
## Script second purpose: create daily acquisition graphs of all instrumentation variables ##
## Run time: the script may run daily (e.g. by using a crontab) or may be used when needed ##
##_______________________________________________________________________________________________________________________##
## Authors: Luca Naitza, Davide Putero ##
## Organization: National Research Council of Italy, Institute for Atmospheric Science and Climate (CNR-ISAC) ##
## Address: Via Gobetti 101, 40129, Bologna, Italy ##
## Project Contact: Paolo Cristofanelli ##
## Email: [email protected] ##
## Phone number: (+39) 051 639 9597 ##
##_______________________________________________________________________________________________________________________##
## Script filename: NOX_D20_1810.R ##
## Version Date: July 2019
## Feb. 19th, 2019: added duplicates removal
## Jul. 31st, 2019: "days_ref" substituted with "dependent_col", i.e., the number of dependent data columns (in the header)
###########################################################################################################################
# > > > > > > > > > > > > > > I N S T R U C T I O N S < < < < < < < < < < < < < < < < < < < < < < < < #
#
# This script consists of several parts and sub-parts.
#
# Part 0 is the setting section, in which the User should replace the marked values (e.g. directory paths)
# Part 0.1 defines environmental variables, such as raw data and destination directories. The user should modify these values.
# Part 0.2 specifies some characteristics of raw data tables, such as file extension, fields separators, etc. The user should
# modify these values according to his/her tables format. If the procedure requires more than one parameter
# setting, Part 0.2 is divided in sub-sub-parts (e.g. 0.2.1 , 0.2.2,...)
# Part 0.3 specifies the characteristics of User Station/Laboratory/Instrument/parameter. Most of these variables are used
# as metadata in the EBAS file format header. The User should provide proper information.
#
# Part 1 is another setting section; it should not be modified by the user unless strictly needed.
# Part 1.1 sets and loads the most commonly used R libraries. The user should not modify this sub-part.
# Part 1.2 specifies the time variables used in the processing. The user should not modify this sub-part. If the user needs
# to apply the script to data older than the current year, he/she may modify the lines explicitly marked for this purpose.
#
# Part 2 is the data processing section, it should not be modified by the user.
# Part 2.x contain the code to produce the EBAS format file and to process data. The User should not modify this sub-part(s).
#
# Part 3 is the data reporting section, it should not be modified by the user.
# Part 3.x contain the code to produce graphic reports. The user should not modify this sub-part(s).
#
# > > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < #
###########################################################################################################################
## # PART 0.1 #
## ______________________________________________________________________________________________________________________##
## Setting environmental variables
## ______________________________________________________________________________________________________________________##
## USE: set the following paths of origin raw data and of destination processing data, replacing values with yours
# WARNING: Proper setting up of the following paths and values is crucial. Please, replace the strings correctly
#
setwd("~/")
# -------- RAW DATA PATH ----------------------------------------------------------------------# REPLACE THE FOLLOWING PATHS
CALIB_DIR = '../naitza/NEXTDATA/PROD/CIMONE/GAS/NO/RAW_DATA_UTC/CALIB'
RAW_DIR = '../naitza/NEXTDATA/PROD/CIMONE/GAS/NO/RAW_DATA_UTC'
METEO_RAW_DIR = '../naitza/NEXTDATA/PROD/CIMONE/METEO/METEO/RAW_DATA_UTC'
#
# -------- DATA DESTINATION PATH --------------------------------------------------------------# REPLACE THE FOLLOWING PATHS
L0_DIR = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/LEVEL_0'
L1_DIR = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/LEVEL_1'
L2_DIR = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/LEVEL_2'
L0_ANCIL_DIR = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/LEVEL_0/ANCILLARY'
#
# -------- GRAPH DESTINATION PATH -------------------------------------------------------------# REPLACE THE FOLLOWING PATHS
REP_DIR = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT'
REP_GRAPH_DIR = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT/DAILY_GRAPH'
PLOT_DIR_M = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT/MONTHLY_GRAPH'
PLOT_DIR_S = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT/SEASONAL_GRAPH'
PLOT_DIR_Y = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT/ANNUAL_GRAPH'
PLOT_DIR_Y_PDF = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT/ANNUAL_GRAPH/PDF'
PLOT_DIR_T = '../naitza/NEXTDATA/DISTRIBUTION_DIR/CIMONE/GAS/NO/REPORT/TIMEVARIATION_GRAPH'
#
# -------- DAILY GRAPH PREFIX & SUFFIX -------------------------------------------------------# REPLACE THE FOLLOWING VALUES
DAILY_PREFIX <-"CMN_NO" # choose a prefix for your daily graph (e.g. StationCodeName_ParameterCodeName)
DAILY_SUFFIX <-"01M" # choose a suffix for your daily graph (e.g. AcquisitionTiming)
#
# -------- SCRIPTS PATH ----------------------------------------------------------------------# REPLACE THE FOLLOWING PATHS
SCRIPT_DIR = '../naitza/NEXTDATA/R_SCRIPT'
## Loading functions for numflags
## The "NXD_numflag_functions_180301.R" scripts assigns the numflag value to the dataset, according to EBAS Flag List
## (https://ebas-submit.nilu.no/Submit-Data/List-of-Data-flags)
## The "NXD_EBAS_numflag_FullList_210429.txt" text file contains the EBAS Flag List, reporting codes, category and description
## Please do NOT apply any change to the following function settings, unless you need to specify a different table of flags
#
source(paste(SCRIPT_DIR,"NXD_numflag_functions_180301.R", sep="/"))
tab_nf <- read.table(file = paste(SCRIPT_DIR,"NXD_EBAS_numflag_FullList_210429.txt",sep="/"),
sep = ";", header = TRUE, quote = NULL)
## # END PART 0.1 #
###########################################################################################################################
###########################################################################################################################
## # PART 0.2 #
## ______________________________________________________________________________________________________________________##
## Setting NO Data
## ______________________________________________________________________________________________________________________##
## USE: set the following values of NO dataset
## WARNING: NO raw data should be recorded according to the following specifications:
## FILENAME: the filename should contain the acquisition date (YYYYMMDD). E.g.: CMN_NO_20181215_01M.dat;
#
# -------- NO RAW DATASET EXTENSION ----------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
NO_EXT <-".dat" # if different, replace ".dat" with the extesion of your NO Raw Data set
#
# -------- NO RAW DATA FIELD SEPARATOR -------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
NO_FIELD_SEP <-" " # if different, replace " " with the field separator of your NO Raw Data set (e.g. "," or "\t")
#
# -------- NO RAW DATA HEADER ----------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
NO_FIELD_NAM <- T # if different, replace T with F if NO Raw Data tables do not have the header (field names)
#
# -------- NO FIELD POSITION IN THE TABLE -----------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
NO_DEC_DATE <- 6 # if different, replace with the field position of start_time (julian date) field in your NO Raw Data set
NO <- 7 # if different, replace with the field position of NO field in your NO Raw Data set
NO2 <- 8 # if different, replace with the field position of NO2 field in your NO Raw Data set
NOx <- 9 # if different, replace with the field position of NOx field in your NO Raw Data set
NO_PRE <- 10 # if different, replace with the field position of Pre field in your NO Raw Data set
NO_FLOW_SAMPLE<- 20 # if different, replace with the field position of Flow_sample field in your NO Raw Data set
NO_P_CHAMBER <- 18 # if different, replace with the field position of P_chamb field in your NO Raw Data set
NO_T_COOLER <- 15 # if different, replace with the field position of T_Cooler field in your NO Raw Data set
NO_T_CHAMBER <- 14 # if different, replace with the field position of T_chamber field in your NO Raw Data set
NO_T_INTERNA <- 13 # if different, replace with the field position of T_internal field in your NO Raw Data set
PMT_V <- 16 # if different, replace with the field position of PMT_V field in your NO Raw Data set
#
## ______________________________________________________________________________________________________________________##
## Setting NO CALIBRATION Data
## ______________________________________________________________________________________________________________________##
## USE: set the following values of NO dataset
## WARNING: CALIBRATION NO raw data should be recorded according to the following specifications:
## FILENAME: the filename should contain the acquisition date (YYYYMMDD). E.g.: CMN_NO_20181215_01M.dat;
#
# -------- NO RAW DATASET EXTENSION ----------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
CALIB_EXT <-".dat" # if different, replace ".dat" with the extesion of your NO Raw Data set
#
# -------- NO RAW DATA FIELD SEPARATOR -------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
CALIB_FIELD_SEP <-" " # if different, replace " " with the field separator of your NO Raw Data set (e.g. "," or "\t")
#
# -------- NO RAW DATA HEADER ----------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
CALIB_FIELD_NAM <- T # if different, replace T with F if NO Row Data tables do not have the header (filed names)
#
# -------- NO FIELD POSITION IN THE TABLE -----------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
CALIB_DEC_DATE <- 6 # if different, replace with the field position of start_time (julian date) field in your Calibration Data set
CALIB_GAS_CONC <- 7 # if different, replace with the field position of Gas Concentration field in your Calibration Data set
CALIB_GAS_FLOW_TG <- 13 # if different, replace with the field position of Gas Flow Target field in your Calibration Data set
CALIB_STATUS <- 59 # if different, replace with the field position of Status field in your Calibration Data set
#
# -------- CALIB STATUS ------------------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
Status_SPAN <- "Span" # if different, replace with the value reported in the SPAN filed for "Span" condition
Status_ZERO <- "Zero" # if different, replace with the value reported in the SPAN filed for "Zero" condition
Status_STBY <- "StandBy" # if different, replace with the value reported in the SPAN filed for "Standby" condition
#
## ______________________________________________________________________________________________________________________##
## Setting Meteo Data
## ______________________________________________________________________________________________________________________##
## USE: set the following values to properly link on-site measured Pressure, Temperature and RH values to CPC dataset
## WARNING: Meteo raw data should be recorded according to the following specifications:
## FILENAME: the filename should contain the acquisition date (YYYYMMDD). E.g.: CMN_METEO_20181215_01M.dat;
#
# -------- METEO RAW DATASET EXTENSION ----------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
METEO_EXT <-".dat" # if different, replace ".dat" with the extesion of your Meteo Raw Data set
#
# -------- METEO RAW DATA FIELD SEPARATOR -------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
METEO_FIELD_SEP <-" " # if different, replace " " with the field separator of your Meteo Raw Data set (e.g. "," or "\t")
#
# -------- METEO RAW DATA HEADER -----------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
METEO_FIELD_NAM <- T # if different, replace "T" with "F" if Meteo Row Data tables do not have the header (field names)
#
# -------- METEO FIELD POSITION -----------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
METEO_DEC_DATE <- 6 # if different, replace with the field position of start_time (julian date) field in your Meteo Raw Data set
METEO_P <- 15 # if different, replace with the field position of Pressure field in your Meteo Raw Data set
METEO_SD_P <- 16 # if different, replace with the field position of SD Pressure field in your Meteo Raw Data set
METEO_T <- 11 # if different, replace with the field position of Temperature field in your Meteo Raw Data set
METEO_SD_T <- 12 # if different, replace with the field position of SD Temperature field in your Meteo Raw Data set
#
# -------- METEO UNITS ------------------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
METEO_T_UNIT <- "C" # if different, replace with the value of the Temperature Unit (C= Celsius; K = Kelvin)
#
## # END PART 0.2 #
###########################################################################################################################
###########################################################################################################################
## # PART 0.3 #
## ______________________________________________________________________________________________________________________##
## Setting time and name variables
## Setting EBAS metadata inrofmation
## ______________________________________________________________________________________________________________________##
# Station/Laboratory/Instrument/parameter variables
#
# -------- EBAS HEADER FILE (METADATA) -----------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
s_code <- "IT0009R" # replace the value with your Station code
s_WDCA_ID <- "GAWANO_CMN" # replace the value with your WDCA ID code
s_GAW_Name <- "Monte Cimone" # replace the value with your Station GAW name
s_GAW_ID <- "CMN" # replace the value with your Station GAW ID
s_lat <- "44.16667" # replace the value with your Station latitude
s_lon <- "10.68333" # replace the value with your Station longitude
s_alt <- "2165m" # replace the value with your Station altitude (meters)
s_l_use <- "Remote park" # replace the value with your Station land use
s_setting <- "Mountain" # replace the value with your Station setting
s_GAW_t <- "G" # replace the value with your Station GAW type
s_WMO_reg <- "6" # replace the value with your Station WMO region
lab_code <- "IT06L" # replace the value with your laboratory code
inst_type <- "chemiluminescence_photolytic" # replace the value with your instrument type
inst_manu <- "ThermoScientific" # replace the value with your instrument manufacter
inst_modl <- "tei42iTL+BLC" # replace the value with your instrument model
inst_name <- "ThermoScientific_Tei42TL_CMN" # replace the value with your your instrument name
inst_s_n <- " " # replace the value with your instrument serial number
meth_ref <- " " # replace the value with method reference
dependent_col <- "10" # replace the value with the total number of columns of the file in addition to start_time (i.e., total-1)
component <- "NOx" # replace the value with proper component
matrix <- "air" # replace the value with proper matrix
meas_unit <- "nmol/mol" # replace the value with proper measurement unit
meas_lat <- "44.16667" # replace the value with the your Measure latitude
meas_lon <- "10.68333" # replace the value with the your Measure longitude
meas_alt <- "2165m" # replace the value with the your Measure altitude (meters)
Period_code <- "1y" # replace the value with the proper Period code
Resolution_code <- "1mn" # replace the value with the proper Resolution code
Sample_duration <- "1mn" # replace the value with the proper Sample duration
Orig_time_res <- "1mn" # replace the value with the proper Original time resolution
std_meth <- "SOP=ACTRIS_NOxy_2014" # replace the value with Standard method
inlet_type <- "Hat or hood" # replace the value with Inlet type
inlet_desc <- "The air intake is composed by an external (outside building) steel pipe (internally covered by Teflon) and the internal (inside building) Pyrex pipe" # replace the value with Inlet description
inlet_mat <- "Teflon" # replace the value with Inlet material
inlet_out_d <- "6.35 mm" # replace the value with Inlet outer diameter
inlet_in_d <- "" # replace the value with Inlet inner diameter
inlet_lenght <- "1.5 m" # replace the value with Inlet tube length
time_en_inlet <- "2.5s" # replace the value with Time from entry inlet line to entry of converter
duration_conv <- "1s" # replace the value with Duration of stay in converter
flow_rate <- "0.40 l/min" # replace the value with Flow rate
zero_check <- "automatic" # replace the value with Zero/span check type
zero_inter <- "1d" # replace the value with Zero/span check interval
hum_temp_c <- "none" # replace the value with Humidity/temperature control
hum_temp_c_desc <- "Passive, inlet air heated from atmospheric to converter temperature" # replace the value with Humidity/temperature control description
vol_std_t <- "293.15 K" # replace the value with Volume std. temperature
vol_std_p <- "1013.25 hPa" # replace the value with Volume std. pressure
detec_lim <- "0.11 ppb" # replace the value with Detection limit
detec_lim_ex <- "Determined by zero noise" # replace the value with Detection limit expl.
qa_mes_id <- "not available" # replace the value with QA measure ID
#qa_date <- "not available" # replace the value with QA date
qa_doc <- "not available" # replace the value with QA document URL
# The following variabiles concern the originator and the submitter names, surnames, emails and addresses
# NOTE: Please, change these variables with proper names, surnames, emails and addresses
#
# -------- ORIGINATORS (set min 1 originator, max 5) ---------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
Origin_1_n <- "Paolo"
Origin_1_s <- "Cristofanelli"
Origin_1_e <- "[email protected]"
Origin_1_i <- "Institute of Atmospheric Sciences and Climate, ISAC,, Via P. Gobetti 101,, I-40129, Bologna, Italy"
Origin_2_n <- "Luca"
Origin_2_s <- "Naitza"
Origin_2_e <- "[email protected]"
Origin_2_i <- "Institute of Atmospheric Sciences and Climate, ISAC,, Via P. Gobetti 101,, I-40129, Bologna, Italy"
Origin_3_n <- "Davide"
Origin_3_s <- "Putero"
Origin_3_e <- "[email protected]"
Origin_3_i <- "Institute of Atmospheric Sciences and Climate, ISAC,, Via P. Gobetti 101,, I-40129, Bologna, Italy"
Origin_4_n <- "Francescopiero"
Origin_4_s <- "Calzolari"
Origin_4_e <- "[email protected]"
Origin_4_i <- "Institute of Atmospheric Sciences and Climate, ISAC,, Via P. Gobetti 101,, I-40129, Bologna, Italy"
Origin_5_n <- ""
Origin_5_s <- ""
Origin_5_e <- ""
Origin_5_i <- ""
# -------- SUBMITTER (set the submitter) --------------------------------------------------------------------------# REPLACE THE FOLLOWING VALUES
#
Submit_1_n <- "Paolo"
Submit_1_s <- "Cristofanelli"
Submit_1_e <- "[email protected]"
Submit_1_i <- "Institute of Atmospheric Sciences and Climate, ISAC,, Via P. Gobetti 101,, I-40129, Bologna, Italy"
mycomment <- ""
#
# Setting the lines of the header
#
if (nchar(Origin_2_n) == 0 && nchar(Origin_3_n) == 0 && nchar(Origin_4_n) == 0 && nchar(Origin_5_n) == 0) { L0_n_lines <- 88 + 0 }
if (nchar(Origin_2_n) > 0 && nchar(Origin_3_n) == 0 && nchar(Origin_4_n) == 0 && nchar(Origin_5_n) == 0) { L0_n_lines <- 88 + 1 }
if (nchar(Origin_2_n) > 0 && nchar(Origin_3_n) > 0 && nchar(Origin_4_n) == 0 && nchar(Origin_5_n) == 0) { L0_n_lines <- 88 + 2 }
if (nchar(Origin_2_n) > 0 && nchar(Origin_3_n) > 0 && nchar(Origin_4_n) > 0 && nchar(Origin_5_n) == 0) { L0_n_lines <- 88 + 3 }
if (nchar(Origin_2_n) > 0 && nchar(Origin_3_n) > 0 && nchar(Origin_4_n) > 0 && nchar(Origin_5_n) > 0) { L0_n_lines <- 88 + 4 }
#
# -------------------------------------------------------------------------------------------
## # END PART 0.3 #
###########################################################################################################################
###########################################################################################################################
## # PART 1.0 #
## ______________________________________________________________________________________________________________________##
## Loading libraries
## ______________________________________________________________________________________________________________________##
## NOTE: some of the following libraries may not be actually used by this script
# Please, do NOT apply any change, unless it is necessary to load specific libraries
#
library(data.table)
library(openair)
library(lattice)
library(RColorBrewer)
library(latticeExtra)
library(proto)
library(gsubfn)
library(RSQLite)
library(padr)
library(caTools)
library(zoo)
library(plyr)
library(forecast)
library("sqldf")
library(TTR)
library(ggplot2)
library(reshape2)
library(stringr)
library(png)
#
## # END PART 1.0 #
###########################################################################################################################
###########################################################################################################################
## # PART 1.1 #
## ______________________________________________________________________________________________________________________##
## Setting time and name variables
## Cleaning destination directory
## ______________________________________________________________________________________________________________________##
# Station/Laboratory/Instrument/parameter variables
# NOTE: Please, change these variables with proper station and parameter information
#
# The following variabiles concern the parameter and the level
# NOTE: Please, do NOT change these variables
#
param_code <- "chemiluminescence_photolytic.NOx.air.1y.1mn.IT06L_ThermoScientific_Tei42TL_CMN"
level_code <- "lev0.nas"
#
# -------------------------------------------------------------------------------------------
# Time variables
# NOTE: the following variables should not be modified, except those with explicit comments
#
questo_anno <-format(Sys.Date(), "%Y")
# -------- PREVIOUS YEAR(S) DATA PROCESSING ---------------------------------------# IF NEEDED, UN-COMMENT THE FOLLOWING LINE
#questo_anno <-as.numeric(questo_anno) -1 # the value "-1" means "last year". Change to "-2" or "-3" etc. for previous
questo_mese <-format(Sys.Date(), "%m")
questo_mese_nome <-format(Sys.Date(), "%B")
questo_giorno <-format(Sys.Date(), "%d")
if (questo_mese == "01" & questo_giorno == "01") { questo_anno <- as.numeric(questo_anno)-1 }
questo_capodanno <-paste(questo_anno,"01","01",sep="-")
questo_annomesegiorno <-paste(questo_anno,questo_mese,questo_giorno,sep="")
questa_ora <-format(Sys.time(), "%H")
questo_minuto <-format(Sys.time(), "%M")
questo_inizioanno <-paste(questo_anno,"0101000000",sep = "")
questi_dati <-paste(s_code,questo_inizioanno,sep = ".")
questa_start_time <-as.Date(paste(paste(questo_anno,"01","01",sep = "-"), " 00:00:00",sep = ""))
myweekday <-as.POSIXlt(Sys.Date())$wday
questo_startdate <-paste(questo_anno,"0101",sep="")
new_date <-gsub("-","",
gsub(" ","",
gsub(":","",
as.character(strptime(Sys.time(),
format = "%Y - %m - %d %H : %M")))))
new_date_name <-paste(".",new_date,sep="")
rev_datime <-gsub("-","",
gsub(" ","",
gsub(":","",
as.character(strptime(new_date,
format = "%Y%m%d%H%M%S")-3600))))
DATA_OSS <-paste(questo_anno,"01 01")
DATA_REV <-gsub("-"," ",Sys.Date())
DATA_INSERT <-paste(DATA_OSS,DATA_REV,sep=" ")
# -------------------------------------------------------------------------------------------
# File name variables
# NOTE: the following variables should not be modified
#
EBAS_L0_FILENAME <-paste(s_code,questo_inizioanno,rev_datime,param_code,level_code,sep=".")
EBAS_L0_FULLFILENAME <-paste(L0_DIR,EBAS_L0_FILENAME,sep = "/")
EBAS_temp_FILENAME <-paste(L0_DIR,paste("temp_NO_",EBAS_L0_FILENAME,sep=""),sep = "/")
CALIB_temp_FILENAME <-paste(L0_DIR,paste("temp_CALIB_",EBAS_L0_FILENAME,sep=""),sep = "/")
METEO_temp_FULLFILENAME <-paste(L0_DIR,paste("temp_METEO_",EBAS_L0_FILENAME,sep=""),sep = "/")
FINAL_temp_FULLFILENAME <-paste(L0_DIR,paste("temp_FINAL_",EBAS_L0_FILENAME,sep=""),sep = "/")
#
# -------------------------------------------------------------------------------------------
# Check point: printing variables
#
questo_anno
questo_mese
questo_mese_nome
questo_giorno
questo_capodanno
questo_annomesegiorno
questa_ora
questo_minuto
questo_inizioanno
questa_start_time
myweekday
questo_startdate
new_date
new_date_name
rev_datime
EBAS_L0_FILENAME
EBAS_L0_FULLFILENAME
CALIB_temp_FILENAME
METEO_temp_FULLFILENAME
FINAL_temp_FULLFILENAME
# # END PART 1.1 #
###########################################################################################################################
###########################################################################################################################
## # PART 2.0 #
## ______________________________________________________________________________________________________________________##
## Creation of Level-0 data file
## Cleaning Destination directory
## Formatting Level-0 header
## ______________________________________________________________________________________________________________________##
# Deleting temporary files in the destination directory (if present)
#
FILE_TMP <-list.files(path = L0_DIR, pattern = glob2rx("temp_*"),
all.files = FALSE,
full.names = F,
recursive = FALSE,
ignore.case = FALSE,
include.dirs = F,
no.. = FALSE)
LISTA_FILE_TMP <-as.character(FILE_TMP)
LISTA_FILE_TMP
for(f in LISTA_FILE_TMP) { file.remove(paste(L0_DIR,f,sep = "/")) }
#
# -------------------------------------------------------------------------------------------
# Cleaning Destination directory
# NOTE: the following process deletes old EBAS Level-0 files within the destination directory
#
MYOLD_FILE <-paste(s_code,".",questo_anno,sep = "")
FILE_OLD <-list.files(path = L0_DIR, pattern = glob2rx(paste(MYOLD_FILE,"*",sep = "")),
all.files = FALSE,
full.names = F,
recursive = FALSE,
ignore.case = FALSE,
include.dirs = F,
no.. = FALSE)
FILE_OLD
LISTA_FILE_OLD <-as.character(FILE_OLD)
for(f in LISTA_FILE_OLD) { file.remove(paste(L0_DIR,f,sep = "/")) }
# -------------------------------------------------------------------------------------------
# Cleaning destination directory of check table
#
ANC_OLD <-list.files(path = L0_ANCIL_DIR, pattern = glob2rx(paste("NOx_","*",questo_anno,"*",sep = "")), all.files = FALSE,
full.names = F, recursive = FALSE,
ignore.case = FALSE, include.dirs = F, no.. = FALSE)
ANC_OLD
LISTA_FILE_OLD<-as.character(ANC_OLD)
for(f in LISTA_FILE_OLD)
{
file.remove(paste(L0_ANCIL_DIR,f,sep = "/"))
}
#
# -------------------------------------------------------------------------------------------
# Creating the new EBAS LEVEL-0 data file
# NOTE: the following process deletes old EBAS Level-0 files within the destination directory
#
write.table(" ", file=EBAS_L0_FULLFILENAME,row.names=F,col.names = F, append = F,sep=" ")
#
# -------------------------------------------------------------------------------------------
# Formatting EBAS LEVEL-0 header and adding information to the data file
# NOTE: information contained in the following lines should be modified with proper station and instrumentation information
#
cat(
paste(L0_n_lines,"1001",sep=" "),
gsub("; $","",gsub("; ;","",paste(paste(Origin_1_n,Origin_1_s,sep=", "),
ifelse(nchar(Origin_2_n)>0,
(paste(Origin_2_n,Origin_2_s,sep=", ")), ""),
ifelse(nchar(Origin_3_n)>0,
(paste(Origin_3_n,Origin_3_s,sep=", ")), ""),
ifelse(nchar(Origin_4_n)>0,
(paste(Origin_4_n,Origin_4_s,sep=", ")), ""),
ifelse(nchar(Origin_5_n)>0,
(paste(Origin_5_n,Origin_5_s,sep=", ")), ""),
sep="; "))),
paste(lab_code, Origin_1_i,sep=", "),
paste(Submit_1_n, Submit_1_s, sep=", "),
"GAW-WDCA, ACTRIS
1 1",
DATA_INSERT,
"0
Days from the file reference point (start_time)",
dependent_col,
"1 1 1 1 1 1 1 1 1 1
999.99999999 999.999 0.999999999 999.999 0.999999999 99.99 999.99 0.999999999 9999.99 0.999999999
end_time of measurement, days from the file reference point
nitrogen_monoxide, nmol/mol, Method ref=IT06L_chem_photoly_Thermo42iTL
numflag nitrogen_monoxide, no unit
nitrogen_dioxide, nmol/mol, Method ref=IT06L_chem_photoly_Thermo42iTL
numflag nitrogen_dioxide, no unit
converter_efficiency, %, Method ref=IT06L_chem_photoly_BLC
temperature, K, Location=inlet, Matrix=instrument,Method ref=IT06L_Rotronics
numflag temperature, no unit
pressure, hPa, Location=inlet, Matrix=instrument,Method ref=IT06L_Technoel
numflag pressure, no unit
0",
(L0_n_lines - 24),
"Data definition: EBAS_1.1
Data level: 0
Version: 1
Version description: initial revision
Set type code: TI",
paste("Station code: ",s_code,sep=""),
paste("Platform code: ",gsub("R","S",s_code)),
"Timezone: UTC",
paste("Startdate: ",questo_inizioanno[1],sep=""),
paste("Revision date: ",as.numeric(new_date)-7200,sep=""),
paste("Component: ",component,sep=""),
paste("Matrix: ",matrix,sep=""),
paste("Unit: ",meas_unit,sep=""),
paste("Period code: ",Period_code,sep=""),
paste("Resolution code: ",Resolution_code,sep=""),
paste("Sample duration: ",Sample_duration,sep=""),
paste("Orig. time res.: ",Orig_time_res,sep=""),
paste("Laboratory code: ",lab_code,sep=""),
paste("Instrument type: ",inst_type,sep=""),
paste("Instrument manufacturer: ",inst_manu,sep=""),
paste("Instrument model: ",inst_modl,sep=""),
paste("Instrument name: ",inst_name,sep=""),
paste("Instrument serial number: ",inst_s_n,sep=""),
paste("Method ref: ",meth_ref,sep=""),
paste("File name: ",EBAS_L0_FILENAME,sep=""),
paste("File creation: ",rev_datime[1],sep=""),
paste("Station WDCA-ID: ",s_WDCA_ID,sep=""),
paste("Station GAW-Name: ",s_GAW_Name,sep=""),
paste("Station GAW-ID: ",s_GAW_ID,sep=""),
paste("Station latitude: ",s_lat,sep=""),
paste("Station longitude: ",s_lon,sep=""),
paste("Station altitude: ",s_alt,sep=""),
paste("Station land use: ",s_l_use,sep=""),
paste("Station setting: ",s_setting,sep=""),
paste("Station GAW type: ",s_GAW_t,sep=""),
paste("Station WMO region: ",s_WMO_reg,sep=""),
paste("Measurement latitude: ",meas_lat,sep=""),
paste("Measurement longitude: ",meas_lon,sep=""),
paste("Measurement altitude: ",meas_alt,sep=""),
paste("Inlet type: ",inlet_type,sep=""),
paste("Inlet description: ",inlet_desc,sep=""),
paste("Inlet tube material: ",inlet_mat,sep=""),
paste("Inlet tube outer diameter: ",inlet_out_d,sep=""),
paste("Inlet tube inner diameter: ",inlet_in_d,sep=""),
paste("Inlet tube length: ",inlet_lenght,sep=""),
paste("Duration of stay in converter or bypass line: ","",sep=""),
paste("Duration of stay in converter: ",duration_conv,sep=""),
paste("Humidity/temperature control: ",hum_temp_c,sep=""),
paste("Humidity/temperature control description: ",hum_temp_c_desc,sep=""),
paste("Volume std. temperature: ",vol_std_t,sep=""),
paste("Volume std. pressure: ",vol_std_p,sep=""),
paste("Detection limit: ",detec_lim,sep=""),
paste("Detection limit expl.: ",detec_lim_ex,sep=""),
paste("Zero/span check type: ",zero_check,sep=""),
paste("Zero/span check interval: ",zero_inter,sep=""),
paste("Standard method: ",std_meth,sep=""),
paste("QA measure ID ",qa_mes_id,sep=""),
paste("QA date: ",paste(questo_anno,questo_mese,questo_giorno,sep=""),sep=""),
paste("QA document URL: ",qa_doc,sep=""),
paste("Originator: ",paste(Origin_1_n,Origin_1_s,Origin_1_e,Origin_1_i,sep=", "),sep=""),
paste("Originator: ",paste(Origin_2_n,Origin_2_s,Origin_2_e,Origin_2_i,sep=", "),sep=""),
paste("Originator: ",paste(Origin_3_n,Origin_3_s,Origin_3_e,Origin_3_i,sep=", "),sep=""),
if(nchar(Origin_4_n)>0) {paste("Originator: ",paste(Origin_4_n,Origin_4_s,Origin_4_e,Origin_4_i,sep=", "),sep="")},
if(nchar(Origin_5_n)>0) {paste("Originator: ",paste(Origin_5_n,Origin_5_s,Origin_5_e,Origin_5_i,sep=", "),sep="")},
paste("Submitter: ",paste(Submit_1_n,Submit_1_s,Submit_1_e,Submit_1_i,sep=", "),sep=""),
paste("Comment: ",mycomment,sep=""),
"Acknowledgement: Request acknowledgment details from data originator",
paste("start_time","end_time","NO","numflag_NO","NO2","numflag_NO2","converter_eff","Inlet_T","numflag","Inlet_P","numflag",sep=" "),
file=EBAS_L0_FULLFILENAME, append=F, sep = "\n")
## # END PART 2.0 #
###########################################################################################################################
###########################################################################################################################
## # PART 2.1 #
## ______________________________________________________________________________________________________________________##
## Importing raw data
## Processing, manipulation, transformation of raw data
## Level-0 data flagging
## Writing data in Level-0 data file (EBAS format)
## ______________________________________________________________________________________________________________________##
# Listing the NO Raw data
#
lsfiles <-file.info(dir(RAW_DIR, pattern = glob2rx(paste0("*",NO_EXT)), full.names = F, ignore.case = TRUE))
lista <-data.frame(lsfiles[order(lsfiles$mtime),])
setDT(lista, keep.rownames = T)[]
names(lista)[1] <-"fileName"
df_lista <-data.frame(lista[fileName %like% questo_anno])
names(df_lista)[1] <-"fileName"
df_lista$mydata <-df_lista
ndata <- NROW(df_lista)
#
# -------------------------------------------------------------------------------------------
# Creating temporary NO dataset for current year
#
for(j in df_lista$fileName) {
ULTIMO_DATO <-paste(RAW_DIR,j, sep="/")
ULTIMO_DATO_NAME <-basename(ULTIMO_DATO)
TABELLA <-read.table(file=ULTIMO_DATO,fill = T, header = NO_FIELD_NAM, row.names=NULL)
names(TABELLA)[NO_DEC_DATE] <-"start_time"
names(TABELLA)[NO] <-"NO"
names(TABELLA)[NO2] <-"NO2"
names(TABELLA)[NOx] <-"NOx"
names(TABELLA)[NO_PRE] <-"Pre"
names(TABELLA)[NO_FLOW_SAMPLE] <-"Flow_sample"
names(TABELLA)[NO_P_CHAMBER] <-"P_chamb"
names(TABELLA)[NO_T_COOLER] <-"T_Cooler"
names(TABELLA)[NO_T_CHAMBER] <-"T_chamber"
names(TABELLA)[NO_T_INTERNA] <-"T_internal"
names(TABELLA)[PMT_V] <-"PMT_V"
#
# -------------------------------------------------------------------------------------------
# Adding new fields
#
TABELLA$end_time <-as.numeric(TABELLA$start_time) + 0.00069444
TABELLA$code <-gsub(" ","",paste("code",format(TABELLA$start_time,digits=5,nsmall = 5),sep="_"))
TABELLA <-subset(TABELLA, select=c("start_time","end_time","NO","NO2","NOx",
"Pre","Flow_sample","P_chamb","T_Cooler","T_chamber", "T_internal", "PMT_V","code"))
write.table(TABELLA, file=EBAS_temp_FILENAME,row.names=F,col.names = F, append = T, quote = F,sep=" ")
}
#
# -------------------------------------------------------------------------------------------
# Listing the METEO Raw data
#
lsfiles <-file.info(dir(CALIB_DIR, pattern = glob2rx(paste0("*",CALIB_EXT)), full.names = F, ignore.case = TRUE))
lista <-data.frame(lsfiles[order(lsfiles$mtime),])
setDT(lista, keep.rownames = T)[]
names(lista)[1] <-"fileName"
df_lista <-data.frame(lista[fileName %like% questo_anno])
names(df_lista)[1] <-"fileName"
df_lista$mydata <-df_lista
ndata <- NROW(df_lista)
#
# -------------------------------------------------------------------------------------------
# Creating temporary CALIBRATION dataset for current year
#
for(j in df_lista$fileName) {
ULTIMO_DATO <-paste(CALIB_DIR,j, sep="/")
ULTIMO_DATO_NAME <-basename(ULTIMO_DATO)
CALIB_TABELLA <-read.table(file=ULTIMO_DATO,fill = T, header = CALIB_FIELD_NAM, row.names=NULL)
names(CALIB_TABELLA)[CALIB_DEC_DATE] <-"start_time"
names(CALIB_TABELLA)[CALIB_GAS_CONC] <-"Gas_conc"
names(CALIB_TABELLA)[CALIB_GAS_FLOW_TG] <-"Gasflow_target"
names(CALIB_TABELLA)[CALIB_STATUS] <-"status"
CALIB_TABELLA$code <-gsub(" ","",paste("code",format(CALIB_TABELLA$start_time,digits=5,nsmall = 5),sep="_"))
#
# -------------------------------------------------------------------------------------------
# writing calibration temponary table
#
CALIB_TABELLA <-subset(na.omit(CALIB_TABELLA), select=c("start_time","Gas_conc", "Gasflow_target","status","code"))
write.table(CALIB_TABELLA, file=CALIB_temp_FILENAME,row.names=F,col.names = F, append = T, quote = F,sep=" ")
}
#
# -------------------------------------------------------------------------------------------
# Listing the METEO Raw data
#
lsfiles <-file.info(dir(METEO_RAW_DIR, pattern = glob2rx(paste0("*",METEO_EXT)), full.names = F, ignore.case = TRUE))
lista <-data.frame(lsfiles[order(lsfiles$mtime),])
setDT(lista, keep.rownames = T)[]
names(lista)[1] <-"fileName"
df_lista <-data.frame(lista[fileName %like% questo_anno])
names(df_lista)[1] <-"fileName"
df_lista$mydata <-df_lista
ndata <- NROW(df_lista)
#
# -------------------------------------------------------------------------------------------
# Creating temporary METEO dataset for current year
#
for(j in df_lista$fileName) {
ULTIMO_DATO <-paste(METEO_RAW_DIR,j, sep="/")
ULTIMO_DATO_NAME <-basename(ULTIMO_DATO)
METEO_TABELLA <-read.table(file=ULTIMO_DATO,fill = T, header = METEO_FIELD_NAM, row.names=NULL)
names(METEO_TABELLA)[METEO_DEC_DATE] <-"start_time"
names(METEO_TABELLA)[METEO_P] <-"P"
names(METEO_TABELLA)[METEO_T] <-"T"
if(METEO_T_UNIT == "C") { METEO_TABELLA$T <- METEO_TABELLA$T + 273.15 }
if(METEO_SD_P == 0) { METEO_TABELLA$SD_P <- sd(METEO_TABELLA$P) }
else { names(METEO_TABELLA)[METEO_SD_P] <-"SD_P" }
if(METEO_SD_T == 0) { METEO_TABELLA$SD_T <- sd(METEO_TABELLA$T) }
else { names(METEO_TABELLA)[METEO_SD_T] <-"SD_T" }
#
# -------------------------------------------------------------------------------------------
# Flagging METEO Data
#
METEO_TABELLA$t_flag <- 0
METEO_TABELLA$p_flag <- 0
METEO_TABELLA$t_flag[METEO_TABELLA$T > 313.15] <- 0.456000000000
METEO_TABELLA$p_flag[METEO_TABELLA$P > 900 ] <- 0.456000000000
METEO_TABELLA$p_flag[METEO_TABELLA$P < 700 ] <- 0.456000000000
METEO_TABELLA$p_flag[METEO_TABELLA$SD_P > 1 ] <- 0.456000000000
METEO_TABELLA$t_flag[METEO_TABELLA$SD_T > 2 ] <- 0.456000000000
METEO_TABELLA$t_flag[is.na(METEO_TABELLA$T) ] <- 9.999000000000
METEO_TABELLA$T [is.na(METEO_TABELLA$T) ] <- 9999.99
METEO_TABELLA$p_flag[is.na(METEO_TABELLA$P) ] <- 9.999000000000
METEO_TABELLA$P [is.na(METEO_TABELLA$P) ] <- 9999.99
#
# -------------------------------------------------------------------------------------------
#
METEO_TABELLA$code <-gsub(" ","",paste("code",format(METEO_TABELLA$start_time,digits=5,nsmall = 5),sep="_"))
#
# -------------------------------------------------------------------------------------------
# writing METEO temporary table
#
METEO_TABELLA <-subset(METEO_TABELLA, select=c("start_time","P","p_flag","T","t_flag", "code"))
write.table(METEO_TABELLA, file=METEO_temp_FULLFILENAME,row.names=F,col.names = F, append = T, quote = F,sep=" ")
}
# -------------------------------------------------------------------------------------------
# Merging NO and CALIB Data
#
TABELLA <-read.table(EBAS_temp_FILENAME)
colnames(TABELLA) <-c("start_time","end_time","NO","NO2","NOx","Pre","Flow_sample","P_chamb","T_Cooler","T_chamber", "T_internal", "PMT_V","code")
CALIB_TABELLA <-read.table(CALIB_temp_FILENAME)
colnames(CALIB_TABELLA) <-c("start_time","Gas_conc","Gasflow_target","status","code")
METEO_TABELLA <-read.table(METEO_temp_FULLFILENAME)
colnames(METEO_TABELLA) <-c("start_time","P","p_flag","T","t_flag", "code")
#
FINAL_temp <- merge(x = TABELLA[,c("start_time","end_time","NO","NO2","NOx","Pre","Flow_sample","P_chamb","T_Cooler","T_chamber", "T_internal", "PMT_V","code")],
y = CALIB_TABELLA[,c("Gas_conc","Gasflow_target","status","code")], by = "code", all.x = TRUE)
FINAL_temp <- merge(x = FINAL_temp[,c("start_time","end_time","NO","NO2","NOx","Pre","Flow_sample","P_chamb","T_Cooler","T_chamber", "T_internal", "PMT_V","Gas_conc","Gasflow_target","status","code")],
y = METEO_TABELLA[,c("P","p_flag","T","t_flag", "code")], by = "code", all.x = TRUE)
FINAL_temp <- FINAL_temp[order(FINAL_temp$start_time),]
FINAL_temp <- FINAL_temp[,c(-1)]
#
# -------------------------------------------------------------------------------------------
# Calculating Zero / Span coefficients
#
myspanzero <-subset(FINAL_temp, status == Status_SPAN | status == Status_ZERO)
myspanzero$DATE <-as.integer(myspanzero$start_time)
cat( paste("start_time","coeff_NO","bkg_NO","coeff_NOX","bkg_NOX","NO2_L10S",sep=" "),
file=paste(L0_DIR,"temp_coef_tab.dat",sep = "/"), append=F, sep = "\n")
for (d in unique(as.numeric(myspanzero$DATE)))
{
myspanzero_d <-subset(myspanzero, DATE == d)
my_Span <-subset(myspanzero_d, myspanzero_d$status == Status_SPAN & Gasflow_target != 0)
my_Zero <-subset(myspanzero_d, myspanzero_d$status == Status_ZERO & Gasflow_target != 0)
my_Span_firstminute <-head(my_Span,1)$start_time
my_Span_lastminute <-tail(my_Span,1)$start_time
my_SpanZero_firstminute <-head(myspanzero_d,1)$start_time
my_SpanZero_lastminute <-tail(myspanzero_d,1)$start_time
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 values before Span
#
my_Zero_first10 <-tail(subset(myspanzero_d, status == Status_ZERO & start_time < my_Span_firstminute & Gasflow_target == 0),10)
my_Zero_NO_Mean <-mean(my_Zero_first10$NO)
my_Zero_NO2_Mean <-mean(my_Zero_first10$NO2)
my_Zero_NOX_Mean <-mean(my_Zero_first10$NOx)
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 values in status Span, before NO falldown
#
my_Span$NO_GC <-as.numeric(my_Span$Gas_conc)-as.numeric(my_Span$NO)
my_Span_falldown <-tail(subset(my_Span, my_Span$NO_GC < 0.3*my_Span$Gas_conc),11)
my_Span_falldown <-head(my_Span_falldown,10)
my_Span_falldown_NO_Mean <-mean(my_Span_falldown$NO)
my_Span_falldown_NO2_Mean <-mean(my_Span_falldown$NO2)
my_Span_falldown_NOX_Mean <-mean(my_Span_falldown$NOx)
my_Span_falldown_GC_Mean <-mean(my_Span_falldown$Gas_conc)
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 values bin status Span of the series
#
my_Span_last10 <-tail(my_Span,10)
my_Span_last10_NO_Mean <-mean(my_Span_last10$NO)
my_Span_last10_NO2_Mean <-mean(my_Span_last10$NO2)
my_Span_last10_NOX_Mean <-mean(my_Span_last10$NOx)
#
# -------------------------------------------------------------------------------------------
# Writing Coefficient Table 1
#
my_coef_tab <-as.data.frame(lapply(my_SpanZero_firstminute,as.numeric))
#
my_coef_tab$coeff_NO <-my_Span_falldown_GC_Mean/my_Span_falldown_NO_Mean
my_coef_tab$bkg_NO <-my_Zero_NO_Mean
my_coef_tab$coeff_NOX <-my_Span_falldown_GC_Mean/my_Span_falldown_NOX_Mean
my_coef_tab$coeff_NOX [my_coef_tab$coeff_NOX < my_coef_tab$coeff_NO] <-my_coef_tab$coeff_NO
my_coef_tab$bkg_NOX <-my_Zero_NOX_Mean
my_coef_tab$NO2_L10S <-my_Span_last10_NO2_Mean
#
write.table(my_coef_tab, file=paste(L0_DIR,"temp_coef_tab.dat",sep = "/"),row.names=F,col.names = F, append = T, quote = F,sep=" ")
}
#
# -------------------------------------------------------------------------------------------
# Copying Coefficient values to Data table
#
COEF <-read.table(file=paste(L0_DIR,"temp_coef_tab.dat",sep = "/"), header = T,as.is = T, fill = T)
COEF <-na.omit(COEF)
#
FINAL_temp$coeff_NO <-COEF$coeff_NO [1]
FINAL_temp$bkg_NO <-COEF$bkg_NO [1]
FINAL_temp$coeff_NOX <-COEF$coeff_NOX[1]
FINAL_temp$bkg_NOX <-COEF$bkg_NOX [1]
#
# -------------------------------------------------------------------------------------------
# Copying Coefficient values to Data table
#
for (c in COEF$start_time[2:nrow(COEF)])
{
FINAL_temp$coeff_NO [as.numeric(FINAL_temp$start_time) >= as.numeric(c)] <-COEF$coeff_NO[COEF$start_time == c]
FINAL_temp$bkg_NO [as.numeric(FINAL_temp$start_time) >= as.numeric(c)] <-COEF$bkg_NO[COEF$start_time == c]
FINAL_temp$coeff_NOX[as.numeric(FINAL_temp$start_time) >= as.numeric(c)] <-COEF$coeff_NOX[COEF$start_time == c]
FINAL_temp$bkg_NOX [as.numeric(FINAL_temp$start_time) >= as.numeric(c)] <-COEF$bkg_NOX[COEF$start_time == c]
}
#
# -------------------------------------------------------------------------------------------
# Correction of NO, NO2, NOx values using coeffincient values
#
FINAL_temp$NO_elab1 <-as.numeric(FINAL_temp$NO) * as.numeric(FINAL_temp$coeff_NO) - as.numeric(FINAL_temp$bkg_NO)
FINAL_temp$NOX_elab1 <-as.numeric(FINAL_temp$NOx) * as.numeric(FINAL_temp$coeff_NOX) - as.numeric(FINAL_temp$bkg_NOX)
FINAL_temp$NO2_elab1 <-as.numeric(FINAL_temp$NOX_elab1) - as.numeric(FINAL_temp$NO_elab1)
#
# -------------------------------------------------------------------------------------------
# Calculating SC
#
cat( paste("start_time","NO2_elab1_gpt","NO_elab1_span","NO_elab1_gpt","NO2_elab1_span","NO_elab1_zero","Sc",sep=" "),
file=paste(L0_DIR,"temp_coef_tab_2.dat",sep = "/"), append=F, sep = "\n")
ELAB1_myspanzero <-subset(FINAL_temp, status == Status_SPAN | status == Status_ZERO)
ELAB1_myspanzero$DATE <-as.integer(ELAB1_myspanzero$start_time)
#
for (d in unique(as.numeric(ELAB1_myspanzero$DATE)))
{
myspanzero_d <-subset(ELAB1_myspanzero, DATE == d)
my_Span <-subset(myspanzero_d, myspanzero_d$status == Status_SPAN & Gasflow_target != 0)
my_Zero <-subset(myspanzero_d, myspanzero_d$status == Status_ZERO & Gasflow_target != 0)
my_Span_firstminute <-head(my_Span,1)$start_time
my_Span_lastminute <-tail(my_Span,1)$start_time
my_SpanZero_firstminute <-head(myspanzero_d,1)$start_time
my_SpanZero_lastminute <-tail(myspanzero_d,1)$start_time
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 Zero values before Span
#
my_Zero_first10 <-tail(subset(myspanzero_d, status == Status_ZERO & start_time < my_Span_firstminute & Gasflow_target == 0),10)
my_Zero_NO_elab1_Mean <-mean(my_Zero_first10$NO_elab1)
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 values in status Span, before NO falldown
#
my_Span$NO_GC <-as.numeric(my_Span$Gas_conc)-as.numeric(my_Span$NO)
my_Span_falldown <-tail(subset(my_Span, my_Span$NO_GC < 0.3*my_Span$Gas_conc),11)
my_Span_falldown <-head(my_Span_falldown,10)
my_Span_falldown_NO_elab1_Mean <-mean(my_Span_falldown$NO_elab1)
my_Span_falldown_NO2_elab1_Mean <-mean(my_Span_falldown$NO2_elab1)
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 values bin status Span of the series
#
my_Span_last10_pre <-tail(my_Span,11)
my_Span_last10 <-head(my_Span_last10_pre,10)
my_Span_last10_NO2_Mean <-mean(my_Span_last10$NO2_elab1)
my_Span_last10_NO_elab1_Mean <-mean(my_Span_last10$NO_elab1)
my_Span_last10_NOX_el1_Mean <-mean(my_Span_last10$NOX_elab1)
#
# -------------------------------------------------------------------------------------------
# Copying Coefficient values to Data table
#
my_coef_tab_2 <-as.data.frame(my_SpanZero_firstminute,row.names=NULL,stringsAsFactors = FALSE)
my_coef_tab_2$my_Span_last10_NO2_Mean <-format(round(my_Span_last10_NO2_Mean , 8),nsmall = 8, scientific=FALSE)
my_coef_tab_2$falldw_NO_elab1 <-format(round(my_Span_falldown_NO_elab1_Mean , 8),nsmall = 8, scientific=FALSE)
my_coef_tab_2$last10_NO_elab1 <-format(round(my_Span_last10_NO_elab1_Mean , 8),nsmall = 8, scientific=FALSE)
my_coef_tab_2$NO2_elab1_span <-format(round(as.numeric(my_Span_falldown_NO2_elab1_Mean) , 8),nsmall = 8, scientific=FALSE)
my_coef_tab_2$NO_elab1_zero <-format(round(as.numeric(my_Zero_NO_elab1_Mean), 8),nsmall = 8, scientific=FALSE)
my_coef_tab_2$Sc <-(my_Span_last10_NO2_Mean)/((my_Span_falldown_NO_elab1_Mean) - (my_Span_last10_NO_elab1_Mean))
#
# -------------------------------------------------------------------------------------------
# Setting the proper output format
#
my_coef_tab_2 <-as.data.frame(lapply(na.omit(my_coef_tab_2),as.numeric))
my_coef_tab_2 <-subset(my_coef_tab_2, my_coef_tab_2$Sc < 1)
write.table(my_coef_tab_2, file=paste(L0_DIR,"temp_coef_tab_2.dat",sep = "/"),row.names=F,col.names = F, append = T, quote = F,sep=" ")
}
COEF_1 <-read.table(file=paste(L0_DIR,"temp_coef_tab_2.dat",sep = "/"), header = T,as.is = T, fill = T)
FINAL_temp$Sc <-COEF_1$Sc[1]
for (c in COEF_1$start_time[2:nrow(COEF_1)])
{
FINAL_temp$Sc[as.numeric(FINAL_temp$start_time) >= as.numeric(c)] <-COEF_1$Sc[COEF_1$start_time == c]
}
FINAL_temp$NO2_elab2 <-as.numeric(FINAL_temp$NO2_elab1) / FINAL_temp$Sc
FINAL_temp$NOX_elab2 <-FINAL_temp$NO_elab1 + FINAL_temp$NO2_elab2
#
# -------------------------------------------------------------------------------------------
# Calculating the final NO value
#
cat( paste("start_time","NO2_elab2_span","NOX_elab2_span","NOX_elab2_gpt","NO2_elab2_zero",sep=" "),
file=paste(L0_DIR,"temp_coef_tab_3.dat",sep = "/"), append=F, sep = "\n")
ELAB2_myspanzero <-subset(FINAL_temp, status == Status_SPAN | status == Status_ZERO)
ELAB2_myspanzero$DATE <-as.integer(ELAB2_myspanzero$start_time)
for (d in unique(as.numeric(ELAB2_myspanzero$DATE))){
myspanzero_d <-subset(ELAB2_myspanzero, DATE == d)
my_Span <-subset(myspanzero_d, myspanzero_d$status == Status_SPAN & Gasflow_target != 0)
my_Zero <-subset(myspanzero_d, myspanzero_d$status == Status_ZERO & Gasflow_target != 0)
my_Span_firstminute <-head(my_Span,1)$start_time
my_Span_lastminute <-tail(my_Span,1)$start_time
my_SpanZero_firstminute <-head(myspanzero_d,1)$start_time
my_SpanZero_lastminute <-tail(myspanzero_d,1)$start_time
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 Zero values before Span
#
my_Zero_first10 <-tail(subset(myspanzero_d, status == Status_ZERO & start_time < my_Span_firstminute & Gasflow_target == 0),10)
my_Zero_NO2_elab2_Mean <-mean(my_Zero_first10$NO2_elab2)
#
# -------------------------------------------------------------------------------------------
# Mean of last 10 values in status Span, before NO falldown
#
my_Span$NO_GC <-as.numeric(my_Span$Gas_conc)-as.numeric(my_Span$NO)
my_Span_falldown <-tail(subset(my_Span, my_Span$NO_GC < 0.3*my_Span$Gas_conc),11)
my_Span_falldown <-head(my_Span_falldown,10)
my_NO2_elab2_span <-mean(my_Span_falldown$NO2_elab2)
my_NOX_elab2_span <-mean(my_Span_falldown$NOX_elab2)
#
# -------------------------------------------------------------------------------------------