From d5495ebfea0040352e631d47092023a46241eed5 Mon Sep 17 00:00:00 2001 From: kamischi <karl-michael.schindler@web.de> Date: Wed, 31 May 2023 17:00:57 +0200 Subject: [PATCH] Add command line options for file I/O Also add a version --- source/f90/Makefile | 32 ++++++++- source/f90/eels-boson.f90 | 25 ++++++-- source/f90/get_commandline_options.f90 | 89 ++++++++++++++++++++++++++ source/f90/version.inc | 2 + 4 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 source/f90/get_commandline_options.f90 create mode 100644 source/f90/version.inc diff --git a/source/f90/Makefile b/source/f90/Makefile index b842283..3a8a62b 100644 --- a/source/f90/Makefile +++ b/source/f90/Makefile @@ -36,12 +36,40 @@ fint1_mod.mod: surlos_mod.mod fint2_mod.mod: surlos_mod.mod fint3_mod.mod: surlos_mod.mod +getoptsubs = getopt.o text.o system.o constants.o date_and_time.o kinds.o dummy_variables.o + +getoptmods = sufr_getopt.mod sufr_text.mod sufr_system.mod sufr_constants.mod sufr_date_and_time.mod sufr_kinds.mod sufr_dummy.mod + +sufr_getopt.mod: sufr_text.mod + $(FC) $(FFLAGS) -ffree-line-length-180 -c -o getopt.o ../getopt-libs/libsufr-0.7.7/src/getopt.f90 + +sufr_text.mod: sufr_system.mod + $(FC) $(FFLAGS) -ffree-line-length-160 -c -o text.o ../getopt-libs/libsufr-0.7.7/src/text.f90 + +sufr_system.mod: sufr_constants.mod sufr_dummy.mod + $(FC) $(FFLAGS) -c -o system.o ../getopt-libs/libsufr-0.7.7/src/system.f90 + +sufr_constants.mod: sufr_date_and_time.mod + $(FC) $(FFLAGS) -ffree-line-length-256 -c -o constants.o ../getopt-libs/libsufr-0.7.7/src/constants.f90 + +sufr_date_and_time.mod: sufr_kinds.mod + $(FC) $(FFLAGS) -ffree-line-length-150 -c -o date_and_time.o ../getopt-libs/libsufr-0.7.7/src/date_and_time.f90 + +sufr_kinds.mod: + $(FC) $(FFLAGS) -c -o kinds.o ../getopt-libs/libsufr-0.7.7/src/kinds.f90 + +sufr_dummy.mod: + $(FC) $(FFLAGS) -c -o dummy_variables.o ../getopt-libs/libsufr-0.7.7/src/dummy_variables.f90 + eelssubs = doeels.o usurlo.o quanc8.o fun.o queels.o fint1.o fint2.o fint3.o surlos.o seteps.o phint.o qrat.o eels: eels.f90 change_working_dir.o $(eelssubs) $(eelsmods) $(FC) $(FFLAGS) -o eelsf90 eels.f90 change_working_dir.o $(eelssubs) -eels-boson: eels-boson.f90 change_working_dir.o $(eelssubs) $(eelsmods) $(bosonsubs) $(bosonmods) - $(FC) $(FFLAGS) -o eels-boson eels-boson.f90 change_working_dir.o $(eelssubs) $(bosonsubs) +get_commandline_options.o: get_commandline_options.f90 $(getoptmods) $(getoptsubs) + $(FC) $(FFLAGS) -c -o get_commandline_options.o get_commandline_options.f90 + +eels-boson: eels-boson.f90 get_commandline_options.o $(getoptsubs) $(getoptmods) $(eelssubs) $(eelsmods) $(bosonsubs) $(bosonmods) + $(FC) $(FFLAGS) -o eels-boson eels-boson.f90 get_commandline_options.o $(getoptsubs) $(eelssubs) $(bosonsubs) pylibs: doeels-py doboson-py diff --git a/source/f90/eels-boson.f90 b/source/f90/eels-boson.f90 index 435e1c7..481eb15 100644 --- a/source/f90/eels-boson.f90 +++ b/source/f90/eels-boson.f90 @@ -10,6 +10,8 @@ program eels_boson implicit none + include 'version.inc' + double precision :: e0, theta, phia, phib, wmin, wmax, dw integer :: i, j, jos, k, l, layers, neps, nper, nw logical :: user @@ -28,11 +30,20 @@ program eels_boson double precision :: asym, emax, emin, gauss, t, width integer :: ioStatus - - call change_working_dir() - + + character (len = :), allocatable :: eelsin_name, eelsou_name + character (len = :), allocatable :: bosin_name, bosou_name + + ! the historic default names + eelsin_name = 'eelsin' + eelsou_name = ' ' ! eelsou is not needed + bosin_name = 'bosin' + bosou_name = 'bosou' + + call get_commandline_options(eelsin_name, eelsou_name, bosin_name, bosou_name) + ! *** read spectrometer parameters - open(unit = 11, file = 'eelsin') + open(unit = 11, file = eelsin_name) ! impact energy (ev) read(11, *) e0 ! incidence angle (%) @@ -47,7 +58,7 @@ program eels_boson ! comment lines read(11, '(a72)') (comment(k), k = 1, 2) - write(*,*) 'program eels (September 2020)' + write(*,*) 'program eels (September 2020), version: ', version write(*,'(a, f6.2, a, f5.1, a, f5.2, a, f5.2, a)') ' e0 = ', e0, & ' eV, theta = ', theta, '°, phia = ', phia, '°, phib = ', phib, '°' write(*,'(a, g11.4, a, g11.4, a, g11.4, a)') ' energy losses from', & @@ -135,7 +146,7 @@ program eels_boson layers, neps, nper, name, size(name), thick, epsinf, nos, osc, size(osc, 2),& contrl, mode, wn_array, f, size(wn_array)) - open(unit = 13, file = 'bosin') + open(unit = 13, file = bosin_name) ! target temperature (Kelvin) read(13, *) t ! width of the instrumental response (cm**-1) @@ -165,7 +176,7 @@ program eels_boson call doboson(t, width, gauss, asym, emin, emax, & wmin, wmax, nw, f, xout, yout, nout) - open(unit = 14, file = 'bosou') + open(unit = 14, file = bosou_name) write(14, '(a, a, f6.1, a, f5.2)') comment(1), 'T =', t, ' GAUSS =', gauss write(14, *) comment(2) do i = 1, nout diff --git a/source/f90/get_commandline_options.f90 b/source/f90/get_commandline_options.f90 new file mode 100644 index 0000000..e3448fa --- /dev/null +++ b/source/f90/get_commandline_options.f90 @@ -0,0 +1,89 @@ +subroutine get_commandline_options(eelsin_name, eelsou_name, bosin_name, bosou_name) + +! This routine defines the commandline options, parses the commandline and sets the +! filenames of I/O files. +! +! It uses sufr_getopt from libSUFR and has been derived from getopt_long_example.f90 +! +! KMS + + use sufr_getopt, only: getopt_t, getopt_long, longOption, optArg, getopt_long_help + + implicit none + + character (len = *) :: eelsin_name, eelsou_name, bosin_name, bosou_name + + include 'version.inc' + + character :: option + integer :: status + + ! Set up the longOpts struct to define the valid options: + ! short option, long option, argument (no = 0 / yes = 1), short description + type(getopt_t) :: longOpts(8) = & + [ & + getopt_t('v', 'version', 0, 'Print version'), & + getopt_t('V', 'version', 0, 'Print version'), & + getopt_t('h', 'help', 0, 'Print options'), & + getopt_t('d', 'dir', 1, 'Change I/O directory'), & + getopt_t('e', 'eelsin', 1, 'Name of EELS input file (eelsin)'), & + getopt_t('f', 'eelsou', 1, 'Name of EELS output file (optional)'), & + getopt_t('b', 'bosin', 1, 'Name of BOSON input file (bosin)'), & + getopt_t('c', 'bosou', 1, 'Name of BOSON output file (bosou)') & + ] + + do ! scan all the command-line parameters + + ! getopt_long() returns a single character" ">","!",".", or the short-option character (e.g. "a" for -a). + ! It also sets two 'global' variables through the SUFR_getopt module: + ! - longOption: the full option (e.g. "-a" or "--all") including the dashes + ! - optArg: the argument following the option (if required and present) + option = getopt_long(longOpts) + + ! Do different things depending on the option returned: + select case(option) + case('d') ! Change I/O directory + status = chdir(trim(optArg)) + if (status /= 0) then + write (*,*) 'WARNING: change directory failed!' + write (*,*) 'Directory tried: ', trim(optarg) + write (*,*) 'Error code (see: man chdir): ', status + write (*,*) 'Continuing in the start directory.' + write (*,*) '' + end if + case('e') + eelsin_name = optArg + case('f') + eelsou_name = optArg + case('b') + bosin_name = optArg + case('c') + bosou_name = optArg + case('v') + write (*,*) 'eels-boson version: ' // version + stop + case('V') + write (*,*) 'eels-boson version: ' // version + stop + case('h') + call getopt_long_help(longOpts) + stop + case('!') ! Unknown option (starting with "-" or "--") + write (*,*) 'WARNING: unknown option: ' // trim(optArg) + call getopt_long_help(longOpts) + stop + case('.') ! Parameter is not an option (i.e., it doesn't start with "-" or "--") + write (*,*) 'WARNING: parameter without option: ' // trim(optArg) + call getopt_long_help(longOpts) + stop + case default + write (*,*) 'WARNING: unknown option: ' // trim(longOption) + call getopt_long_help(longOpts) + stop + case('>') ! Last parameter. Exit case statement + exit + end select + end do + + return +end subroutine get_commandline_options diff --git a/source/f90/version.inc b/source/f90/version.inc new file mode 100644 index 0000000..453b451 --- /dev/null +++ b/source/f90/version.inc @@ -0,0 +1,2 @@ +! version of eels-boson + character (len = *), parameter :: version = '1.0.0' -- GitLab