PartMC  2.6.1
extract_aero_particles.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_particles program.
7 
8 !> Read NetCDF output files and write out the individual particle
9 !> masses.
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_filename, out_filename
19  type(aero_data_t) :: aero_data
20  type(aero_state_t) :: aero_state
21  integer :: index, i_repeat, i_part, i_spec, out_unit, i_char
22  real(kind=dp) :: time, del_t
23  character(len=PMC_UUID_LEN) :: uuid
24  type(option_s) :: opts(2)
25 
26  call pmc_mpi_init()
27 
28  opts(1) = option_s("help", .false., 'h')
29  opts(2) = option_s("output", .true., 'o')
30 
31  out_filename = ""
32 
33  do
34  select case(getopt("ho:", opts))
35  case(char(0))
36  exit
37  case('h')
38  call print_help()
39  stop
40  case('o')
41  out_filename = optarg
42  case( '?' )
43  call print_help()
44  call die_msg(885067714, 'unknown option: ' // trim(optopt))
45  case default
46  call print_help()
47  call die_msg(516704561, 'unhandled option: ' // trim(optopt))
48  end select
49  end do
50 
51  if (optind /= command_argument_count()) then
52  call print_help()
53  call die_msg(605963222, &
54  'expected exactly one non-option filename argument')
55  end if
56 
57  call get_command_argument(optind, in_filename)
58 
59  if (out_filename == "") then
60  i_char = scan(in_filename, '.', back=.true.)
61  if (i_char == 0) then
62  out_filename = trim(in_filename) // "_aero_particles.txt"
63  else
64  out_filename = trim(in_filename(1:(i_char - 1))) &
65  // "_aero_particles.txt"
66  end if
67  end if
68 
69  call input_state(in_filename, index, time, del_t, i_repeat, uuid, &
70  aero_data=aero_data, aero_state=aero_state)
71 
72  write(*,'(a)') "Output file: " // trim(out_filename)
73  write(*,'(a)') " Output data is for time = " &
74  // trim(real_to_string(time)) // " (s)"
75  write(*,'(a)') " Each row of output is one particle."
76  write(*,'(a)') " The columns of output are:"
77  write(*,'(a)') " column 1: particle ID number"
78  write(*,'(a)') " column 2: number concentration (m^{-3})"
79  write(*,'(a)') " column 3: particle diameter (m)"
80  write(*,'(a)') " column 4: particle total mass (kg)"
81  do i_spec = 1,aero_data_n_spec(aero_data)
82  write(*,'(a,i2,a,a,a,e10.4,a)') ' column ', i_spec + 4, ': ', &
83  trim(aero_data%name(i_spec)), ' mass (kg) - density = ', &
84  aero_data%density(i_spec), ' (kg/m^3)'
85  end do
86 
87  call open_file_write(out_filename, out_unit)
88  do i_part = 1,aero_state_n_part(aero_state)
89  write(out_unit, '(i15,e30.15e3,e30.15e3,e30.15e3)', advance='no') &
90  aero_state%apa%particle(i_part)%id, &
91  aero_state_particle_num_conc(aero_state, &
92  aero_state%apa%particle(i_part), aero_data), &
93  aero_particle_diameter(aero_state%apa%particle(i_part), aero_data), &
94  aero_particle_mass(aero_state%apa%particle(i_part), aero_data)
95  do i_spec = 1,aero_data_n_spec(aero_data)
96  write(out_unit, '(e30.15e3)', advance='no') &
97  aero_particle_species_mass(aero_state%apa%particle(i_part), &
98  i_spec, aero_data)
99  end do
100  write(out_unit, *) ''
101  end do
102  call close_file(out_unit)
103 
104  call pmc_mpi_finalize()
105 
106 contains
107 
108  subroutine print_help()
109 
110  write(*,'(a)') 'Usage: extract_aero_particles [options] <netcdf_prefix>'
111  write(*,'(a)') ''
112  write(*,'(a)') 'options are:'
113  write(*,'(a)') ' -h, --help Print this help message.'
114  write(*,'(a)') ' -o, --out <file> Output filename.'
115  write(*,'(a)') ''
116  write(*,'(a)') 'Examples:'
117  write(*,'(a)') ' extract_aero_particles data_0001_00000001.nc'
118  write(*,'(a)') ''
119 
120  end subroutine print_help
121 
122 end program extract_aero_particles
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_aero_state::aero_state_n_part
elemental integer function aero_state_n_part(aero_state)
Return the current number of particles.
Definition: aero_state.F90:88
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
pmc_aero_particle::aero_particle_diameter
elemental real(kind=dp) function aero_particle_diameter(aero_particle, aero_data)
Total diameter of the particle (m).
Definition: aero_particle.F90:371
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
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::real_to_string
character(len=pmc_util_convert_string_len) function real_to_string(val)
Convert a real to a string format.
Definition: util.F90:782
getopt_m
Definition: getopt.F90:90
pmc_aero_state::aero_state_particle_num_conc
real(kind=dp) function aero_state_particle_num_conc(aero_state, aero_particle, aero_data)
The number concentration of a single particle (m^{-3}).
Definition: aero_state.F90:511
extract_aero_particles
program extract_aero_particles
Read NetCDF output files and write out the individual particle masses.
Definition: extract_aero_particles.F90:10
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_particle::aero_particle_species_mass
elemental real(kind=dp) function aero_particle_species_mass(aero_particle, i_spec, aero_data)
Mass of a single species in the particle (kg).
Definition: aero_particle.F90:217
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_aero_particle::aero_particle_mass
elemental real(kind=dp) function aero_particle_mass(aero_particle, aero_data)
Total mass of the particle (kg).
Definition: aero_particle.F90:202