PartMC  2.2.0
extract_aero_time.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_aero_size program.
00007 
00008 !> Read NetCDF output files and write out the aerosol number or mass
00009 !> size distributions in text format.
00010 program extract_aero_size
00011 
00012   use pmc_aero_state
00013   use pmc_aero_particle
00014   use pmc_output
00015   use pmc_mpi
00016   use getopt_m
00017 
00018   character(len=PMC_MAX_FILENAME_LEN) :: in_prefix, out_filename
00019   character(len=PMC_MAX_FILENAME_LEN), allocatable :: filename_list(:)
00020   character(len=1000) :: tmp_str
00021   type(aero_data_t) :: aero_data
00022   type(aero_state_t) :: aero_state
00023   integer :: index, i_repeat, i_spec, out_unit
00024   integer :: i_file, n_file
00025   real(kind=dp) :: time, del_t
00026   character(len=PMC_UUID_LEN) :: uuid, run_uuid
00027   real(kind=dp), allocatable :: particle_num_concs(:), particle_masses(:)
00028   real(kind=dp), allocatable :: times(:), time_num_concs(:), time_mass_concs(:)
00029   real(kind=dp), allocatable :: time_species_concs(:,:)
00030   type(option_s) :: opts(2)
00031 
00032   call pmc_mpi_init()
00033 
00034   opts(1) = option_s("help", .false., 'h')
00035   opts(2) = option_s("output", .true., 'o')
00036 
00037   out_filename = ""
00038 
00039   do
00040      select case(getopt("ho:", opts))
00041      case(char(0))
00042         exit
00043      case('h')
00044         call print_help()
00045         stop
00046      case('o')
00047         out_filename = optarg
00048      case( '?' )
00049         call print_help()
00050         call die_msg(514364550, 'unknown option: ' // trim(optopt))
00051      case default
00052         call print_help()
00053         call die_msg(603100341, 'unhandled option: ' // trim(optopt))
00054      end select
00055   end do
00056 
00057   if (optind /= command_argument_count()) then
00058      call print_help()
00059      call die_msg(967032896, 'expected exactly one non-option prefix argument')
00060   end if
00061 
00062   call get_command_argument(optind, in_prefix)
00063 
00064   if (out_filename == "") then
00065      out_filename = trim(in_prefix) // "_aero_time.txt"
00066   end if
00067 
00068   call aero_data_allocate(aero_data)
00069   call aero_state_allocate(aero_state)
00070 
00071   allocate(filename_list(0))
00072   call input_filename_list(in_prefix, filename_list)
00073   n_file = size(filename_list)
00074   call assert_msg(323514871, n_file > 0, &
00075        "no NetCDF files found with prefix: " // trim(in_prefix))
00076 
00077   call input_state(filename_list(1), index, time, del_t, i_repeat, uuid, &
00078        aero_data=aero_data, aero_state=aero_state)
00079   run_uuid = uuid
00080 
00081   allocate(times(n_file))
00082   allocate(time_num_concs(n_file))
00083   allocate(time_mass_concs(n_file))
00084   allocate(time_species_concs(n_file, aero_data%n_spec))
00085 
00086   allocate(particle_num_concs(0))
00087   allocate(particle_masses(0))
00088 
00089   do i_file = 1,n_file
00090      call input_state(filename_list(i_file), index, time, del_t, i_repeat, &
00091           uuid, aero_data=aero_data, aero_state=aero_state)
00092 
00093      call assert_msg(397906326, uuid == run_uuid, &
00094           "UUID mismatch between " // trim(filename_list(1)) // " and " &
00095           // trim(filename_list(i_file)))
00096 
00097      times(i_file) = time
00098      call aero_state_num_concs(aero_state, particle_num_concs)
00099      time_num_concs(i_file) = sum(particle_num_concs)
00100      call aero_state_masses(aero_state, aero_data, particle_masses)
00101      time_mass_concs(i_file) = sum(particle_masses * particle_num_concs)
00102      do i_spec = 1,aero_data%n_spec
00103         call aero_state_masses(aero_state, aero_data, particle_masses, &
00104              include=(/aero_data%name(i_spec)/))
00105         time_species_concs(i_file, i_spec) &
00106              = sum(particle_masses * particle_num_concs)
00107      end do
00108   end do
00109 
00110   write(*,'(a,a)') "Output file: ", trim(out_filename)
00111   write(*,'(a)') "  Each row of output is one time."
00112   write(*,'(a)') "  The columns of output are:"
00113   write(*,'(a)') "    column  1: time (s)"
00114   write(*,'(a)') "    column  2: aerosol number concentration (#/m^3)"
00115   write(*,'(a)') "    column  3: aerosol mass concentration (kg/m^3)"
00116   do i_spec = 1,aero_data%n_spec
00117      write(*,'(a,i2,a,a,a)') "    column ", i_spec + 3, ": aerosol ", &
00118           trim(aero_data%name(i_spec)), " concentration (kg/m^3)"
00119   end do
00120 
00121   call open_file_write(out_filename, out_unit)
00122   do i_file = 1,n_file
00123      write(out_unit, '(e30.15e3)', advance='no') times(i_file)
00124      write(out_unit, '(e30.15e3)', advance='no') time_num_concs(i_file)
00125      write(out_unit, '(e30.15e3)', advance='no') time_mass_concs(i_file)
00126      do i_spec = 1,aero_data%n_spec
00127         write(out_unit, '(e30.15e3)', advance='no') &
00128              time_species_concs(i_file, i_spec)
00129      end do
00130      write(out_unit, '(a)') ''
00131   end do
00132   call close_file(out_unit)
00133 
00134   deallocate(times)
00135   deallocate(time_num_concs)
00136   deallocate(time_mass_concs)
00137   deallocate(time_species_concs)
00138   deallocate(particle_num_concs)
00139   deallocate(particle_masses)
00140   deallocate(filename_list)
00141   call aero_data_deallocate(aero_data)
00142   call aero_state_deallocate(aero_state)
00143 
00144   call pmc_mpi_finalize()
00145 
00146 contains
00147 
00148   subroutine print_help()
00149 
00150     write(*,'(a)') 'Usage: extract_aero_time [options] <netcdf_prefix>'
00151     write(*,'(a)') ''
00152     write(*,'(a)') 'options are:'
00153     write(*,'(a)') '  -h, --help        Print this help message.'
00154     write(*,'(a)') '  -o, --out <file>  Output filename.'
00155     write(*,'(a)') ''
00156     write(*,'(a)') 'Examples:'
00157     write(*,'(a)') '  extract_aero_time data_0001'
00158     write(*,'(a)') ''
00159 
00160   end subroutine print_help
00161 
00162 end program extract_aero_size