PartMC  2.2.1
extract_gas.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_gas program.
00007 
00008 !> Read NetCDF output files and write out the gas mixing ratios in text
00009 !> format.
00010 program extract_gas
00011 
00012   use pmc_gas_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(gas_data_t) :: gas_data
00021   type(gas_state_t) :: gas_state
00022   integer :: index, i_repeat, i_spec, out_unit
00023   integer :: i_file, n_file
00024   real(kind=dp) :: time, del_t
00025   character(len=PMC_UUID_LEN) :: uuid, run_uuid
00026   real(kind=dp), allocatable :: times(:), gas_mixing_ratios(:,:)
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(478715112, 'unknown option: ' // trim(optopt))
00048      case default
00049         call print_help()
00050         call die_msg(935521190, '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(744333329, '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) // "_gas.txt"
00063   end if
00064 
00065   call gas_data_allocate(gas_data)
00066   call gas_state_allocate(gas_state)
00067 
00068   allocate(filename_list(0))
00069   call input_filename_list(in_prefix, filename_list)
00070   n_file = size(filename_list)
00071   call assert_msg(579059629, n_file > 0, &
00072        "no NetCDF files found with prefix: " // trim(in_prefix))
00073 
00074   call input_state(filename_list(1), index, time, del_t, i_repeat, uuid, &
00075        gas_data=gas_data, gas_state=gas_state)
00076   run_uuid = uuid
00077 
00078   allocate(times(n_file))
00079   allocate(gas_mixing_ratios(n_file, gas_data%n_spec))
00080 
00081   do i_file = 1,n_file
00082      call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
00083           uuid, gas_data=gas_data, gas_state=gas_state)
00084 
00085      call assert_msg(390171757, uuid == run_uuid, &
00086           "UUID mismatch between " // trim(filename_list(1)) // " and " &
00087           // trim(filename_list(i_file)))
00088 
00089      times(i_file) = time
00090      gas_mixing_ratios(i_file, :) = gas_state%mix_rat
00091   end do
00092 
00093   write(*,'(a,a)') "Output file: ", trim(out_filename)
00094   write(*,'(a)') "  Each row of output is one time."
00095   write(*,'(a)') "  The columns of output are:"
00096   write(*,'(a)') "    column  1: time (s)"
00097   do i_spec = 1,gas_data%n_spec
00098      write(*,'(a,i2,a,a,a)') "    column ", i_spec + 1, ": gas ", &
00099           trim(gas_data%name(i_spec)), " mixing ratio (ppb)"
00100   end do
00101 
00102   call open_file_write(out_filename, out_unit)
00103   do i_file = 1,n_file
00104      write(out_unit, '(e30.15e3)', advance='no') times(i_file)
00105      do i_spec = 1,gas_data%n_spec
00106         write(out_unit, '(e30.15e3)', advance='no') &
00107              gas_mixing_ratios(i_file, i_spec)
00108      end do
00109      write(out_unit, '(a)') ''
00110   end do
00111   call close_file(out_unit)
00112 
00113   deallocate(times)
00114   deallocate(gas_mixing_ratios)
00115   call gas_data_deallocate(gas_data)
00116   call gas_state_deallocate(gas_state)
00117 
00118   call pmc_mpi_finalize()
00119 
00120 contains
00121 
00122   subroutine print_help()
00123 
00124     write(*,'(a)') 'Usage: extract_gas [options] <netcdf_prefix>'
00125     write(*,'(a)') ''
00126     write(*,'(a)') 'options are:'
00127     write(*,'(a)') '  -h, --help        Print this help message.'
00128     write(*,'(a)') '  -o, --out <file>  Output filename.'
00129     write(*,'(a)') ''
00130     write(*,'(a)') 'Examples:'
00131     write(*,'(a)') '  extract_gas data_0001'
00132     write(*,'(a)') ''
00133 
00134   end subroutine print_help
00135 
00136 end program extract_gas