	PROGRAM DexFlip
C
C	Version 1.0
C	By Michael Knappertsbusch, 14 September 2015
C
C	!!! batch processing mode !!!
C
C	This program composes data that were created with KeelWidth13.out. (for example from RESKW_files)
C	and simultaneously mirrors coiling dependent parameters in dextral forms.
C	
C
C	Explanation:
C	Tests of menardiform globorotalids show sinistral or dextral coiling with varying proportions depending on
C	species and ages. Because all specimens were fixed in the slide preparations with the spiral side always
C	facing to the left side, the final chamber chamber of a sinistral form is directed to the bottom of the
C	image when seen in keel view, while in a dextral forms the final chamber is directed to the top of the image.
C	For geometric analysis this difference must be accounted for if coiling dependent parameters are considered.
C	Parameters, that depend on the coiling direction include D10, D90, PHI1, PHI2, ARESLO, ARESUO, RL, and RU.
C	Coiling-invariant parameters are NCHAMB, DX, DY, R, AREA, D9010, DEV, A, B, RATIO, PHI3, DMIN, and DMAX.
C
C	In order to compare geometric measurements of sinsitral tests with dextral ones, the pairs of coiling
C	dependent parameters were exchanged against each other if specimens showed dextral coiling. This operation
C	is nothing else than vertically mirroring the image for coiling dependent parameters.
C
C	This program was adapted from Compose6.0.f.
C	(In fact, program DexFlip is identical to Compose6.0 but with the additional feature of mirroring
C	coiling dependent variables in dextrally coiling tests).
C
C
C****	Important note:
C	DexFlip.out is for usage in combination with "Cycle II" programs KeelWidth13.out, Compose6.0.out,
C	Stat_Prep5.0.out and Stats9.0.out. Make sure you use the correct versions of these
C	programs for your analysis !
C
C
C
C	Input files:
C	  1.) FILE_LIST, contains the names of all individual files to be treated.
C	  2.) The individual files with the measurements of each specimen. The files,
C	      that contain the measurements have the general name
C	      "RESKW_sample_species_nChamb_COIL". They were generated with program
C	      KeelWidt13.out.
C	      There are two options for data input:
C
C	      If SEL is equal to 0, the input variables are (in this order):
C	      SPECIMEN,AGE,N,SPECIES,NCHAMB,COIL,DX,DY,R,AREA,D10,D90,D9010,DMIN,DMAX,PHI1,PHI2,ARESLO,ARESUP,DEV,Phi3,RL,RU
C
C	      Alternatively, if SEL is equal to 1, the input variables are (in this order):
C	      SPECIMEN,AGE,N,SPECIES,NCHAMB,COIL,DX,DY,R,AREA,D10,D90,D9010,DMIN,DMAX,PHI1,PHI2,A,B,RATIO,Phi3,RL,RU
C
C
C	Output files:
C		One file with the name "Composed_Dflip" is generated (Dflip stands for dextrally flipped).
C		In each file "RESKW_sample_species_nChamb_COIL" the program strips off 
C		the header information and writes the morphometric
C		data into "Composed_files". This file can be read with a commercial data
C		plotting program (Cricket Graph, Excel) to generate scatter plots.
C
C	SEL:      = If SEL=0, input data contain ARESLO, ARESUP and DEV. If SEL=1, input data contain A, B and RATIO instead.
C	SPECIMEN: = Name of the specimen in a sample
C	AGE:      = Age, in million years
C	N:        = Number of points in original outline
C	SPECIES:  = Name of the species (given as a 7 chars long code)
C	NCHAMB:   = Number of chambers in last whorl
C	COIL:     = Coiling direction
C	DX,DY:    = Width (spiral height) and length in keel view (in micrometers, mu)
C	R:        = Ratio of DX/DY
C	AREA:     = Area in keel view, in mm2
C	D10:	  = Lower relative keelwidth (at 10% axial extension), expressed as fraction of the width of the shell (D10/DX).
C	D90	  = Upper relative keelwidth (at 90% axial extension), expressed as fraction of the width of the shell (D90/DX).
C	D9010	  = Asolute difference of (D90-D10), expressed as fraction of the width of the shell [ABS(D90-D10)/DX].
C	DMIN:	  = DMIN=MIN(D10,D90)/DX
C	DMAX:	  = DMAX=MAX(D10,D90)/DX
C	PHI1:	  = Upper keel angle, in degrees (d)
C	PHI2:	  = Lower keel angle, in degrees (d)
C	PHI3:	  = Spiral angle in keel view, in degrees (d)
C	ARESLO:	  = Lower keel residual area, in mm2 
C	ARESUP:	  = Upper keel residual area, in mm2
C	DEV:	  = Deviation of total keel residual area from the value of AREA, in per cent.
C	A:	  = Spiral convexity A in micrometers (mu)
C	B:        = Umbilical convexity B in micrometers (mu)
C	RATIO:    = A/B
C	RL,RU:    = Radii of Lower and Upper keel region (osculating) circles, in micrometers (mu).
C
C
	INTEGER N,SEL,NSEL
	REAL AGE,DX,DY,R,AREA,D10,D90
	REAL D9010,DMIN,DMAX,PHI1,PHI2
	REAL ARESLO,ARESUP,DEV,PHI3,RL,RU
	REAL A,B,RATIO
	CHARACTER*1  COIL
	CHARACTER*2  NCHAMB
	CHARACTER*7  SPECIES
	CHARACTER*17 SPECIMEN
	CHARACTER*20 FILE_LIST			! FILE_LIST reads the name of the file containing the INPUT files. 
	CHARACTER*1  HEADER1
	CHARACTER*31 INPUT			! Reads the filenames of "RESKW_sample_species_nChamb_COIL"
