PartMC  2.2.0
extract_env.F90
Go to the documentation of this file.
00001 ! Copyright (C) 2009-2011 Matthew West
00002 ! Licensed under the GNU General Public License version 2 or (at your
00003 ! option) any later version. See the file COPYING for details.
00004 
00005 !> \file
00006 !> The extract_env program.
00007 
00008 !> Read NetCDF output files and write out the environment variables in
00009 !> text format.
00010 program extract_env
00011 
00012   use pmc_env_state
00013   use pmc_output
00014   use pmc_mpi
00015   use getopt_m
00016 
00017   character(len=PMC_MAX_FILENAME_LEN) :: in_prefix, out_filename
00018   character(len=PMC_MAX_FILENAME_LEN), allocatable :: filename_list(:)
00019   character(len=1000) :: tmp_str
00020   type(env_state_t) :: env_state
00021   integer :: index, i_repeat, i_spec, out_unit
00022   integer :: i_file, n_file
00023   real(kind=dp) :: time, del_t
00024   character(len=PMC_UUID_LEN) :: uuid, run_uuid
00025   real(kind=dp), allocatable :: times(:), temps(:), rel_humids(:)
00026   real(kind=dp), allocatable :: pressures(:), mix_heights(:)
00027   type(option_s) :: opts(2)
00028 
00029   call pmc_mpi_init()
00030 
00031   opts(1) = option_s("help", .false., 'h')
00032   opts(2) = option_s("output", .true., 'o')
00033 
00034   out_filename = ""
00035 
00036   do
00037      select case(getopt("ho:", opts))
00038      case(char(0))
00039         exit
00040      case('h')
00041         call print_help()
00042         stop
00043      case('o')
00044         out_filename = optarg
00045      case( '?' )
00046         call print_help()
00047         call die_msg(909107230, 'unknown option: ' // trim(optopt))
00048      case default
00049         call print_help()
00050         call die_msg(368158543, 'unhandled option: ' // trim(optopt))
00051      end select
00052   end do
00053 
00054   if (optind /= command_argument_count()) then
00055      call print_help()
00056      call die_msg(410427558, 'expected exactly one non-option prefix argument')
00057   end if
00058 
00059   call get_command_argument(optind, in_prefix)
00060 
00061   if (out_filename == "") then
00062      out_filename = trim(in_prefix) // "_env.txt"
00063   end if
00064 
00065   call env_state_allocate(env_state)
00066 
00067   allocate(filename_list(0))
00068   call input_filename_list(in_prefix, filename_list)
00069   n_file = size(filename_list)
00070   call assert_msg(399220907, n_file > 0, &
00071        "no NetCDF files found with prefix: " // trim(in_prefix))
00072 
00073   call input_state(filename_list(1), index, time, del_t, i_repeat, uuid, &
00074        env_state=env_state)
00075   run_uuid = uuid
00076 
00077   allocate(times(n_file))
00078   allocate(temps(n_file))
00079   allocate(rel_humids(n_file))
00080   allocate(pressures(n_file))
00081   allocate(mix_heights(n_file))
00082 
00083   do i_file = 1,n_file
00084      call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
00085           uuid, env_state=env_state)
00086 
00087      call assert_msg(390171757, uuid == run_uuid, &
00088           "UUID mismatch between " // trim(filename_list(1)) // " and " &
00089           // trim(filename_list(i_file)))
00090 
00091      times(i_file) = time
00092      temps(i_file) = env_state%temp
00093      rel_humids(i_file) = env_state%rel_humid
00094      pressures(i_file) = env_state%pressure
00095      mix_heights(i_file) = env_state%height
00096   end do
00097 
00098   write(*,'(a,a)') "Output file: ", trim(out_filename)
00099   write(*,'(a)') "  Each row of output is one time."
00100   write(*,'(a)') "  The columns of output are:"
00101   write(*,'(a)') "    column 1: time (s)"
00102   write(*,'(a)') "    column 2: temperature (K)"
00103   write(*,'(a)') "    column 3: relative_humidity (1)"
00104   write(*,'(a)') "    column 4: pressure (Pa)"
00105   write(*,'(a)') "    column 5: mixing height (m)"
00106 
00107   call open_file_write(out_filename, out_unit)
00108   do i_file = 1,n_file
00109      write(out_unit, '(e30.15e3)', advance='no') times(i_file)
00110      write(out_unit, '(e30.15e3)', advance='no') temps(i_file)
00111      write(out_unit, '(e30.15e3)', advance='no') rel_humids(i_file)
00112      write(out_unit, '(e30.15e3)', advance='no') pressures(i_file)
00113      write(out_unit, '(e30.15e3)', advance='no') mix_heights(i_file)
00114      write(out_unit, '(a)') ''
00115   end do
00116   call close_file(out_unit)
00117 
00118   deallocate(times)
00119   deallocate(temps)
00120   deallocate(rel_humids)
00121   deallocate(pressures)
00122   deallocate(mix_heights)
00123   call env_state_deallocate(env_state)
00124 
00125   call pmc_mpi_finalize()
00126 
00127 contains
00128 
00129   subroutine print_help()
00130 
00131     write(*,'(a)') 'Usage: extract_env [options] <netcdf_prefix>'
00132     write(*,'(a)') ''
00133     write(*,'(a)') 'options are:'
00134     write(*,'(a)') '  -h, --help        Print this help message.'
00135     write(*,'(a)') '  -o, --out <file>  Output filename.'
00136     write(*,'(a)') ''
00137     write(*,'(a)') 'Examples:'
00138     write(*,'(a)') '  extract_env data_0001'
00139     write(*,'(a)') ''
00140 
00141   end subroutine print_help
00142 
00143 end program extract_env