subroutine uEMEP_read_roadlink_emission_data !Reads in NORTRIP formatted emission data and places it in the correct road links use uEMEP_definitions implicit none integer i,j integer unit_in logical :: exists logical nxtdat_flag integer counter real, allocatable :: inputdata_rl_temp(:) integer, allocatable :: inputdata_int_rl_id(:) integer emission_date_array(6) integer n_roadlink_emission_compound character(16) n_roadlink_emission_compound_str(10) character(256) n_roadlink_emission_unit_str character(256) n_roadlink_emission_date_str integer n_roadlink_emission,n_roadlink_emission_time integer time_index_temp,t_match_index,t integer date_array_temp(6) integer n_roadlink_emission_selected character(256) format_temp character(len=32) :: fmt double precision emission_date_number_start,emission_date_number write(unit_logfile,'(A)') '' write(unit_logfile,'(A)') '================================================================' write(unit_logfile,'(A)') 'Reading road link emission data (uEMEP_read_roadlink_emission_data)' write(unit_logfile,'(A)') '================================================================' pathfilename_rl(2)=trim(pathname_rl(2))//trim(filename_rl(2)) !write(*,*) pathname_rl(2),filename_rl(2),pathfilename_rl(2) !Test existence of the road link filename (2). If does not exist then use default inquire(file=trim(pathfilename_rl(2)),exist=exists) if (.not.exists) then write(unit_logfile,'(A,A)') ' ERROR: Road link emission file does not exist: ', trim(pathfilename_rl(2)) stop endif !Open the file for reading unit_in=20 open(unit_in,file=pathfilename_rl(2),access='sequential',status='old',readonly) write(unit_logfile,'(a)') ' Opening road link file(ascii) '//trim(pathfilename_rl(2)) rewind(unit_in) call nxtdat(unit_in, nxtdat_flag) !read the header to find out how many links there are !read(unit_in,'(a)',ERR=20) temp_str read(unit_in,*,ERR=20) n_roadlink_emission_compound write(unit_logfile,'(a,i)') ' Number of road link emission compounds= ', n_roadlink_emission_compound call nxtdat(unit_in, nxtdat_flag) read(unit_in,*,ERR=20) n_roadlink_emission_compound_str(1:n_roadlink_emission_compound) write(fmt,'(a,i0,a)') '(a,', n_roadlink_emission_compound, 'a16)' write(unit_logfile,fmt) ' Road link emission compounds= ', n_roadlink_emission_compound_str(1:n_roadlink_emission_compound) call nxtdat(unit_in, nxtdat_flag) read(unit_in,*,ERR=20) n_roadlink_emission_unit_str write(unit_logfile,'(a,a)') ' Road link emission units= ', trim(n_roadlink_emission_unit_str) call nxtdat(unit_in, nxtdat_flag) read(unit_in,*,ERR=20) n_roadlink_emission_date_str write(unit_logfile,'(a,a)') ' Road link emission start date= ', trim(n_roadlink_emission_date_str) call nxtdat(unit_in, nxtdat_flag) read(unit_in,*,ERR=20) n_roadlink_emission,n_roadlink_emission_time write(unit_logfile,'(a,i)') ' Number of road links= ', n_roadlink_emission write(unit_logfile,'(a,i)') ' Number of time steps= ', n_roadlink_emission_time if (n_roadlink_emission.ne.n_roadlinks_major) then write(unit_logfile,'(A,2i12)') 'ERROR: Number of emission road links is not the same as the static road links: ',n_roadlink_emission,n_roadlinks_major stop endif !Check that start time and end time are covered in the emission data before progressing further format_temp='yyyymmddHH' call datestr_to_date(n_roadlink_emission_date_str,format_temp,emission_date_array) if (use_single_time_loop_flag) then time_index_temp=end_time_loop_index else time_index_temp=subgrid_dim(t_dim_index) endif t_match_index=0 !write(*,*) shape(val_dim_nc) !do t=1,time_index_temp ! call number_to_date(val_dim_nc(t,time_dim_nc_index),date_array_temp,ref_year_EMEP) ! if (date_array_temp(1).eq.emission_date_array(1).and.date_array_temp(2).eq.emission_date_array(2).and. & ! date_array_temp(3).eq.emission_date_array(3).and.date_array_temp(4).eq.emission_date_array(4)) then ! t_match_index=t ! endif ! write(*,'(4i)') date_array_temp(1:4) !enddo emission_date_number_start=date_to_number(emission_date_array,ref_year_EMEP) call number_to_date(val_dim_nc(1,time_dim_nc_index),date_array_temp,ref_year_EMEP) do t=1,n_roadlink_emission_time emission_date_number=emission_date_number_start+(t-1)/24. call number_to_date(emission_date_number,emission_date_array,ref_year_EMEP) if (date_array_temp(1).eq.emission_date_array(1).and.date_array_temp(2).eq.emission_date_array(2).and. & date_array_temp(3).eq.emission_date_array(3).and.date_array_temp(4).eq.emission_date_array(4)) then t_match_index=t endif !write(*,'(4i)') emission_date_array(1:4) enddo if (t_match_index.eq.0) then write(unit_logfile,'(A,6i6)') 'ERROR: No starting date found in road emission data: ',emission_date_array stop else write(unit_logfile,'(A,6i6)') ' Road link emission starting date found. Index: ',t_match_index write(unit_logfile,'(A,6i6)') ' Road link emission starting date found. Index: ',t_match_index endif if (n_roadlink_emission_time-t_match_index+1.lt.time_index_temp) then write(unit_logfile,'(A,2i6)') 'ERROR: Not enough time data in road link emission files: ',n_roadlink_emission_time-t_match_index+1,time_index_temp stop else write(unit_logfile,'(A,6i6)') ' Road link emission ending date found. Index: ',t_match_index+time_index_temp-1 endif !endif !Allocate the arrays after reading in the number of roads n_roadlink_emission_selected=n_roadlink_emission if (reduce_roadlink_region_flag) then n_roadlink_emission_selected=n_roadlinks_major_selected endif allocate (inputdata_rl_emissions(n_roadlink_emission_selected,n_roadlink_emission_time,n_roadlink_emission_compound)) allocate (inputdata_rl_temp(n_roadlink_emission_time)) allocate (inputdata_int_rl_id(n_roadlink_emission_selected)) !Initialise inputdata_rl_temp=0. counter=0 !Read the data call nxtdat(unit_in, nxtdat_flag) do i=1,n_roadlink_emission if (valid_link_flag(i)) then counter=counter+1 read(unit_in,*,ERR=20) inputdata_int_rl_id(counter) !write(*,*) i,inputdata_int_rl_id(i) do j=1,n_roadlink_emission_compound read(unit_in,*) inputdata_rl_temp(1:n_roadlink_emission_time) if (j.le.n_pollutant_loop) then inputdata_rl_emissions(counter,1:time_index_temp,j)=inputdata_rl_temp(t_match_index:t_match_index+time_index_temp-1) endif enddo !write(*,*) counter,inputdata_rl_emissions(counter,10,1),inputdata_rl_emissions(counter,10,2) else read(unit_in,*,ERR=20) do j=1,n_roadlink_emission_compound read(unit_in,*) enddo endif enddo write(unit_logfile,'(a,i)') ' Number of road links that should be read = ', n_roadlink_emission_selected write(unit_logfile,'(a,i)') ' Number of road links read = ', counter close(unit_in,status='keep') !Check that road link ID's match do i=1,n_roadlinks if (inputdata_int_rl(i,id_rl_index).ne.inputdata_int_rl_id(inputdata_int_rl(i,major_index_rl_index))) then write(unit_logfile,'(A,3i12)') 'ERROR: Mismatch of road link IDs in the emission files: ',i,inputdata_int_rl(i,id_rl_index),inputdata_int_rl_id(i) stop endif enddo if (allocated(inputdata_rl_temp)) deallocate(inputdata_rl_temp) if (allocated(inputdata_int_rl_id)) deallocate(inputdata_int_rl_id) return 20 write(unit_logfile,'(2A)') 'ERROR reading road link emission file: ',trim(pathfilename_rl(2)) stop end subroutine uEMEP_read_roadlink_emission_data