c
C This version: The target plane is angled at 45 degrees to the X,Y plane. If the collector face plane 
c is sloping at 45 deg, the target plane will be vertical.
c
c This version: Position data obtained from input file. Output includes reflected ray intersections
c on the target plane, written to a file, WITH THE X,Y ORDER REVERSED, so that scatter plots
c correspond to visual images when looking up at the target. Each hexagonal reflector is given
c spherical curvature (and results could be interpreted as coming from circular reflectors).
c
	PROGRAM IMAGE02SPHEREV
c To determine data for a scatter plot representing the image formed on the collector target plane. 
c Data for any number of hexagonal mirror units are read (one unit at a time) from a file, and seven 
c (x,y) image points per unit (centre and six vertices at radius RAD) are written to an output file.
c (1) For the vertex (x,y) positions on the reflector plane, the mirror normals are angled inwards
c by INANG degrees.
c (2) For the input sun elevation GAMMA, the mirror is rotated on its axis for best aim from its 
c centre, and the reflection vectors from the non-central points are found, allowing for displacement
c of those points (assumed not on the mirror axis of rotation).
c 
c The reflection vectors are intersected with the target, and positions added to the output file.

c Input:
c 1) Input mirror unit data file; output scatter plot file
c 2) Gamma used for output
c 3) Offset radius to vertices in original units (e.g. mm), inwards angle at vertices (degrees)

	IMPLICIT NONE
c Variables with an 'E' refer to offset (vertex) mirror elements, with an 'N' = mirror normal,
c variables with 'STAR' refer to rotated axes: the y* axis is aligned with
c negative mirror axis, x* is in the xy (horizontal) plane, z* is normal to both.
	REAL*8 XP,YP,ZP, L,XA,YA,ZA, C11,C12,C13,C21,C22,
	1	C23,C31,C32,C33,GAMMA, XN,YN,ZN, XNSTAR,YNSTAR,ZNSTAR,
	2	XNE(6),YNE(6),ZNE(6),XNESTAR(6),YNESTAR(6),ZNESTAR(6),
	3	R,RE(6), X2PRI,Y2PRI,Z2PRI, D,DOLD,XOLD,YOLD,ZOLD, 
	4	TH1,A,INCR, OFFY,OFFX, RDEG, HEIGHT,XDIST,YDIST,
	5	THETA,THETAPRI(6),T, XERR,YERR,ERRDIST, ENDROT, SGN,  
	6	PI,ANG,OFFXSTAR(6),OFFYSTAR(6),OFFZSTAR(6), XESTAR(6),
	7	YESTAR(6),ZESTAR(6),XE2PRI(6),YE2PRI(6),ZE2PRI(6), 
	8	XE(6),YE(6),ZE(6), RAD, INANG, VLEN, ZERR, DSCALE
	PARAMETER (INCR=0.0001, ENDROT=150.0, RDEG=57.29577951, 
	1	PI=3.141592654)
	INTEGER I
	CHARACTER FIL*50

      PRINT*,'Mirror unit datafile:'
	READ 5, FIL
	OPEN (1,FILE=FIL, STATUS='OLD')
    5	FORMAT (A)

      PRINT*,'Scatter plot output file:'
	READ 5, FIL
	OPEN (2,FILE=FIL, STATUS='UNKNOWN')

	PRINT*,'Gamma (degrees) for output:'
	READ*,  GAMMA

	PRINT*, 'Radius to vertices in original units (e.g. mm),'
	PRINT*, 'vertex inwards angle (degrees):'
	READ*, RAD, INANG

