	PROGRAM GRID
C
C	Version 2.2, 4.3.2005, by M. Knappertsbusch.
C	Modified from Version 2.1 from 6.4.1998.
C	Modified 18. August 1999 from Version 2.0.
C
C	. . .Batch operation mode. . .
C
C	Verbesserte Version: Dimension der Matrizen erhoeht
C	(kann nun bis 100x100 gridding files machen).
C	DX und DY koennen jetzt variabel gewaehlt werden.
C	Bereich des Gridding geht von 0 - XMax in X Richtung,
C	und von                       0 - YMax in Y Richtung.
C	Ausgabe wurde verbessert.
C
C	Modification 18. August 1999: Removed header info from
C	"rotated files", program usage is now less confusing to 
C	other persons.
C
C	Gridding program for C. leptoporus.
C	Program calculates bivariate frequencies from X,Y
C	data. Binwidth in X is DX (diameter), and in Y
C	it is DY (elements).
C
C	It applies the same grid as in Contour plot by
C	Dave Lazarus.
C
C	Arrays: FREQ(I,J), with I=Diameter (=X) and J=Elements (=Y).
C
C	LABELX andLABELY are to indicate the categories in the
C	output file.
C
C
	INTEGER FREQ(1:100,1:100)
	REAL LABELX(1:100),LABELY(1:100)
	REAL XMAX,YMAX
	INTEGER I,J,K
	INTEGER NX,NY
	REAL X,Y,DX,DY
	CHARACTER*20 LIST
	CHARACTER*24 INPUT
	CHARACTER*25 STRING,OUTPUT
	CHARACTER*1 TAB
C
	WRITE(9,*) '. . .Gridding X,Y data, version 2.2. . .'
	WRITE(9,*) '     Batch operation mode'
	WRITE(9,*) ' '
	WRITE(9,*) 'The program reads a file containing the'
	WRITe(9,*) 'names of all files to be gridded'
	WRITE(9,*) '(The individual filenames must have names'
	WRITE(9,*) 'WITH EXACTLY 20 CHARACTERS)'
	WRITE(9,*) ' '
C
	TAB=CHAR(9)		! Making a tabulator. Note, this is Macintosh specific.
C
	WRITE(9,*) 'Enter upper limit of X and Y (Xmax,Ymax)'
	READ(9,*) XMAX,YMAX
	WRITE(9,*) 'Enter intervals (DX,DY)'
	READ(9,*) DX,DY
C
C	Determine number of intervals in X and Y direction
C
	NX=NINT(XMAX/DX)
	NY=NINT(YMAX/DY)
	WRITE(9,*) NX,' X-intervals'
	WRITE(9,*) NY,' Y-intervals'
C
C	File name handling:
C
	WRITE(9,*) '. . .Enter list with sample names. . .'
	WRITE(9,*) '            (Max. 20 chars)'
	READ(9,1) LIST
 1	FORMAT(A20)
C
C	Open that file list and read sample names as variable STRING, and derive names of
C	INPUT and OUTPUT files from STRING (K indicates the number of files gridded):
C
	K=0
	OPEN(14,FILE=LIST,STATUS='OLD')
 2	READ(14,3,END=99) STRING
 3	FORMAT(A25)
C
	INPUT=STRING(1:20)
	OUTPUT=STRING(1:20)//'.gridded'
C
C	Now, open current input and output files:
C
	OPEN(15,FILE=INPUT,STATUS='OLD')
	OPEN(16,FILE=OUTPUT,STATUS='NEW')
C
C	Reading header lines of rotated X,Y data:
C
 8	FORMAT(A1)
C
C	Initialize the gridding procedure:
C	Initialize FREQ (I=Diameter, J=Elements):
C
	DO 10,J=1,NY
	  DO 5, I=1,NX
	    FREQ(I,J)=0
 5	  CONTINUE
 10	CONTINUE
C
C
C	Determine Labels for X and Y values:
C
	DO 11, I=1,NX
	  LABELX(I)=I*DX
 11	CONTINUE
C
	DO 12, I=1,NY
	  LABELY(I)=I*DY
 12	CONTINUE
C
C
 15	READ(15,*,END=999) X,Y
C
C	Now gridding the data:
C	I runs for Diameter, J runs for Elements.
C
	DO 40, J=1,NY
	  DO 30, I=1,NX
	  IF (((J-1)*DY.LE.Y).AND.(Y.LT.J*DY)) THEN
	    IF (((I-1)*DX.LE.X).AND.(X.LT.I*DX)) THEN
	      FREQ(I,J)=FREQ(I,J)+1
	    END IF
	  END IF
 30	  CONTINUE
 40	CONTINUE
	GOTO 15
C
C	Writing results to output file:
C
 999	WRITE(16,50) OUTPUT
	WRITE(16,86) 'DeltaX, DeltaY, Number of X-intervals,
     * Number Y-intervals:'
	WRITE(16,*) DX,',',DY,',',NX,',',NY
	WRITE(16,8) ' '
 	WRITE(16,70) 'DY I, DX -',TAB,
     *(LABELX(I)-DX,'-',LABELX(I),TAB,I=1,NX)
 	WRITE(16,*) ' '
 	DO 60, J=1,NY
 	  WRITE(16,80) LABELY(J)-DY,'-',LABELY(J),
     *' : ',TAB,(FREQ(I,J),TAB,I=1,NX)
 60	CONTINUE
C
	CLOSE(15)
	CLOSE(16)
	K=K+1
	GOTO 2
C
 50	FORMAT(A25)
 70	FORMAT(A10,A1,100(F6.1,A1,F6.1,1A))
 80	FORMAT(F6.1,A1,F6.1,A3,A1,100(I4,1A))
 85	FORMAT(A52)
 86	FORMAT(A58)
C
 99	WRITE(9,*) K,' files gridded'
 	PAUSE 100
 100	CONTINUE
 	STOP
	END