	Program MagCorr
C
C	By Michael Knappertsbusch
C	Version 2.0, 4.9.2008, modified from version 1
C	For AMOR3 from bachelor thesis of Lukas Widmer (2008).
C
C	This program corrects the magnifications, that are determined and displayed
C	in AMOR3 (formula file Programme/AMOR3/settins.ini), into the correct values
C	at the zoom wheel of the Leica MZ6 microscope. The method is by segment-wise
C	linear interpolation (segments I through VIII) as described in section 3 of
C	Knappertsbusch (2008). Fine calibration of AMOR3. MorphCol Supplement #11,
C	September, 2008.
C
C
C	Input is file INPUT (i.e. an ascii text file called list_of_files, which AMOR3
C	generates and which contains the name of each image taken together with the
C	corresponding (AMOR3 calculated) magnification. The format of this information
C	is explained in the following example:
C
C		3001r,1.90
C
C	The first 4 characters indicate the specimen number in that slide (characters 1-2
C	are the field number, characters 3-4 are the specimen [usually 01 when working with
C	AMOR3]). The fifth character is a lowercase r, indicating a raw file format (required
C	by the program Trace_AMOR1_batch.out). The sixth charcter is a comma (can be used herein
C	as a delimiter between specimen number and magnification). The following four characters
C	indicate the magnification as calculated from AMOR3.
C
C
C	Output is file OUTPUT (the file containing the image file names and corresponding
C	corrected magnifications). The output file can the be imported into
C	program Trace_AMOR1_batch.out for outline extraction.

C	Variables:
C	X = Magnification returned by AMOR3
C	Y = Corrected magnification
C
C
	INTEGER N
	DOUBLE PRECISION X,Y
	CHARACTER*13 INPUT
	CHARACTER*20 OUTPUT
	CHARACTER*5 IMAGE
C
	WRITE(9,*) '***********************************'
	WRITE(9,*) '*                                 *'
	WRITE(9,*) '*           MagCorr               *'
	WRITE(9,*) '* Corrects the magnification      *'
	WRITE(9,*) '*        from AMOR3               *'
	WRITE(9,*) '*                                 *'
	WRITE(9,*) '* Version 2 from 4 September 2008 *'
	WRITE(9,*) '*    by Michael Knappertsbusch    *'
	WRITE(9,*) '*                                 *'
	WRITE(9,*) '***********************************'
	WRITE(9,*) ' '
	WRITE(9,*) '. . .Enter the "list_of_files" from AMOR3. . .'
	READ(9,5) INPUT
 5	FORMAT(A13)
C
C
C***	Generating an output file for the corrected magnifications:
C
	WRITE(9,*) '. . .Enter an output name. . .'
	WRITE(9,*) '              (max 20 chars)'
	READ(9,6) OUTPUT
 6	FORMAT(A20)
	OPEN(15,FILE=OUTPUT,STATUS='NEW')
C
C
C***	Write a header line for the output to the screen:
C
	WRITE(9,*) 'SPECIMEN #, MagAMOR3, Corrected Mag'
C
C
C
C***	Opening and reading the input file:
C
	OPEN(14,FILE=INPUT,STATUS='OLD')
	N=0						! N=Counter for the files in list_of_files
 10	READ(14,*,END=100) IMAGE,X
 	N=N+1
C
C
	IF (X.LT.0.8) THEN				! [X < 0.80; segment I ]
	  Y=1.030303*X-0.0242424
	ELSE IF ((X.GE.0.8).AND.(X.LT.1.01)) THEN	! [0.80 ² X < 1.01; segment II ]
	  Y=0.9523809*X+0.0380953
	ELSE IF ((X.GE.1.01).AND.(X.LT.1.25)) THEN	! [1.01 ² X < 1.25; segment III ]
	  Y=1.0416666*X-0.0520833
	ELSE IF ((X.GE.1.25).AND.(X.LT.1.59)) THEN	! [1.25 ² X < 1.59; segment IV ]
	  Y=1.0294117*X-0.0367647
	ELSE IF ((X.GE.1.59).AND.(X.LT.1.975)) THEN	! [1.59 ² X < 1.975; segment V ]
	  Y=1.038961*X-0.051948
	ELSE IF ((X.GE.1.975).AND.(X.LT.2.505)) THEN	! [1.975 ² X < 2.505; segment VI ]
	  Y=0.9433962*X+0.1367925
	ELSE IF ((X.GE.2.505).AND.(X.LT.3.13)) THEN	! [2.505 ² X < 3.13; segment VII ]
	  Y=1.12*X-0.3056
	ELSE IF (X.GE.3.13) THEN			! [3.13 ² X; segment VIII ]
	  Y=0.9876543*X+0.108642
	END IF
C
C
C***	Output:
C
	WRITE(9,50) IMAGE,X,Y				! Output of results to screen
	WRITE(15,60) IMAGE,Y				! Output of results to file
 50	FORMAT(A5,'      ,   ',F4.2,'  ,   ',F4.2)
 60	FORMAT(A5,',',F4.2)
	GOTO 10
C
C
 100	WRITE(9,*) ' '
 	WRITE(9,*) '. . .',N,' magnifications corrected. . .'
	PAUSE 101
 101	CONTINUE
C
	STOP
	END
	
	