c Read axis and receiver vectors (normalization will be calc'd); positions in mm or m here).
	DO WHILE (.TRUE.)

      	READ (1,*, END=999) XP,YP,ZP, XA,YA,ZA, L

C The sign of YA determines which side of the collector the chosen reflector is on,
c and therefore how to set up the reflector normal and its rotation about its axis.
	SGN = SIGN(1.0D0,YA) * (-1.0D0)

	ANG = PI/2.0 - ACOS(1.0/L)
C Note that the following places the reflector symmetrically w.r.t. its axis.
C Vertex offsets set clockwise starting at 6 o'clock.
	OFFXSTAR(1) = RAD
	OFFYSTAR(1) = 0.0
	OFFZSTAR(1) = 0.0
	OFFXSTAR(2) = 0.5*RAD
	OFFYSTAR(2) = -0.866*RAD*COS(ANG)
	OFFZSTAR(2) = 0.866*RAD*SIN(ANG) * SGN
	OFFXSTAR(3) = -0.5*RAD
	OFFYSTAR(3) = -0.866*RAD*COS(ANG)
	OFFZSTAR(3) = 0.866*RAD*SIN(ANG) * SGN
	OFFXSTAR(4) = -1.0*RAD
	OFFYSTAR(4) = 0.0
	OFFZSTAR(4) = 0.0
	OFFXSTAR(5) = -0.5*RAD
	OFFYSTAR(5) = 0.866*RAD*COS(ANG)
	OFFZSTAR(5) = -0.866*RAD*SIN(ANG) * SGN
	OFFXSTAR(6) = 0.5*RAD
	OFFYSTAR(6) = 0.866*RAD*COS(ANG)
	OFFZSTAR(6) = -0.866*RAD*SIN(ANG) * SGN


	HEIGHT = ZP
	XDIST = XP
	YDIST = YP

	A = SQRT(XP**2 + YP**2 + ZP**2)
	XP = XP/A
	YP = YP/A
	ZP = ZP/A

c Set up the elements of the axis conversion array.
	C33 = SQRT(1.0-ZA**2)
	C11 = -YA/C33 * SGN
	C12 = XA/C33 * SGN
	C13 = 0.0
	C21 = -XA * SGN
	C22 = -YA * SGN
	C23 = -ZA * SGN
	C31 = -XA*ZA/C33
	C32 = -YA*ZA/C33


	R = SQRT(1.0-1.0/L**2)
	YNSTAR = -1.0/L * SGN

c >>>>>> Part (1) — setting the mirror directions at the vertex (or 'edge') positions. For spherical mirror,
c the directions can be fixed at the start.

	XNSTAR = 0.0
	ZNSTAR = -R

	VLEN = RAD/TAN(INANG/RDEG)
	Print*, 'VLEN=', VLEN

	DO 60 I=1,6
		XNESTAR(I) = OFFXSTAR(I) + XNSTAR*VLEN
		YNESTAR(I) = OFFYSTAR(I) + YNSTAR*VLEN
		ZNESTAR(I) = OFFZSTAR(I) + ZNSTAR*VLEN
		A = SQRT(XNESTAR(I)**2 + YNESTAR(I)**2 + ZNESTAR(I)**2)
		XNESTAR(I) = XNESTAR(I)/A
		YNESTAR(I) = YNESTAR(I)/A
		ZNESTAR(I) = ZNESTAR(I)/A
   60	CONTINUE




c Find length and angle in the (x*,z*) plane, and then the angular difference (theta') from centre vector.
	THETA = 90.0
      DO 94 I=1,6
	RE(I) = SQRT(XNESTAR(I)**2 + ZNESTAR(I)**2)
	T = ATAN(ZNESTAR(I)/XNESTAR(I))*RDEG
	IF (T .LT. 0.0) T = T + 180.0
	THETAPRI(I) = T - THETA

CCCC	Print*, 'THETAPRI=', THETAPRI(I)
   94 CONTINUE


c >>>>>>> Part (2) — find intersections with target at the output value of gamma.

c Find angle around axis for minimum pointing error at mirror centre.
	DOLD = 999.0
	DO 180 THETA=30.0,ENDROT,INCR
		XNSTAR = -R*COS(THETA/RDEG)
		ZNSTAR = -R*SIN(THETA/RDEG)
		XN = C11*XNSTAR + C21*YNSTAR + C31*ZNSTAR
		YN = C12*XNSTAR + C22*YNSTAR + C32*ZNSTAR
		ZN = C13*XNSTAR + C23*YNSTAR + C33*ZNSTAR
		A = 2.0*(XN*COS(GAMMA/RDEG) + ZN*SIN(GAMMA/RDEG))
		X2PRI = A*XN - COS(GAMMA/RDEG)
		Y2PRI = A*YN
		Z2PRI = A*ZN - SIN(GAMMA/RDEG)
		D = (X2PRI-XP)**2 + (Y2PRI-YP)**2 + (Z2PRI-ZP)**2
		IF (D.GT.DOLD) THEN
			GO TO 190
		ELSE
			DOLD = D
			XOLD = X2PRI
			YOLD = Y2PRI
			ZOLD = Z2PRI
		ENDIF
  180	CONTINUE
  190	THETA = THETA-INCR
C      Print*, 'Theta-pt2 = ', theta

C	print*, x2pri,y2pri,z2pri
c^^^^^^^^^^^^^^^^^^^^^^^^^^^^


c Rotate position offset vectors and mirror normals for edge points, and convert to base coords.
      DO 191 I=1,6
	XESTAR(I) = OFFXSTAR(I)*COS((THETA-90.0)/RDEG) - 
	1	OFFZSTAR(I)*SIN((THETA-90.0)/RDEG)
	YESTAR(I) = OFFYSTAR(I)
	ZESTAR(I) = OFFXSTAR(I)*SIN((THETA-90.0)/RDEG) + 
	1	OFFZSTAR(I)*COS((THETA-90.0)/RDEG)
	XE(I) = C11*XESTAR(I) + C21*YESTAR(I) + C31*ZESTAR(I)
	YE(I) = C12*XESTAR(I) + C22*YESTAR(I) + C32*ZESTAR(I)
	ZE(I) = C13*XESTAR(I) + C23*YESTAR(I) + C33*ZESTAR(I)

C	print*, xe(i),ye(i),ze(i)
C*****************************

	XNESTAR(I) = -RE(I)*COS((THETA+THETAPRI(I))/RDEG)
c	(YNESTAR = const)
	ZNESTAR(I) = -RE(I)*SIN((THETA+THETAPRI(I))/RDEG)
	XNE(I) = C11*XNESTAR(I) + C21*YNESTAR(I) + C31*ZNESTAR(I)
	YNE(I) = C12*XNESTAR(I) + C22*YNESTAR(I) + C32*ZNESTAR(I)
	ZNE(I) = C13*XNESTAR(I) + C23*YNESTAR(I) + C33*ZNESTAR(I)
c	print*, xne,yne,zne
c^^^^^^^^^^^^^^^^^^^^^^^^^^^^

c Find reflection vectors from edge elements.
	A = 2.0*(XNE(I)*COS(GAMMA/RDEG) + ZNE(I)*SIN(GAMMA/RDEG))
	XE2PRI(I) = A*XNE(I) - COS(GAMMA/RDEG)
	YE2PRI(I) = A*YNE(I)
	ZE2PRI(I) = A*ZNE(I) - SIN(GAMMA/RDEG)

C	print*, xe2pri,ye2pri,ze2pri
c^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  191 CONTINUE


c Output results for centre element.
c (using chord length instead of arc length — no sig diff when small.)
c	PRINT*,'Mirror rotation angle (degrees) =', REAL(THETA)
c	PRINT*,'Pointing error (milliradians, then vector) at centre:'
c	PRINT*,REAL(SQRT(DOLD)*1000.0),REAL(XOLD-XP),REAL(YOLD-YP),
c	1	REAL(ZOLD-ZP)

c Intersect reflection vectors with target plane, and report (x,y) posns in 
c original units. First the mirror centre:
	T = HEIGHT/ZOLD
	XERR = (T*XOLD)-XDIST
	YERR = (T*YOLD)-YDIST
C>>>>>>>>>>>N.B. Output reversed so that plots correspond to a view of the target from below.
C	WRITE (2,195)  -REAL(YERR), -REAL(XERR)

C+++++++++++++++++++++++++++++++++++++++++++++++++++++

c	Centre reflection intersecting with Vertical plane:
	T = (XDIST+HEIGHT)/(XOLD+ZOLD)
	XERR = T*XOLD-XDIST
	YERR = T*YOLD-YDIST
	ZERR = T*ZOLD-HEIGHT
C>>>>>>>>>>>N.B. Output reversed so that plots correspond to a view of the target from below.
	WRITE (2,195)  -REAL(YERR), -REAL(XERR*1.41421)
  195   FORMAT (F9.4,F9.4,F9.4)

c	PRINT*,'x,y,z errors (on the Vertical target) for centre:'
c	PRINT*, -REAL(YERR), -REAL(XERR*1.41421), REAL(ZERR*1.41421)

C++++++++++++++++++++++++++++++++++++++++++++++++++++++

c	ERRDIST = SQRT(XERR**2 +YERR**2)
c	PRINT*,'Target: error distance and (x,y) for mirror centre.'
c	PRINT*,REAL(ERRDIST), REAL(XERR), REAL(YERR)


c Then edge elements:
      DO 196 I=1,6
c	T = (HEIGHT-ZE(I))/ZE2PRI(I)
c	XERR = ((T*XE2PRI(I))+XE(I)) - XDIST
c	YERR = ((T*YE2PRI(I))+YE(I)) - YDIST

C>>>>>>>>>>>N.B. Output reversed so that plots correspond to a view of the target from below.
C	WRITE (2,195)  -REAL(YERR), -REAL(XERR*1.41421)
C  196 CONTINUE
C       WRITE (2,*)

c++++++++++++++++++++++++++++++++++++++++++++++++

	DSCALE = (XDIST-XE(I) + HEIGHT-ZE(I)) / (XE2PRI(I)+ZE2PRI(I))
C	print*, dscale
	XERR = XE(I) + DSCALE*XE2PRI(I)-XDIST
	YERR = YE(I) + DSCALE*YE2PRI(I)-YDIST
	ZERR = ZE(I) + DSCALE*ZE2PRI(I)-HEIGHT
	WRITE (2,195) -REAL(YERR),-REAL(XERR*1.41421) !!!!,REAL(ZERR*1.41421)
  196 CONTINUE
        WRITE (2,*)

c++++++++++++++++++++++++++++++++++++++++++++++++

	ENDDO

  999 STOP
	END

