c$Header: /weru/cvs/weps/weps.src/manage/old.src/mfinit.for.keep,v 1.1.1.1 1999-03-12 17:05:28 wagner Exp $ c c subroutine mfinit i (i_sr, fname) c c + + + PURPOSE + + + c Mfinit should be called during the initialization stage of the the c main weps program. Mfinit searches the management data file; marking c the start sections of each subregion, while storing the number of c years in each subregion's management cycle. c c + + + KEYWORDS + + + c tillage, management file, initialization c c + + + PARAMETERS AND COMMON BLOCKS + + + *$noereference include 'p1werm.inc' include 'manage/man.inc' include 'manage/asd.inc' *$reference c c + + + ARGUMENT DECLARATIONS + + + integer i_sr character*30 fname c c + + + ARGUMENT DEFINITIONS + + + c i_sr - current subregion c fname - management file name c c + + + LOCAL VARIABLES + + + integer maxsr, j, sr, ios c c record length of the management file (actual, max, min) integer x integer maxrsz, minrsz c c + + + SUBROUTINES CALLED + + + c asdini - aggregate size distribution initialization c is this really a good place to call asdini? c c + + + FUNCTION DECLARATONS + + + c c + + + DATA INITIALIZATIONS + + + do 01 sr=1, mnsub start(sr) = 0 curnt(sr) = 0 01 continue maxsr = 0 c keep maxrsz less than 80 maxrsz = 78 minrsz = 60 c c + + + END SPECIFICATIONS + + + c c Attempt to find out what the record length is. c We should be able to deal with both DOS and UNIX c derived MANAGEMENT files. c They must not be mixed though!!! Ie. some lines cannot c contain the DOS CRLF combination at the end of a line c some of them only UNIX LF at the end of a line. c ALL lines or records must all be of equal length!!!!!! c c Note: We still have a problem reading DOS format files c under UNIX. I think the CR gives the UNIX FORTRAN c fits, but don't know why - LEW c c Hmmm, we might want to eventually do a check to see if the c record (line) length is constant throughout the entire file c to guarantee compatibility. Would also help run down a c problem like someone entering tabs in the MANAGEMENT file c (FORTRAN won't like them anyway but it could help find them). open (unit=10, file=fname, access='direct', status='old', & form='unformatted', recl=maxrsz, err=30) c Get at least one whole record - hopefully more than one line read (10, rec=1, iostat=ios, err=30) mline(:maxrsz) C print*, mline c Find the last printable ASCII character in the record ('<') do 02 x=minrsz, maxrsz if(mline(x:x).eq.'<') then goto 03 endif 02 continue c Determine the "record length" which is dependent upon the c whether it is a Unix or PC format ASCII file or c We do this by reading the 2nd and 3rd lines guessing at c the correct record length. If we can read both lines in c correctly then we feel confident we have determined the c correct record length. 03 do 05 rcl=maxrsz, minrsz, -1 close (10) open (unit=10, file=fname, access='direct', status='old', & form='unformatted', recl=rcl, err=30) read (10, rec=2, end=20, iostat=ios, err=30) mline(:rcl) C print*, mline c Last printable ASCII char match what we found in the 1st line? if (mline(x:x).eq.'<') then read (10, rec=3, end=20, iostat=ios,err=30) mline(:rcl) if (mline(x:x).eq.'<') then if ((rcl-x) .eq. 1) then C print*, C & 'MANAGEMENT file record length (UNIX format) is: ', C & rcl goto 09 elseif ((rcl-x) .eq. 2) then C print *, C & 'MANAGEMENT file record length (DOS format) is: ', C & rcl goto 09 else C print *, C & 'MANAGEMENT file record length (Translated format) is: ', C & rcl goto 09 endif endif endif 05 continue c c Read in datafile, marking start positions of each subregion. c 09 j = 1 c First need to find the version of the management file we are c going to read. All files should now have a version #. ANH 08 read (10, rec=j, iostat=ios, end=20, err=30) mline(:rcl) if (mline (1:9).eq.'Version: ') then c We have found the version # of the management file c Lets see what it is C print*, 'Using ',mline(1:9), mline(10:13), ' for the management file' if (mline(10:13).ne.'1.00') then print*, 'Version 1.00 is needed for this release.' print*, 'You need to convert the file to the correct format.' stop endif c Don't want to find the "*START" line in the management file c prior to finding the "Version: #" line. elseif (mline (1:6).eq. '*START') then print *, & 'ERROR: Found *START before finding management file version' stop else j=j+1 goto 08 endif c Found the "Version: #" line so get ready to read in the next line. j=j+1 c not *EOF read c Looking for the "*START" line now for each subregion. 10 read (10, rec=j, iostat=ios, end=20, err=30) mline(:rcl) if (mline (1:6).eq.'*START') then c A start position was found, which subregion is it for? read (mline (8:10), '(i3)') sr c 'maxsr' holds biggest subregion # found in MANAGEMENT file. if (sr. gt. maxsr) then maxsr=sr if (maxsr. gt. mnsub) then print*, & 'ERROR: Inconsistency with number of subregions:' print *, maxsr, ' found in MANAGEMENT file but', & mnsub, ' is absolute maximum possible for program' stop endif if (maxsr .gt. n_sr) then print*, & 'WARNING: Inconsistency with number of subregions:' print *, maxsr, ' found in MANAGEMENT file but', & n_sr, ' specified for simulation run' endif endif c c Obtain the number of years for the subregion's management cycle read (mline (12:14), '(i3)') period(sr) c 'start' points to where the subregion starts. (the start flag) start(sr)=j c Initialize the curnt record ptr to the next line and c read in the first line for that subregion curnt(sr)=j+1 c not *END (for that subregion) read 15 read (10, rec=curnt(sr), iostat=ios, err=30) manlin(sr)(:rcl) if (manlin(sr) (1:1) .eq. 'D') go to 18 if (manlin(sr) (1:4) .eq. '*EOF') then print *, & 'ERROR: Found *EOF before finding *END for subregion ', & sr, ' in MANAGEMENT file' stop endif if (manlin(sr) (1:6) .eq. '*START') then print *, & 'ERROR: Found *START before finding *END for subregion ', & sr, ' in MANAGEMENT file' stop endif if (manlin(sr) (1:4) .ne. '*END') then curnt(sr) = curnt(sr)+1 go to 15 else c End of this subregion section found ('*END') c Set to zero if no management practices for subregion. c (j=0 if no man practices found?) j = curnt(sr) curnt(sr) = 0 goto 19 endif 18 j = curnt(sr) 19 continue c C Used for debugging purposes C Output info about each subregion's management cycle C print *, 'Management cycle is ', period(sr), C & ' years for Subregion ', sr C print *, 'The *START line is: ', start(sr) C & ' first operation line is: ', curnt(sr) endif j=j+1 if (mline (1:4) .ne.'*EOF') go to 10 c 20 if (maxsr.lt.n_sr) then print*, & 'ERROR: Inconsistency with number of subregions:' print *, maxsr, ' found in management file but', & n_sr, ' specified for simulation run' stop endif c call asdini() goto 40 c 30 print*, 'Error opening/reading the data file within mfinit.for' &,' ios=',ios,' ',mline 40 end c c$Log: not supported by cvs2svn $ c Revision 1.7 1998/09/03 18:34:18 jt c check in a full copy of WEPS - I hope ??? c c Revision 1.6 1995/11/07 15:56:29 jt c Changed the way we read management file to find the end of the line. c c Revision 1.5 1995/09/22 19:14:55 jt c changed error print statement #30 to say that error occurred in 'mfinit.for' c c Revision 1.4 1995/09/18 17:03:00 wagner c Added modifications necessary for WATCOM compiler c c Revision 1.3 1995/09/13 15:54:44 wagner c Necessary changes made to allow FORTRAN src files (*.for) to use the c extended FORTRAN include statement rather than the MICROSOFT $INCLUDE c directive as previously used. This is required to allow use of other c FORTRAN compilers. c c Changes have been made to the prologue.mk, epilogue.mk, and the Unix c master startup.mk files as well as the src files. c c Revision 1.2 1995/03/09 19:54:16 jt c changed a reference of WERM to WEPS c c Revision 1.1.1.1 1995/01/18 04:19:54 wagner c Initial checkin c c Revision 1.21 1994/04/19 18:52:11 jt c added stuff for management f file input c c Revision 1.20 1992/10/17 18:47:44 wagner c Hopefully, we can now read in either DOS or UNIX format c input files and mfinit handle the differences automatically. c c Revision 1.19 1992/10/14 04:30:52 wagner c *** empty log message *** c c Revision 1.18 1992/10/13 19:41:56 wagner c Reduces minimum line length of a MANAGEMENT file c c Revision 1.17 1992/10/13 08:31:05 wagner c Major changes involving reading management file. c c Revision 1.16 1992/10/11 06:25:09 wagner c Added code to determine fixed record length. c Appears that DOS format still gives UNIX compiler c grief, but don't know why. Must have something to do with CR's. c Also set current line pointer for each subregion to zero if c no operations are in the management cycle for those subregions. c Some additional error checking is now performed also. c c Revision 1.15 1992/10/10 21:15:07 wagner c Converted TILLAGE submodel to MANAGEMENT submodel. c c Revision 1.14 1992/06/29 20:14:35 dudley c added fortran coding conventions c