	PROGRAM SplitwStan
C
C	Version 2.0
C	By Michael Knappertsbusch, 21 September 2023
C
C	!!! Single file processing mode !!!
C
C	Program to correct for frequencies of specimen data if within a sample unequal splits of size
C	fractions were measured AND for standardisation of specimen data across samples.
C	These corrections apply after treatment of outline data with programs KeelWidth13 and DexFlip.
C	Input file to SplitwStan is file Composed_Dflip_ed. It is generated by dexflipping files of 
C	RESKW_Specimen_Species_nCH_COIL to Composed_Dflip, and then editing by manually inserting the
C	splitnumber NSPL at the start of each line, leading to file Composed_Dflip_ed.
C
C	  Explanations:
C	    Splitweighting (correction for unequal splits between size fractions within a sample):
C		For efficiency reasons sometimes unequal sample splits between size fractions within a sample were
C		imaged and analyzed.
C		Example: Only 1 specimen was found in the entire split (1/1 split) of the 250-1000 microns size fraction,
C		while exceedingly many specimens were found in the 125-250 microns and 63-125 microns size fractions.
C		In such a case, for example only 1/4 was used from the 125-250 microns and 63-125 microns fractions.
C		For calculation of statistics (means, medians, etc) it was therefore necessary to compensate for using
C		different splits between size fractions of a sample. In this example this was compensated by listing the
C		measurements from the 250-1000 mu fraction (1/1 split) 1x (NSPL=1), while measurements from the smaller
C		size fractions (1/4 split each) were listed 4x (NSPL=4).
C
C	    Standardisation (correction for unequal sample split numbers across samples. This correction occurs after
C	  	Splitweighting within samples was accomplished. In our Cipero Coast study we decided to account for
C		unequal sample splits across samples by up-scaling (standardizing) each sample to the entire (1/1) split.
C		Example: If in sample X 1/1 split was imaged and analysed and in sample Y only 1/4 split was used,
C		then the measurements from sample Y was listed 4x prior to statistical treatment.
C
C	    In program SplitwStan the split number NSPL (first column of input file) is considered to represent the
C	    combined effect of splitweighting and standardisation.
C
C****	Modified from SplitwStan version 1.0:
C	In version 2 the name of the output file is composed from the sample name and the species.
C	In order to use this version the data must be sorted by sample/species/#Ch/Coil.
C
C
C****	Important note:
C	Program SplitwStan.out is for usage in combination with "Cycle II" programs KeelWidth13.out, and DexFlip.out.
C
C
C
C	Input:
C	  One INPUT file Composed_Dflip_ed, that was obtained with DexFlip.out applied to
C	  RESKW_Specimen_Species_nCH_COIL files from program KeelWidth13.0.out.
C	  After manual editing, the Composed_Dflip_ed file has the splitnumbers (NSPL) in the first column of each record.
C	  The format of the input file has thus the following form (in this order):
C
C	  NSPL,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:
C	  One file with the name INPUT_Spw is generated (Spw stands for splitweighted between size
C	  fractions AND standardized across samples).
C
C	  Note, that in the output file the splitnumbers are not returned any more.
C	  Each specimen data record is now replicated NSPL times according to the splitnumber.
C
C
C	NSPL:     = Combined splitnumber for splitweighting between size fractions per sample and standardization across samples
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	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,NSPL,I
	REAL AGE,DX,DY,R,AREA,D10,D90
	REAL D9010,DMIN,DMAX,PHI1,PHI2
	REAL PHI3,RL,RU
	REAL A,B,RATIO
	CHARACTER*1  COIL
	CHARACTER*2  NCHAMB
	CHARACTER*7  SPECIES
	CHARACTER*17 SPECIMEN
C
	CHARACTER*17 INPUT				! Name of input file is "Composed_Dflip"
	CHARACTER*25 OUTPUT				! Name of output file
	CHARACTER*1 HEADER1				! * in first line
C
C
	WRITE(9,*) '********************************************'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*            Program SplitwStan            *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*   Corrects for unequal size fractions    *'
	WRITE(9,*) '*  within a sample and standardizes across *'
	WRITE(9,*) '*                  samples                 *'
	WRITE(9,*) '* (= preparation for statistical analysis) *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*      . . .single processing mode. . .    *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*   Version 2.0 by Michael Knappertsbusch  *'
	WRITE(9,*) '*                 20.09.2023               *'
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '*                                          *'		
	WRITE(9,*) '* For usage in combination with programs   *'
	WRITE(9,*) '*    KeelWidth13.out, and DexFlip.out      *'	
	WRITE(9,*) '*                                          *'
	WRITE(9,*) '********************************************'
C
C
C
C	First, open input and output files and read the data matrix from the input file:
C
	WRITE(9,*) 'Enter the name of the input file with
     1 data (17 chars long)'
     	WRITE(9,*) ' Example: Composed_Dflip_ed '
	READ(9,'(A17)') INPUT				! Composed_Dflip is input file
C
C	Open input file:
C
	OPEN(10,FILE=INPUT,STATUS='OLD')
C
C	Generate the name of the OUTPUT file:
C	First read characters from the first two lines
C
	READ(10,'(A1)') HEADER1				! First line
	READ(10,'(A1)') HEADER1				! Advance to second line
C
	READ(10,30) NSPL,SPECIMEN,AGE,N,SPECIES,	! Advance to line 3, read NSPL,SPECIMEN,AGE,N,SPECIES,NCHAMB,COIL
     1 NCHAMB,COIL
C
	IF (NCHAMB.EQ.' 3') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_3'
	ELSE IF (NCHAMB.EQ.' 4') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_4'
	ELSE IF (NCHAMB.EQ.' 5') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_5'
	ELSE IF (NCHAMB.EQ.' 6') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_6'
	ELSE IF (NCHAMB.EQ.' 7') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_7'
	ELSE IF (NCHAMB.EQ.' 8') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_8'
	ELSE IF (NCHAMB.EQ.' 9') THEN			! Special cases if NCHAMB is less than 10 (e.g. 1 char instead of 2)
	  NCHAMB='_9'
	END IF
C	
 30	FORMAT(I2,2X,A17,2X,F5.2,2X,I4,2X,A7,
     1 1X,A2,2X,A1)
C
C	Compose name of output file (25 characters long):
C
	OUTPUT=SPECIMEN(1:6)//'_'//SPECIES//
     1 '_'//NCHAMB//'Ch_'//COIL//'_Spw'
C
	OPEN(20,FILE=OUTPUT,STATUS='NEW')
C
	REWIND(10)					! Reset cursor of INPUT file at beginning of file.
C
C
C
C
C	Read the first line of the header block in INPUT and write it to the OUTPUT file.
C
	OPEN(10,FILE=INPUT,STATUS='OLD')
C
	READ(10,'(A1)') HEADER1				! In Composed_Dflip the first line is a * so, that it can be read by Cricket Graph.
	WRITE(20,'(A1)') HEADER1
C
C	Write column labels to OUTPUT file
C
	WRITE(20,40)					! Labelling columns in OUTPUT file.
 40	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	Advance to the second line of the INPUT file by reading a character of it:
C
	READ(10,'(A1)') HEADER1
C
C
C***	Main loop starts here:
C
C
C	Read data lines from INPUT file:
C
 45	READ(10,50,END=999) NSPL,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
 50	FORMAT(I2,2X,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	Replicate data line into OUTPUT file according to the split number (NSPL), but without writing NSPL:
C
	DO 60,I=1,NSPL
 	WRITE(20,70) 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
 60	CONTINUE
C
 70	FORMAT(A17,',',X,F5.2,',',X,I4,',',X,A7,
     1 ',',A2,',',X,A1,',',X,F6.1,',',X,F6.1,
     2 ',',X,F6.4,',',X,F7.4,',',X,F7.2,',',X,F7.2,
     3 ',',X,F7.2,',',X,F7.2,',',X,F7.2,',',X,F7.3,
     4 ',',X,F7.3,',',X,F7.2,',',6X,F7.2,',',5X,F7.3,
     5 ',',5X,F7.3,',',X,F8.3,',',X,F8.3)
C
	GOTO 45
C
 999	WRITE(9,*) 'Sample splitweighted and standardized'
 	WRITE(9,*) '...One new file ',OUTPUT,' created...'
C
 	PAUSE 100
 100	CONTINUE
C
	STOP
	END
