-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNORTRIP_save_uemep_emissions.f90
813 lines (647 loc) · 35.4 KB
/
NORTRIP_save_uemep_emissions.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
!NORTRIP_save_uemep_emissions.f90
!----------------------------------------------------------------------
subroutine NORTRIP_save_uemep_emissions
use NORTRIP_definitions
implicit none
integer :: unit_out
character(24) uemep_start_date_str
character(24) uemep_end_date_str
character(24) :: uemep_date_format_str='yyyymmddHH'
character(256) temp_name,filename_temp
logical exists
integer a_start(num_date_index),a_end(num_date_index)
real conversion
character(12) :: pm_str=''
real emis_road
real length_road_km(0:n_roads)
real, save, allocatable :: emis_sum(:)
real emis_time(n_time)
integer unit_count
integer n_time_save
integer x_loop,save_size(8)
integer n_roads_total_save
integer n_x_save
unit_out=unit_save_emissions
!Declare functions
n_time_save=max_time_save-min_time_save+1
if (ro_tot.eq.1) then
write(unit_logfile,'(A)') '----------------------------------------------------------------'
write(unit_logfile,'(A)') 'Saving uEMEP emissions to file'
write(unit_logfile,'(A)') '----------------------------------------------------------------'
!Set start and end date stamps
n_time_save=max_time_save-min_time_save+1
a_start=date_data(:,min_time_save)
a_end=date_data(:,max_time_save)
call date_to_datestr(a_start,uemep_date_format_str,uemep_start_date_str)
call date_to_datestr(a_end,uemep_date_format_str,uemep_end_date_str)
!Change the date stamp to follow NILU conventions
!call incrtm(-1,a_start(1),a_start(2),a_start(3),a_start(4))
if (.not.allocated(emis_sum)) then
allocate (emis_sum(num_size+2))
endif
!Check that path exists after filling in date stamp
call date_to_datestr_bracket(a_start,path_output_emis,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
inquire(directory=trim(temp_name),exist=exists)
if (.not.exists) then
write(unit_logfile,'(A)')'ERROR: Path '//trim(temp_name)//' does not exist. No file saved'
return
endif
endif
!NORTRIP emissions are g/km/hour.
conversion=1.
!conversion=1.e6/1000./3600 !Convert to ug/s/m for use in EMEP (ug/s)
unit_count=0
n_x_save=4
save_size(1)=pm_10;save_size(2)=pm_25;save_size(3)=pm_exhaust;save_size(4)=nox_exhaust
do x_loop=1,n_x_save
x=save_size(x_loop)
unit_count=x_loop
unit_out=unit_save_emissions+unit_count
if (x.eq.pm_25) pm_str='PM25'
if (x.eq.pm_10) pm_str='PM10'
if (x.eq.pm_exhaust) pm_str='EP'
if (x.eq.nox_exhaust) pm_str='NOX'
if (ro_tot.eq.1) then
!Calculate number of roads that will be saved
n_roads_total_save=0
if (use_single_road_loop_flag) then
do ro=1,n_roads_total
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
n_roads_total_save=n_roads_total_save+1
endif
enddo
else
do ro=1,n_roads
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
n_roads_total_save=n_roads_total_save+1
endif
enddo
endif
!Open the outputfile for date
temp_name=trim(path_output_emis)//trim(filename_output_emis)//'_'//trim(pm_str)//'_'//trim(uemep_start_date_str)//'-'//trim(uemep_end_date_str)//'.txt'
!Put in date in path and filename if required
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
write(unit_logfile,'(a)') ' Filename= '//trim(temp_name)
open(unit_out,file=temp_name,status='replace')
write(unit_out,'(A)') '# NORTRIP emission output for uEMEP'
if (x.eq.pm_25.or.x.eq.pm_10) then
if (available_airquality_data(EP_emis_index).or.exhaust_EF_available.ne.0) then
write(unit_out,'(A,A)') '# Hourly traffic non-exhaust and exhaust emission for compound: ',trim(pm_str)
else
write(unit_out,'(A,A)') '# Hourly traffic non-exhaust only emission for compound: ',trim(pm_str)
endif
endif
if (x.eq.pm_exhaust) then
if (available_airquality_data(EP_emis_index).or.exhaust_EF_available.ne.0) then
write(unit_out,'(A,A)') '# Hourly traffic exhaust emission for compound: ',trim(pm_str)
else
write(unit_out,'(A)') '# No exhaust emission data available'
endif
endif
if (x.eq.nox_exhaust) then
if (available_airquality_data(NOX_emis_index).or.NOX_EF_available.ne.0) then
write(unit_out,'(A,A)') '# Hourly traffic exhaust emission for compound: ',trim(pm_str)
else
write(unit_out,'(A)') '# No exhaust emission data available'
endif
endif
write(unit_out,'(A)') '# Emission data unit'
write(unit_out,'(A)') 'g/km/hr'
write(unit_out,'(A)') '# Emission data start and end date'
write(unit_out,'(A)') trim(uemep_start_date_str)//' '//trim(uemep_end_date_str)
write(unit_out,'(A)') '# Number of roads Number of hours'
write(unit_out,'(2i)') n_roads_total_save,n_time_save
write(unit_out,'(A)') '# RoadLinkID Emission'
emis_sum(x)=0.
endif
!Note that if line_or_grid_data_flag(ro).eq.0 then this is a link without traffic data.
!Must be saved but with no information
do ro=n_roads_start,n_roads_end
!Length of road in km
if (length_road(ro).eq.0) then
length_road_km(ro)=sqrt((x_road(1,ro)-x_road(2,ro))**2+(y_road(1,ro)-y_road(2,ro))**2)/1000.
else
length_road_km(ro)=length_road(ro)/1000.
endif
do ti=min_time_save,max_time_save
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.-1.or.line_or_grid_data_flag(ro).eq.3) then
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
if (x.eq.pm_exhaust) then
emis_road=sum(E_road_data(exhaust_index,pm_25,E_total_index,ti,:,ro))*conversion
elseif (x.eq.nox_exhaust) then
emis_road=0.
if (available_airquality_data(NOX_emis_index)) then
emis_road=emis_road+airquality_data(NOX_emis_index,ti,ro)*conversion
elseif (NOX_EF_available.ne.0) then
do v=1,num_veh
emis_road=emis_road+traffic_data(N_v_index(v),ti,ro)*NOX_EF(v,ro)*conversion
enddo
else
emis_road=0.
endif
else
emis_road=sum(E_road_data(total_dust_index,x,E_total_index,ti,:,ro))*conversion
endif
else
!These are roads without traffic data. Shouldn't go here but is possible if reading links to use instead of using ADT
emis_road=0.
endif
!Save emissions when using single roads to get it in the right order
!Write them at end of calculation
emis_time(ti)=emis_road
emis_sum(x)=emis_sum(x)+emis_road*length_road_km(ro)
endif
enddo
!write(*,*) ro_tot,ro,road_ID(ro_tot),n_time_save
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.-1.or.line_or_grid_data_flag(ro).eq.3) then
if (use_single_road_loop_flag) then
write(unit_out,'(i12,<n_time_save>es12.4)') road_ID(ro_tot),emis_time(min_time_save:max_time_save)
else
write(unit_out,'(i12,<n_time_save>es12.4)') road_ID(ro),emis_time(min_time_save:max_time_save)
endif
endif
enddo
if (ro_tot.eq.n_roads_total) then
close (unit_out)
!Conversion is for g to kg
write(unit_logfile,'(A,a,a,es12.2)') ' Total emissions of ',trim(pm_str),' (kg) for all road links over this period = ',emis_sum(x)*1.e-3*dt
endif
enddo
if (allocated(emis_sum).and.(ro_tot.eq.n_roads_total)) deallocate(emis_sum)
end subroutine NORTRIP_save_uemep_emissions
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine NORTRIP_save_uemep_emissions_all
use NORTRIP_definitions
implicit none
integer :: unit_out
character(24) uemep_start_date_str
character(24) uemep_end_date_str
character(24) :: uemep_date_format_str='yyyymmddHH'
character(256) temp_name,filename_temp
logical exists
integer a_start(num_date_index),a_end(num_date_index)
real conversion
character(12) :: pm_str=''
real emis_road
real length_road_km(0:n_roads)
real, save, allocatable :: emis_sum(:)
real emis_time(n_time)
integer unit_count
integer n_time_save
integer x_loop
integer n_roads_total_save
integer n_x_save,n_x_save_max
parameter (n_x_save_max=8)
integer save_size(n_x_save_max)
integer save_source(n_x_save_max)
character(10) save_str(n_x_save_max)
character(256) file_str
!Standard without sand or salt
n_x_save=4
save_size(1)=pm_10;save_size(2)=pm_25;save_size(3)=pm_exhaust;save_size(4)=nox_exhaust
save_str(1)='PM10';save_str(2)='PM25';save_str(3)='PMex';save_str(4)='NOX'
save_source(1)=total_dust_index;save_source(2)=total_dust_index;save_source(3)=exhaust_index;save_source(4)=NOX_emis_index
file_str='all'
if (index(calculation_type,'uEMEP_sand_salt').gt.0) then
!With sand and salt
n_x_save=6
save_size(5)=pm_10;save_size(6)=pm_10;save_size(7)=pm_25;save_size(8)=pm_25
save_str(5)='PM10_sand';save_str(6)='PM10_salt';save_str(7)='PM25_sand';save_str(8)='PM25_salt'
save_source(5)=sand_index;save_source(6)=salt_index(na);save_source(7)=sand_index;save_source(8)=salt_index(na)
file_str='all_sand_salt'
elseif (index(calculation_type,'uEMEP_sand').gt.0) then
!With sand
n_x_save=5
save_size(5)=pm_10;save_size(6)=pm_25;
save_str(5)='PM10_sand';save_str(6)='PM25_sand';
save_source(5)=sand_index;save_source(6)=sand_index;
file_str='all_sand'
elseif (index(calculation_type,'uEMEP_salt').gt.0) then
!With salt
n_x_save=5
save_size(5)=pm_10;save_size(6)=pm_25;
save_str(5)='PM10_salt';save_str(6)='PM25_salt'
save_source(5)=salt_index(na);save_source(6)=salt_index(na)
file_str='all_salt'
endif
unit_out=unit_save_emissions
!Declare functions
n_time_save=max_time_save-min_time_save+1
if (ro_tot.eq.1) then
write(unit_logfile,'(A)') '----------------------------------------------------------------'
write(unit_logfile,'(A)') 'Saving uEMEP emissions all to file'
write(unit_logfile,'(A)') '----------------------------------------------------------------'
!Set start and end date stamps
n_time_save=max_time_save-min_time_save+1
a_start=date_data(:,min_time_save)
a_end=date_data(:,max_time_save)
call date_to_datestr(a_start,uemep_date_format_str,uemep_start_date_str)
call date_to_datestr(a_end,uemep_date_format_str,uemep_end_date_str)
!Change the date stamp to follow NILU conventions
!call incrtm(-1,a_start(1),a_start(2),a_start(3),a_start(4))
if (.not.allocated(emis_sum)) then
allocate (emis_sum(n_x_save))
emis_sum=0.
endif
!Check that path exists after filling in date stamp
call date_to_datestr_bracket(a_start,path_output_emis,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
inquire(directory=trim(temp_name),exist=exists)
if (.not.exists) then
write(unit_logfile,'(A)')'ERROR: Path '//trim(temp_name)//' does not exist. No file saved'
return
endif
endif
!NORTRIP emissions are g/km/hour.
conversion=1.
!conversion=1.e6/1000./3600 !Convert to ug/s/m for use in EMEP (ug/s)
if (ro_tot.eq.1) then
!Calculate number of roads that will be saved
n_roads_total_save=0
if (use_single_road_loop_flag) then
do ro=1,n_roads_total
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
n_roads_total_save=n_roads_total_save+1
endif
enddo
else
do ro=1,n_roads
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
n_roads_total_save=n_roads_total_save+1
endif
enddo
endif
!Check the total emission data for PM10 and PM2.5 for NaNs and stops if it finds any before writing
do ro=n_roads_start,n_roads_end
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
do x_loop=1,2
x=save_size(x_loop)
do ti=min_time_save,max_time_save
if (isnan(sum(E_road_data(total_dust_index,x,E_total_index,ti,:,ro)))) then
write(unit_logfile,'(a)') ' ERROR: NaNs in uEMEP emission output data. Stopping before writing. Check input meteo data.'
stop 39
endif
enddo
enddo
endif
enddo
!Open the outputfile for date
!temp_name=trim(path_output_emis)//trim(filename_output_emis)//'_'//'all'//'_'//trim(uemep_start_date_str)//'-'//trim(uemep_end_date_str)//'.txt'
temp_name=trim(path_output_emis)//trim(filename_output_emis)//'_'//trim(file_str)//'_'//trim(uemep_start_date_str)//'.txt'
!Set the finished file name if it is to be used
if (trim(finished_file_append).ne.'') then
finished_filename=trim(path_output_emis)//trim(filename_output_emis)//'_'//trim(file_str)//'_'//trim(uemep_start_date_str)//'.'//trim(finished_file_append)
call date_to_datestr_bracket(a_start,finished_filename,finished_filename)
call date_to_datestr_bracket(a_start,finished_filename,finished_filename)
call date_to_datestr_bracket(a_start,finished_filename,finished_filename)
endif
!Put in date in path and filename if required
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
write(unit_logfile,'(a)') ' Filename= '//trim(temp_name)
open(unit_out,file=temp_name,status='replace')
write(unit_out,'(A)') '# NORTRIP emission output for uEMEP'
write(unit_out,'(A)') '# Hourly traffic non-exhaust and exhaust emission '
write(unit_out,'(A)') '# Number of compounds '
write(unit_out,'(i4)') n_x_save
write(unit_out,'(A)') '# Compound names '
write(unit_out,'(A,<n_x_save>A)') save_str(1:n_x_save)
write(unit_out,'(A)') '# Emission data unit'
write(unit_out,'(A)') '"g/km/hr"'
write(unit_out,'(A)') '# Emission data start and end date'
write(unit_out,'(A)') trim(uemep_start_date_str)//' '//trim(uemep_end_date_str)
write(unit_out,'(A)') '# Number of roads Number of hours'
write(unit_out,'(2i)') n_roads_total_save,n_time_save
write(unit_out,'(A)') '# RoadLinkID Emission'
emis_sum=0.
endif
!Note that if line_or_grid_data_flag(ro).eq.0 then this is a link without traffic data.
!Must be saved but with no information
do ro=n_roads_start,n_roads_end
!Length of road in km for calculating sum of emissions
if (length_road(ro).eq.0) then
length_road_km(ro)=sqrt((x_road(1,ro)-x_road(2,ro))**2+(y_road(1,ro)-y_road(2,ro))**2)/1000.
else
length_road_km(ro)=length_road(ro)/1000.
endif
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.-1.or.line_or_grid_data_flag(ro).eq.3) then
if (use_single_road_loop_flag) then
write(unit_out,'(i12)') road_ID(ro_tot)
else
write(unit_out,'(i12)') road_ID(ro)
endif
do x_loop=1,n_x_save
x=save_size(x_loop)
s=save_source(x_loop)
do ti=min_time_save,max_time_save
emis_road=0.
!If line_or_grid_data_flag(ro).eq.-1 then missing data and so set to 0
if (line_or_grid_data_flag(ro).eq.1.or.line_or_grid_data_flag(ro).eq.3) then
if (x.eq.pm_exhaust) then
emis_road=sum(E_road_data(exhaust_index,pm_25,E_total_index,ti,:,ro))*conversion
elseif (x.eq.nox_exhaust) then
emis_road=0.
if (available_airquality_data(NOX_emis_index)) then
emis_road=emis_road+airquality_data(NOX_emis_index,ti,ro)*conversion
elseif (NOX_EF_available.ne.0) then
do v=1,num_veh
emis_road=emis_road+traffic_data(N_v_index(v),ti,ro)*NOX_EF(v,ro)*conversion
enddo
endif
else
emis_road=sum(E_road_data(s,x,E_total_index,ti,:,ro))*conversion
endif
endif
!Save emissions when using single roads to get it in the right order
!Write them at end of calculation
emis_time(ti)=emis_road
emis_sum(x_loop)=emis_sum(x_loop)+emis_road*length_road_km(ro)
enddo
!Write the time values
write(unit_out,'(<n_time_save>es12.4)') emis_time(min_time_save:max_time_save)
enddo
endif
enddo
if (ro_tot.eq.n_roads_total) then
close (unit_out)
!Write the total emissions to the log file
do x_loop=1,n_x_save
x=save_size(x_loop)
s=save_source(x_loop)
!Conversion is for g to kg
write(unit_logfile,'(A,a,a,es12.2)') ' Total emissions of ',save_str(x_loop),' (kg) for all road links over this period = ',emis_sum(x_loop)*1.e-3*dt
enddo
endif
if (allocated(emis_sum).and.(ro_tot.eq.n_roads_total)) deallocate(emis_sum)
end subroutine NORTRIP_save_uemep_emissions_all
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine NORTRIP_save_uEMEP_grid_emissions
use NORTRIP_definitions
implicit none
integer :: unit_out=40
character(24) uemep_start_date_str
character(24) uemep_end_date_str
character(24) :: uemep_date_format_str='yyyymmddHH'
character(256) temp_name,filename_temp
logical exists
integer a_start(num_date_index),a_end(num_date_index)
real conversion
character(8) :: pm_str=''
real x_grid(2),y_grid(2)
!real f_grid(grid_dim(1),grid_dim(2),n_roads)
real f_grid(0:n_roads)
real length_road_km(0:n_roads)
integer unit_count
real, save :: emis_sum(num_size+2)
real emis_road
integer save_size(4)
integer x_loop,n_time_save
!Declare functions
real line_fraction_in_grid_func
!Declare quick or slow gridding
logical :: quick_grid=.false.
integer grid_start(2),grid_end(2)
integer grid_start_temp(2),grid_end_temp(2)
real x_temp(3),y_temp(3)
logical grid_inside
!return
if (index(calculation_type,'gridded').gt.0) then
quick_grid=.true.
endif
!Always true
quick_grid=.true.
unit_out=unit_save_grid_emissions
save_size(1)=pm_10;save_size(2)=pm_25;save_size(3)=pm_exhaust;save_size(4)=nox_exhaust
if (ro_tot.eq.1) then
!write(*,*) grid_dim(1),grid_dim(2),n_roads
!stop
if (.not.allocated(emis_grid)) allocate (emis_grid(grid_dim(1),grid_dim(2),num_size+2,n_time))
emis_grid=0.
!Open log file
!if (unit_logfile.gt.0) then
! open(unit_logfile,file=filename_log,status='old',position='append')
!endif
write(unit_logfile,'(A)') '----------------------------------------------------------------'
write(unit_logfile,'(A)') 'Saving uEMEP gridded emissions to file'
write(unit_logfile,'(A)') '----------------------------------------------------------------'
!Set start and end date stamps
a_start=date_data(:,min_time_save)
a_end=date_data(:,max_time_save)
!Chnage the date stamp to follow NILU conventions
!call incrtm(-1,a_start(1),a_start(2),a_start(3),a_start(4))
!Check that path exists after filling in date stamp
call date_to_datestr_bracket(a_start,path_output_emis,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
inquire(directory=trim(temp_name),exist=exists)
if (.not.exists) then
write(unit_logfile,'(A)')'ERROR: Path '//trim(temp_name)//' does not exist. No file saved'
return
endif
endif
!Convert from g/km/hr g/m/s
!conversion=1./1000./3600.
conversion=1.
!This error trap indicates if NaN values in the grid but will not fix the problem which will propogate through the init files
do ro=n_roads_start,n_roads_end
!Length of road in km
if (length_road(ro).eq.0) then
length_road_km(ro)=sqrt((x_road(1,ro)-x_road(2,ro))**2+(y_road(1,ro)-y_road(2,ro))**2)/1000.
else
length_road_km(ro)=length_road(ro)/1000.
endif
!Check data for NaN
do ti=min_time_save,max_time_save
do tr=1,num_track
if (isnan(E_road_data(total_dust_index,pm_10,E_total_index,ti,tr,ro)).or.isnan(E_road_data(total_dust_index,pm_25,E_total_index,ti,tr,ro))) then
!E_road_data(total_dust_index,x,E_total_index,ti,tr,ro))=0.
write(unit_logfile,'(A,2i12,A,i12,A,A)')'WARNING: NaN found for road link index/ID ',ro,road_ID(ro),' at time index ',ti,' and time ',trim(date_str(3,ti))
stop
endif
if (E_road_data(total_dust_index,pm_10,E_total_index,ti,tr,ro).lt.0.or.E_road_data(total_dust_index,pm_25,E_total_index,ti,tr,ro).lt.0) then
!E_road_data(total_dust_index,x,E_total_index,ti,tr,ro))=0.
write(unit_logfile,'(A,2i12,A,i12,A,A)')'WARNING: Negative emission found for road link index/ID ',ro,road_ID(ro),' at time index ',ti,' and time ',trim(date_str(3,ti))
stop
endif
enddo
enddo
enddo
!Calculate the emissions in each grid
!write(*,*) grid_dim,grid_0,grid_delta
!This assumes that we are using the one road at a time option. Whole thing should be built better
ro=0
if (quick_grid) then
!Assumed gridding onto a lat lon grid and use just the lat lon road centre position to make it general for all calculation types
x_temp(3)=LON(ro)
y_temp(3)=LAT(ro)
x_temp(1)=lon_road(1,ro)
x_temp(2)=lon_road(2,ro)
y_temp(1)=lat_road(1,ro)
y_temp(2)=lat_road(2,ro)
!line_fraction_in_grid_func does not work for 0 length, so perturb slightly. Use the (3) index to keep the central position of the line
!x_temp(2)=x_temp(3)+grid_delta(1)/10000
! y_temp(2)=y_temp(3)+grid_delta(2)/10000
!x_temp(1)=x_temp(3)-grid_delta(1)/10000
!y_temp(1)=y_temp(3)-grid_delta(2)/10000
!x_temp=x_road(:,ro)
!y_tempy_road(:,ro)
grid_start_temp(1)=floor((x_temp(1)-grid_0(1))/grid_delta(1))+1
grid_start_temp(2)=floor((y_temp(1)-grid_0(2))/grid_delta(2))+1
grid_end_temp(1)=floor((x_temp(2)-grid_0(1))/grid_delta(1))+1
grid_end_temp(2)=floor((y_temp(2)-grid_0(2))/grid_delta(2))+1
grid_start(1)=min(grid_start_temp(1),grid_end_temp(1))
grid_end(1)=max(grid_start_temp(1),grid_end_temp(1))
grid_start(2)=min(grid_start_temp(2),grid_end_temp(2))
grid_end(2)=max(grid_start_temp(2),grid_end_temp(2))
if (grid_start(1).lt.1.or.grid_start(1).gt.grid_dim(1)) write(unit_logfile,'(A,3i)') 'Warning: Road start outside of X grid. Not using (ro,grid_end,grid_dim) ',ro,grid_start(1),grid_dim(1)
if (grid_start(2).lt.1.or.grid_start(2).gt.grid_dim(2)) write(unit_logfile,'(A,3i)') 'Warning: Road start outside of Y grid. Not using (ro,grid_end,grid_dim) ',ro,grid_start(2),grid_dim(2)
if (grid_end(1).lt.1.or.grid_end(1).gt.grid_dim(1)) write(unit_logfile,'(A,3i)') 'Warning: Road end outside of x grid. Not using (ro,grid_end,grid_dim) ',ro,grid_end(1),grid_dim(1)
if (grid_end(2).lt.1.or.grid_end(2).gt.grid_dim(2)) write(unit_logfile,'(A,3i)') 'Warning: Road end outside of Y grid. Not using (ro,grid_end,grid_dim) ',ro,grid_end(2),grid_dim(2)
grid_inside=.false.
if (grid_start(1).ge.1.and.grid_start(1).le.grid_dim(1).and.grid_start(2).ge.1.and.grid_start(2).le.grid_dim(2).and.grid_end(1).ge.1.and.grid_end(1).le.grid_dim(1).and.grid_end(2).ge.1.and.grid_end(2).le.grid_dim(2)) then
grid_inside=.true.
endif
!grid_start(1)=max(1,min(grid_start(1),grid_dim(1)))
!grid_start(2)=max(1,min(grid_start(2),grid_dim(2)))
!grid_end(1)=max(1,min(grid_end(1),grid_dim(1)))
!grid_end(2)=max(1,min(grid_end(2),grid_dim(2)))
else
grid_start=1
grid_end=grid_dim
endif
if (grid_inside) then
do j=grid_start(2),grid_end(2)
do i=grid_start(1),grid_end(1)
x_grid(1)=grid_0(1)+grid_delta(1)*(i-1)
x_grid(2)=grid_0(1)+grid_delta(1)*i
y_grid(1)=grid_0(2)+grid_delta(2)*(j-1)
y_grid(2)=grid_0(2)+grid_delta(2)*j
do ro=n_roads_start,n_roads_end
!if (adt_road(ro).ge.grid_adt_cutoff(1).and.adt_road(ro).lt.grid_adt_cutoff(2)) then
!if (line_or_grid_data_flag(ro).ne.3) write(*,*) line_or_grid_data_flag(ro)
if (line_or_grid_data_flag(ro).eq.2.or.line_or_grid_data_flag(ro).eq.3) then
!Check this logic!!!!
!if (line_or_grid_data_flag(ro).ge.1) then
!f_grid(ro)=line_fraction_in_grid_func(x_grid,y_grid,x_road(:,ro),y_road(:,ro))
f_grid(ro)=line_fraction_in_grid_func(x_grid,y_grid,x_temp(1:2),y_temp(1:2))
!if (f_grid(ro).lt.1.0.and.f_grid(ro).gt.0.0) write(*,*) ro,f_grid(ro)
else
f_grid(ro)=0.
endif
enddo
do x_loop=1,4
x=save_size(x_loop)
do ti=min_time_save,max_time_save
do ro=n_roads_start,n_roads_end
if (x.eq.pm_exhaust) then
emis_road=sum(E_road_data(exhaust_index,pm_25,E_total_index,ti,:,ro))
elseif (x.eq.nox_exhaust) then
emis_road=0.
if (available_airquality_data(NOX_emis_index)) then
emis_road=emis_road+airquality_data(NOX_emis_index,ti,ro)*conversion
elseif (NOX_EF_available.ne.0) then
do v=1,num_veh
emis_road=emis_road+traffic_data(N_v_index(v),ti,ro)*NOX_EF(v,ro)*conversion
enddo
else
emis_road=0.
endif
else
emis_road=sum(E_road_data(total_dust_index,x,E_total_index,ti,:,ro))
endif
emis_grid(i,j,x,ti)=emis_grid(i,j,x,ti)+length_road_km(ro)*f_grid(ro)*emis_road*conversion
enddo
!emis_grid(i,j,x,ti)=sum(length_road(:)*f_grid(:)*sum(E_road_data(total_dust_index,x,E_total_index,ti,:,:),1))
!write(*,*) length_road(ro),f_grid(ro),E_road_data(total_dust_index,x,E_total_index,ti,tr,ro),conversion
enddo
enddo
enddo
enddo
endif
!Save if the last road
if (ro_tot.eq.n_roads_total) then
unit_count=0
!Set date text in file
n_time_save=max_time_save-min_time_save+1
call date_to_datestr(a_start,uemep_date_format_str,uemep_start_date_str)
call date_to_datestr(a_end,uemep_date_format_str,uemep_end_date_str)
!Set the finished filename, but only if the emissions flag is false since this also saves a finished file.
if (trim(finished_file_append).ne.''.and..not.NORTRIP_save_uEMEP_emissions_flag) then
finished_filename=trim(path_output_emis)//trim(filename_output_grid_emis)//'_'//trim(uemep_start_date_str)//'.'//trim(finished_file_append)
call date_to_datestr_bracket(a_start,finished_filename,finished_filename)
call date_to_datestr_bracket(a_start,finished_filename,finished_filename)
call date_to_datestr_bracket(a_start,finished_filename,finished_filename)
endif
do x_loop=1,4
x=save_size(x_loop)
unit_count=x_loop
unit_out=unit_save_grid_emissions+unit_count
if (x.eq.pm_25) pm_str='PM25'
if (x.eq.pm_10) pm_str='PM10'
if (x.eq.pm_exhaust) pm_str='EP'
if (x.eq.nox_exhaust) pm_str='NOX'
!Open the outputfile for date
temp_name=trim(path_output_emis)//trim(filename_output_grid_emis)//'_'//trim(pm_str)//'_'//trim(uemep_start_date_str)//'-'//trim(uemep_end_date_str)//'.txt'
!Put in date in path and filename if required
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
call date_to_datestr_bracket(a_start,temp_name,temp_name)
write(unit_logfile,'(a)') ' Filename= '//trim(temp_name)
open(unit_out,file=temp_name,status='replace')
write(unit_out,'(A)') '# NORTRIP grid emission output for uEMEP. Ordered visually with max y position at top'
if (x.eq.pm_25.or.x.eq.pm_10) then
if (available_airquality_data(EP_emis_index).or.exhaust_EF_available.ne.0) then
write(unit_out,'(A,A)') '# Hourly traffic non-exhaust and exhaust emission for compound: ',trim(pm_str)
else
write(unit_out,'(A,A)') '# Hourly traffic non-exhaust only emission for compound: ',trim(pm_str)
endif
endif
if (x.eq.pm_exhaust) then
if (available_airquality_data(EP_emis_index).or.exhaust_EF_available.ne.0) then
write(unit_out,'(A,A)') '# Hourly traffic exhaust emission for compound: ',trim(pm_str)
else
write(unit_out,'(A)') '# No exhaust emission data available'
endif
endif
if (x.eq.nox_exhaust) then
if (available_airquality_data(NOX_emis_index).or.NOX_EF_available.ne.0) then
write(unit_out,'(A,A)') '# Hourly traffic exhaust emission for compound: ',trim(pm_str)
else
write(unit_out,'(A)') '# No exhaust emission data available'
endif
endif
write(unit_out,'(A)') '# Emission data unit'
write(unit_out,'(A)') 'g/hr'
write(unit_out,'(A)') '# Emission data start and end date'
write(unit_out,'(A)') trim(uemep_start_date_str)//' '//trim(uemep_end_date_str)
write(unit_out,'(A)') '# Number of grids (i,j) Number of hours'
write(unit_out,'(3i)') grid_dim(1),grid_dim(2),n_time_save
write(unit_out,'(A)') '# Lower left center (x,y) grid position (m)'
write(unit_out,'(2f12.2)') grid_0(1),grid_0(2)
write(unit_out,'(A)') '# Grid spacing (x,y) (m)'
write(unit_out,'(2f12.2)') grid_delta(1),grid_delta(2)
emis_sum(x)=0.
!Save the emission data in time
do ti=min_time_save,max_time_save
write(unit_out,'(i8)') ti
do j=grid_dim(2),1,-1
write(unit_out,'(<grid_dim(1)>es12.3)') (emis_grid(i,j,x,ti),i=1,grid_dim(1))
do i=1,grid_dim(1)
emis_sum(x)=emis_sum(x)+emis_grid(i,j,x,ti)
enddo
enddo
enddo
close (unit_out)
write(unit_logfile,'(A,a,a,es12.2)') ' Total emissions of ',trim(pm_str),' (kg) for all grids over this period = ',emis_sum(x)*1.e-3*dt
enddo
endif
end subroutine NORTRIP_save_uEMEP_grid_emissions
!----------------------------------------------------------------------