PartMC  2.6.1
extract_env.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_env program.
7 
8 !> Read NetCDF output files and write out the environment variables in
9 !> text format.
10 program extract_env
11 
12  use pmc_env_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(env_state_t) :: env_state
21  integer :: index, i_repeat, i_spec, out_unit
22  integer :: i_file, n_file
23  real(kind=dp) :: time, del_t
24  character(len=PMC_UUID_LEN) :: uuid, run_uuid
25  real(kind=dp), allocatable :: times(:), temps(:), rel_humids(:)
26  real(kind=dp), allocatable :: pressures(:), mix_heights(:)
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(909107230, 'unknown option: ' // trim(optopt))
48  case default
49  call print_help()
50  call die_msg(368158543, '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(410427558, '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) // "_env.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(399220907, 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  env_state=env_state)
73  run_uuid = uuid
74 
75  allocate(times(n_file))
76  allocate(temps(n_file))
77  allocate(rel_humids(n_file))
78  allocate(pressures(n_file))
79  allocate(mix_heights(n_file))
80 
81  do i_file = 1,n_file
82  call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
83  uuid, env_state=env_state)
84 
85  call assert_msg(276800431, 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  temps(i_file) = env_state%temp
91  rel_humids(i_file) = env_state%rel_humid
92  pressures(i_file) = env_state%pressure
93  mix_heights(i_file) = env_state%height
94  end do
95 
96  write(*,'(a,a)') "Output file: ", trim(out_filename)
97  write(*,'(a)') " Each row of output is one time."
98  write(*,'(a)') " The columns of output are:"
99  write(*,'(a)') " column 1: time (s)"
100  write(*,'(a)') " column 2: temperature (K)"
101  write(*,'(a)') " column 3: relative_humidity (1)"
102  write(*,'(a)') " column 4: pressure (Pa)"
103  write(*,'(a)') " column 5: mixing height (m)"
104 
105  call open_file_write(out_filename, out_unit)
106  do i_file = 1,n_file
107  write(out_unit, '(e30.15e3)', advance='no') times(i_file)
108  write(out_unit, '(e30.15e3)', advance='no') temps(i_file)
109  write(out_unit, '(e30.15e3)', advance='no') rel_humids(i_file)
110  write(out_unit, '(e30.15e3)', advance='no') pressures(i_file)
111  write(out_unit, '(e30.15e3)', advance='no') mix_heights(i_file)
112  write(out_unit, '(a)') ''
113  end do
114  call close_file(out_unit)
115 
116  deallocate(times)
117  deallocate(temps)
118  deallocate(rel_humids)
119  deallocate(pressures)
120  deallocate(mix_heights)
121 
122  call pmc_mpi_finalize()
123 
124 contains
125 
126  subroutine print_help()
127 
128  write(*,'(a)') 'Usage: extract_env [options] <netcdf_prefix>'
129  write(*,'(a)') ''
130  write(*,'(a)') 'options are:'
131  write(*,'(a)') ' -h, --help Print this help message.'
132  write(*,'(a)') ' -o, --out <file> Output filename.'
133  write(*,'(a)') ''
134  write(*,'(a)') 'Examples:'
135  write(*,'(a)') ' extract_env data_0001'
136  write(*,'(a)') ''
137 
138  end subroutine print_help
139 
140 end program extract_env
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
pmc_env_state::env_state_t
Current environment state.
Definition: env_state.F90:29
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_env_state
The env_state_t structure and associated subroutines.
Definition: env_state.F90:9
pmc_output
Write data in NetCDF format.
Definition: output.F90:68
getopt_m::optind
integer optind
Definition: getopt.F90:94
extract_env
program extract_env
Read NetCDF output files and write out the environment variables in text format.
Definition: extract_env.F90:10
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