PartMC  2.6.1
extract_sectional_aero_time.F90
Go to the documentation of this file.
1 ! Copyright (C) 2009-2012, 2016 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_sectional_aero_time program.
7 
8 !> Read NetCDF sectional output files and write out the time evolution of
9 !> aerosol number and mass concentrations in text format.
11 
12  use pmc_aero_binned
13  use pmc_output
14  use pmc_mpi
15  use getopt_m
16 
17  character(len=PMC_MAX_FILENAME_LEN) :: in_prefix, out_filename
18  character(len=PMC_MAX_FILENAME_LEN), allocatable :: filename_list(:)
19  type(bin_grid_t) :: bin_grid
20  type(aero_data_t) :: aero_data
21  type(aero_binned_t) :: aero_binned
22  integer :: index, out_unit
23  integer :: i_file, n_file, i_spec
24  real(kind=dp) :: time, del_t
25  character(len=PMC_UUID_LEN) :: uuid, run_uuid
26  real(kind=dp), allocatable :: times(:), time_num_concs(:), time_mass_concs(:)
27  real(kind=dp), allocatable :: time_species_concs(:,:)
28  type(option_s) :: opts(2)
29 
30  call pmc_mpi_init()
31 
32  opts(1) = option_s("help", .false., 'h')
33  opts(2) = option_s("output", .true., 'o')
34 
35  out_filename = ""
36 
37  do
38  select case(getopt("ho:", opts))
39  case(char(0))
40  exit
41  case('h')
42  call print_help()
43  stop
44  case('o')
45  out_filename = optarg
46  case( '?' )
47  call print_help()
48  call die_msg(559277549, 'unknown option: ' // trim(optopt))
49  case default
50  call print_help()
51  call die_msg(247146202, 'unhandled option: ' // trim(optopt))
52  end select
53  end do
54 
55  if (optind /= command_argument_count()) then
56  call print_help()
57  call die_msg(959981535, 'expected exactly one non-option prefix argument')
58  end if
59 
60  call get_command_argument(optind, in_prefix)
61 
62  if (out_filename == "") then
63  out_filename = trim(in_prefix) // "_aero_time.txt"
64  end if
65 
66  allocate(filename_list(0))
67  call input_filename_list(in_prefix, filename_list)
68  n_file = size(filename_list)
69  call assert_msg(875939143, n_file > 0, &
70  "no NetCDF files found with prefix: " // trim(in_prefix))
71 
72  call input_sectional(filename_list(1), index, time, del_t, uuid, &
73  bin_grid=bin_grid, aero_data=aero_data, aero_binned=aero_binned)
74  run_uuid = uuid
75 
76  allocate(times(n_file))
77  allocate(time_num_concs(n_file))
78  allocate(time_mass_concs(n_file))
79  allocate(time_species_concs(n_file, aero_data_n_spec(aero_data)))
80 
81  do i_file = 1,n_file
82  call input_sectional(filename_list(i_file), index, time, del_t, uuid, &
83  bin_grid=bin_grid, aero_data=aero_data, aero_binned=aero_binned)
84 
85  call assert_msg(865522513, uuid == run_uuid, &
86  "UUID mismatch between " // trim(filename_list(1)) // " and " &
87  // trim(filename_list(i_file)))
88 
89  times(i_file) = time
90  time_num_concs(i_file) = sum(aero_binned%num_conc * bin_grid%widths)
91  ! FIXME: line below assumes bin_grid%widths is constant
92  time_species_concs(i_file, :) = sum(aero_binned%vol_conc &
93  * bin_grid%widths(1), 1) * aero_data%density
94  time_mass_concs(i_file) = sum(time_species_concs(i_file, :))
95  end do
96 
97  write(*,'(a,a)') "Output file: ", trim(out_filename)
98  write(*,'(a)') " Each row of output is one time."
99  write(*,'(a)') " The columns of output are:"
100  write(*,'(a)') " column 1: time (s)"
101  write(*,'(a)') " column 2: aerosol number concentration (#/m^3)"
102  write(*,'(a)') " column 3: aerosol mass concentration (kg/m^3)"
103  do i_spec = 1,aero_data_n_spec(aero_data)
104  write(*,'(a,i2,a,a,a)') " column ", i_spec + 3, ": aerosol ", &
105  trim(aero_data%name(i_spec)), " concentration (kg/m^3)"
106  end do
107 
108  call open_file_write(out_filename, out_unit)
109  do i_file = 1,n_file
110  write(out_unit, '(e30.15e3)', advance='no') times(i_file)
111  write(out_unit, '(e30.15e3)', advance='no') time_num_concs(i_file)
112  write(out_unit, '(e30.15e3)', advance='no') time_mass_concs(i_file)
113  do i_spec = 1,aero_data_n_spec(aero_data)
114  write(out_unit, '(e30.15e3)', advance='no') &
115  time_species_concs(i_file, i_spec)
116  end do
117  write(out_unit, '(a)') ''
118  end do
119  call close_file(out_unit)
120 
121  deallocate(times)
122  deallocate(time_num_concs)
123  deallocate(time_mass_concs)
124  deallocate(time_species_concs)
125  deallocate(filename_list)
126 
127  call pmc_mpi_finalize()
128 
129 contains
130 
131  subroutine print_help()
132 
133  write(*,'(a)') 'Usage: extract_sectional_aero_time [options] ' &
134  // '<netcdf_prefix>'
135  write(*,'(a)') ''
136  write(*,'(a)') 'options are:'
137  write(*,'(a)') ' -h, --help Print this help message.'
138  write(*,'(a)') ' -o, --out <file> Output filename.'
139  write(*,'(a)') ''
140  write(*,'(a)') 'Examples:'
141  write(*,'(a)') ' extract_sectional_aero_time data_0001'
142  write(*,'(a)') ''
143 
144  end subroutine print_help
145 
146 end program extract_sectional_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_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
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_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_output::input_sectional
subroutine input_sectional(filename, index, time, del_t, uuid, bin_grid, aero_data, aero_binned, gas_data, gas_state, env_state)
Input sectional data.
Definition: output.F90:716
pmc_aero_binned
The aero_binned_t structure and associated subroutines.
Definition: aero_binned.F90:9
pmc_aero_binned::aero_binned_t
Aerosol number and volume distributions stored per bin.
Definition: aero_binned.F90:37
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
pmc_bin_grid::bin_grid_t
1D grid, either logarithmic or linear.
Definition: bin_grid.F90:33
print_help
subroutine print_help()
Definition: extract_aero_particles.F90:109
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
extract_sectional_aero_time
program extract_sectional_aero_time
Read NetCDF sectional output files and write out the time evolution of aerosol number and mass concen...
Definition: extract_sectional_aero_time.F90:10