C
C
C
	WRITE(9,*) '********************************************'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*               Program DexFlip            *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*   Composes data and mirrors coiling-     *'
	WRITE(9,*) '*  dependent parameters for dextral forms  *'
	WRITE(9,*) '*     (= preparation for plotting data)    *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*      . . .batch processing mode. . .     *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*   Version 1.0 by Michael Knappertsbusch  *'
	WRITE(9,*) '*                 14.09.2015               *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*                                          *'		
	WRITE(9,*) '* For usage in combination with programs   *'
	WRITE(9,*) '*    KeelWidth13.out, Compose6.0.out,      *'
	WRITE(9,*) '*    Stat_Prep5.0.out, and Stats9.0.out    *'	
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '********************************************'
C
C
C
C	First, information is needed whether SEL is 0 or 1:
C	Get name of the file list containing the input files,
C	and start reading the INPUT file names.
C
	WRITE(9,*) 'Enter the name of the list with
     1 all samples (max 20 chars long)'
     	READ(9,'(A20)') FILE_LIST
C
C
C	Open FILE_LIST, read the first INPUT file name from it:
	OPEN(13,FILE=FILE_LIST,STATUS='OLD')
	READ(13,'(A31)',END=999) INPUT
C
C	Open that INPUT file, and read the first 4 lines of the header block in it:
C
	OPEN(12,FILE=INPUT,STATUS='OLD')
C
	DO 3 I=1,4
	  READ(12,'(A1)') HEADER1
 3	CONTINUE
C
C	Get SEL in line 5:
C
	READ(12,4) SEL
 4	FORMAT(I1)
C
	CLOSE(12)				! Close first INPUT file
	REWIND(13)				! Keep FILE_LIST open, but set pointer to start
C
C
C
C	Now, open output file, and write a * in the first line in it so, that
C	it can be read by Cricket Graph. Then write column labels depending on the value of SEL:
C
	OPEN(14,FILE='Composed_Dflip',STATUS='NEW')
	WRITE(14,'(A1)') '*'
	IF (SEL.EQ.0) THEN
	  WRITE(14,2)				! Labelling columns if SEL=0 (output with ARESLO, ARESUP, and DEV)
	END IF
	IF (SEL.EQ.1) THEN
	  WRITE(14,7)				! Labelling columns if SEL=1 (output with convexities A, B, and RATIO)
	END IF
