PartMC  2.2.1
extract_aero_particles.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_particles program.
00007 
00008 !> Read NetCDF output files and write out the individual particle
00009 !> masses.
00010 program extract_aero_particles
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_filename, out_filename
00019   type(aero_data_t) :: aero_data
00020   type(aero_state_t) :: aero_state
00021   integer :: index, i_repeat, i_part, i_spec, out_unit, i_char
00022   real(kind=dp) :: time, del_t
00023   character(len=PMC_UUID_LEN) :: uuid
00024   type(aero_particle_t), pointer :: aero_particle
00025   type(option_s) :: opts(2)
00026 
00027   call pmc_mpi_init()
00028 
00029   opts(1) = option_s("help", .false., 'h')
00030   opts(2) = option_s("output", .true., 'o')
00031 
00032   out_filename = ""
00033 
00034   do
00035      select case(getopt("ho:", opts))
00036      case(char(0))
00037         exit
00038      case('h')
00039         call print_help()
00040         stop
00041      case('o')
00042         out_filename = optarg
00043      case( '?' )
00044         call print_help()
00045         call die_msg(885067714, 'unknown option: ' // trim(optopt))
00046      case default
00047         call print_help()
00048         call die_msg(516704561, 'unhandled option: ' // trim(optopt))
00049      end select
00050   end do
00051 
00052   if (optind /= command_argument_count()) then
00053      call print_help()
00054      call die_msg(605963222, &
00055           'expected exactly one non-option filename argument')
00056   end if
00057 
00058   call get_command_argument(optind, in_filename)
00059 
00060   if (out_filename == "") then
00061      i_char = scan(in_filename, '.', back=.true.)
00062      if (i_char == 0) then
00063         out_filename = trim(in_filename) // "_aero_particles.txt"
00064      else
00065         out_filename = trim(in_filename(1:(i_char - 1))) &
00066              // "_aero_particles.txt"
00067      end if
00068   end if
00069 
00070   call aero_data_allocate(aero_data)
00071   call aero_state_allocate(aero_state)
00072 
00073   call input_state(in_filename, index, time, del_t, i_repeat, uuid, &
00074        aero_data=aero_data, aero_state=aero_state)
00075 
00076   write(*,'(a)') "Output file: " // trim(out_filename)
00077   write(*,'(a)') "  Output data is for time = " &
00078        // trim(real_to_string(time)) // " (s)"
00079   write(*,'(a)') "  Each row of output is one particle."
00080   write(*,'(a)') "  The columns of output are:"
00081   write(*,'(a)') "    column  1: particle ID number"
00082   write(*,'(a)') "    column  2: number concentration (m^{-3})"
00083   write(*,'(a)') "    column  3: particle diameter (m)"
00084   write(*,'(a)') "    column  4: particle total mass (kg)"
00085   do i_spec = 1,aero_data%n_spec
00086      write(*,'(a,i2,a,a,a,e10.4,a)') '    column ', i_spec + 4, ': ', &
00087           trim(aero_data%name(i_spec)), ' mass (kg) - density = ', &
00088           aero_data%density(i_spec), ' (kg/m^3)'
00089   end do
00090 
00091   call open_file_write(out_filename, out_unit)
00092   do i_part = 1,aero_state%apa%n_part
00093      aero_particle => aero_state%apa%particle(i_part)
00094      write(out_unit, '(i15,e30.15e3,e30.15e3,e30.15e3)', advance='no') &
00095           aero_particle%id, &
00096           aero_state_particle_num_conc(aero_state, aero_particle), &
00097           aero_particle_diameter(aero_particle), &
00098           aero_particle_mass(aero_particle, aero_data)
00099      do i_spec = 1,aero_data%n_spec
00100         write(out_unit, '(e30.15e3)', advance='no') &
00101              aero_particle_species_mass(aero_particle, i_spec, aero_data)
00102      end do
00103      write(out_unit, *) ''
00104   end do
00105   call close_file(out_unit)
00106 
00107   call aero_data_deallocate(aero_data)
00108   call aero_state_deallocate(aero_state)
00109 
00110   call pmc_mpi_finalize()
00111 
00112 contains
00113 
00114   subroutine print_help()
00115 
00116     write(*,'(a)') 'Usage: extract_aero_particles [options] <netcdf_prefix>'
00117     write(*,'(a)') ''
00118     write(*,'(a)') 'options are:'
00119     write(*,'(a)') '  -h, --help        Print this help message.'
00120     write(*,'(a)') '  -o, --out <file>  Output filename.'
00121     write(*,'(a)') ''
00122     write(*,'(a)') 'Examples:'
00123     write(*,'(a)') '  extract_aero_particles data_0001_00000001.nc'
00124     write(*,'(a)') ''
00125 
00126   end subroutine print_help
00127 
00128 end program extract_aero_particles