PartMC  2.3.0
extract_aero_particles.F90
Go to the documentation of this file.
1 ! Copyright (C) 2009-2011 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(aero_particle_t), pointer :: aero_particle
25  type(option_s) :: opts(2)
26 
27  call pmc_mpi_init()
28 
29  opts(1) = option_s("help", .false., 'h')
30  opts(2) = option_s("output", .true., 'o')
31 
32  out_filename = ""
33 
34  do
35  select case(getopt("ho:", opts))
36  case(char(0))
37  exit
38  case('h')
39  call print_help()
40  stop
41  case('o')
42  out_filename = optarg
43  case( '?' )
44  call print_help()
45  call die_msg(885067714, 'unknown option: ' // trim(optopt))
46  case default
47  call print_help()
48  call die_msg(516704561, 'unhandled option: ' // trim(optopt))
49  end select
50  end do
51 
52  if (optind /= command_argument_count()) then
53  call print_help()
54  call die_msg(605963222, &
55  'expected exactly one non-option filename argument')
56  end if
57 
58  call get_command_argument(optind, in_filename)
59 
60  if (out_filename == "") then
61  i_char = scan(in_filename, '.', back=.true.)
62  if (i_char == 0) then
63  out_filename = trim(in_filename) // "_aero_particles.txt"
64  else
65  out_filename = trim(in_filename(1:(i_char - 1))) &
66  // "_aero_particles.txt"
67  end if
68  end if
69 
70  call aero_data_allocate(aero_data)
71  call aero_state_allocate(aero_state)
72 
73  call input_state(in_filename, index, time, del_t, i_repeat, uuid, &
74  aero_data=aero_data, aero_state=aero_state)
75 
76  write(*,'(a)') "Output file: " // trim(out_filename)
77  write(*,'(a)') " Output data is for time = " &
78  // trim(real_to_string(time)) // " (s)"
79  write(*,'(a)') " Each row of output is one particle."
80  write(*,'(a)') " The columns of output are:"
81  write(*,'(a)') " column 1: particle ID number"
82  write(*,'(a)') " column 2: number concentration (m^{-3})"
83  write(*,'(a)') " column 3: particle diameter (m)"
84  write(*,'(a)') " column 4: particle total mass (kg)"
85  do i_spec = 1,aero_data%n_spec
86  write(*,'(a,i2,a,a,a,e10.4,a)') ' column ', i_spec + 4, ': ', &
87  trim(aero_data%name(i_spec)), ' mass (kg) - density = ', &
88  aero_data%density(i_spec), ' (kg/m^3)'
89  end do
90 
91  call open_file_write(out_filename, out_unit)
92  do i_part = 1,aero_state%apa%n_part
93  aero_particle => aero_state%apa%particle(i_part)
94  write(out_unit, '(i15,e30.15e3,e30.15e3,e30.15e3)', advance='no') &
95  aero_particle%id, &
96  aero_state_particle_num_conc(aero_state, aero_particle), &
97  aero_particle_diameter(aero_particle), &
98  aero_particle_mass(aero_particle, aero_data)
99  do i_spec = 1,aero_data%n_spec
100  write(out_unit, '(e30.15e3)', advance='no') &
101  aero_particle_species_mass(aero_particle, i_spec, aero_data)
102  end do
103  write(out_unit, *) ''
104  end do
105  call close_file(out_unit)
106 
107  call aero_data_deallocate(aero_data)
108  call aero_state_deallocate(aero_state)
109 
110  call pmc_mpi_finalize()
111 
112 contains
113 
114  subroutine print_help()
115 
116  write(*,'(a)') 'Usage: extract_aero_particles [options] <netcdf_prefix>'
117  write(*,'(a)') ''
118  write(*,'(a)') 'options are:'
119  write(*,'(a)') ' -h, --help Print this help message.'
120  write(*,'(a)') ' -o, --out <file> Output filename.'
121  write(*,'(a)') ''
122  write(*,'(a)') 'Examples:'
123  write(*,'(a)') ' extract_aero_particles data_0001_00000001.nc'
124  write(*,'(a)') ''
125 
126  end subroutine print_help
127 
128 end program extract_aero_particles
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:509
subroutine die_msg(code, error_msg)
Error immediately.
Definition: util.F90:133
character function getopt(optstring, longopts)
Definition: getopt.F90:131
subroutine close_file(unit)
Close a file and de-assign the unit.
Definition: util.F90:225
The aero_particle_t structure and associated subroutines.
elemental real(kind=dp) function aero_particle_species_mass(aero_particle, i_spec, aero_data)
Mass of a single species in the particle (kg).
subroutine aero_state_allocate(aero_state)
Allocates aerosol arrays.
Definition: aero_state.F90:83
elemental real(kind=dp) function aero_particle_diameter(aero_particle)
Total diameter of the particle (m).
subroutine pmc_mpi_finalize()
Shut down MPI.
Definition: mpi.F90:88
subroutine pmc_mpi_init()
Initialize MPI.
Definition: mpi.F90:55
real(kind=dp) function aero_state_particle_num_conc(aero_state, aero_particle)
The number concentration of a single particle (m^{-3}).
Definition: aero_state.F90:505
subroutine aero_data_deallocate(aero_data)
Frees all storage.
Definition: aero_data.F90:116
The aero_state_t structure and assocated subroutines.
Definition: aero_state.F90:9
Wrapper functions for MPI.
Definition: mpi.F90:13
The current collection of aerosol particles.
Definition: aero_state.F90:63
Single aerosol particle data structure.
subroutine aero_state_deallocate(aero_state)
Deallocates a previously allocated aerosol.
Definition: aero_state.F90:100
subroutine print_help()
program extract_aero_particles
Read NetCDF output files and write out the individual particle masses.
elemental real(kind=dp) function aero_particle_mass(aero_particle, aero_data)
Total mass of the particle (kg).
subroutine aero_data_allocate(aero_data)
Allocate storage for aero_data.
Definition: aero_data.F90:69
character(len=pmc_util_convert_string_len) function real_to_string(val)
Convert a real to a string format.
Definition: util.F90:759
Aerosol material properties and associated data.
Definition: aero_data.F90:40
Write data in NetCDF format.
Definition: output.F90:68
subroutine open_file_write(filename, unit)
Open a file for writing with an automatically assigned unit and test that it succeeds. The file should be closed with close_file().
Definition: util.F90:205