-
Notifications
You must be signed in to change notification settings - Fork 4
/
uEMEP_chemistry_NO2.f90
1488 lines (1205 loc) · 66.2 KB
/
uEMEP_chemistry_NO2.f90
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
subroutine uEMEP_chemistry_control
use uEMEP_definitions
implicit none
real, allocatable :: subgrid_dummy(:,:,:,:,:,:)
real, allocatable :: comp_subgrid_dummy(:,:,:,:)
real, allocatable :: comp_source_subgrid_dummy(:,:,:,:,:)
real, allocatable :: comp_source_EMEP_subgrid_dummy(:,:,:,:,:)
real, allocatable :: comp_source_EMEP_additional_subgrid_dummy(:,:,:,:,:)
integer in_region_loop, n_in_region_loop
!These are calculated in the Chemistry routine. Fist declared here. Are global variables
if (.not.allocated(comp_source_subgrid)) allocate(comp_source_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (.not.allocated(comp_source_EMEP_subgrid)) allocate(comp_source_EMEP_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (.not.allocated(comp_source_EMEP_additional_subgrid)) allocate(comp_source_EMEP_additional_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (trace_emissions_from_in_region) then
n_in_region_loop=2
if (.not.allocated(subgrid_dummy)) allocate (subgrid_dummy(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_subgrid_index,n_source_index,n_pollutant_loop))
if (.not.allocated(comp_subgrid_dummy))allocate (comp_subgrid_dummy(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index))
subgrid_dummy=0
comp_subgrid_dummy=0
if (.not.allocated(comp_source_subgrid_dummy)) allocate(comp_source_subgrid_dummy(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (.not.allocated(comp_source_EMEP_subgrid_dummy)) allocate(comp_source_EMEP_subgrid_dummy(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (.not.allocated(comp_source_EMEP_additional_subgrid_dummy)) allocate(comp_source_EMEP_additional_subgrid_dummy(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
comp_source_subgrid_dummy=0
comp_source_EMEP_subgrid_dummy=0
comp_source_EMEP_additional_subgrid_dummy=0
else
n_in_region_loop=1
endif
do in_region_loop=1,n_in_region_loop
write(unit_logfile,'(a)')''
write(unit_logfile,'(a)')'--------------------------'
if (in_region_loop.eq.1) write(unit_logfile,'(a)')'Chemistry for all contributions'
if (in_region_loop.eq.2) write(unit_logfile,'(a)')'Chemistry only for inside regional contributions'
write(unit_logfile,'(a)')'--------------------------'
!Loop over the normal subgrid and the in region subgrid by saving to a dummy variable and pputting back when finished
if (trace_emissions_from_in_region.and.in_region_loop.eq.2) then
subgrid_dummy=subgrid
comp_subgrid_dummy=comp_subgrid
subgrid=subgrid_from_in_region
comp_subgrid=comp_subgrid_from_in_region
!comp_source_subgrid=comp_source_subgrid_from_in_region
!comp_source_EMEP_subgrid=comp_source_EMEP_subgrid_from_in_region
!comp_source_EMEP_additional_subgrid=comp_source_EMEP_additional_subgrid_from_in_region
!These are calculated in the chemistry routine
comp_source_subgrid_dummy=comp_source_subgrid
comp_source_EMEP_subgrid_dummy=comp_source_EMEP_subgrid
comp_source_EMEP_additional_subgrid_dummy=comp_source_EMEP_additional_subgrid
endif
call uEMEP_chemistry
if (trace_emissions_from_in_region.and.in_region_loop.eq.2) then
subgrid_from_in_region=subgrid
comp_subgrid_from_in_region=comp_subgrid
comp_source_subgrid_from_in_region=comp_source_subgrid
comp_source_EMEP_subgrid_from_in_region=comp_source_EMEP_subgrid
comp_source_EMEP_additional_subgrid_from_in_region=comp_source_EMEP_additional_subgrid
subgrid=subgrid_dummy
comp_subgrid=comp_subgrid_dummy
comp_source_subgrid=comp_source_subgrid_dummy
comp_source_EMEP_subgrid=comp_source_EMEP_subgrid_dummy
comp_source_EMEP_additional_subgrid=comp_source_EMEP_additional_subgrid_dummy
endif
enddo !from_in_region loop
if (trace_emissions_from_in_region) then
if (allocated(subgrid_dummy)) deallocate (subgrid_dummy)
if (allocated(comp_subgrid_dummy)) deallocate (comp_subgrid_dummy)
if (allocated(comp_source_subgrid_dummy)) deallocate(comp_source_subgrid_dummy)
if (allocated(comp_source_EMEP_subgrid_dummy)) deallocate(comp_source_EMEP_subgrid_dummy)
if (allocated(comp_source_EMEP_additional_subgrid_dummy)) deallocate(comp_source_EMEP_additional_subgrid_dummy)
endif
end subroutine uEMEP_chemistry_control
subroutine uEMEP_chemistry
!Routine for doing the chemistry calculations in uEMEP
use uEMEP_definitions
implicit none
integer i,j
real nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature
real nox_out,no2_out,o3_out,p_bg_out,p_out
integer t,t_start,t_end
integer i_source,i_subsource,emep_subsource
integer i_pollutant
logical :: nox_available=.false.
!integer i_comp,i_file
!character(256) temp_name
integer i_integral,j_integral
real FF_loc,distance_grid
integer i_cross_integral,j_cross_integral,i_nc,j_nc
real sum_p_bg_out,sum_p_out,count_p_out
real max_p_bg_out,max_p_out,min_p_bg_out,min_p_out
!NB. Additional is calculated but not necessarily saved!
real nox_bg_additional,no2_bg_additional,o3_bg_additional
!Search for nox in the pollutants
do i_pollutant=1,n_pollutant_loop
if (pollutant_loop_index(i_pollutant).eq.nox_nc_index) nox_available=.true.
enddo
!Leave the chemistry routine if nox is not available
if (.not.nox_available) return
write(unit_logfile,'(A)') ''
write(unit_logfile,'(A)') '================================================================'
write(unit_logfile,'(A)') 'Calculating chemistry for NO2 (uEMEP_chemistry)'
write(unit_logfile,'(A)') '================================================================'
if (no2_chemistry_scheme_flag.eq.0) then
write(unit_logfile,'(A)') 'No chemistry used'
elseif (no2_chemistry_scheme_flag.eq.1) then
write(unit_logfile,'(A)') 'Photostationary state used'
elseif (no2_chemistry_scheme_flag.eq.2) then
write(unit_logfile,'(A)') 'Photochemistry with time scale used'
elseif (no2_chemistry_scheme_flag.eq.3) then
write(unit_logfile,'(A)') 'Romberg parameterisation used'
elseif (no2_chemistry_scheme_flag.eq.4) then
write(unit_logfile,'(A)') 'SRM parameterisation used'
elseif (no2_chemistry_scheme_flag.eq.5) then
write(unit_logfile,'(A)') 'During parameterisation used'
endif
t_start=1
t_end=subgrid_dim(t_dim_index)
i_subsource=1
emep_subsource=1
comp_subgrid(:,:,:,no2_index)=0
comp_subgrid(:,:,:,nox_index)=0
comp_subgrid(:,:,:,o3_index)=0
nox_bg=0.;no2_bg=0.;o3_bg=0.;nox_loc=0.;f_no2_loc=0.;J_photo=0.;temperature=0.;
!Before calculating travel time then include the other EMEP sources not downscaled
!Travel time is set to EMEP Grid_width/FFgrid
do t=t_start,t_end
do j=1,subgrid_dim(y_dim_index)
do i=1,subgrid_dim(x_dim_index)
i_cross_integral=crossreference_target_to_integral_subgrid(i,j,x_dim_index)
j_cross_integral=crossreference_target_to_integral_subgrid(i,j,y_dim_index)
FF_loc=1.
if (hourly_calculations) then
FF_loc=max(FF_min_dispersion,meteo_subgrid(i_cross_integral,j_cross_integral,t,FFgrid_subgrid_index))
elseif (annual_calculations) then
FF_loc=max(FF_min_dispersion,1./meteo_subgrid(i_cross_integral,j_cross_integral,t,inv_FFgrid_subgrid_index))
endif
i_nc=crossreference_target_to_emep_subgrid(i,j,x_dim_index)
j_nc=crossreference_target_to_emep_subgrid(i,j,y_dim_index)
if (EMEP_projection_type.eq.LL_projection_index) then
distance_grid=111000.*sqrt(dgrid_nc(lon_nc_index)*cos(var1d_nc(j_nc,lat_nc_index)*pi/180.)*dgrid_nc(lat_nc_index))
else
!Assumed LCC or PS
distance_grid=sqrt(dgrid_nc(lon_nc_index)*dgrid_nc(lat_nc_index))
endif
do i_source=1,n_source_index
if (calculate_emep_source(i_source).and..not.calculate_source(i_source)) then
!traveltime_subgrid(i,j,t,1,:)=traveltime_subgrid(i,j,t,1,:) &
! +distance_grid/FF_loc*subgrid(i,j,t,emep_local_subgrid_index,i_source,:)**traveltime_power
!traveltime_subgrid(i,j,t,2,:)=traveltime_subgrid(i,j,t,2,:)+subgrid(i,j,t,emep_local_subgrid_index,i_source,:)**traveltime_power
!write(*,'(3i,4f12.2)') i,j,i_source,distance_grid,FF_loc,distance_grid/FF_loc,subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
endif
enddo
enddo
enddo
enddo
!Calculate the weighted travel time from the totals calculated in uEMEP_subgrid_dispersion
!do t=t_start,t_end
!traveltime_subgrid(:,:,t,3,:)=traveltime_subgrid(:,:,t,1,:)/traveltime_subgrid(:,:,t,2,:)
!Invert it to get the time scale
!traveltime_subgrid(:,:,t,1)=1./traveltime_subgrid(:,:,t,1)
!Set none valid to 12 hours (long time)
!where (traveltime_subgrid(:,:,t,2,:).eq.0) traveltime_subgrid(:,:,t,3,:)=3600.*12.
!write(*,*) t
!write(*,*) traveltime_subgrid(:,:,t,1)
!enddo
do j=1,subgrid_dim(y_dim_index)
do i=1,subgrid_dim(x_dim_index)
!write(*,'(2i4,<subgrid_dim(t_dim_index)>f6.1)') i,j,traveltime_subgrid(i,j,:,1)/60.
enddo
enddo
sum_p_bg_out=0.
sum_p_out=0.
count_p_out=0
max_p_bg_out=-1000.;min_p_bg_out=1000.;max_p_out=-1000.;min_p_out=1000.
do t=t_start,t_end
do j=1,subgrid_dim(y_dim_index)
do i=1,subgrid_dim(x_dim_index)
if (use_subgrid(i,j,allsource_index)) then
i_integral=crossreference_target_to_integral_subgrid(i,j,x_dim_index)
j_integral=crossreference_target_to_integral_subgrid(i,j,y_dim_index)
J_photo=meteo_subgrid(i_integral,j_integral,t,J_subgrid_index)
temperature=meteo_subgrid(i_integral,j_integral,t,t2m_subgrid_index)
nox_bg=subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
!Do not do this because the EMEP contributions are already added into the local contribution
!Add the additional non-downscaled EMEP local source to the background as these have not been used to calulate travel times
!do i_source=1,n_source_index
!if (calculate_EMEP_source(i_source).and..not.calculate_source(i_source).and.i_source.ne.allsource_index) then
! nox_bg=nox_bg+subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
!endif
!enddo
if (EMEP_additional_grid_interpolation_size.gt.0) then
nox_bg_additional=subgrid(i,j,t,emep_additional_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
!do i_source=1,n_source_index
!if (calculate_EMEP_source(i_source).and..not.calculate_source(i_source).and.i_source.ne.allsource_index) then
! nox_bg_additional=nox_bg_additional+subgrid(i,j,t,emep_additional_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
!endif
!enddo
!subgrid(i,j,t,emep_additional_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))=nox_bg_additional
endif
!nox_bg=subgrid(i,j,t,emep_subgrid_index,allsource_index,emep_subsource)
!nox_bg=comp_subgrid(i,j,t,nox_index)*(14.+16.*2.)/14.
o3_bg=comp_EMEP_subgrid(i,j,t,o3_index)
!o3_bg=comp_EMEP_subgrid(i,j,t,o3_index)+48./46.*comp_EMEP_subgrid(i,j,t,no2_index)*subgrid(i,j,t,emep_local_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
f_no2_loc=0.
nox_loc=0.
do i_source=1,n_source_index
if (calculate_source(i_source)) then
do i_subsource=1,n_subsource(i_source)
f_no2_loc=f_no2_loc+emission_factor(no2_index,i_source,i_subsource)/emission_factor(nox_index,i_source,i_subsource)*subgrid(i,j,t,local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
nox_loc=nox_loc+subgrid(i,j,t,local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
enddo
endif
if (calculate_emep_source(i_source).and..not.calculate_source(i_source)) then
do i_subsource=1,n_subsource(i_source)
f_no2_loc=f_no2_loc+f_no2_emep*subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
nox_loc=nox_loc+subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
enddo
endif
enddo
if (nox_loc.ne.0.0) then
f_no2_loc=f_no2_loc/nox_loc
else
f_no2_loc=0.
endif
!no2_bg=max(0.,comp_subgrid(i,j,t,no2_index)-f_no2_loc*subgrid(i,j,t,emep_local_subgrid_index,allsource_index,emep_subsource))
!no2_bg=comp_EMEP_subgrid(i,j,t,no2_index)*subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
no2_bg=comp_EMEP_subgrid(i,j,t,no2_index)*nox_bg/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
o3_bg=max(0.0,comp_EMEP_subgrid(i,j,t,o3_index)+48./46.*(comp_EMEP_subgrid(i,j,t,no2_index)-no2_bg)) !Conserve Ox when removing NO2 in the background. Cannot be less than 0
!no2_bg=comp_subgrid(i,j,t,no2_index)
!Assume stationary state to derive no2 and o3 background
if (no2_background_chemistry_scheme_flag.eq.1) then
call uEMEP_nonlocal_NO2_O3(nox_bg,comp_EMEP_subgrid(i,j,t,nox_index),comp_EMEP_subgrid(i,j,t,no2_index),comp_EMEP_subgrid(i,j,t,o3_index),J_photo,temperature,f_no2_emep,no2_bg,o3_bg)
endif
if (EMEP_additional_grid_interpolation_size.gt.0) then
no2_bg_additional=comp_EMEP_subgrid(i,j,t,no2_index)*nox_bg_additional/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
if (no2_background_chemistry_scheme_flag.eq.1) then
call uEMEP_nonlocal_NO2_O3(nox_bg_additional,comp_EMEP_subgrid(i,j,t,nox_index),comp_EMEP_subgrid(i,j,t,no2_index),comp_EMEP_subgrid(i,j,t,o3_index),J_photo,temperature,f_no2_emep,no2_bg_additional,o3_bg_additional)
else
!o3_bg_additional=comp_EMEP_subgrid(i,j,t,o3_index)
o3_bg_additional=max(0.0,comp_EMEP_subgrid(i,j,t,o3_index)+48./46.*(comp_EMEP_subgrid(i,j,t,no2_index)-no2_bg_additional)) !Conserve Ox when removing NO2 in the background
endif
comp_source_EMEP_additional_subgrid(i,j,t,o3_index,allsource_index)=o3_bg_additional
comp_source_EMEP_additional_subgrid(i,j,t,no2_index,allsource_index)=no2_bg_additional
endif
!Set the background O3 level. use all_source for the nonlocal.
!subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(o3_nc_index))=o3_bg
comp_source_EMEP_subgrid(i,j,t,o3_index,allsource_index)=o3_bg
comp_source_EMEP_subgrid(i,j,t,no2_index,allsource_index)=no2_bg
!write(*,*) nox_bg,subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
!if (J_photo.ne.0) then
!write(*,'(A,3I6,f5.2,6f12.3)') 'IN : ',i,j,t,no2_bg/nox_bg,nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo
!endif
!if (nox_loc+nox_bg.lt.0.) write(*,*) t,nox_loc,nox_bg,nox_loc+nox_bg
if (no2_chemistry_scheme_flag.eq.0) then
nox_out=nox_bg+nox_loc
no2_out=no2_bg+nox_loc*f_no2_loc
o3_out=o3_bg
elseif (no2_chemistry_scheme_flag.eq.1) then
call uEMEP_photostationary_NO2(nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,nox_out,no2_out,o3_out,p_bg_out,p_out)
elseif (no2_chemistry_scheme_flag.eq.2) then
!write(*,'(7f8.2,f12.2,2f8.2)') nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,traveltime_subgrid(i,j,t,1,pollutant_loop_back_index(nox_index))
call uEMEP_phototimescale_NO2(nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,traveltime_subgrid(i,j,t,3,pollutant_loop_back_index(nox_nc_index))*traveltime_scaling,nox_out,no2_out,o3_out,p_bg_out,p_out)
!write(*,'(7f8.2,f12.2,2f8.2)') nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,traveltime_subgrid(i,j,t,1),no2_out/nox_out,o3_out/o3_bg
elseif (no2_chemistry_scheme_flag.eq.3) then
call uEMEP_Romberg_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out,romberg_parameters)
!write(*,'(8f8.3)') nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out
elseif (no2_chemistry_scheme_flag.eq.4) then
call uEMEP_SRM_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out,SRM_parameters)
elseif (no2_chemistry_scheme_flag.eq.5) then
call uEMEP_During_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,comp_EMEP_subgrid(i,j,t,nox_index),comp_EMEP_subgrid(i,j,t,no2_index),comp_EMEP_subgrid(i,j,t,o3_index),J_photo,temperature,nox_out,no2_out,o3_out,p_bg_out,p_out)
endif
sum_p_bg_out=sum_p_bg_out+p_bg_out
sum_p_out=sum_p_out+p_out
count_p_out=count_p_out+1
max_p_bg_out=max(max_p_bg_out,p_bg_out);min_p_bg_out=min(min_p_bg_out,p_bg_out)
max_p_out=max(max_p_out,p_out);min_p_out=min(min_p_out,p_out)
!write(*,*) nox_out-subgrid(i,j,t,total_subgrid_index,allsource_index,1)
comp_subgrid(i,j,t,o3_index)=o3_out
comp_subgrid(i,j,t,no2_index)=no2_out
comp_subgrid(i,j,t,nox_index)=nox_out
!if (J_photo.ne.0) then
!write(*,'(A,3I6,f5.2,6f12.3)') 'OUT: ',i,j,t,no2_out/nox_out,nox_out,no2_out,o3_out,p_bg_out,p_out
!write(*,'(A,5I6,f5.2,3f12.3,1es12.2,2f12.3)') 'IN: ',i,j,i_integral,j_integral,t,no2_bg/nox_bg,nox_bg,no2_bg,o3_bg,J_photo,p_bg_out,p_out
! write(*,*)
!endif
!if (no2_out.gt.nox_out) write(*,*)no2_out,nox_out,nox_bg,nox_loc
!else
! comp_subgrid(i,j,t,:)=0.
!endif
else
comp_subgrid(i,j,t,o3_index)=NODATA_value
comp_subgrid(i,j,t,no2_index)=NODATA_value
comp_subgrid(i,j,t,nox_index)=NODATA_value
endif
enddo
enddo
enddo
write(*,'(A,2f12.3)') 'P value (nonlocal,local): ',sum_p_bg_out/count_p_out,sum_p_out/count_p_out
write(*,'(A,2f12.3)') 'P max (nonlocal,local): ',max_p_bg_out,max_p_out
write(*,'(A,2f12.3)') 'P min (nonlocal,local): ',min_p_bg_out,min_p_out
end subroutine uEMEP_chemistry
subroutine uEMEP_source_fraction_chemistry
!Special source allocation for no2 based on leaving out one source at a time in the chemistry calculation
!This will always give a sum less, but not much less than, the total no2
!This is normalised in order for it to be used
!Vhemistry scheme must have been run prior to implementing this
use uEMEP_definitions
implicit none
integer i,j
real nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature
real nox_out,no2_out,o3_out,p_bg_out,p_out
integer t,t_start,t_end
integer i_source,i_subsource,emep_subsource
integer i_pollutant
logical :: nox_available=.false.
!integer i_comp,i_file
!character(256) temp_name
integer i_integral,j_integral
integer remove_source
real sum_no2_source_subgrid,sum_o3_source_subgrid
real, allocatable :: comp_source_temp_subgrid(:,:,:,:,:)
real, allocatable :: comp_source_EMEP_temp_subgrid(:,:,:,:,:)
!Search for nox in the pollutants
do i_pollutant=1,n_pollutant_loop
if (pollutant_loop_index(i_pollutant).eq.nox_nc_index) nox_available=.true.
enddo
!Leave the chemistry routine if nox is not available
if (.not.nox_available) return
if (.not.allocated(comp_source_subgrid)) allocate(comp_source_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (calculate_EMEP_additional_grid_flag) then
if (.not.allocated(comp_source_additional_subgrid)) allocate(comp_source_additional_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
!Temporary array for storing the comp_source_subgrid to avoid rewriting large parts of the routine when running the additional version
if (.not.allocated(comp_source_temp_subgrid)) allocate(comp_source_temp_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
if (.not.allocated(comp_source_EMEP_temp_subgrid)) allocate(comp_source_EMEP_temp_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
comp_source_temp_subgrid=comp_source_subgrid
comp_source_EMEP_temp_subgrid=comp_source_EMEP_subgrid
comp_source_EMEP_subgrid=comp_source_EMEP_additional_subgrid
endif
! Already allocated in chemistry call
!if (.not.allocated(comp_source_EMEP_subgrid)) allocate(comp_source_EMEP_subgrid(subgrid_dim(x_dim_index),subgrid_dim(y_dim_index),subgrid_dim(t_dim_index),n_compound_index,n_source_index))
!write(unit_logfile,'(A)') ''
!write(unit_logfile,'(A)') '================================================================'
write(unit_logfile,'(A)') 'Calculating chemistry source contribution for NO2 (uEMEP_source_fraction_chemistry)'
!write(unit_logfile,'(A)') '================================================================'
if (no2_chemistry_scheme_flag.eq.0) then
write(unit_logfile,'(A)') 'No chemistry used'
elseif (no2_chemistry_scheme_flag.eq.1) then
write(unit_logfile,'(A)') 'Photostationary state used'
elseif (no2_chemistry_scheme_flag.eq.2) then
write(unit_logfile,'(A)') 'Photochemistry with time scale used'
elseif (no2_chemistry_scheme_flag.eq.3) then
write(unit_logfile,'(A)') 'Romberg parameterisation used'
elseif (no2_chemistry_scheme_flag.eq.4) then
write(unit_logfile,'(A)') 'SRM parameterisation used'
elseif (no2_chemistry_scheme_flag.eq.5) then
write(unit_logfile,'(A)') 'During parameterisation used'
endif
t_start=1
t_end=subgrid_dim(t_dim_index)
i_subsource=1
emep_subsource=1
nox_bg=0.;no2_bg=0.;o3_bg=0.;nox_loc=0.;f_no2_loc=0.;J_photo=0.;temperature=0.;
!Weighted travel time already calculated
do t=t_start,t_end
do j=1,subgrid_dim(y_dim_index)
do i=1,subgrid_dim(x_dim_index)
if (use_subgrid(i,j,allsource_index)) then
i_integral=crossreference_target_to_integral_subgrid(i,j,x_dim_index)
j_integral=crossreference_target_to_integral_subgrid(i,j,y_dim_index)
J_photo=meteo_subgrid(i_integral,j_integral,t,J_subgrid_index)
temperature=meteo_subgrid(i_integral,j_integral,t,t2m_subgrid_index)
if (calculate_EMEP_additional_grid_flag) then
nox_bg=subgrid(i,j,t,emep_additional_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
else
nox_bg=subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
endif
!Add the additional non-downscaled EMEP local source to the background as these have not been used to calulate travel times
!do i_source=1,n_source_index
!if (calculate_EMEP_source(i_source).and..not.calculate_source(i_source).and.i_source.ne.allsource_index) then
! nox_bg=nox_bg+subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
!endif
!enddo
o3_bg=comp_EMEP_subgrid(i,j,t,o3_index)
!o3_bg=comp_EMEP_subgrid(i,j,t,o3_index)+48./46.*comp_EMEP_subgrid(i,j,t,no2_index)*subgrid(i,j,t,emep_local_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
do remove_source=1,n_source_index
if (calculate_source(remove_source).or.remove_source.eq.allsource_index.or.(calculate_emep_source(remove_source).and..not.calculate_source(remove_source))) then
!if (calculate_source(remove_source).or.remove_source.eq.allsource_index.or.calculate_emep_source(remove_source)) then
f_no2_loc=0.
nox_loc=0.
do i_source=1,n_source_index
if (calculate_source(i_source)) then
if (remove_source.ne.i_source) then
do i_subsource=1,n_subsource(i_source)
f_no2_loc=f_no2_loc+emission_factor(no2_index,i_source,i_subsource)/emission_factor(nox_index,i_source,i_subsource)*subgrid(i,j,t,local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
nox_loc=nox_loc+subgrid(i,j,t,local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
enddo
endif
endif
!Include the local EMEP that are not being downscaled
if (.not.calculate_EMEP_additional_grid_flag) then
if (calculate_emep_source(i_source).and..not.calculate_source(i_source)) then
if (remove_source.ne.i_source) then
do i_subsource=1,n_subsource(i_source)
f_no2_loc=f_no2_loc+f_no2_emep*subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
nox_loc=nox_loc+subgrid(i,j,t,emep_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
enddo
endif
endif
endif
if (calculate_EMEP_additional_grid_flag) then
!If calculating the additional region then use the additional local EMEP not being downscaled
if (calculate_emep_source(i_source).and..not.calculate_source(i_source)) then
if (remove_source.ne.i_source) then
do i_subsource=1,n_subsource(i_source)
f_no2_loc=f_no2_loc+f_no2_emep*subgrid(i,j,t,emep_additional_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
nox_loc=nox_loc+subgrid(i,j,t,emep_additional_local_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
enddo
endif
endif
!If calculating the additional region then include the difference BG-BG_additional to the local EMEP that is being downscaled
if (calculate_source(i_source)) then
if (remove_source.ne.i_source) then
do i_subsource=1,n_subsource(i_source)
f_no2_loc=f_no2_loc+f_no2_emep* &
(subgrid(i,j,t,emep_nonlocal_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index)) &
-subgrid(i,j,t,emep_additional_nonlocal_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index)))
nox_loc=nox_loc+subgrid(i,j,t,emep_nonlocal_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index)) &
-subgrid(i,j,t,emep_additional_nonlocal_subgrid_index,i_source,pollutant_loop_back_index(nox_nc_index))
enddo
endif
endif
endif
enddo
if (nox_loc.ne.0.0) then
f_no2_loc=f_no2_loc/nox_loc
else
f_no2_loc=0.
endif
!Use the all source index to calculate the contribution from the background
!This is done by removing all the sources, rather than the difference as done for the local sources
!This is because the chemistry is disturbed when removing background nox and no2
if (remove_source.ne.allsource_index) then
!nox_bg=subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
!no2_bg=comp_EMEP_subgrid(i,j,t,no2_index)*subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
no2_bg=comp_EMEP_subgrid(i,j,t,no2_index)*nox_bg/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
o3_bg=max(0.0,comp_EMEP_subgrid(i,j,t,o3_index)+48./46.*(comp_EMEP_subgrid(i,j,t,no2_index)-no2_bg)) !Conserve Ox when removing NO2 in the background. Cannot be less than 0
else
!nox_bg=subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
!no2_bg=comp_EMEP_subgrid(i,j,t,no2_index)*subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
no2_bg=comp_EMEP_subgrid(i,j,t,no2_index)*nox_bg/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))
o3_bg=max(0.0,comp_EMEP_subgrid(i,j,t,o3_index)+48./46.*(comp_EMEP_subgrid(i,j,t,no2_index)-no2_bg)) !Conserve Ox when removing NO2 in the background. Cannot be less than 0
nox_loc=0.
f_no2_loc=0.
endif
!Assume stationary state to derive no2 and o3 background. Overwrites the previous setting
if (no2_background_chemistry_scheme_flag.eq.1) then
call uEMEP_nonlocal_NO2_O3(nox_bg,comp_EMEP_subgrid(i,j,t,nox_index),comp_EMEP_subgrid(i,j,t,no2_index),comp_EMEP_subgrid(i,j,t,o3_index),J_photo,temperature,f_no2_emep,no2_bg,o3_bg)
endif
if (no2_chemistry_scheme_flag.eq.0) then
nox_out=nox_bg+nox_loc
no2_out=no2_bg+nox_loc*f_no2_loc
o3_out=o3_bg
elseif (no2_chemistry_scheme_flag.eq.1) then
call uEMEP_photostationary_NO2(nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,nox_out,no2_out,o3_out,p_bg_out,p_out)
elseif (no2_chemistry_scheme_flag.eq.2) then
!write(*,'(i,7f8.2,f12.2,2f8.2)') remove_source,nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,traveltime_subgrid(i,j,t,1,pollutant_loop_back_index(nox_nc_index))
call uEMEP_phototimescale_NO2(nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,traveltime_subgrid(i,j,t,3,pollutant_loop_back_index(nox_nc_index))*traveltime_scaling,nox_out,no2_out,o3_out,p_bg_out,p_out)
!write(*,'(i,7f8.2,f12.2,3f8.4)') remove_source,nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,traveltime_subgrid(i,j,t,1,pollutant_loop_back_index(nox_nc_index)),nox_out,no2_out,o3_out
elseif (no2_chemistry_scheme_flag.eq.3) then
call uEMEP_Romberg_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out,romberg_parameters)
elseif (no2_chemistry_scheme_flag.eq.4) then
call uEMEP_SRM_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out,SRM_parameters)
elseif (no2_chemistry_scheme_flag.eq.5) then
call uEMEP_During_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,comp_EMEP_subgrid(i,j,t,nox_index),comp_EMEP_subgrid(i,j,t,no2_index),comp_EMEP_subgrid(i,j,t,o3_index),J_photo,temperature,nox_out,no2_out,o3_out,p_bg_out,p_out)
endif
!write(*,*) nox_out-subgrid(i,j,t,total_subgrid_index,allsource_index,1)
!For background just use the result without any sources.
!There is a problem disturbing the chemistry by removing the background nox and no2 but not changing the o3
if (remove_source.eq.allsource_index) then
comp_source_subgrid(i,j,t,no2_index,remove_source)=no2_bg
comp_source_subgrid(i,j,t,o3_index,remove_source)=o3_bg
else
!Avoid round off errors which can occur with small numbers
comp_source_subgrid(i,j,t,no2_index,remove_source)=max(0.0,comp_subgrid(i,j,t,no2_index)-no2_out)
!Can be negative and can be greater than 1 so do not limit
comp_source_subgrid(i,j,t,o3_index,remove_source)=comp_subgrid(i,j,t,o3_index)-o3_out
!comp_source_EMEP_subgrid(i,j,t,no2_index,remove_source)=max(0.0,comp_subgrid(i,j,t,no2_index)-no2_out)
!Can be negative and can be greater than 1 so do not limit
!comp_source_EMEP_subgrid(i,j,t,o3_index,remove_source)=comp_subgrid(i,j,t,o3_index)-o3_out
!write(*,*) i,j,comp_source_subgrid(i,j,t,o3_index,remove_source)
endif
endif
enddo
else
comp_source_subgrid(i,j,t,:,:)=NODATA_value
endif
!Normalise the contributions
!Calculate the sum
sum_no2_source_subgrid=0.
sum_o3_source_subgrid=0.
do i_source=1,n_source_index
!if (calculate_source(i_source).or.i_source.eq.allsource_index) then
!if (calculate_source(i_source)) then
if (calculate_source(i_source).or.(calculate_emep_source(i_source).and..not.calculate_source(i_source))) then
sum_no2_source_subgrid=sum_no2_source_subgrid+comp_source_subgrid(i,j,t,no2_index,i_source)
sum_o3_source_subgrid=sum_o3_source_subgrid+comp_source_subgrid(i,j,t,o3_index,i_source)
endif
!if (calculate_emep_source(i_source).and..not.calculate_source(i_source)) then
! sum_no2_source_subgrid=sum_no2_source_subgrid+comp_source_EMEP_subgrid(i,j,t,no2_index,i_source)
! sum_o3_source_subgrid=sum_o3_source_subgrid+comp_source_EMEP_subgrid(i,j,t,o3_index,i_source)
!endif
enddo
!Set the background fractions so they will not be adjusted with normalisation
!comp_source_subgrid(i,j,t,no2_index,allsource_index)=comp_source_subgrid(i,j,t,no2_index,allsource_index)/comp_subgrid(i,j,t,no2_index)
!comp_source_subgrid(i,j,t,o3_index,allsource_index)=comp_source_subgrid(i,j,t,o3_index,allsource_index)/comp_subgrid(i,j,t,o3_index)
do i_source=1,n_source_index
!if (calculate_source(i_source)) then
if (calculate_source(i_source).or.(calculate_emep_source(i_source).and..not.calculate_source(i_source))) then
!Adjust for the background and normalise
if (sum_no2_source_subgrid.ne.0) then
comp_source_subgrid(i,j,t,no2_index,i_source)=comp_source_subgrid(i,j,t,no2_index,i_source)/sum_no2_source_subgrid &
*(comp_subgrid(i,j,t,no2_index)-comp_source_EMEP_subgrid(i,j,t,no2_index,allsource_index))
else
comp_source_subgrid(i,j,t,no2_index,i_source)=0
!comp_source_subgrid(i,j,t,no2_index,allsource_index)=(comp_subgrid(i,j,t,no2_index)-comp_source_EMEP_subgrid(i,j,t,no2_index,allsource_index))
endif
!write(*,*) i,j,i_source,sum_o3_source_subgrid
if (sum_o3_source_subgrid.ne.0) then
comp_source_subgrid(i,j,t,o3_index,i_source)=comp_source_subgrid(i,j,t,o3_index,i_source)/sum_o3_source_subgrid &
*(comp_subgrid(i,j,t,o3_index)-comp_source_EMEP_subgrid(i,j,t,o3_index,allsource_index))
else
comp_source_subgrid(i,j,t,o3_index,i_source)=0
!comp_source_subgrid(i,j,t,o3_index,allsource_index)=(comp_subgrid(i,j,t,o3_index)-comp_source_EMEP_subgrid(i,j,t,o3_index,allsource_index))
endif
if (comp_subgrid(i,j,t,no2_index).le.0) comp_source_subgrid(i,j,t,no2_index,i_source)=0
if (comp_subgrid(i,j,t,o3_index).le.0) comp_source_subgrid(i,j,t,o3_index,i_source)=0
!if (comp_source_subgrid(i,j,t,no2_index,i_source).le.0) comp_source_subgrid(i,j,t,no2_index,i_source)=0.
!if (comp_source_subgrid(i,j,t,o3_index,i_source).le.0) comp_source_subgrid(i,j,t,o3_index,i_source)=0
endif
enddo
!write(*,'(2i4,6f12.6)') i,j,sum_no2_source_subgrid,sum_no2_source_subgrid/comp_subgrid(i,j,t,no2_index),comp_source_subgrid(i,j,t,no2_index,allsource_index) &
! ,comp_source_subgrid(i,j,t,no2_index,traffic_index),comp_source_subgrid(i,j,t,no2_index,shipping_index),comp_source_subgrid(i,j,t,no2_index,heating_index)
!write(*,'(2i4,6f12.6)') i,j,sum_o3_source_subgrid,sum_o3_source_subgrid/comp_subgrid(i,j,t,o3_index),comp_source_subgrid(i,j,t,o3_index,allsource_index) &
! ,comp_source_subgrid(i,j,t,o3_index,traffic_index),comp_source_subgrid(i,j,t,o3_index,shipping_index),comp_source_subgrid(i,j,t,o3_index,heating_index)
!if (comp_source_subgrid(i,j,t,nor_index,allsource_index).lt.0.or.comp_source_subgrid(i,j,t,no2_index,traffic_index).lt.0.or.no2_source_subgrid(i,j,t,shipping_index).lt.0.or.no2_source_fraction_subgrid(i,j,t,heating_index).lt.0) then
! write(*,*) 'Traffic value less than 0. comp_subgrid =',comp_subgrid(i,j,t,no2_index),comp_EMEP_subgrid(i,j,t,no2_index) &
! ,comp_EMEP_subgrid(i,j,t,no2_index)*subgrid(i,j,t,emep_nonlocal_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index))/subgrid(i,j,t,emep_subgrid_index,allsource_index,pollutant_loop_back_index(nox_nc_index)) &
! ,comp_EMEP_subgrid(i,j,t,o3_index)
! stop
!endif
enddo
enddo
enddo
!Transfer the arrays to the right outputs
if (calculate_EMEP_additional_grid_flag) then
comp_source_additional_subgrid=comp_source_subgrid
comp_source_subgrid=comp_source_temp_subgrid
comp_source_EMEP_subgrid=comp_source_EMEP_temp_subgrid
!EMEP_additional is unchanged
if (allocated(comp_source_temp_subgrid)) deallocate(comp_source_temp_subgrid)
if (allocated(comp_source_EMEP_temp_subgrid)) deallocate(comp_source_EMEP_temp_subgrid)
endif
end subroutine uEMEP_source_fraction_chemistry
subroutine uEMEP_photostationary_NO2(nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,nox_out,no2_out,o3_out,p_bg_out,p_out)
!use uEMEP_definitions
implicit none
real, intent(in) :: nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature
real, intent(out) :: nox_out,no2_out,o3_out,p_bg_out,p_out
integer no2_i,no_i,nox_i,o3_i,ox_i,nox_bg_i,no2_bg_i,n_i
parameter (n_i=7)
real Na,Na_fac,k1
real mass(n_i)
real mmass(n_i)
real mol(n_i)
real f_no2,f_ox,Jd,fac_sqrt
real :: min_nox=1.0e-6
DATA mmass /46.,30.,46.,48.,47.,46.,46./
no2_i=1;no_i=2;nox_i=3;o3_i=4;ox_i=5;nox_bg_i=6;no2_bg_i=7
Na=6.022e23 !(molecules/mol)
Na_fac=Na/1.0e12 !Conversion from ug/m3 to molecules/cm3 included
k1=1.4e-12*exp(-1310./temperature); !(cm^3/s) and temperature in Kelvin
!mmass(1:n_i)=(/46.,30.,46.,48.,47.,46.,46.,46./)
mass(1:n_i)=0.
mol(1:n_i)=0.
!Test for 0 NOx. If so leave the routine
mass(nox_i)=nox_loc+nox_bg
! if (mass(nox_i).eq.0.) then
if (mass(nox_i).le.min_nox) then
nox_out=0.
no2_out=0.
o3_out=o3_bg
return
endif
!Check the photostationary assumption for the input data
mass(nox_i)=nox_bg
mass(no2_i)=no2_bg
mass(o3_i)=o3_bg
mol=mass/mmass*Na_fac !(molecules per cm3)
mol(ox_i)=mol(o3_i)+mol(no2_i)
mol(no_i)=max(0.0,mol(nox_i)-mol(no2_i))
!Test the photostationary state for the bg input data
if (J_photo.ne.0.and.mol(no_i).ne.0..and.mol(o3_i).ne.0.) then
p_bg_out=J_photo*mol(no2_i)/k1/mol(o3_i)/mol(no_i)
else
!p_bg_out=mol(no2_i)/mol(ox_i)
p_bg_out=mol(no2_i)/(mol(ox_i)+mol(nox_i)-abs(mol(ox_i)-mol(nox_i)))*2.
endif
!if (J_photo.ne.0.) write(*,*) p_bg_out,J_photo,mol(no2_i),k1,mol(o3_i),mol(no_i)
!Add the local contribution for calculation
mass(nox_i)=nox_loc+nox_bg
mass(no2_i)=f_no2_loc*nox_loc+no2_bg
mass(o3_i)=o3_bg
!mass(ox_i)=o3_bg+mass(no2_i)
mol=mass/mmass*Na_fac !(molecules per cm3)
mol(ox_i)=mol(o3_i)+mol(no2_i)
mol(no_i)=max(0.0,mol(nox_i)-mol(no2_i))
f_no2=mol(no2_i)/mol(nox_i)
f_ox=mol(ox_i)/mol(nox_i)
!Test the photostationary state for the input data. Will not be in equilibrium
if (J_photo.ne.0.and.mol(no_i).ne.0..and.mol(o3_i).ne.0.) then
p_out=J_photo*mol(no2_i)/k1/mol(o3_i)/mol(no_i)
else
!p_out=mol(no2_i)/mol(ox_i)
p_out=mol(no2_i)/(mol(ox_i)+mol(nox_i)-abs(mol(ox_i)-mol(nox_i)))*2.
endif
!Set the photolysis rate
Jd=J_photo/k1/mol(nox_i)
!Calculate fraction of NO2 in photostationary state
fac_sqrt=max(0.0,(1.+f_ox+Jd)**2-4.*f_ox)
!if (J_photo.ne.0) then
f_no2=0.5*((1.+f_ox+Jd)-sqrt(fac_sqrt))
!else
! f_no2=min(1.0,f_ox)
!endif
!write(*,'(A,9ES12.1)') 'MOL: ',mol(nox_i),mol(no2_i),mol(o3_i),mol(ox_i),f_no2,f_ox,Jd,f_no2,p_bg_out
!Convert back to mass
mol(no2_i)=f_no2*mol(nox_i);
mol(o3_i)=max(0.,mol(ox_i)-mol(no2_i)) !Rounding errors possible
mol(no_i)=max(0.,mol(nox_i)-mol(no2_i)) !Rounding errors possible
mass=mol*mmass/Na_fac !(ug/m3)
no2_out=mass(no2_i)
nox_out=mass(nox_i)
o3_out=mass(o3_i)
!Check output
if (J_photo.ne.0.and.mol(no_i).ne.0..and.mol(o3_i).ne.0.) then
p_out=J_photo*mol(no2_i)/k1/mol(o3_i)/mol(no_i)
else
!p_out=mol(no2_i)/mol(ox_i)
p_out=mol(no2_i)/(mol(ox_i)+mol(nox_i)-abs(mol(ox_i)-mol(nox_i)))*2.
endif
!write(*,'(A,9ES12.1)') 'MASS: ',mass(nox_i),mass(no2_i),mass(o3_i),mass(ox_i),f_no2,f_ox,Jd,f_no2,p_out
end subroutine uEMEP_photostationary_NO2
subroutine uEMEP_phototimescale_NO2(nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,time_scale,nox_out,no2_out,o3_out,p_bg_out,p_out)
!use uEMEP_definitions
implicit none
real, intent(in) :: nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,time_scale
real, intent(out) :: nox_out,no2_out,o3_out,p_bg_out,p_out
integer no2_i,no_i,nox_i,o3_i,ox_i,nox_bg_i,no2_bg_i,n_i
parameter (n_i=7)
real Na,Na_fac,k1
real mass(n_i)
real mmass(n_i)
real mol(n_i)
real fac_sqrt
real f_no2,f_ox,Jd,Jd_bg
real :: min_nox=1.0e-6
real c,b,BB,td,f_no2_0,f_no2_ps
complex(4) AA
real p_tot_out,f_ox_bg,f_no2_bg_ps,f_no2_bg
DATA mmass /46.,30.,46.,48.,47.,46.,46./
no2_i=1;no_i=2;nox_i=3;o3_i=4;ox_i=5;nox_bg_i=6;no2_bg_i=7
Na=6.022e23 !(molecules/mol)
Na_fac=Na/1.0e12 !Conversion from ug/m3 to molecules/cm3 included
k1=1.4e-12*exp(-1310./temperature); !(cm^3/s) and temperature in Kelvin
!mmass(1:n_i)=(/46.,30.,46.,48.,47.,46.,46.,46./)
mass(1:n_i)=0.
mol(1:n_i)=0.
!Test for 0 NOx. If so leave the routine
mass(nox_i)=nox_loc+nox_bg
! if (mass(nox_i).eq.0.) then
if (mass(nox_i).le.min_nox) then
nox_out=0.
no2_out=0.
o3_out=o3_bg
return
endif
!Check the photostationary assumption for the input data
mass(nox_i)=nox_bg
mass(no2_i)=no2_bg
mass(o3_i)=o3_bg
mol=mass/mmass*Na_fac !(molecules per cm3)
mol(ox_i)=mol(o3_i)+mol(no2_i)
mol(no_i)=max(0.0,mol(nox_i)-mol(no2_i))
f_ox_bg=mol(ox_i)/mol(nox_i)
Jd_bg=J_photo/k1/mol(nox_i)
f_no2_bg_ps=0.5*((1+f_ox_bg+Jd_bg)-sqrt((1+f_ox_bg+Jd_bg)**2-4.*f_ox_bg))
f_no2_bg=mol(no2_i)/mol(nox_i)
p_bg_out=f_no2_bg/f_no2_bg_ps
!Check input
if (J_photo.ne.0.and.mol(no_i).ne.0..and.mol(o3_i).ne.0.) then
p_bg_out=J_photo*mol(no2_i)/k1/mol(o3_i)/mol(no_i)
else
!p_bg_out=mol(no2_i)/mol(ox_i)
p_bg_out=mol(no2_i)/(mol(ox_i)+mol(nox_i)-abs(mol(ox_i)-mol(nox_i)))*2.
endif
!if (J_photo.ne.0.) write(*,*) p_bg_out,J_photo,mol(no2_i),k1,mol(o3_i),mol(no_i)
!Add the local contribution for calculation
mass(nox_i)=nox_loc+nox_bg
mass(no2_i)=f_no2_loc*nox_loc+no2_bg
mass(o3_i)=o3_bg
!mass(ox_i)=o3_bg+mass(no2_i)
mol=mass/mmass*Na_fac !(molecules per cm3)
mol(ox_i)=mol(o3_i)+mol(no2_i)
mol(no_i)=max(0.0,mol(nox_i)-mol(no2_i))
f_no2=mol(no2_i)/mol(nox_i)
f_ox=mol(ox_i)/mol(nox_i)
!Set the photolysis rate
Jd=J_photo/k1/mol(nox_i)
!Calculate photostationary for total nox, ox
fac_sqrt=max(0.,(1+f_ox+Jd)**2-4.*f_ox)
f_no2_ps=0.5*((1+f_ox+Jd)-sqrt(fac_sqrt))
p_tot_out=f_no2/f_no2_ps
!Test the photostationary state for the input data
!if (J_photo.ne.0.and.mol(no_i).ne.0..and.mol(o3_i).ne.0.) then
! p_out=J_photo*mol(no2_i)/k1/mol(o3_i)/mol(no_i)
!else
! p_out=mol(no2_i)/mol(nox_i)
!endif
!Calculate fraction of NO2 based on the time scale
!fac_sqrt=max(0.0,(1+f_ox+Jd)**2-4*f_ox)
c=f_ox
b=1+f_ox+Jd
BB=sqrt(max(0.0,b**2-4.*c))!max avoids roundoff errors
td=time_scale*k1*mol(nox_i)
f_no2_0=f_no2
!if (J_photo.ne.0) then
!f_no2=0.5*((1+f_ox+Jd)-sqrt(fac_sqrt))
!if (b.lt.100.)
AA=clog(cmplx((BB+b-2.*f_no2_0)/(BB-b+2.*f_no2_0)))
f_no2=real(-BB/2.*((exp(AA+BB*td)-1.)/(exp(AA+BB*td)+1.))+b/2.)
!if (BB*td.gt.50.) f_no2=-BB/2.+b/2.
if (isnan(f_no2)) f_no2=-BB/2.+b/2.
!write(*,*) f_no2,AA,nox_loc,f_no2_loc,AA,BB,Jd,mol(nox_i),k1
fac_sqrt=max(0.0,(1+f_ox+Jd)**2-4.*f_ox)
f_no2_ps=0.5*((1+f_ox+Jd)-sqrt(fac_sqrt))
p_out=f_no2/f_no2_ps
!write(*,*) p_bg_out,p_tot_out,p_out,AA,BB,b,td,exp(AA+BB*td),f_ox
!write(*,*) o3_bg,no2_bg,nox_bg
!write(*,*) c,b,BB,AA,(BB/2.+b/2.-f_no2_0)/(BB/2.-b/2.+f_no2_0),f_no2
!else
! f_no2=1.0
!endif
!write(*,'(A,9ES12.1)') 'MOL: ',mol(nox_i),mol(no2_i),mol(o3_i),mol(ox_i),f_no2,f_ox,Jd,f_no2,p_bg_out
!Convert back to mass
mol(no2_i)=max(0.,f_no2*mol(nox_i))
mol(o3_i)=max(0.,mol(ox_i)-mol(no2_i)) !Rounding errors possible
mol(no_i)=max(0.,mol(nox_i)-mol(no2_i)) !Rounding errors possible
mass=mol*mmass/Na_fac !(ug/m3)
no2_out=mass(no2_i)
nox_out=mass(nox_i)
o3_out=mass(o3_i)
if (isnan(no2_out)) then
write(*,'(8a12)') 'nox_bg','no2_bg','o3_bg','nox_loc','f_no2_loc','J_photo','temperature','time_scale'
write(*,'(8es12.2)') nox_bg,no2_bg,o3_bg,nox_loc,f_no2_loc,J_photo,temperature,time_scale
write(*,'(4a12)') 'f_no2','BB','b','b**2-4.*c'
write(*,'(4es12.2)') f_no2,BB,b,b**2-4.*c
stop
endif
!Check output
if (J_photo.ne.0.and.mol(no_i).ne.0..and.mol(o3_i).ne.0.) then
p_out=J_photo*mol(no2_i)/k1/mol(o3_i)/mol(no_i)
else
!p_out=mol(no2_i)/mol(ox_i)
p_out=mol(no2_i)/(mol(ox_i)+mol(nox_i)-abs(mol(ox_i)-mol(nox_i)))*2.
endif
!write(*,'(6f12.2)') 1./(k1*mol(nox_i)),1./J_photo,time_scale,f_no2/f_no2_ps,p_out,nox_loc/(nox_loc+nox_bg)
!write(*,'(A,9ES12.1)') 'MASS: ',mass(nox_i),mass(no2_i),mass(o3_i),mass(ox_i),f_no2,f_ox,Jd,f_no2,p_out
end subroutine uEMEP_phototimescale_NO2
subroutine uEMEP_Romberg_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out,romberg_parameters)
implicit none
real, intent(in) :: nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc
real, intent(in) :: romberg_parameters(3)
real, intent(out) :: nox_out,no2_out,o3_out
!From Norwegian obs fit
!real :: a_rom=20
!real :: b_rom=30
!From model fit
real :: a_rom=30
real :: b_rom=35
real :: c_rom=0.20
real ox_init,no2_init,no2_equ
real beta,F,K
!Gral values 30 35 0.18
!Bächlin and Bösinger (2008) 29 35 0.217
if (romberg_parameters(1).ne.0) then
a_rom=romberg_parameters(1)
b_rom=romberg_parameters(2)
c_rom=romberg_parameters(3)
endif
nox_out=nox_bg+nox_loc
no2_equ=a_rom*nox_bg/(nox_bg+b_rom)+nox_bg*c_rom
no2_out=a_rom*nox_out/(nox_out+b_rom)+nox_out*c_rom
no2_out=no2_out-no2_equ+no2_bg
no2_out=max(no2_bg,no2_out)
no2_init=no2_bg+f_no2_loc*nox_loc
!Small adjustments for molecular weights
ox_init=no2_init*47./46.+o3_bg*47./48.
o3_out=ox_init*48./47.-no2_out*48./46.
!o3_out=o3_bg.+no2_bg*48./46.-no2_out*48./46.
end subroutine uEMEP_Romberg_NO2
subroutine uEMEP_SRM_NO2(nox_bg,no2_bg,nox_loc,o3_bg,f_no2_loc,nox_out,no2_out,o3_out,SRM_parameters)
implicit none