PartMC  2.6.1
extract_gas.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_gas program.
7 
8 !> Read NetCDF output files and write out the gas mixing ratios in text
9 !> format.
10 program extract_gas
11 
12  use pmc_gas_state
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  character(len=1000) :: tmp_str
20  type(gas_data_t) :: gas_data
21  type(gas_state_t) :: gas_state
22  integer :: index, i_repeat, i_spec, out_unit
23  integer :: i_file, n_file
24  real(kind=dp) :: time, del_t
25  character(len=PMC_UUID_LEN) :: uuid, run_uuid
26  real(kind=dp), allocatable :: times(:), gas_mixing_ratios(:,:)
27  type(option_s) :: opts(2)
28 
29  call pmc_mpi_init()
30 
31  opts(1) = option_s("help", .false., 'h')
32  opts(2) = option_s("output", .true., 'o')
33 
34  out_filename = ""
35 
36  do
37  select case(getopt("ho:", opts))
38  case(char(0))
39  exit
40  case('h')
41  call print_help()
42  stop
43  case('o')
44  out_filename = optarg
45  case( '?' )
46  call print_help()
47  call die_msg(478715112, 'unknown option: ' // trim(optopt))
48  case default
49  call print_help()
50  call die_msg(935521190, 'unhandled option: ' // trim(optopt))
51  end select
52  end do
53 
54  if (optind /= command_argument_count()) then
55  call print_help()
56  call die_msg(744333329, 'expected exactly one non-option prefix argument')
57  end if
58 
59  call get_command_argument(optind, in_prefix)
60 
61  if (out_filename == "") then
62  out_filename = trim(in_prefix) // "_gas.txt"
63  end if
64 
65  allocate(filename_list(0))
66  call input_filename_list(in_prefix, filename_list)
67  n_file = size(filename_list)
68  call assert_msg(579059629, n_file > 0, &
69  "no NetCDF files found with prefix: " // trim(in_prefix))
70 
71  call input_state(filename_list(1), index, time, del_t, i_repeat, uuid, &
72  gas_data=gas_data, gas_state=gas_state)
73  run_uuid = uuid
74 
75  allocate(times(n_file))
76  allocate(gas_mixing_ratios(n_file, gas_data_n_spec(gas_data)))
77 
78  do i_file = 1,n_file
79  call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
80  uuid, gas_data=gas_data, gas_state=gas_state)
81 
82  call assert_msg(390171757, uuid == run_uuid, &
83  "UUID mismatch between " // trim(filename_list(1)) // " and " &
84  // trim(filename_list(i_file)))
85 
86  times(i_file) = time
87  gas_mixing_ratios(i_file, :) = gas_state%mix_rat
88  end do
89 
90  write(*,'(a,a)') "Output file: ", trim(out_filename)
91  write(*,'(a)') " Each row of output is one time."
92  write(*,'(a)') " The columns of output are:"
93  write(*,'(a)') " column 1: time (s)"
94  do i_spec = 1,gas_data_n_spec(gas_data)
95  write(*,'(a,i2,a,a,a)') " column ", i_spec + 1, ": gas ", &
96  trim(gas_data%name(i_spec)), " mixing ratio (ppb)"
97  end do
98 
99  call open_file_write(out_filename, out_unit)
100  do i_file = 1,n_file
101  write(out_unit, '(e30.15e3)', advance='no') times(i_file)
102  do i_spec = 1,gas_data_n_spec(gas_data)
103  write(out_unit, '(e30.15e3)', advance='no') &
104  gas_mixing_ratios(i_file, i_spec)
105  end do
106  write(out_unit, '(a)') ''
107  end do
108  call close_file(out_unit)
109 
110  deallocate(times)
111  deallocate(gas_mixing_ratios)
112 
113  call pmc_mpi_finalize()
114 
115 contains
116 
117  subroutine print_help()
118 
119  write(*,'(a)') 'Usage: extract_gas [options] <netcdf_prefix>'
120  write(*,'(a)') ''
121  write(*,'(a)') 'options are:'
122  write(*,'(a)') ' -h, --help Print this help message.'
123  write(*,'(a)') ' -o, --out <file> Output filename.'
124  write(*,'(a)') ''
125  write(*,'(a)') 'Examples:'
126  write(*,'(a)') ' extract_gas data_0001'
127  write(*,'(a)') ''
128 
129  end subroutine print_help
130 
131 end program extract_gas
pmc_mpi::pmc_mpi_init
subroutine pmc_mpi_init()
Initialize MPI.
Definition: mpi.F90:56
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_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_gas
program extract_gas
Read NetCDF output files and write out the gas mixing ratios in text format.
Definition: extract_gas.F90:10
pmc_gas_state
The gas_state_t structure and associated subroutines.
Definition: gas_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_gas_state::gas_state_t
Current state of the gas mixing ratios in the system.
Definition: gas_state.F90:33
pmc_output
Write data in NetCDF format.
Definition: output.F90:68
getopt_m::optind
integer optind
Definition: getopt.F90:94
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_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