C
C
 2	FORMAT('Specimen,            Age
     1,#pts,Species,#Ch,Coil,DX (mu),DY (mu)
     2,R=DX/DY,Ar (mm2),  D10 %,   D90 %,   D9010%
     3,  Dmin%,   Dmax%, Phi1(d), Phi2(d)
     4, ARESLO (mm2),  ARESUP (mm2), DEV (%)
     5,   Phi3(d),  RL (mu),  RU (mu)')
C
 7	FORMAT('Specimen,            Age
     1,#pts,Species,#Ch,Coil,DX (mu),DY (mu)
     2,R=DX/DY,Ar (mm2),  D10 %,   D90 %,   D9010%
     3,  Dmin%,   Dmax%,  Phi1(d), Phi2(d)
     4, A (mu),        B (mu),    Ratio A/B
     5,   Phi3(d),  RL (mu),  RU (mu)')
C
C
C
C
C
C***	Main sample loop starts here:
C
C	Read an INPUT file from FILE_LIST:
 5	READ(13,'(A31)',END=999) INPUT
 	WRITE(9,6) INPUT
 6	FORMAT('. . .Sample ',A31,'. . .')
C
C	Open the INPUT file, and read the header block (first 4 lines) in it:
C
	OPEN(15,FILE=INPUT,STATUS='OLD')
C
	DO 10 I=1,4
	  READ(15,'(A1)') HEADER1
 10	CONTINUE
C
	READ(15,4) NSEL					! For comparison of SEL in each individual INPUT file
	IF (NSEL.NE.SEL) THEN				! Check, whether all INPUT files contain the same variables
	  WRITE(9,*) 'Error: Mixed values of SEL'	
	  WRITE(9,*) '    Check input files'
	  WRITE(9,*) '     Program stopped'
	  PAUSE 1001
 1001	  CONTINUE
 	  CLOSE(14,STATUS='DELETE')
	  STOP
	END IF
C
C	Read line 6 with column labels:
C
	READ(15,'(A1)') HEADER1
C
C
C	Read subsequent data lines in that file:
C
	IF (SEL.EQ.0) THEN
 20	READ(15,30,END=99) SPECIMEN,AGE,N,SPECIES,
     1 NCHAMB,COIL,DX,DY,R,AREA,D10,D90,
     2 D9010,DMIN,DMAX,PHI1,PHI2,
     3 ARESLO,ARESUP,DEV,PHI3,RL,RU
C
 30	FORMAT(A17,2X,F5.2,2X,I4,2X,A7,
     1 1X,A2,2X,A1,2X,F6.1,2X,F6.1,
     2 2X,F6.4,2X,F7.4,2X,F7.2,2X,F7.2,
     3 2X,F7.2,2X,F7.2,2X,F7.2,2X,F7.3,
     4 2X,F7.3,2X,F7.4,7X,F7.4,6X,F7.3,
     5 6X,F7.3,2X,F8.3,2X,F8.3)
C
C	Dextral mirroring: If there is a dextral form exchange columns of pairs with coiling dependent
C	parameters (D10,D90; PHI1,PHI2; ARESLO,ARESUP; and RL,RU). Then write data to output file:
C
	IF (COIL.EQ.'d') THEN		! Dextral coiling test: exchange coiling dependent parameters.
C
	WRITE(14,40) SPECIMEN,AGE,N,SPECIES,
     1 NCHAMB,COIL,DX,DY,R,AREA,D90,D10,
     2 D9010,DMIN,DMAX,PHI2,PHI1,
     3 ARESUP,ARESLO,DEV,PHI3,RU,RL
C
	ELSE				! Sinistral coiling test: Leave parameters unchanged.
C
	WRITE(14,40) SPECIMEN,AGE,N,SPECIES,
     1 NCHAMB,COIL,DX,DY,R,AREA,D10,D90,
     2 D9010,DMIN,DMAX,PHI1,PHI2,
     3 ARESLO,ARESUP,DEV,PHI3,RL,RU
C
	END IF
C
 40	FORMAT(A17,', ',F5.2,', ',I4,', ',A7
     1',',A2,', ',A1,', ',F6.1,', ',F6.1
     2', ',F6.4,', ',F7.4,', ',F7.2,', ',F7.2
     3', ',F7.2,', ',F7.2,', ',F7.2,', ',F7.3
     4', ',F7.3,', ',F7.4,',      ',F7.4,
     5',     ',F7.3,',     ',F7.3,', ',F8.3
     6,', ',F8.3)
C
	GOTO 20		! Read next data line
	END IF
C
C
C
	IF (SEL.EQ.1) THEN
 25	READ(15,31,END=99) SPECIMEN,AGE,N,SPECIES,
     1 NCHAMB,COIL,DX,DY,R,AREA,D10,D90,
     2 D9010,DMIN,DMAX,PHI1,PHI2,
     3 A,B,RATIO,PHI3,RL,RU
C
 31	FORMAT(A17,2X,F5.2,2X,I4,2X,A7,
     1 1X,A2,2X,A1,2X,F6.1,2X,F6.1,
     2 2X,F6.4,2X,F7.4,2X,F7.2,2X,F7.2,
     3 2X,F7.2,2X,F7.2,2X,F7.2,2X,F7.3,
     4 2X,F7.3,2X,F7.2,7X,F7.2,6X,F7.3,
     5 6X,F7.3,2X,F8.3,2X,F8.3)     
C
C	Dextral mirroring: If there is a dextral form exchange columns of pairs with coiling dependent
C	parameters (D10,D90; PHI1,PHI2 and RL,RU). Then write data to output file:
C
	IF (COIL.EQ.'d') THEN		! Dextral coiling test: exchange coiling dependent parameters.
C
	WRITE(14,41) SPECIMEN,AGE,N,SPECIES,
     1 NCHAMB,COIL,DX,DY,R,AREA,D90,D10,
     2 D9010,DMIN,DMAX,PHI2,PHI1,
     3 A,B,RATIO,PHI3,RU,RL
C
	ELSE				! Sinistral coiling test: Leave parameters unchanged.
C
	WRITE(14,41) SPECIMEN,AGE,N,SPECIES,
     1 NCHAMB,COIL,DX,DY,R,AREA,D10,D90,
     2 D9010,DMIN,DMAX,PHI1,PHI2,
     3 A,B,RATIO,PHI3,RL,RU
C
	END IF
C
 41	FORMAT(A17,', ',F5.2,', ',I4,', ',A7
     1',',A2,', ',A1,', ',F6.1,', ',F6.1
     2', ',F6.4,', ',F7.4,', ',F7.2,', ',F7.2
     3', ',F7.2,', ',F7.2,', ',F7.2,', ',F7.3
     4', ',F7.3,', ',F7.2,',      ',F7.2,
     5',     ',F7.3,',     ',F7.3,', ',F8.3
     6', ',F8.3)
C
	GOTO 25		! Read next data line     
	END IF
C
C
 99	CLOSE(15)	! Close actual sample file
 	GOTO 5		! Read next sample in FILE_LIST
C
 999	WRITE(9,*) 'One file "Composed_Dflip" created'
 	PAUSE 1000
 1000	CONTINUE
 	STOP
 	END
	
