subroutine ch_cap ( c ) ! !******************************************************************************* ! !! CH_CAP capitalizes a single character. ! ! ! Modified: ! ! 30 November 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character C, the character to capitalize. ! implicit none ! character c integer itemp ! itemp = ichar ( c ) if ( 97 <= itemp .and. itemp <= 122 ) then c = char ( itemp - 32 ) end if return end function ch_eqi ( c1, c2 ) ! !******************************************************************************* ! !! CH_EQI is a case insensitive comparison of two characters for equality. ! ! ! Examples: ! ! CH_EQI ( 'A', 'a' ) is .TRUE. ! ! Modified: ! ! 28 July 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character C1, C2, the characters to compare. ! ! Output, logical CH_EQI, the result of the comparison. ! implicit none ! logical ch_eqi character c1 character c1_cap character c2 character c2_cap ! c1_cap = c1 c2_cap = c2 call ch_cap ( c1_cap ) call ch_cap ( c2_cap ) if ( c1_cap == c2_cap ) then ch_eqi = .true. else ch_eqi = .false. end if return end function ch_is_digit ( c ) ! !******************************************************************************* ! !! CH_IS_DIGIT returns .TRUE. if a character is a decimal digit. ! ! ! Modified: ! ! 15 January 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character C, the character to be analyzed. ! ! Output, logical CH_IS_DIGIT, .TRUE. if C is a digit, .FALSE. otherwise. ! implicit none ! character c logical ch_is_digit ! if ( lge ( c, '0' ) .and. lle ( c, '9' ) ) then ch_is_digit = .true. else ch_is_digit = .false. end if return end subroutine ch_low ( c ) ! !******************************************************************************* ! !! CH_LOW lowercases a single character. ! ! ! Modified: ! ! 19 July 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character C, the character to be lowercased. ! implicit none ! character c integer itemp ! itemp = ichar ( c ) if ( 65 <= itemp .and. itemp <= 90 ) then c = char ( itemp + 32 ) end if return end subroutine ch_swap ( c1, c2 ) ! !******************************************************************************* ! !! CH_SWAP swaps two characters. ! ! ! Modified: ! ! 30 July 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character C1, C2. On output, the values of C1 and ! C2 have been interchanged. ! implicit none ! character c1 character c2 character c0 ! c0 = c1 c1 = c2 c2 = c0 return end subroutine ch_to_digit ( c, digit ) ! !******************************************************************************* ! !! CH_TO_DIGIT returns the integer value of a base 10 digit. ! ! ! Example: ! ! C DIGIT ! --- ----- ! '0' 0 ! '1' 1 ! ... ... ! '9' 9 ! ' ' 0 ! 'X' -1 ! ! Modified: ! ! 04 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character C, the decimal digit, '0' through '9' or blank ! are legal. ! ! Output, integer DIGIT, the corresponding integer value. If C was ! 'illegal', then DIGIT is -1. ! implicit none ! character c integer digit ! if ( lge ( c, '0' ) .and. lle ( c, '9' ) ) then digit = ichar ( c ) - 48 else if ( c == ' ' ) then digit = 0 else digit = -1 end if return end subroutine digit_inc ( c ) ! !******************************************************************************* ! !! DIGIT_INC increments a decimal digit. ! ! ! Example: ! ! Input Output ! ----- ------ ! '0' '1' ! '1' '2' ! ... ! '8' '9' ! '9' '0' ! 'A' 'A' ! ! Modified: ! ! 04 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character C, a digit to be incremented. ! implicit none ! character c integer digit call ch_to_digit ( c, digit ) if ( digit == -1 ) then return end if digit = digit + 1 if ( digit == 10 ) then digit = 0 end if call digit_to_ch ( digit, c ) return end subroutine digit_to_ch ( digit, c ) ! !******************************************************************************* ! !! DIGIT_TO_CH returns the character representation of a decimal digit. ! ! ! Example: ! ! DIGIT C ! ----- --- ! 0 '0' ! 1 '1' ! ... ... ! 9 '9' ! 17 '*' ! ! Modified: ! ! 04 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer DIGIT, the digit value between 0 and 9. ! ! Output, character C, the corresponding character, or '*' if DIGIT ! was illegal. ! implicit none ! character c integer digit ! if ( 0 <= digit .and. digit <= 9 ) then c = char ( digit + 48 ) else c = '*' end if return end subroutine file_advance_to_string ( iunit, s, line, ierror ) ! !******************************************************************************* ! !! FILE_ADVANCE_TO_STRING searches ahead in a text file for a string. ! ! ! Discussion: ! ! The file should already have been opened. ! ! Modified: ! ! 05 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer IUNIT, the unit number associated with the open file. ! ! Input, character ( len = * ) S, a string to search for. ! ! Output, character ( len = * ) LINE: ! If IERROR = 0, the line of the file that was just read, ! and which contains the string. ! If IERROR = 1, then LINE is blank. ! ! Output, integer IERROR, error flag. ! 0, no error, the string was found. ! 1, error, the end of the file was reached. ! implicit none ! logical, parameter :: DEBUG = .true. integer ierror integer ios integer iunit character ( len = * ) line integer num_read character ( len = * ) s ! ierror = 0 num_read = 0 do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if num_read = num_read + 1 if ( index ( line, trim ( s ) ) /= 0 ) then return end if end do line = ' ' ierror = 1 if ( DEBUG ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_ADVANCE_TO_STRING - Warning!' write ( *, '(a)' ) ' Did not find the string:' write ( *, '(a)' ) ' ' // trim ( s ) write ( *, '(a,i6)' ) ' Number of lines read was ', num_read end if return end subroutine file_append ( file_name, ierror, iunit, nrec ) ! !******************************************************************************* ! !! FILE_APPEND allows a user to append new information to an old file. ! ! ! Discussion: ! ! This routine was created to address the fact that ANSI FORTRAN ! does not let one easily append information to a sequential ! access file once it has been closed. In order to allow a user ! to append new information, we create a new, writeable copy ! of the file by means of a temporary copy. ! ! On input, the file should not be open. On output, the file is ! open, the file is writeable, and the file I/O pointer is ! ready to write a new line following the last line of the ! original contents of the file. ! ! Note: ! ! It is assumed that each line of the file is no longer than ! 256 characters. ! ! The copied lines will not have any trailing blanks. ! ! A temporary file will be created with the name "append.tmp". ! ! Modified: ! ! 09 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file to ! which more information is to be appended. ! ! Output, integer IERROR, error flag. ! ! 0, no error occurred. ! 1, an error occurred while trying to open the new file. ! ! Output, integer IUNIT, the FORTRAN unit number associated with the file. ! ! Output, integer NREC, the number of records (that is, lines) ! that are already in the file. ! implicit none ! character ( len = * ) file_name character ( len = 80 ) file_temp integer ierror integer ios integer iunit integer iunit2 character ( len = 256 ) line integer nrec ! ierror = 0 file_temp = 'append.tmp' ! ! Open old file as readable. If it doesn't exist, we can ! skip ahead. Otherwise, also open new file as writeable. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_APPEND - Note:' write ( *, '(a)' ) ' This is a new file.' open ( unit = iunit, file = file_name, status = 'new', iostat = ios ) if ( ios /= 0 ) then ierror = 4 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_APPEND - Fatal error!' write ( *, '(a)' ) ' Unexpected error while opening file.' return end if nrec = 0 return end if rewind iunit call get_unit ( iunit2 ) open ( unit = iunit2, file = file_temp, status = 'new', iostat = ios ) if ( ios /= 0 ) then ierror = 4 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_APPEND - Fatal error!' write ( *, '(a)' ) ' Unexpected error while opening file:' write ( *, '(a)' ) ' ' // trim ( file_temp ) return end if ! ! Copy data from old file into temporary file. ! nrec = 0 do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if nrec = nrec + 1 write ( iunit2, '(a)' ) trim ( line ) end do write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_APPEND - Note:' write ( *, '(a,i6)' ) ' The number of records in the file is ', nrec ! ! Delete the old copy of the file. ! close ( unit = iunit, status = 'delete' ) ! ! Mark the end of the temporary file, close it, ! then reopen it with "OLD" status. ! endfile ( unit = iunit2 ) close ( unit = iunit2 ) open ( unit = iunit2, file = file_temp, status = 'old', iostat = ios ) if ( ios /= 0 ) then ierror = 4 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_APPEND - Fatal error!' write ( *, '(a)' ) ' Unexpected error while opening file:' write ( *, '(a)' ) ' ' // trim ( file_temp ) return end if rewind iunit2 ! ! Create a new version of the old file, opening it with ! "STATUS = 'NEW'" so that it is writeable. ! open ( unit = iunit, file = file_name, status = 'new', iostat = ios ) if ( ios /= 0 ) then ierror = 4 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_APPEND - Fatal error!' write ( *, '(a)' ) ' Unexpected error while opening file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if ! ! Read each line from the temporary file, and copy it ! back into the old file. ! do read ( iunit2, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if write ( iunit, '(a)' ) trim ( line ) end do ! ! Delete the temporary file, and return. ! close ( unit = iunit2, status = 'delete' ) return end subroutine file_char_count ( file_name, nchar ) ! !******************************************************************************* ! !! FILE_CHAR_COUNT counts the number of characters in a file. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. ! ! Modified: ! ! 26 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer NCHAR, the number of characters in the file. ! implicit none ! character ( len = * ) file_name integer ios integer iunit character ( len = 256 ) line integer nchar ! nchar = 0 ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then nchar = - 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_CHAR_COUNT - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if ! ! Read the lines, count the characters. ! ! Right now, I don't know how to determine which characters in LINE ! were read from the file and which represent padding. ! ! I tried going to the last nonnull, but it looks like, at least on ! my system, LINE gets filled with blanks. So I'll just count ! til the last non-blank, but that doesn't distinguish between a ! blank that really was in the original file and should be counted, ! and a blank that is just padding. ! do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if nchar = nchar + len_trim ( line ) end do close ( unit = iunit ) return end subroutine file_column_count ( file_name, ncolumn ) ! !******************************************************************************* ! !! FILE_COLUMN_COUNT counts the number of columns in the first line of a file. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. ! ! Most lines of the file is presumed to consist of NCOLUMN words, separated ! by spaces. There may also be some blank lines, and some comment lines, ! which have a "#" in column 1. ! ! The routine tries to find the first non-comment non-blank line and ! counts the number of words in that line. ! ! If all lines are blanks or comments, it goes back and tries to analyze ! a comment line. ! ! Modified: ! ! 21 June 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer NCOLUMN, the number of columns assumed to be in the file. ! implicit none ! character ( len = * ) file_name logical got_one integer ios integer iunit character ( len = 256 ) line integer ncolumn ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', form = 'formatted', & access = 'sequential', iostat = ios ) if ( ios /= 0 ) then ncolumn = - 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COLUMN_COUNT - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if ! ! Read one line, but skip blank lines and comment lines. ! got_one = .false. do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if if ( len_trim ( line ) == 0 ) then cycle end if if ( line(1:1) == '#' ) then cycle end if got_one = .true. exit end do if ( .not. got_one ) then rewind ( iunit ) do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if if ( len_trim ( line ) == 0 ) then cycle end if got_one = .true. exit end do end if close ( unit = iunit ) if ( .not. got_one ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COLUMN_COUNT - Warning!' write ( *, '(a)' ) ' The file does not seem to contain any data.' ncolumn = 0 return end if call word_count ( line, ncolumn ) return end subroutine file_column_range ( file_name, ncolumn, col_min, col_max ) ! !******************************************************************************* ! !! FILE_COLUMN_RANGE determines the minimum and maximum ranges of each column. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. ! ! Each line of the file is presumed to consist of NCOLUMN words, separated ! by spaces. ! ! The routine computes the range of each column. ! ! Modified: ! ! 19 February 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Input, integer NCOLUMN, the number of columns assumed to be in the file. ! ! Output, real COL_MIN(NCOLUM), COL_MAX(NCOLUMN), the minimum and maximum ! for each column. ! implicit none ! integer ncolumn ! real col_max(ncolumn) real col_min(ncolumn) character ( len = * ) file_name integer ierror integer ios integer iunit integer j character ( len = 256 ) line integer nrow real x(ncolumn) ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', form = 'formatted', & access = 'sequential', iostat = ios ) if ( ios /= 0 ) then ncolumn = - 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COLUMN_RANGE - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if nrow = 0 col_min(1:ncolumn) = 0.0E+00 col_max(1:ncolumn) = 0.0E+00 do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if call s_to_rvec ( line, ncolumn, x, ierror ) if ( ierror /= 0 ) then exit end if nrow = nrow + 1 if ( nrow == 1 ) then col_min(1:ncolumn) = x(1:ncolumn) col_max(1:ncolumn) = x(1:ncolumn) else do j = 1, ncolumn col_min(j) = min ( col_min(j), x(j) ) col_max(j) = max ( col_max(j), x(j) ) end do end if end do close ( unit = iunit ) return end subroutine file_copy ( old_file_name, new_file_name, ierror ) ! !******************************************************************************* ! !! FILE_COPY makes a copy of a file. ! ! ! Discussion: ! ! The file is assumed to be sequential access, with variable ! length records. ! ! Modified: ! ! 26 June 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) OLD_FILE_NAME, the name of the file ! to be copied. ! ! Input, character ( len = * ) NEW_FILE_NAME, the name of the copy of ! the file. ! ! Output, integer IERROR, error flag. ! 0, no error occurred. ! 1, the file names are the same. ! 2, a free unit number could not be found for the old file. ! 3, the routine could not open the old file. ! 4, a free unit number could not be found for the new file. ! 5, the routine could not open the new file. ! implicit none ! integer ierror integer ios character ( len = 256 ) line character ( len = * ) new_file_name integer new_unit character ( len = * ) old_file_name integer old_unit ! ierror = 0 ! ! Make sure the file names aren't the same. ! if ( new_file_name == old_file_name ) then ierror = 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COPY - Fatal error!' write ( *, '(a)' ) ' The old and new file names are identical.' return end if ! ! Open the old file. ! call get_unit ( old_unit ) if ( old_unit == 0 ) then ierror = 2 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COPY - Fatal error!' write ( *, '(a)' ) ' Could not get a unit number for the old file.' return end if open ( unit = old_unit, file = old_file_name, status = 'old', & iostat = ios ) if ( ios /= 0 ) then ierror = 3 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COPY - Fatal error!' write ( *, '(a)' ) ' Could not open the old file:' write ( *, '(a)' ) ' ' // trim ( old_file_name ) return end if ! ! Open the new file. ! call get_unit ( new_unit ) if ( new_unit == 0 ) then ierror = 4 close ( unit = old_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COPY - Fatal error!' write ( *, '(a)' ) ' Could not get a free unit for the copy file.' return end if open ( unit = new_unit, file = new_file_name, status = 'replace', & iostat = ios ) if ( ios /= 0 ) then ierror = 5 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_COPY - Fatal error!' write ( *, '(a)' ) ' Could not open the new file:' write ( *, '(a)' ) ' ' // trim ( new_file_name ) close ( unit = old_unit ) return end if ! ! Read an old line, write a new line. ! do read ( old_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if write ( new_unit, '(a)' ) trim ( line ) end do close ( unit = old_unit ) endfile ( unit = new_unit ) close ( unit = new_unit ) return end subroutine file_delete ( file_name ) ! !******************************************************************************* ! !! FILE_DELETE deletes a named file if it exists. ! ! ! Discussion: ! ! You might want to call this routine to get rid of any old copy ! of a file, before trying to open a new copy with the OPEN argument: ! status = 'new'. ! ! It's not always safe to open a file with " STATUS = 'UNKNOWN' ". ! For instance, on the SGI, the most recent version of the FORTRAN ! compiler seems to go crazy when I open an unformatted direct ! access file this way. It creates an enormous file (of somewhat ! random size). The problem goes away if I delete any old copy ! using this routine, and then open a fresh copy with ! " STATUS = 'NEW' ". It's a scary world. ! ! Modified: ! ! 19 February 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! implicit none ! logical file_exist character ( len = * ) file_name integer ios integer iunit logical lfile ! ! Does the file exist? ! if ( .not. file_exist ( file_name ) ) then return end if ! ! Get a free unit number. ! call get_unit ( iunit ) if ( iunit == 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_DELETE: Warning!' write ( *, '(a)' ) ' A free FORTRAN unit could not be found.' return end if write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_DELETE: deleting old version of ' // & trim ( file_name ) open ( unit = iunit, file = file_name, status = 'old', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_DELETE: Warning!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if close ( unit = iunit, status = 'delete' ) return end function file_exist ( file_name ) ! !******************************************************************************* ! !! FILE_EXIST reports whether a file exists. ! ! ! Modified: ! ! 19 February 2002 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, logical FILE_EXIST, is TRUE if the file exists. ! implicit none ! character ( len = * ) file_name logical file_exist ! inquire ( file = file_name, exist = file_exist ) return end subroutine file_get_next_word ( iunit, word, text, num_text, ierror ) ! !******************************************************************************* ! !! FILE_GET_NEXT_WORD returns the next word and trailing context from a file. ! ! ! Discussion: ! ! The file should have been opened before calling this routine. ! The file should contain ASCII text, which can be thought of as ! words separated by one or more blanks. ! ! Modified: ! ! 12 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters ! ! Input, integer IUNIT, the unit number associated with the file. ! ! Output, character ( len = * ) WORD, the next word in the file. If the ! current line of the file is blank, or if the file has been exhausted, ! WORD will be set to ' '. ! ! Input/output, character ( len = * ) TEXT, the remaining text of the line ! that contains the information in WORD. On each call, the next word ! in TEXT is extracted until TEXT is empty, when it is refilled by ! reading another line from the file. Because TEXT contains information ! needed by this routine, it should not be altered by the user ! between calls. ! ! Input/output, integer NUM_TEXT, the number of lines read from the file. ! Before the first call to this routine, the user should set NUM_TEXT ! to 0. ! ! Output, integer IERROR, error flag. ! 0, no error, another word was read, and returned in WORD. ! 1, end of file. WORD and TEXT were set to ' '. ! implicit none ! integer ierror integer ihi integer ilo integer ios integer iunit integer lenc integer num_text character ( len = * ) text character ( len = * ) word ! ierror = 0 ! ! If NUM_TEXT is zero, then initialize TEXT. ! if ( num_text <= 0 ) then num_text = 0 text = ' ' end if ! ! If TEXT is blank, try to read a new line from the file. ! if ( text == ' ' ) then read ( iunit, '(a)', iostat = ios ) text if ( ios /= 0 ) then ierror = 1 word = ' ' text = ' ' return end if num_text = num_text + 1 if ( text == ' ' ) then word = ' ' return end if end if ! ! Extract the next word from TEXT into WORD and return. ! lenc = len_trim ( text ) ! ! Find ILO, the index of the first nonblank in TEXT. ! ilo = 1 do while ( text(ilo:ilo) == ' ' ) ilo = ilo + 1 end do ! ! Find IHI, the index of the last consecutive nonblank after the one at ILO. ! ihi = ilo do while ( ihi+1 <= lenc ) if ( text(ihi+1:ihi+1) == ' ' ) then exit end if ihi = ihi + 1 end do ! ! Set WORD. ! word = text(ilo:ihi) ! ! Slide TEXT to the left. ! if ( ihi+1 <= lenc ) then text = text(ihi+1:) else text = ' ' end if return end subroutine file_insert ( input_unit, output_unit, nrec ) ! !******************************************************************************* ! !! FILE_INSERT copies the contents of an input file into an output file. ! ! ! Discussion: ! ! Both the input and output files should already be opened by the ! user. The routine simply reads a line from the input file and ! writes it to the output file. The input file is assumed to be ! a simple, sequential access text file, with records no longer ! than 256 characters. ! ! Modified: ! ! 06 January 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer INPUT_UNIT, the unit number associated with the input file. ! ! Input, integer OUTPUT_UNIT, the unit number associated with the ! output file. ! ! Output, integer NREC, the number of records copied from the input file ! to the output file. ! implicit none ! integer input_unit integer ios character ( len = 256 ) line integer nrec integer output_unit ! nrec = 0 ! ! Make sure the file names aren't the same. ! if ( input_unit == output_unit ) then return end if do read ( input_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then return end if nrec = nrec + 1 write ( output_unit, '(a)' ) trim ( line ) end do return end subroutine file_line_count ( file_name, nline ) ! !******************************************************************************* ! !! FILE_LINE_COUNT counts the number of lines in a file. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. ! ! Blank lines and comment lines, which begin with '#', are not counted. ! ! Modified: ! ! 21 June 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer NLINE, the number of lines found in the file. ! implicit none ! character ( len = * ) file_name integer ios integer iunit character ( len = 256 ) line integer nline ! nline = 0 ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', form = 'formatted', & access = 'sequential', iostat = ios ) if ( ios /= 0 ) then nline = - 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_LINE_COUNT - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' // trim ( file_name ) return end if ! ! Count the lines. ! do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if if ( len_trim ( line ) == 0 ) then cycle end if if ( line(1:1) == '#' ) then cycle end if nline = nline + 1 end do close ( unit = iunit ) return end subroutine file_line_get ( file_name, nline, line ) ! !******************************************************************************* ! !! FILE_LINE_GET gets a particular line of a file. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. ! ! Modified: ! ! 21 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Input, integer NLINE, the line number to be read. ! ! Output, character ( len = * ) LINE, the text of the line. ! implicit none ! character ( len = * ) file_name integer i integer ios integer iunit character ( len = * ) line integer nline ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', form = 'formatted', & access = 'sequential', iostat = ios ) if ( ios /= 0 ) then line = ' ' write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_LINE_GET - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if ! ! Count the lines. ! do i = 1, nline read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then line = ' ' write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_LINE_GET - Fatal error!' write ( *, '(a)' ) ' Unexpected end of file.' return end if end do close ( unit = iunit ) return end subroutine file_merge ( file_name_1, file_name_2, file_name_3, n1, n2, n3 ) ! !******************************************************************************* ! !! FILE_MERGE merges two sorted files into a third. ! ! ! Modified: ! ! 12 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME_1, FILE_NAME_2, the names of the ! two input files to be merged. ! ! Input, character ( len = * ) FILE_NAME_3, the name of the output file to ! be created. ! ! Output, integer N1, N2, N3, the number of lines of text in the ! two input files and the output file. ! implicit none ! character ( len = * ) file_name_1 character ( len = * ) file_name_2 character ( len = * ) file_name_3 integer file_unit_1 integer file_unit_2 integer file_unit_3 integer ios integer n1 integer n2 integer n3 character ( len = 100 ) word1 character ( len = 100 ) word2 character ( len = 100 ) word3 ! n1 = 0 n2 = 0 n3 = 0 call get_unit ( file_unit_1 ) open ( unit = file_unit_1, file = file_name_1, status = 'old', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_MERGE - Fatal error!' write ( *, '(a)' ) ' Could not open the input file #1.' return end if call get_unit ( file_unit_2 ) open ( unit = file_unit_2, file = file_name_2, status = 'old', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_MERGE - Fatal error!' write ( *, '(a)' ) ' Could not open the input file #2.' return end if call get_unit ( file_unit_3 ) open ( unit = file_unit_3, file = file_name_3, status = 'replace', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_MERGE - Fatal error!' write ( *, '(a)' ) ' Could not open the output file:' write ( *, '(a)' ) ' ' // trim ( file_name_3 ) return end if word1 = ' ' word2 = ' ' do if ( word1 == ' ' ) then read ( file_unit_1, '(a)', iostat = ios ) word1 if ( ios == 0 ) then call s_low ( word1 ) n1 = n1 + 1 end if if ( word1 == ' ' ) then word1 = '_END_' close ( unit = file_unit_1 ) end if end if if ( word2 == ' ' ) then read ( file_unit_2, '(a)', iostat = ios ) word2 if ( ios == 0 ) then call s_low ( word2 ) n2 = n2 + 1 end if if ( word2 == ' ' ) then word2 = '_END_' close ( unit = file_unit_2 ) end if end if if ( word1 == '_END_' .and. word2 == '_END_' ) then exit else if ( word1 /= '_END_' .and. word2 == '_END_' ) then word3 = word1 word1 = ' ' else if ( word1 == '_END_' .and. word2 /= '_END_' ) then word3 = word2 word2 = ' ' else if ( llt ( word1, word2 ) ) then word3 = word1 word1 = ' ' else if ( word1 == word2 ) then word3 = word1 word1 = ' ' word2 = ' ' else word3 = word2 word2 = ' ' end if end if write ( file_unit_3, '(a)' ) trim ( word3 ) n3 = n3 + 1 end do endfile ( unit = file_unit_3 ) close ( unit = file_unit_3 ) return end subroutine file_name_ext_get ( file_name, i, j ) ! !******************************************************************************* ! !! FILE_NAME_EXT_GET determines the "extension" of a file name. ! ! ! Definition: ! ! The "extension" of a filename is the string of characters ! that appears after the LAST period in the name. A file ! with no period, or with a period as the last character ! in the name, has a "null" extension. ! ! Note: ! ! Blanks are unusual in filenames. This routine ignores all ! trailing blanks, but will treat initial or internal blanks ! as regular characters acceptable in a file name. ! ! Examples: ! ! FILE_NAME I J ! ! bob.for 4 7 ! N.B.C.D 6 7 ! Naomi. 6 6 ! Arthur 0 0 ! .com 1 1 ! ! Modified: ! ! 17 July 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, a file name to be examined. ! ! Output, integer I, J, the indices of the first and last characters ! in the file extension. ! ! If no period occurs in FILE_NAME, then ! I = J = 0; ! Otherwise, ! I is the position of the LAST period in FILE_NAME, and J is the ! position of the last nonblank character following the period. ! implicit none ! character ( len = * ) file_name integer i integer j integer s_index_last ! i = s_index_last ( file_name, '.' ) if ( i /= 0 ) then j = len_trim ( file_name ) else j = 0 end if return end subroutine file_name_ext_swap ( file_name, ext ) ! !******************************************************************************* ! !! FILE_NAME_EXT_SWAP replaces the current "extension" of a file name. ! ! ! Definition: ! ! The "extension" of a filename is the string of characters ! that appears after the LAST period in the name. A file ! with no period, or with a period as the last character ! in the name, has a "null" extension. ! ! Examples: ! ! Input Output ! ================ ========= ! FILE_NAME EXT FILE_NAME ! ! bob.for obj bob.obj ! bob.bob.bob txt bob.bob.txt ! bob yak bob.yak ! ! Modified: ! ! 09 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character ( len = * ) FILE_NAME, a file name. ! On output, the extension of the file has been changed. ! ! Input, character ( len = * ) EXT, the extension to be used on the output ! copy of FILE_NAME, replacing the current extension if any. ! implicit none ! character ( len = * ) ext character ( len = * ) file_name integer i integer j integer len_max integer len_name ! len_max = len ( file_name ) len_name = len_trim ( file_name ) call file_name_ext_get ( file_name, i, j ) if ( i == 0 ) then if ( len_name + 1 > len_max ) then return end if len_name = len_name + 1 file_name(len_name:len_name) = '.' i = len_name + 1 else i = i + 1 file_name(i:j) = ' ' end if file_name(i:) = ext return end subroutine file_name_inc ( file_name ) ! !******************************************************************************* ! !! FILE_NAME_INC generates the next filename in a series. ! ! ! Discussion: ! ! It is assumed that the digits in the name, whether scattered or ! connected, represent a number that is to be increased by 1 on ! each call. If this number is all 9's on input, the output number ! is all 0's. Non-numeric letters of the name are unaffected, and ! if the name contains no digits, then nothing is done. ! ! Examples: ! ! Input Output ! ----- ------ ! a7to11.txt a7to12.txt ! a7to99.txt a8to00.txt ! a9to99.txt a0to00.txt ! cat.txt cat.txt ! ! Modified: ! ! 09 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character ( len = * ) FILE_NAME. ! On input, a character string to be incremented. ! On output, the incremented string. ! implicit none ! character c logical ch_is_digit character ( len = * ) file_name integer i integer lens ! lens = len_trim ( file_name ) do i = lens, 1, -1 c = file_name(i:i) if ( ch_is_digit ( c ) ) then call digit_inc ( c ) file_name(i:i) = c if ( c /= '0' ) then return end if end if end do return end subroutine file_para_count ( file_name, npara ) ! !******************************************************************************* ! !! FILE_PARA_COUNT counts the number of paragraphs in a file. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. A paragraph is ! a sequence of nonblank lines. ! ! Modified: ! ! 13 August 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer NPARA, the number of paragraphs found in the file. ! implicit none ! character ( len = * ) file_name integer ios integer iunit integer lenc integer lenc_old character ( len = 256 ) line integer npara ! npara = 0 ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', form = 'formatted', & access = 'sequential', iostat = ios ) if ( ios /= 0 ) then npara = - 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_PARA_COUNT - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if ! ! Count the paragraphs. ! lenc = 0 do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if lenc_old = lenc lenc = len_trim ( line ) if ( lenc > 0 .and. lenc_old <= 0 ) then npara = npara + 1 end if end do close ( unit = iunit ) return end subroutine file_paren_check ( file_name ) ! !******************************************************************************* ! !! FILE_PAREN_CHECK checks a file for generalized parenthesis errors. ! ! ! Discussion: ! ! The check made is that the current number of left parentheses read must ! always be at least as great as the number of right parentheses read. ! Moreover, when we reach the end of the file, the numbers must be equal. ! ! Modified: ! ! 27 September 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! implicit none ! character ( len = * ) file_name integer file_unit integer i integer ios character ( len = 256 ) line integer line_len integer line_num integer sum_p ! sum_p = 0 ! ! Open the file. ! call get_unit ( file_unit ) open ( unit = file_unit, file = file_name, status = 'old', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_PAREN_CHECK - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if line_num = 0 do read ( file_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if line_num = line_num + 1 line_len = len_trim ( line ) do i = 1, line_len if ( line(i:i) == '(' ) then sum_p = sum_p + 1 else if ( line(i:i) == ')' ) then sum_p = sum_p - 1 if ( sum_p < 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_PAREN_CHECK - Warning!' write ( *, '(a)' ) ' Parenthesis error in the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) write ( *, '(a,i6)' ) & ' An illegal right parenthesis occurs on line', & line_num write ( *, '(a)' ) ' ' // trim ( line ) return end if end if end do end do close ( unit = file_unit ) if ( sum_p /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_PAREN_CHECK - Warning!' write ( *, '(a)' ) ' Parenthesis error in the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) write ( *, '(a,i6)' ) ' Number of missing right parentheses: ', sum_p else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_PAREN_CHECK - Note:' write ( *, '(a)' ) ' Parenthesis checks passed for file:' write ( *, '(a)' ) ' ' // trim ( file_name ) end if return end subroutine file_print ( file_name ) ! !******************************************************************************* ! !! FILE_PRINT prints the contents of a text file. ! ! ! Modified: ! ! 20 January 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! implicit none ! character ( len = * ) file_name integer ios integer iunit character ( len = 256 ) line ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_PRINT - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if write ( *, '(a)' ) trim ( line ) end do close ( unit = iunit ) return end subroutine file_reverse_rows ( input_name, output_name ) ! !*********************************************************************** ! !! FILE_REVERSE_ROWS makes a copy of a file with the lines in reverse order. ! ! ! Example: ! ! Input file: ! ! This is the tale ! of three little pigs ! and their tails. ! ! Output file: ! ! and their tails. ! of three little pigs ! This is the tale ! ! Modified: ! ! 28 June 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) INPUT_NAME, the name of the file to ! be reversed. ! ! Input, character ( len = * ) OUTPUT_NAME, the name of the file to be ! created, contained a reversed copy of the input file. ! implicit none ! integer, parameter :: MAXLINE = 500 ! integer i integer input_count character ( len = * ) input_name integer input_unit integer ios character ( len = MAXLINE ) line integer output_count character ( len = * ) output_name integer output_unit ! ! Open the input file. ! call get_unit ( input_unit ) open ( unit = input_unit, file = input_name, status = 'old', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_REVERSE_ROWS - Fatal error!' write ( *, '(a)' ) ' Could not open the input file:' write ( *, '(a)' ) ' ' // trim ( input_name ) return end if ! ! Move to the end of the input file. ! input_count = 0 do read ( input_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if input_count = input_count + 1 end do ! ! Open the output file. ! call get_unit ( output_unit ) open ( unit = output_unit, file = output_name, status = 'replace', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_REVERSE_ROWS - Fatal error!' write ( *, '(a)' ) ' Could not open the output file:' write ( *, '(a)' ) ' ' // trim ( output_name ) return end if ! ! Read backwards. ! backspace ( unit = input_unit, iostat = ios ) do i = input_count, 1, -1 backspace ( unit = input_unit, iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_REVERSE_ROWS - Fatal error!' write ( *, '(a)' ) ' IOS nonzero on backspace.' exit end if read ( input_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)') 'FILE_REVERSE_ROWS - Fatal error!' write ( *, '(a)' ) ' IOS nonzero in read' exit end if write ( output_unit, '(a)' ) trim ( line ) backspace ( unit = input_unit ) end do close ( unit = input_unit ) endfile ( unit = output_unit ) close ( unit = output_unit ) return end subroutine file_reverse_columns ( input_name, output_name ) ! !*********************************************************************** ! !! FILE_REVERSE_COLUMNS makes a copy of a file with each lines reversed. ! ! ! Example: ! ! Input file: ! ! This is the tale ! of three little pigs ! and their tails. ! ! Output file: ! ! elat eht si sihT ! sgip elttil eerht fo ! .sliat rieht dna ! ! Modified: ! ! 28 June 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) INPUT_NAME, the name of the file to ! be reversed. ! ! Input, character ( len = * ) OUTPUT_NAME, the name of the file to be ! created, contained a copy of the input file with the columns reversed. ! implicit none ! integer, parameter :: MAXLINE = 500 ! integer i integer input_count character ( len = * ) input_name integer input_unit integer ios character ( len = MAXLINE ) line integer output_count character ( len = * ) output_name integer output_unit ! ! Open the input file. ! call get_unit ( input_unit ) open ( unit = input_unit, file = input_name, status = 'old', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_REVERSE_COLUMNS - Fatal error!' write ( *, '(a)' ) ' Could not open the input file:' write ( *, '(a)' ) ' ' // input_name return end if ! ! Open the output file. ! call get_unit ( output_unit ) open ( unit = output_unit, file = output_name, status = 'replace', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_REVERSE_COLUMNS - Fatal error!' write ( *, '(a)' ) ' Could not open the output file:' write ( *, '(a)' ) ' ' // trim ( output_name ) return end if do read ( input_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if call s_reverse ( line ) write ( output_unit, '(a)' ) trim ( line ) end do close ( unit = input_unit ) endfile ( unit = output_unit ) close ( unit = output_unit ) return end subroutine file_tag_check ( file_name, left, right ) ! !******************************************************************************* ! !! FILE_TAG_CHECK checks a file for generalized parenthesis errors. ! ! ! Discussion: ! ! The check made is that the current number of left "parentheses" read must ! always be at least as great as the number of right "parentheses" read. ! Moreover, when we reach the end of the file, the numbers must be equal. ! ! Typical examples of left and right parentheses might be: ! ! (), [], {}, <>,

, 'do' 'end do' ! ! Modified: ! ! 27 September 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Input, character ( len = * ) LEFT, RIGHT, the left and right ! parentheses marks. ! implicit none ! character ( len = * ) file_name integer file_unit integer i integer ios character ( len = * ) left integer left_len integer left_pos character ( len = 256 ) line character ( len = 256 ) line_copy integer line_len integer line_num character ( len = * ) right integer right_len integer right_pos integer sum_p ! sum_p = 0 left_len = len ( left ) right_len = len ( right ) ! ! Open the file. ! call get_unit ( file_unit ) open ( unit = file_unit, file = file_name, status = 'old', & form = 'formatted', access = 'sequential', iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_TAG_CHECK - Fatal error!' write ( *, '(a)' ) ' Could not open the input file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if line_num = 0 do read ( file_unit, '(a)', iostat = ios ) line line_copy = line if ( ios /= 0 ) then exit end if line_num = line_num + 1 line_len = len_trim ( line ) do left_pos = index ( line, left ) if ( left_pos == 0 ) then left_pos = line_len + 1 end if right_pos = index ( line, right ) if ( right_pos == 0 ) then right_pos = line_len + 1 end if if ( left_pos < right_pos ) then sum_p = sum_p + 1 line = adjustl ( line(left_pos+left_len:) ) line_len = len_trim ( line ) else if ( right_pos < left_pos ) then sum_p = sum_p - 1 line = adjustl ( line(right_pos+right_len:) ) line_len = len_trim ( line ) if ( sum_p < 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_TAG_CHECK - Warning!' write ( *, '(a)' ) ' Tag error in the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) write ( *, '(a,i6)' ) ' An illegal right tag occurs on line', & line_num write ( *, '(a)' ) ' ' // trim ( line_copy ) return end if else exit end if end do end do close ( unit = file_unit ) if ( sum_p /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_TAG_CHECK - Warning!' write ( *, '(a)' ) ' Tag error in the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) write ( *, '(a,i6)' ) ' Number of missing right tags: ', sum_p else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_TAG_CHECK - Note:' write ( *, '(a)' ) ' Tag checks passed for file:' write ( *, '(a)' ) ' ' // trim ( file_name ) end if return end subroutine file_unique ( input_file_name, input_lines, output_file_name, & output_lines ) ! !******************************************************************************* ! !! FILE_UNIQUE makes a copy of the unique lines of a sorted file. ! ! ! Discussion: ! ! Actually, the input file doesn't have to be sorted. The routine ! simply reads each line of the input file, and writes it to the ! output file if it is distinct from the previous input line. ! ! Modified: ! ! 26 June 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) INPUT_FILE_NAME, the name of the input file. ! ! Output, integer INPUT_LINES, the number of lines in the input file. ! ! Input, character ( len = * ) OUTPUT_FILE_NAME, the name of the output file. ! ! Output, integer OUTPUT_LINES, the number of lines in the output file. ! implicit none ! character ( len = * ) input_file_name integer input_lines integer input_unit integer ios character ( len = 256 ) line character ( len = 256 ) line_old character ( len = * ) output_file_name integer output_lines integer output_unit ! input_lines = 0 output_lines = 0 line_old = ' ' call get_unit ( input_unit ) open ( unit = input_unit, file = input_file_name, status = 'old', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_UNIQUE - Error!' write ( *, '(a)' ) ' Could not open the input file:' write ( *, '(a)' ) ' ' // trim ( input_file_name ) return end if call get_unit ( output_unit ) open ( unit = output_unit, file = output_file_name, status = 'replace', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_UNIQUE - Error!' write ( *, '(a)' ) ' Could not open the output file:' write ( *, '(a)' ) ' ' // trim ( output_file_name ) close ( unit = input_unit ) return end if do read ( input_unit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if input_lines = input_lines + 1 if ( input_lines == 1 .or. line /= line_old ) then write ( output_unit, '(a)' ) trim ( line ) output_lines = output_lines + 1 line_old = line end if end do close ( unit = input_unit ) endfile ( unit = output_unit ) close ( unit = output_unit ) return end subroutine file_word_count ( file_name, nword ) ! !******************************************************************************* ! !! FILE_WORD_COUNT counts the number of words in a file. ! ! ! Discussion: ! ! The file is assumed to be a simple text file. ! ! Modified: ! ! 27 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) FILE_NAME, the name of the file. ! ! Output, integer NLINE, the number of lines found in the file. ! implicit none ! character ( len = * ) file_name integer ios integer iunit character ( len = 256 ) line integer nplus integer nword ! nword = 0 ! ! Open the file. ! call get_unit ( iunit ) open ( unit = iunit, file = file_name, status = 'old', form = 'formatted', & access = 'sequential', iostat = ios ) if ( ios /= 0 ) then nword = - 1 write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'FILE_WORD_COUNT - Fatal error!' write ( *, '(a)' ) ' Could not open the file:' write ( *, '(a)' ) ' ' // trim ( file_name ) return end if ! ! Read the lines. ! do read ( iunit, '(a)', iostat = ios ) line if ( ios /= 0 ) then exit end if call word_count ( line, nplus ) nword = nword + nplus end do close ( unit = iunit ) return end subroutine get_unit ( iunit ) ! !******************************************************************************* ! !! GET_UNIT returns a free FORTRAN unit number. ! ! ! Discussion: ! ! A "free" FORTRAN unit number is an integer between 1 and 99 which ! is not currently associated with an I/O device. A free FORTRAN unit ! number is needed in order to open a file with the OPEN command. ! ! Modified: ! ! 02 March 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer IUNIT. ! ! If IUNIT = 0, then no free FORTRAN unit could be found, although ! all 99 units were checked (except for units 5 and 6). ! ! Otherwise, IUNIT is an integer between 1 and 99, representing a ! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6 ! are special, and will never return those values. ! implicit none ! integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end function len_nonnull ( s ) ! !******************************************************************************* ! !! LEN_NONNULL returns the length of a string up to the last non-null character. ! ! ! Modified: ! ! 26 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S, the string to measure. ! ! Output, integer LEN_NONNULL, the length of the string, up to the last ! non-null character. ! implicit none ! integer i integer len_nonnull integer len_s character, parameter :: NULL = char ( 0 ) character ( len = * ) s ! len_s = len ( s ) do i = len_s, 1, -1 if ( s(i:i) /= NULL ) then len_nonnull = i return end if end do len_nonnull = 0 return end subroutine number_inc ( s ) ! !******************************************************************************* ! !! NUMBER_INC increments the integer represented by a string. ! ! ! Example: ! ! Input Output ! ----- ------ ! '17' '18' ! 'cat3' 'cat4' ! '2for9' '3for0' ! '99thump' '00thump' ! ! Discussion: ! ! If the string contains characters that are not digits, they will ! simply be ignored. If the integer is all 9's on input, then ! the output will be all 0's. ! ! Modified: ! ! 15 January 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character ( len = * ) S, a string representing an integer. ! implicit none ! logical ch_is_digit integer i integer lens character ( len = * ) s ! lens = len_trim ( s ) do i = lens, 1, -1 if ( ch_is_digit ( s(i:i) ) ) then call digit_inc ( s(i:i) ) if ( s(i:i) /= '0' ) then return end if end if end do return end function s_eqi ( s1, s2 ) ! !******************************************************************************* ! !! S_EQI is a case insensitive comparison of two strings for equality. ! ! ! Examples: ! ! S_EQI ( 'Anjana', 'ANJANA' ) is .TRUE. ! ! Modified: ! ! 14 April 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S1, S2, the strings to compare. ! ! Output, logical S_EQI, the result of the comparison. ! implicit none ! character c1 character c2 integer i integer len1 integer len2 integer lenc logical s_eqi character ( len = * ) s1 character ( len = * ) s2 ! len1 = len ( s1 ) len2 = len ( s2 ) lenc = min ( len1, len2 ) s_eqi = .false. do i = 1, lenc c1 = s1(i:i) c2 = s2(i:i) call ch_cap ( c1 ) call ch_cap ( c2 ) if ( c1 /= c2 ) then return end if end do do i = lenc + 1, len1 if ( s1(i:i) /= ' ' ) then return end if end do do i = lenc + 1, len2 if ( s2(i:i) /= ' ' ) then return end if end do s_eqi = .true. return end function s_index_last ( s, sub ) ! !******************************************************************************* ! !! S_INDEX_LAST finds the LAST occurrence of a given substring. ! ! ! Discussion: ! ! It returns the location in the string at which the substring SUB is ! first found, or 0 if the substring does not occur at all. ! ! The routine is also trailing blank insensitive. This is very ! important for those cases where you have stored information in ! larger variables. If S is of length 80, and SUB is of ! length 80, then if S = 'FRED' and SUB = 'RED', a match would ! not be reported by the standard FORTRAN INDEX, because it treats ! both variables as being 80 characters long! This routine assumes that ! trailing blanks represent garbage! ! ! This means that this routine cannot be used to find, say, the last ! occurrence of a substring 'A ', since it assumes the blank space ! was not specified by the user, but is, rather, padding by the ! system. However, as a special case, this routine can properly handle ! the case where either S or SUB is all blanks. ! ! Modified: ! ! 14 April 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S, the string to be searched. ! ! Input, character ( len = * ) SUB, the substring to search for. ! ! Output, integer S_INDEX_LAST. 0 if SUB does not occur in ! the string. Otherwise S_INDEX_LAST = I, where S(I:I+LENS-1) = SUB, ! where LENS is the length of SUB, and is the last place ! this happens. ! implicit none ! integer i integer j integer llen1 integer llen2 character ( len = * ) s integer s_index_last character ( len = * ) sub ! s_index_last = 0 llen1 = len_trim ( s ) llen2 = len_trim ( sub ) ! ! In case S or SUB is blanks, use LEN ! if ( llen1 == 0 ) then llen1 = len ( s ) end if if ( llen2 == 0 ) then llen2 = len ( sub ) end if if ( llen2 > llen1 ) then return end if do j = 1, llen1+1-llen2 i = llen1 + 2 - llen2 - j if ( s(i:i+llen2-1) == sub ) then s_index_last = i return end if end do return end subroutine s_low ( s ) ! !******************************************************************************* ! !! S_LOW replaces all uppercase letters by lowercase ones. ! ! ! Modified: ! ! 19 July 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character ( len = * ) S, the string to be ! transformed. On output, the string is all lowercase. ! implicit none ! integer i character ( len = * ) s ! do i = 1, len_trim ( s ) call ch_low ( s(i:i) ) end do return end subroutine s_reverse ( s ) ! !******************************************************************************* ! !! S_REVERSE reverses the characters in a string. ! ! ! Examples: ! ! Input Output ! ! ' Cat' 'taC ' ! 'Goo gol ' 'log ooG ' ! ! Modified: ! ! 14 April 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, character ( len = * ) S, the string to reverse. ! Trailing blanks are ignored. ! implicit none ! integer i integer j integer nchar character ( len = * ) s ! ! CFT can't handle the string assignment STRING(I:I) = STRING(J:J) ! so we have to do some mumbo jumbo. ! nchar = len_trim ( s ) do i = 1, nchar / 2 j = nchar + 1 - i call ch_swap ( s(i:i), s(j:j) ) end do return end subroutine s_to_r ( s, r, ierror, lchar ) ! !******************************************************************************* ! !! S_TO_R reads a real number from a string. ! ! ! Discussion: ! ! This routine will read as many characters as possible until it reaches ! the end of the string, or encounters a character which cannot be ! part of the real number. ! ! Legal input is: ! ! 1 blanks, ! 2 '+' or '-' sign, ! 2.5 spaces ! 3 integer part, ! 4 decimal point, ! 5 fraction part, ! 6 'E' or 'e' or 'D' or 'd', exponent marker, ! 7 exponent sign, ! 8 exponent integer part, ! 9 exponent decimal point, ! 10 exponent fraction part, ! 11 blanks, ! 12 final comma or semicolon. ! ! with most quantities optional. ! ! Examples: ! ! S R ! ! '1' 1.0 ! ' 1 ' 1.0 ! '1A' 1.0 ! '12,34,56' 12.0 ! ' 34 7' 34.0 ! '-1E2ABCD' -100.0 ! '-1X2ABCD' -1.0 ! ' 2E-1' 0.2 ! '23.45' 23.45 ! '-4.2E+2' -420.0 ! '17d2' 1700.0 ! '-14e-2' -0.14 ! 'e2' 100.0 ! '-12.73e-9.23' -12.73 * 10.0**(-9.23) ! ! Modified: ! ! 12 February 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S, the string containing the ! data to be read. Reading will begin at position 1 and ! terminate at the end of the string, or when no more ! characters can be read to form a legal real. Blanks, ! commas, or other nonnumeric data will, in particular, ! cause the conversion to halt. ! ! Output, real R, the real value that was read from the string. ! ! Output, integer IERROR, error flag. ! ! 0, no errors occurred. ! ! 1, 2, 6 or 7, the input number was garbled. The ! value of IERROR is the last type of input successfully ! read. For instance, 1 means initial blanks, 2 means ! a plus or minus sign, and so on. ! ! Output, integer LCHAR, the number of characters read from ! the string to form the number, including any terminating ! characters such as a trailing comma or blanks. ! implicit none ! logical ch_eqi character c integer ierror integer ihave integer isgn integer iterm integer jbot integer jsgn integer jtop integer lchar integer nchar integer ndig real r real rbot real rexp real rtop character ( len = * ) s character, parameter :: TAB = char ( 9 ) ! nchar = len_trim ( s ) ierror = 0 r = 0.0E+00 lchar = - 1 isgn = 1 rtop = 0.0E+00 rbot = 1.0E+00 jsgn = 1 jtop = 0 jbot = 1 ihave = 1 iterm = 0 do lchar = lchar + 1 c = s(lchar+1:lchar+1) ! ! Blank or TAB character. ! if ( c == ' ' .or. c == TAB ) then if ( ihave == 2 ) then else if ( ihave == 6 .or. ihave == 7 ) then iterm = 1 else if ( ihave > 1 ) then ihave = 11 end if ! ! Comma. ! else if ( c == ',' .or. c == ';' ) then if ( ihave /= 1 ) then iterm = 1 ihave = 12 lchar = lchar + 1 end if ! ! Minus sign. ! else if ( c == '-' ) then if ( ihave == 1 ) then ihave = 2 isgn = - 1 else if ( ihave == 6 ) then ihave = 7 jsgn = - 1 else iterm = 1 end if ! ! Plus sign. ! else if ( c == '+' ) then if ( ihave == 1 ) then ihave = 2 else if ( ihave == 6 ) then ihave = 7 else iterm = 1 end if ! ! Decimal point. ! else if ( c == '.' ) then if ( ihave < 4 ) then ihave = 4 else if ( ihave >= 6 .and. ihave <= 8 ) then ihave = 9 else iterm = 1 end if ! ! Exponent marker. ! else if ( ch_eqi ( c, 'E' ) .or. ch_eqi ( c, 'D' ) ) then if ( ihave < 6 ) then ihave = 6 else iterm = 1 end if ! ! Digit. ! else if ( ihave < 11 .and. lge ( c, '0' ) .and. lle ( c, '9' ) ) then if ( ihave <= 2 ) then ihave = 3 else if ( ihave == 4 ) then ihave = 5 else if ( ihave == 6 .or. ihave == 7 ) then ihave = 8 else if ( ihave == 9 ) then ihave = 10 end if call ch_to_digit ( c, ndig ) if ( ihave == 3 ) then rtop = 10.0E+00 * rtop + real ( ndig ) else if ( ihave == 5 ) then rtop = 10.0E+00 * rtop + real ( ndig ) rbot = 10.0E+00 * rbot else if ( ihave == 8 ) then jtop = 10 * jtop + ndig else if ( ihave == 10 ) then jtop = 10 * jtop + ndig jbot = 10 * jbot end if ! ! Anything else is regarded as a terminator. ! else iterm = 1 end if ! ! If we haven't seen a terminator, and we haven't examined the ! entire string, go get the next character. ! if ( iterm == 1 .or. lchar+1 >= nchar ) then exit end if end do ! ! If we haven't seen a terminator, and we have examined the ! entire string, then we're done, and LCHAR is equal to NCHAR. ! if ( iterm /= 1 .and. lchar+1 == nchar ) then lchar = nchar end if ! ! Number seems to have terminated. Have we got a legal number? ! Not if we terminated in states 1, 2, 6 or 7! ! if ( ihave == 1 .or. ihave == 2 .or. ihave == 6 .or. ihave == 7 ) then ierror = ihave return end if ! ! Number seems OK. Form it. ! if ( jtop == 0 ) then rexp = 1.0E+00 else if ( jbot == 1 ) then rexp = 10.0E+00**( jsgn * jtop ) else rexp = jsgn * jtop rexp = rexp / jbot rexp = 10.0E+00**rexp end if end if r = isgn * rexp * rtop / rbot return end subroutine s_to_rvec ( s, n, rvec, ierror ) ! !******************************************************************************* ! !! S_TO_RVEC reads a real vector from a string. ! ! ! Modified: ! ! 19 February 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S, the string to be read. ! ! Input, integer N, the number of values expected. ! ! Output, real RVEC(N), the values read from the string. ! ! Output, integer IERROR, error flag. ! 0, no errors occurred. ! -K, could not read data for entries -K through N. ! implicit none ! integer n ! integer i integer ierror integer ilo integer lchar real rvec(n) character ( len = * ) s ! i = 0 ilo = 1 do while ( i < n ) i = i + 1 call s_to_r ( s(ilo:), rvec(i), ierror, lchar ) if ( ierror /= 0 ) then ierror = -i exit end if ilo = ilo + lchar end do return end subroutine timestamp ( ) ! !******************************************************************************* ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! ! Example: ! ! May 31 2001 9:45:54.872 AM ! ! Modified: ! ! 31 May 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none ! character ( len = 8 ) ampm integer d character ( len = 8 ) date integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s character ( len = 10 ) time integer values(8) integer y character ( len = 5 ) zone ! call date_and_time ( date, time, zone, values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & trim ( month(m) ), d, y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end subroutine word_count ( s, nword ) ! !******************************************************************************* ! !! WORD_COUNT counts the number of "words" in a string. ! ! ! Modified: ! ! 14 April 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) S, the string to be examined. ! ! Output, integer NWORD, the number of "words" in the string. ! Words are presumed to be separated by one or more blanks. ! implicit none ! logical blank integer i integer lens integer nword character ( len = * ) s ! nword = 0 lens = len ( s ) if ( lens <= 0 ) then return end if blank = .true. do i = 1, lens if ( s(i:i) == ' ' ) then blank = .true. else if ( blank ) then nword = nword + 1 blank = .false. end if end do return end