[Dart-dev] [7098] DART/trunk/utilities/utilities_mod.f90: add set_filename_list() which handles the common case in our namelists

nancy at ucar.edu nancy at ucar.edu
Mon Aug 4 10:49:27 MDT 2014


Revision: 7098
Author:   nancy
Date:     2014-08-04 10:49:26 -0600 (Mon, 04 Aug 2014)
Log Message:
-----------
add set_filename_list() which handles the common case in our namelists
where you can specify an list of input filenames in the namelist itself,
OR you can specify an ascii file which contains a list of filenames, one
per line.  this routine does consistent error checking to be sure that
only one of these two options are specified, and if the input is an
ascii file, reads in the filenames and puts them in the array of names.
tests to be sure more names aren't specified than the max number of
input files declared by the caller.

unrelated change - since index() is a fortran intrinsic, change the
name of a local variable to something slightly different to avoid confusion.
(fortran has many separate namespaces and allows this, but that
doesn't mean it's a good idea!)

Modified Paths:
--------------
    DART/trunk/utilities/utilities_mod.f90

-------------- next part --------------
Modified: DART/trunk/utilities/utilities_mod.f90
===================================================================
--- DART/trunk/utilities/utilities_mod.f90	2014-08-01 21:53:34 UTC (rev 7097)
+++ DART/trunk/utilities/utilities_mod.f90	2014-08-04 16:49:26 UTC (rev 7098)
@@ -166,7 +166,8 @@
           find_namelist_in_file, check_namelist_read, do_nml_term,          &
           set_tasknum, set_output, do_output, set_nml_output, do_nml_file,  &
           E_DBG, E_MSG, E_WARN, E_ERR, DEBUG, MESSAGE, WARNING, FATAL,      &
-          is_longitude_between, get_next_filename, ascii_file_format
+          is_longitude_between, get_next_filename, ascii_file_format,       &
+          set_filename_list
 
 ! this routine is either in the null_mpi_utilities_mod.f90, or in
 ! the mpi_utilities_mod.f90 file, but it is not a module subroutine.
@@ -1622,13 +1623,13 @@
 !#######################################################################
 
 
-function get_next_filename( listname, index )
+function get_next_filename( listname, findex )
 
 ! Arguments are the name of a file which contains a list of filenames.
 ! This routine opens the listfile, and returns the index-th one.
 !
 character(len=*),  intent(in) :: listname
-integer,           intent(in) :: index
+integer,           intent(in) :: findex
 character(len=256)            :: get_next_filename
 
 integer :: i, ios, funit
@@ -1637,7 +1638,7 @@
 
 funit   = open_file(listname, form="FORMATTED", action="READ")
 
-PARSELOOP : do i=1, index
+PARSELOOP : do i=1, findex
 
    read(funit, '(A)', iostat=ios) string
 
@@ -1666,6 +1667,88 @@
 !#######################################################################
 
 
+function set_filename_list(filename_seq, filename_seq_list, caller_name)
+
+! sort out the input lists, return the number of names found,
+! if a list was given put the list into the array, and return
+! -1 if an error.
+
+character(len=*), intent(inout) :: filename_seq(:)
+character(len=*), intent(in)    :: filename_seq_list
+character(len=*), intent(in)    :: caller_name
+integer                         :: set_filename_list
+
+integer :: findex, max_num_input_files
+logical :: from_file
+character(len=32) :: fsource
+
+! here's the logic:
+! if the user specifies neither filename_seq nor filename_seq_list, error
+! if the user specifies both, error.
+! if the user gives a filelist, we make sure the length is not more
+!   than maxfiles and read it into the explicit list and continue.
+! when this routine returns, the function return val is the count
+! and the names are in filename_seq()
+
+if (filename_seq(1) == '' .and. filename_seq_list == '') then
+   call error_handler(E_ERR, caller_name, &
+          'must specify either filenames in the namelist, or a filename containing a list of names', &
+          source,revision,revdate)
+endif
+   
+! make sure the namelist specifies one or the other but not both
+if (filename_seq(1) /= '' .and. filename_seq_list /= '') then
+   call error_handler(E_ERR, caller_name, &
+       'cannot specify both filenames in the namelist and a filename containing a list of names', &
+       source,revision,revdate)
+endif
+
+! if they have specified a file which contains a list, read it into
+! the filename_seq array and set the count.
+if (filename_seq_list /= '') then
+   fsource = 'filenames contained in a list file'
+   from_file = .true.
+else
+   fsource = 'filenames in the namelist'
+   from_file = .false.
+endif
+
+! the max number of names in a namelist file is the size of the array
+! that will be returned.
+max_num_input_files = size(filename_seq)
+do findex = 1, max_num_input_files
+   if (from_file) &
+      filename_seq(findex) = get_next_filename(filename_seq_list, findex)
+
+   if (filename_seq(findex) == '') then
+      if (findex == 1) then
+         call error_handler(E_ERR, caller_name, &
+             'found no '//trim(fsource), source,revision,revdate)
+      endif
+
+      ! return how many files we found, either in the namelist
+      ! or in the list of files
+      set_filename_list = findex - 1
+      return
+   endif
+enddo
+
+! if you're reading from a file, make sure you don't have more
+! names in the file than can fit in the array.
+if (from_file) then
+   if (get_next_filename(filename_seq_list, max_num_input_files+1) /= '') then
+      write(msgstring, *) 'cannot specify more than ',max_num_input_files,' files'
+      call error_handler(E_ERR, caller_name, msgstring, source,revision,revdate)
+   endif
+endif
+
+set_filename_list = max_num_input_files
+
+end function set_filename_list
+
+!#######################################################################
+
+
 function is_longitude_between (lon, minlon, maxlon, doradians, newlon)
 
 !  uniform way to treat longitude ranges, in degrees, on a globe.


More information about the Dart-dev mailing list