PartMC  2.3.0
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  call env_state_allocate(env_state)
66 
67  allocate(filename_list(0))
68  call input_filename_list(in_prefix, filename_list)
69  n_file = size(filename_list)
70  call assert_msg(399220907, n_file > 0, &
71  "no NetCDF files found with prefix: " // trim(in_prefix))
72 
73  call input_state(filename_list(1), index, time, del_t, i_repeat, uuid, &
74  env_state=env_state)
75  run_uuid = uuid
76 
77  allocate(times(n_file))
78  allocate(temps(n_file))
79  allocate(rel_humids(n_file))
80  allocate(pressures(n_file))
81  allocate(mix_heights(n_file))
82 
83  do i_file = 1,n_file
84  call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
85  uuid, env_state=env_state)
86 
87  call assert_msg(276800431, uuid == run_uuid, &
88  "UUID mismatch between " // trim(filename_list(1)) // " and " &
89  // trim(filename_list(i_file)))
90 
91  times(i_file) = time
92  temps(i_file) = env_state%temp
93  rel_humids(i_file) = env_state%rel_humid
94  pressures(i_file) = env_state%pressure
95  mix_heights(i_file) = env_state%height
96  end do
97 
98  write(*,'(a,a)') "Output file: ", trim(out_filename)
99  write(*,'(a)') " Each row of output is one time."
100  write(*,'(a)') " The columns of output are:"
101  write(*,'(a)') " column 1: time (s)"
102  write(*,'(a)') " column 2: temperature (K)"
103  write(*,'(a)') " column 3: relative_humidity (1)"
104  write(*,'(a)') " column 4: pressure (Pa)"
105  write(*,'(a)') " column 5: mixing height (m)"
106 
107  call open_file_write(out_filename, out_unit)
108  do i_file = 1,n_file
109  write(out_unit, '(e30.15e3)', advance='no') times(i_file)
110  write(out_unit, '(e30.15e3)', advance='no') temps(i_file)
111  write(out_unit, '(e30.15e3)', advance='no') rel_humids(i_file)
112  write(out_unit, '(e30.15e3)', advance='no') pressures(i_file)
113  write(out_unit, '(e30.15e3)', advance='no') mix_heights(i_file)
114  write(out_unit, '(a)') ''
115  end do
116  call close_file(out_unit)
117 
118  deallocate(times)
119  deallocate(temps)
120  deallocate(rel_humids)
121  deallocate(pressures)
122  deallocate(mix_heights)
123  call env_state_deallocate(env_state)
124 
125  call pmc_mpi_finalize()
126 
127 contains
128 
129  subroutine print_help()
130 
131  write(*,'(a)') 'Usage: extract_env [options] <netcdf_prefix>'
132  write(*,'(a)') ''
133  write(*,'(a)') 'options are:'
134  write(*,'(a)') ' -h, --help Print this help message.'
135  write(*,'(a)') ' -o, --out <file> Output filename.'
136  write(*,'(a)') ''
137  write(*,'(a)') 'Examples:'
138  write(*,'(a)') ' extract_env data_0001'
139  write(*,'(a)') ''
140 
141  end subroutine print_help
142 
143 end program extract_env
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 input_filename_list(prefix, filename_list)
Find all NetCDF (.nc) filenames that match the given prefix.
Definition: output.F90:580
subroutine close_file(unit)
Close a file and de-assign the unit.
Definition: util.F90:225
The env_state_t structure and associated subroutines.
Definition: env_state.F90:9
subroutine assert_msg(code, condition_ok, error_msg)
Errors unless condition_ok is true.
Definition: util.F90:76
subroutine pmc_mpi_finalize()
Shut down MPI.
Definition: mpi.F90:88
subroutine pmc_mpi_init()
Initialize MPI.
Definition: mpi.F90:55
Current environment state.
Definition: env_state.F90:26
subroutine env_state_deallocate(env_state)
Free all storage.
Definition: env_state.F90:78
Wrapper functions for MPI.
Definition: mpi.F90:13
subroutine print_help()
subroutine env_state_allocate(env_state)
Allocate an empty environment.
Definition: env_state.F90:56
program extract_env
Read NetCDF output files and write out the environment variables in text format.
Definition: extract_env.F90:10
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