subroutine read_prefs( max_t_mthd, max_t_valu, max_t_step ) implicit none real max_t_valu, max_t_step integer i, ic, L, i_lab_beg, i_lab_end, len_lab, i_val_beg, & i_val_end character(10) max_t_mthd character(20) label character(80) line logical file_exists, found_max_t_mthd, found_max_t_valu, & found_max_t_step c *** Set default values in case the preference file can not be read. max_t_mthd = 'model' max_t_valu = 240.0 max_t_step = 20.0 found_max_t_mthd = .false. found_max_t_valu = .false. found_max_t_step = .false. c *** Check to see if the preferences file is available. If not, c *** return the default values. inquire(file='State/prefs.txt',exist=file_exists) if( .not. file_exists ) return c *** Parse the preference file looking for key, value pairs. open(unit=1,file='State/prefs.txt') do L=1, 20000 read(1,'(a80)',end=60) line c *** Skip a comment line flagged with '#' in the first column. if( line(1:1) .eq. '#' ) goto 40 c *** Get the position of the label. A blank line is skipped. i_lab_beg = verify( line, ' ' ) if( i_lab_beg .eq. 0 ) goto 40 i_lab_end = index(line(i_lab_beg+1:),' ')-1 + i_lab_beg len_lab = i_lab_end-i_lab_beg+1 label = line(i_lab_beg:i_lab_end) c *** Check for non-alpha characters in the label and convert and c *** upper case characters to lower case. do i=1, len_lab ic = ichar(label(i:i)) if( ic .lt. 48 .or. & (ic .gt. 57 .and. ic .lt. 65) .or. & (ic .gt. 90 .and. ic .lt. 95) .or. & ic .eq. 96 .or. & ic .gt. 122 ) then print *, 'ERROR: Invalid character in label = ', & trim(label) stop end if if( ic .ge. 65 .and. ic .le. 90 ) then label(i:i) = achar(ic+32) end if end do c *** The string 'eof' signifies the end of file. if( trim(label) .eq. 'eof' ) goto 60 c *** Determine the position of the parameter value. i_val_beg = verify( line(i_lab_end+1:), ' ' ) + i_lab_end if( i_lab_beg .eq. i_lab_end ) then print *, 'ERROR: no value found for label ', trim(label) stop end if i_val_end = index(line(i_val_beg+1:),' ')-1 + i_val_beg c *** Match the label with the corresponding parameter and set its c *** value. select case( label(1:len_lab) ) case( 'max_t_mthd' ) read(line(i_val_beg:i_val_end),'(a10)',err=50) max_t_mthd found_max_t_mthd = .true. case( 'max_t_valu' ) read(line(i_val_beg:i_val_end),*,err=50) max_t_valu found_max_t_valu = .true. case( 'max_t_step' ) read(line(i_val_beg:i_val_end),*,err=50) max_t_step found_max_t_step = .true. end select if( found_max_t_mthd .and. found_max_t_valu .and. & found_max_t_step ) goto 60 40 continue end do 50 print *, 'ERROR: Malformed entry for ', trim(label) stop 60 close(1) return end