aovprms.f90 Source File


Contents

Source Code


Source Code

!
Module mprms
!! Purpose: <br>
!! Module demonstrates reading f90 program global arguments from parameter file
!! and/or command line. <br>
!! Implementation: <br>
!! In the present module as global variables declare and set default values
!! to all code parameters. To reset them from parameter file and/or command line
!! in the Main program call once subroutine ``prog_args`` from this module.
!! All modules may access these values via ``Use mparam`` declaration.
!! You may use ``Only`` phrase to restrict access/prevent confusion with
!! local variables. To make code clearer you may capitalize parameters though
!! it has no impact on the compiler so name variables with care.<br>
!! Operation:<br>
!! The initial declared values are overwritten subsequently by those present
!! in the parameter file and command line. Syntax rules follow those of f90
!! NAMELIST file. Note insensitivity to capitalization and order and
!! use of both space and comma as delimiters.
!! Parameter lists including strings must be enclosed with
!! yet another pair of string marks (see examples).<br>
!! History:<br>
!! Based on an example Namelist use by John S. Urban, 2009<br>
!! http://www.urbanjost.altervista.org/LIBRARY/libGPF/arguments/namelist/index.html<br>
!! (C) alex schwarzenberg-czerny, 2018, alex@camk.edu.pl<br>
!
!! Example parameter file prog_args.par:<br>
!! &par OTHER_INTEGER=2 / <br>
!
!! Example calls:<br>
!! ./t SOME_LOGICAL=T some_real=200e3  OTHER_INTEGER=200 <br>
!! ./t "SOME_STRING='my character string' SOME_INTEGER=10,SOME_REAL=20.30" <br>
!! ./t other_INTEGER=33333, SOME_INTEGER=22222 <br>
      Use aovconst
      Use aovsub
      Implicit None
!
! Declare and initialize a NAMELIST of program global parameters
! Capitalizing prevents confusion with local variables
      Logical :: VERBOSE = .False. !! Printout control
      Logical :: ERRIN = .False. !! Error column present?
      Integer :: NBEST = 5 !! Number of best frequencies
      Integer :: CTMIN = 5 !! minimum bin occupancy
      Real (SP) :: FSAMPL = 7._SP !! oversampling factor
      Real (TIME) :: FR0 = 0. !!  lower frequency
      Real (TIME) :: FRU = 4. !!  upper frequency
      Real (TIME) :: FRS = 0. !!  frequency step (0.-optimum sampling)
                        !!  warning: undersampling risk for FRS>0.
      Integer :: MXFRQ = 1000000 !! max number of frequencies
      Integer :: NPAR = 5 !! number of model parameters
      Integer :: NCOV = 2 !! number of bin coverages
      Character (Len=4), Parameter :: meths (8) = &
         (/ 'AOV ', 'ATR ', 'AMH ', 'PWS ', 'AOVW', 'ATRW', 'AMHW', 'PWSW' /)
      Character (Len=4) :: METHOD = 'AMH' !! select from
                        !! AOV,ATR,AMH,PWS,AOVW,ATRW,AMHW,PWSW
      Character (Len=5) :: SPTYPE = 'AOV' !! select from
                        !! RAW,AOV,MODEL,RESID
      Character (Len=LSTRING) :: DIR = '' !! LC file name prefix
      Character (Len=LSTRING) :: EXT = '.dat' !! LC file name extension
!! obligatory command parameter (no keyword, order matters)
      Character (Len=LSTRING) :: STAR = 'SIMUL' !!  star name;
!! `STAR='SIMUL'` supplies for calculation simulated test data
      Character (Len=LSTRING) :: STRLST = '' !!  star list file name;
!! If non-empty it replaces STAR as source of star names
      Character (Len=LSTRING) :: EPOCH = 'AVE'
!! epoch type: `MIN` of times, `AVE` -average, `MED` -median or else
!! number  given in EPOCH.
!
!! ... just add more variable declaration here and append them to NAMELIST
      Namelist / par / VERBOSE, NBEST, ERRIN, CTMIN, FSAMPL, FR0, FRU, &
         FRS, MXFRQ, NPAR, NCOV, METHOD, SPTYPE, DIR, EXT, STAR, STRLST, &
         EPOCH
Contains
!
      Function prog_args (pname)
!! Inputs arguments succesively from parameter file and command line
!! and resets module NAMELIST global variables, if present in input,
!! as an intended side effect. par and NAMELIST variables are external.
         Integer :: prog_args !! status
         Character (Len=*), Intent (In) :: pname !! name of parameter file
!
         Character (Len=255) :: string
         Integer :: io, j
         Do ! dummy loop to use exit statements
! get parameter file arguments
            string = trim (pname) // '.par'
            Open (2, File=string, Status='old', IoStat=io)
            If (io == 0) Then
               Read (2, Nml=par, IoStat=io)
               If (io /= 0) Print *, 'Parameter file syntax error'
               Close (2)
            Else
               Print *, 'No parameter file ' // trim (string) // ' found'
            End If
! get command line arguments
            string = ''
            io = 1
            Do ! get_command_argument trips string brackets ""''
               Call get_command_argument (io, msg)
               If (LEN_TRIM(msg) == 0) Exit
               j = index (msg, '=')
               Select Case (toupper(msg(1:j-1)))! adds string brackets when needed
               Case ('METHOD','SPTYPE','DIR','EXT','STAR','STRLST','EPOCH')
                  string = trim (string) // ' ' // msg (1:j) // '"' // &
                     trim (msg(j+1:)) // '"'
               Case Default
                  string = trim (string) // ' ' // msg (1:j)//trim (msg(j+1:))
               End Select
               io = io + 1
            End Do
            string = "&par " // trim (string) // " /"
            Read (string, Nml=par, IoStat=io)! internal read of NAMELIST
            If (io /= 0) Print *, 'Commad line syntax error'
            Exit
         End Do
         If (io .Ne. 0) Print *, 'error: ', io
!         Write (*, Nml=par)                  ! use the values
         prog_args = io
      End Function prog_args
      Subroutine print_args (logunit)
         Integer, Intent (In) :: logunit
         Write (logunit, Nml=par)
      End Subroutine print_args
End Module mprms
!
!