PartMC  2.6.1
extract_aero_time.F90
Go to the documentation of this file.
1 ! Copyright (C) 2009-2012 Matthew West
2 ! Licensed under the GNU General Public License version 2 or (at your
3 ! option) any later version. See the file COPYING for details.
4 
5 !> \file
6 !> The extract_aero_time program.
7 
8 !> Read NetCDF output files and write out the time evolution of
9 !> aerosol number and mass concentrations in text format.
11 
12  use pmc_aero_state
14  use pmc_output
15  use pmc_mpi
16  use getopt_m
17 
18  character(len=PMC_MAX_FILENAME_LEN) :: in_prefix, out_filename
19  character(len=PMC_MAX_FILENAME_LEN), allocatable :: filename_list(:)
20  character(len=1000) :: tmp_str
21  type(aero_data_t) :: aero_data
22  type(aero_state_t) :: aero_state
23  integer :: index, i_repeat, i_spec, out_unit
24  integer :: i_file, n_file
25  real(kind=dp) :: time, del_t
26  character(len=PMC_UUID_LEN) :: uuid, run_uuid
27  real(kind=dp), allocatable :: particle_num_concs(:), particle_masses(:)
28  real(kind=dp), allocatable :: times(:), time_num_concs(:), time_mass_concs(:)
29  real(kind=dp), allocatable :: time_species_concs(:,:)
30  type(option_s) :: opts(2)
31 
32  call pmc_mpi_init()
33 
34  opts(1) = option_s("help", .false., 'h')
35  opts(2) = option_s("output", .true., 'o')
36 
37  out_filename = ""
38 
39  do
40  select case(getopt("ho:", opts))
41  case(char(0))
42  exit
43  case('h')
44  call print_help()
45  stop
46  case('o')
47  out_filename = optarg
48  case( '?' )
49  call print_help()
50  call die_msg(514364550, 'unknown option: ' // trim(optopt))
51  case default
52  call print_help()
53  call die_msg(603100341, 'unhandled option: ' // trim(optopt))
54  end select
55  end do
56 
57  if (optind /= command_argument_count()) then
58  call print_help()
59  call die_msg(967032896, 'expected exactly one non-option prefix argument')
60  end if
61 
62  call get_command_argument(optind, in_prefix)
63 
64  if (out_filename == "") then
65  out_filename = trim(in_prefix) // "_aero_time.txt"
66  end if
67 
68  allocate(filename_list(0))
69  call input_filename_list(in_prefix, filename_list)
70  n_file = size(filename_list)
71  call assert_msg(323514871, n_file > 0, &
72  "no NetCDF files found with prefix: " // trim(in_prefix))
73 
74  call input_state(filename_list(1), index, time, del_t, i_repeat, uuid, &
75  aero_data=aero_data, aero_state=aero_state)
76  run_uuid = uuid
77 
78  allocate(times(n_file))
79  allocate(time_num_concs(n_file))
80  allocate(time_mass_concs(n_file))
81  allocate(time_species_concs(n_file, aero_data_n_spec(aero_data)))
82 
83  do i_file = 1,n_file
84  call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
85  uuid, aero_data=aero_data, aero_state=aero_state)
86 
87  call assert_msg(397906326, uuid == run_uuid, &
88  "UUID mismatch between " // trim(filename_list(1)) // " and " &
89  // trim(filename_list(i_file)))
90 
91  times(i_file) = time
92  particle_num_concs = aero_state_num_concs(aero_state, aero_data)
93  time_num_concs(i_file) = sum(particle_num_concs)
94  particle_masses = aero_state_masses(aero_state, aero_data)
95  time_mass_concs(i_file) = sum(particle_masses * particle_num_concs)
96  do i_spec = 1,aero_data_n_spec(aero_data)
97  particle_masses = aero_state_masses(aero_state, aero_data, &
98  include=(/aero_data%name(i_spec)/))
99  time_species_concs(i_file, i_spec) &
100  = sum(particle_masses * particle_num_concs)
101  end do
102  end do
103 
104  write(*,'(a,a)') "Output file: ", trim(out_filename)
105  write(*,'(a)') " Each row of output is one time."
106  write(*,'(a)') " The columns of output are:"
107  write(*,'(a)') " column 1: time (s)"
108  write(*,'(a)') " column 2: aerosol number concentration (#/m^3)"
109  write(*,'(a)') " column 3: aerosol mass concentration (kg/m^3)"
110  do i_spec = 1,aero_data_n_spec(aero_data)
111  write(*,'(a,i2,a,a,a)') " column ", i_spec + 3, ": aerosol ", &
112  trim(aero_data%name(i_spec)), " concentration (kg/m^3)"
113  end do
114 
115  call open_file_write(out_filename, out_unit)
116  do i_file = 1,n_file
117  write(out_unit, '(e30.15e3)', advance='no') times(i_file)
118  write(out_unit, '(e30.15e3)', advance='no') time_num_concs(i_file)
119  write(out_unit, '(e30.15e3)', advance='no') time_mass_concs(i_file)
120  do i_spec = 1,aero_data_n_spec(aero_data)
121  write(out_unit, '(e30.15e3)', advance='no') &
122  time_species_concs(i_file, i_spec)
123  end do
124  write(out_unit, '(a)') ''
125  end do
126  call close_file(out_unit)
127 
128  deallocate(times)
129  deallocate(time_num_concs)
130  deallocate(time_mass_concs)
131  deallocate(time_species_concs)
132  deallocate(filename_list)
133 
134  call pmc_mpi_finalize()
135 
136 contains
137 
138  subroutine print_help()
139 
140  write(*,'(a)') 'Usage: extract_aero_time [options] <netcdf_prefix>'
141  write(*,'(a)') ''
142  write(*,'(a)') 'options are:'
143  write(*,'(a)') ' -h, --help Print this help message.'
144  write(*,'(a)') ' -o, --out <file> Output filename.'
145  write(*,'(a)') ''
146  write(*,'(a)') 'Examples:'
147  write(*,'(a)') ' extract_aero_time data_0001'
148  write(*,'(a)') ''
149 
150  end subroutine print_help
151 
152 end program extract_aero_time
pmc_mpi::pmc_mpi_init
subroutine pmc_mpi_init()
Initialize MPI.
Definition: mpi.F90:56
pmc_aero_data::aero_data_n_spec
elemental integer function aero_data_n_spec(aero_data)
Return the number of aerosol species, or -1 if uninitialized.
Definition: aero_data.F90:236
pmc_mpi
Wrapper functions for MPI.
Definition: mpi.F90:13
pmc_output::input_state
subroutine input_state(filename, index, time, del_t, i_repeat, uuid, aero_data, aero_state, gas_data, gas_state, env_state)
Read the current state.
Definition: output.F90:498
pmc_aero_particle
The aero_particle_t structure and associated subroutines.
Definition: aero_particle.F90:9
pmc_util::open_file_write
subroutine open_file_write(filename, unit)
Open a file for writing with an automatically assigned unit and test that it succeeds....
Definition: util.F90:206
getopt_m::getopt
character function getopt(optstring, longopts)
Definition: getopt.F90:132
pmc_util::die_msg
subroutine die_msg(code, error_msg)
Error immediately.
Definition: util.F90:134
pmc_constants::dp
integer, parameter dp
Kind of a double precision real number.
Definition: constants.F90:12
getopt_m::optarg
character(len=80) optarg
Definition: getopt.F90:92
extract_aero_time
program extract_aero_time
Read NetCDF output files and write out the time evolution of aerosol number and mass concentrations i...
Definition: extract_aero_time.F90:10
pmc_aero_state
The aero_state_t structure and assocated subroutines.
Definition: aero_state.F90:9
pmc_mpi::pmc_mpi_finalize
subroutine pmc_mpi_finalize()
Shut down MPI.
Definition: mpi.F90:89
getopt_m::option_s
Definition: getopt.F90:97
pmc_util::assert_msg
subroutine assert_msg(code, condition_ok, error_msg)
Errors unless condition_ok is true.
Definition: util.F90:77
getopt_m
Definition: getopt.F90:90
pmc_aero_state::aero_state_num_concs
real(kind=dp) function, dimension(aero_state_n_part(aero_state)) aero_state_num_concs(aero_state, aero_data)
Returns the number concentrations of all particles.
Definition: aero_state.F90:1190
pmc_aero_data::aero_data_t
Aerosol material properties and associated data.
Definition: aero_data.F90:49
pmc_output
Write data in NetCDF format.
Definition: output.F90:68
getopt_m::optind
integer optind
Definition: getopt.F90:94
pmc_aero_state::aero_state_masses
real(kind=dp) function, dimension(aero_state_n_part(aero_state)) aero_state_masses(aero_state, aero_data, include, exclude)
Returns the masses of all particles.
Definition: aero_state.F90:1133
getopt_m::optopt
character optopt
Definition: getopt.F90:93
pmc_util::close_file
subroutine close_file(unit)
Close a file and de-assign the unit.
Definition: util.F90:226
print_help
subroutine print_help()
Definition: extract_aero_particles.F90:109
pmc_aero_state::aero_state_t
The current collection of aerosol particles.
Definition: aero_state.F90:63
pmc_output::input_filename_list
subroutine input_filename_list(prefix, filename_list)
Find all NetCDF (.nc) filenames that match the given prefix.
Definition: output.F90:568