!  PMatch.f90 

!	This module is used to share global data with various procedures
MODULE PM_DATA
	IMPLICIT NONE  

!	Global variables

	REAL*8 N_Pump, N_Sgnl, N_Idlr, DKxyz(3)
	REAL*8 Theta_Sgnl_Guess, Theta_Idlr_Guess
	REAL*8 Lambda_Red, Lambda_Green, Lambda_Blue
	REAL*8 WRed, WGreen, WBlue
	REAL*8 D1, Detect_Width, Detect_Angle
	REAL*8 Theta_Tilt_Ext, Theta_Tilt, Phi_Tilt
	INTEGER*4 PMF_Resolution

	REAL*8, PARAMETER :: Pi = 3.141592653589793238

!	Parameters to be passed in via array

	INTEGER*4 Crystal, TypePM, XRes, YRes, ZRes
	REAL*8 XMin, XMax, YMin, YMax
	REAL*8 Crystal_Length, Temperature, Pump_Width, Lambda_Pump
	REAL*8 Theta_Pump, Phi_Pump, Lambda_Sgnl, Theta_Sgnl,  Phi_Sgnl
	REAL*8 Lambda_Idlr, Theta_Idlr, Phi_Idlr, TargetPMFVal
	REAL*8 Cube_Tilt_H, Cube_Tilt_V, Theta_Cut, Phi_Cut
	LOGICAL Angle_External, AutoRange, Stop_On_Err, Use_Previous

!	The positions of each parameter in the array

	INTEGER, PARAMETER :: XMIN_POS = 1
	INTEGER, PARAMETER :: XMAX_POS = 2
	INTEGER, PARAMETER :: XRes_POS = 3
	INTEGER, PARAMETER :: YMIN_POS = 4
	INTEGER, PARAMETER :: YMAX_POS = 5
	INTEGER, PARAMETER :: YRes_POS = 6
	INTEGER, PARAMETER :: ZRes_POS = 7
	INTEGER, PARAMETER :: AUTORANGE_POS      = 8 
	INTEGER, PARAMETER :: ANGLE_EXTERNAL_POS = 9
	INTEGER, PARAMETER :: STOP_ON_ERR_POS    = 10
	INTEGER, PARAMETER :: USE_PREVIOUS_POS   = 11
	INTEGER, PARAMETER :: CRYSTAL_POS        = 12
	INTEGER, PARAMETER :: TYPEPM_POS         = 13
	INTEGER, PARAMETER :: Crystal_Length_Pos = 14 
	INTEGER, PARAMETER :: Temperature_Pos    = 15	
	INTEGER, PARAMETER :: Pump_Width_Pos     = 16
	INTEGER, PARAMETER :: Lambda_Pump_Pos    = 17
	INTEGER, PARAMETER :: Theta_Cut_Pos      = 18
	INTEGER, PARAMETER :: Phi_Cut_Pos        = 19
	INTEGER, PARAMETER :: Cube_Tilt_H_Pos    = 20
	INTEGER, PARAMETER :: Cube_Tilt_V_Pos    = 21
	INTEGER, PARAMETER :: Theta_Pump_Pos     = 22
	INTEGER, PARAMETER :: Phi_Pump_Pos       = 23
	INTEGER, PARAMETER :: Lambda_Sgnl_Pos    = 24
	INTEGER, PARAMETER :: Theta_Sgnl_Pos     = 25
	INTEGER, PARAMETER :: Phi_Sgnl_Pos       = 26
	INTEGER, PARAMETER :: Lambda_Idlr_Pos    = 27
	INTEGER, PARAMETER :: Theta_Idlr_Pos     = 28
	INTEGER, PARAMETER :: Phi_Idlr_Pos       = 29
	INTEGER, PARAMETER :: Target_PMF_Pos     = 30

	INTEGER, PARAMETER :: Num_Of_Params = 30 

!	Plot Type Constants
	INTEGER, PARAMETER :: PLOT_2D    = 0
	INTEGER, PARAMETER :: PLOT_3D    = 1
	INTEGER, PARAMETER :: PLOT_POLAR = 2

!	Plot Name Constants
	INTEGER, PARAMETER :: INDEX_WAVELENGTH      = 0
	INTEGER, PARAMETER :: INDEX_TEMPERATURE     = 1
	INTEGER, PARAMETER :: SEPARATOR_1           = 2
	INTEGER, PARAMETER :: DIFFN_PHI_THETA       = 3
	INTEGER, PARAMETER :: MAXPMF_PHI_THETA      = 4
	INTEGER, PARAMETER :: PMFCOL_PHI_THETA      = 5
	INTEGER, PARAMETER :: PMFCOL_LAMBDA_THETA   = 6
	INTEGER, PARAMETER :: SEPARATOR_2           = 7
	INTEGER, PARAMETER :: THETAOPT_LAMBDA       = 8
	INTEGER, PARAMETER :: PMF_LAMBDA_THETA      = 9
	INTEGER, PARAMETER :: PMFINT_LAMBDA_THETA   = 10
	INTEGER, PARAMETER :: PMF_LAMBDA_PHI        = 11
	INTEGER, PARAMETER :: DEFF_LAMBDA_PHI       = 12
	INTEGER, PARAMETER :: PMFSPREAD_LAMBDA      = 13
	INTEGER, PARAMETER :: SEPARATOR_3           = 14
	INTEGER, PARAMETER :: OPTPMF_THETA_PHI      = 15
	INTEGER, PARAMETER :: OPTPMF_X_Y            = 16
	INTEGER, PARAMETER :: PMF_X_Y               = 17
	INTEGER, PARAMETER :: SEPARATOR_4           = 18
	INTEGER, PARAMETER :: PMF_THETASIG_THETAIDL = 19
	INTEGER, PARAMETER :: PMF_THETAIDL_PHIIDL   = 20
	INTEGER, PARAMETER :: THETAOPT_THETAPUMP    = 21
	INTEGER, PARAMETER :: PMF_THETAPUMP_THETASGNL = 22
	INTEGER, PARAMETER :: PMFSPREAD_THETA_PUMP  = 23
	INTEGER, PARAMETER :: ANGLE_TEST            = 24
	INTEGER, PARAMETER :: REFLECTANCE_PHI       = 25
	INTEGER, PARAMETER :: REFLECTANCE_LAMBDA_PHI= 26

	INTEGER, PARAMETER :: Num_Of_Plots = 27

!	Global variables

	CHARACTER*(256)PlotName, XName, YName
	INTEGER Params_Used(Num_Of_Params)
	INTEGER Plot_Type, PlotSize(3)
	LOGICAL XAxisIsAngle, YAxisIsAngle
	LOGICAL StatusOK, Wavelength_Violation

END MODULE PM_DATA


!***************************************************************************
!
!		Housekeeping Subroutines and Functions for the PMatch DLL
!
!***************************************************************************

SUBROUTINE INITIALIZE(NumPlots, NumCrystals, NumParams, PlotSizePos)
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS : "INITIALIZE" :: INITIALIZE
	USE CRYSTAL_DATA; USE PM_DATA
	IMPLICIT NONE

	INTEGER, INTENT(INOUT) :: NumPlots, NumCrystals, NumParams, PlotSizePos

!	Begin Procedure	

	NumCrystals = NumberOfCrystals
	NumPlots = Num_Of_Plots
	NumParams = Num_Of_Params
	PlotSizePos = XMIN_POS

END SUBROUTINE INITIALIZE

SUBROUTINE GET_CRYSTAL_NAME(Number_In, Name_Out)
!DEC$ ATTRIBUTES STDCALL, DLLEXPORT, ALIAS : "GET_CRYSTAL_NAME" :: GET_CRYSTAL_NAME
!DEC$ ATTRIBUTES VALUE :: Number_In
!DEC$ ATTRIBUTES REFERENCE :: Name_Out
	USE CRYSTAL_DATA
	IMPLICIT NONE

	INTEGER, INTENT(IN) :: Number_In
	CHARACTER*(256), INTENT(OUT) :: Name_Out

!	Begin Procedure	
	CALL SET_CRYSTAL(Number_In)
	Name_Out = CrystalName
END SUBROUTINE GET_CRYSTAL_NAME

SUBROUTINE GET_CRYSTAL_DATA(Number_In, Name_Out, Type_Out, Class_Out, Min_Out, Max_Out)
!DEC$ ATTRIBUTES STDCALL, DLLEXPORT, ALIAS : "GET_CRYSTAL_DATA" :: GET_CRYSTAL_DATA
!DEC$ ATTRIBUTES VALUE :: Number_In
!DEC$ ATTRIBUTES REFERENCE :: Name_Out,Type_Out,Class_Out,Min_Out,Max_Out
	USE CRYSTAL_DATA
	IMPLICIT NONE

	INTEGER, INTENT(IN) :: Number_In
	CHARACTER*(256), INTENT(OUT) :: Name_Out
	INTEGER, INTENT(OUT) :: Type_Out, Class_Out
	REAL*8 Min_Out, Max_Out

!	Begin Procedure	
	CALL SET_CRYSTAL(Number_In)
	Name_Out = CrystalName
	Type_Out = CrystalType
	Class_Out = CrystalClass
	Min_Out = Abs_Min
	Max_Out = Abs_Max
END SUBROUTINE GET_CRYSTAL_DATA

SUBROUTINE GET_PARAM_NAME(Number_In, Name_Out)
!DEC$ ATTRIBUTES STDCALL, DLLEXPORT, ALIAS : "GET_PARAM_NAME" :: GET_PARAM_NAME
!DEC$ ATTRIBUTES VALUE :: Number_In
!DEC$ ATTRIBUTES REFERENCE :: Name_Out
	USE PM_DATA
	IMPLICIT NONE

!	Arguments

	INTEGER, INTENT(IN) :: Number_In
	CHARACTER*(256), INTENT(OUT) :: Name_Out

!	Begin Procedure	

	SELECT CASE (Number_In)
	CASE (XMIN_POS)
		Name_Out = 'X Minimum'
	CASE (XMAX_POS)
		Name_Out = 'X Maximum'
	CASE (XRes_POS)
		Name_Out = 'X Resolution'
	CASE (YMIN_POS)
		Name_Out = 'Y Minimum'
	CASE (YMAX_POS)
		Name_Out = 'Y Maximum'
	CASE (YRes_POS)
		Name_Out = 'Y Resolution'
	CASE (ZRes_Pos)
		Name_Out = 'Z Resolution'
	CASE (CRYSTAL_POS)
		Name_Out = 'Crystal'
	CASE (TYPEPM_POS)
		Name_Out = 'Phasematching Type'
	CASE (Crystal_Length_Pos)
		Name_Out = 'Crystal Length (µm)'
	CASE (Temperature_Pos)
		Name_Out = 'Crystal Temperature (C)'
	CASE (Pump_Width_Pos)
		Name_Out = 'Pump FWHM (µm)'
	CASE (Lambda_Pump_Pos)
		Name_Out = 'Pump Wavelength (µm)'
	CASE (Theta_Cut_Pos)
		Name_Out = 'Crystal Cut, Theta (Deg)'
	CASE (Phi_Cut_Pos)
		Name_Out = 'Crystal Cut, Phi (Deg)'
	CASE(Cube_Tilt_H_Pos)
		Name_Out = 'Cube Horizontal Tilt (Deg)'
	CASE(Cube_Tilt_V_Pos)
		Name_Out = 'Cube Vertical Tilt (Deg)'
	CASE (Theta_Pump_Pos)
		Name_Out = 'Pump Theta (Deg)'
	CASE (Phi_Pump_Pos)
		Name_Out = 'Pump Phi (Deg)'
	CASE (Lambda_Sgnl_Pos)
		Name_Out = 'Signal Wavelength (µm)'
	CASE (Theta_Sgnl_Pos)
		Name_Out = 'Signal Theta (Deg)'
	CASE (Phi_Sgnl_Pos)
		Name_Out = 'Signal Phi (Deg)'
	CASE (AUTORANGE_POS)
		Name_Out = 'Automatically Set Plot Ranges'
	CASE(ANGLE_EXTERNAL_POS)
		Name_Out = 'Plot Using External Angles'
	CASE(STOP_ON_ERR_POS)
		Name_Out = 'Pause to view errors'
	CASE(Lambda_Idlr_Pos)
		Name_Out = 'Lambda Idler (µm)'
	CASE(Theta_Idlr_Pos)
		Name_Out = 'Theta_Idler (Deg)'
	CASE(Phi_Idlr_Pos)
		Name_Out = 'Phi Idler (Deg)'
	CASE(Target_PMF_Pos)
		Name_Out = 'Target Value of PMF'
	END SELECT

END SUBROUTINE GET_PARAM_NAME

SUBROUTINE SET_PLOT_INFO(PlotNumber)
	USE PM_Data
	IMPLICIT NONE
	INTEGER, INTENT(In) :: PlotNumber

	SELECT CASE (PlotNumber)
	CASE (INDEX_WAVELENGTH)
		CALL INFO_INDEX_WAVELENGTH
	CASE (INDEX_TEMPERATURE)
		CALL INFO_INDEX_TEMPERATURE
	CASE (DIFFN_PHI_THETA)
		CALL Info_DIFFN_PHI_THETA
	CASE (MAXPMF_PHI_THETA)
		CALL Info_MAXPMF_PHI_THETA
	CASE (PMFCOL_PHI_THETA)
		CALL Info_PMFCOL_PHI_THETA
	CASE (PMFCOL_LAMBDA_THETA)
		CALL Info_PMFCOL_LAMBDA_THETA
	CASE (THETAOPT_LAMBDA)
		CALL INFO_THETAOPT_LAMBDA
	CASE (PMF_LAMBDA_THETA)
		CALL INFO_PMF_LAMBDA_THETA
	CASE (PMFINT_LAMBDA_THETA)
		CALL INFO_PMFINT_LAMBDA_THETA
	CASE (PMF_LAMBDA_PHI)
		CALL INFO_PMF_LAMBDA_PHI
	CASE (DEFF_LAMBDA_PHI)
		CALL INFO_DEFF_LAMBDA_PHI
	CASE (PMFSPREAD_LAMBDA)
		CALL INFO_PMFSPREAD_LAMBDA 
	CASE (OPTPMF_THETA_PHI)
		CALL INFO_OPTPMF_THETA_PHI
	CASE (OPTPMF_X_Y)
		CALL INFO_OPTPMF_X_Y
	CASE (PMF_X_Y)
		CALL INFO_PMF_X_Y
	CASE (PMF_THETASIG_THETAIDL)
		CALL INFO_PMF_THETASIG_THETAIDL
	CASE (PMF_THETAIDL_PHIIDL) 
		CALL INFO_PMF_THETAIDL_PHIIDL
	CASE (THETAOPT_THETAPUMP)
		CALL INFO_THETAOPT_THETAPUMP
	CASE (PMF_THETAPUMP_THETASGNL)
		CALL INFO_PMF_THETAPUMP_THETASGNL
	CASE (PMFSPREAD_THETA_PUMP)
		CALL INFO_PMFSPREAD_THETA_PUMP
	CASE (ANGLE_TEST)
		CALL INFO_ANGLE_TEST
	CASE (REFLECTANCE_PHI)
		CALL INFO_REFLECTANCE_PHI
	CASE (REFLECTANCE_LAMBDA_PHI)
		CALL INFO_REFLECTANCE_LAMBDA_PHI
	CASE DEFAULT
		PlotName = '-'
		XName = ''
		YName = ''
		PlotSize = 1
		Plot_Type = PLOT_2D
	END SELECT
END SUBROUTINE SET_PLOT_INFO

SUBROUTINE GET_PLOT_INFO(Number_In, PlotName_Out, XName_Out, YName_Out, Params_Used_Out, Plot_Type_Out, PlotSize_Out)
!DEC$ ATTRIBUTES STDCALL, DLLEXPORT, ALIAS : "GET_PLOT_INFO" :: GET_PLOT_INFO
!DEC$ ATTRIBUTES REFERENCE :: PlotName_Out,XName_Out,YName_Out,Plot_Type_Out
!DEC$ ATTRIBUTES VALUE :: Number_In
	USE PM_DATA
	IMPLICIT NONE

!	Arguments

	INTEGER, INTENT(IN) :: Number_In
	CHARACTER*(256), INTENT(OUT) :: PlotName_Out, XName_Out, YName_Out
	INTEGER, INTENT(OUT) :: Params_Used_Out(Num_Of_Params)
	INTEGER, INTENT(OUT) :: Plot_Type_Out, PlotSize_Out(3)

!	Local Variables
	
	INTEGER I

!	Initialize Params Array

	DO I = 1, Num_Of_Params, 1
		Params_Used = 0
	END DO
	
	CALL SET_PLOT_INFO(Number_In)

!	Params that are common to all plots

	Params_Used(XMIN_POS) = 1
	Params_Used(XMAX_POS) = 1
	Params_Used(XRES_POS) = 1
	Params_Used(AUTORANGE_POS) = 1
	IF (Plot_Type .EQ. PLOT_3D) THEN
		Params_Used(YMIN_POS) = 1
		Params_Used(YMAX_POS) = 1
		Params_Used(YRES_POS) = 1
	ENDIF

!	Send the global variables to the outputs

	PlotName_Out = PlotName
	XName_Out = XName
	YName_Out = YName
	DO I = 1, Num_Of_Params, 1
		Params_Used_Out(I) = Params_Used(I)
	END DO
	Plot_Type_Out = Plot_Type
	PlotSize_Out(1) = PlotSize(1)
	PlotSize_Out(2) = PlotSize(2)
	PlotSize_Out(3) = PlotSize(3)

END SUBROUTINE GET_PLOT_INFO

SUBROUTINE MAKE_PLOT(Number_In, Params, Data_Out,XAxis_Out,YAxis_Out)
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS : "MAKE_PLOT" :: MAKE_PLOT
!DEC$ ATTRIBUTES REFERENCE :: Data_Out, XAxis_Out, YAxis_Out
!DEC$ ATTRIBUTES VALUE :: Number_In
	USE DFWIN;USE PM_DATA; USE CRYSTAL_DATA
	IMPLICIT NONE

!	Arguments

	INTEGER, INTENT(IN) :: Number_In
	REAL*8, INTENT(INOUT)  :: Params(Num_Of_Params)
	REAL*8, INTENT(OUT) :: Data_Out(CEILING(Params(XRes_POS)),CEILING(Params(YRes_POS)),CEILING(Params(ZRes_POS)))
	REAL*8, INTENT(OUT) :: XAxis_Out(CEILING(Params(XRes_POS)))
	REAL*8, INTENT(OUT) :: YAxis_Out(CEILING(Params(YRes_POS)))

!	Local Variables

	CHARACTER*20 Title

!	Unpack the Parameters and Initialize

	CALL SET_PLOT_INFO(Number_In)

	Crystal        = Params(Crystal_Pos)
	TypePM         = Params(TypePM_Pos)
	IF ((TypePM .LT. 1) .OR. (TypePM .GT. 3)) THEN
		TypePM = 1
		Params(TypePM_Pos) = 1
	ENDIF
	Crystal_Length = Params(Crystal_Length_Pos)
	Temperature    = Params(Temperature_Pos)
	Pump_Width     = Params(Pump_Width_Pos) / DSQRT(2d0*DLOG(2d0))
	Lambda_Pump    = Params(Lambda_Pump_Pos)
	Theta_Pump     = Params(Theta_Pump_Pos) * Pi/180d0
	Phi_Pump       = Params(Phi_Pump_Pos) * Pi/180d0
	Lambda_Sgnl    = Params(Lambda_Sgnl_Pos)
	Theta_Sgnl     = Params(Theta_Sgnl_Pos) * Pi/180d0
	Phi_Sgnl       = Params(Phi_Sgnl_Pos) * Pi/180d0
	Theta_Cut      = Params(Theta_Cut_Pos) * Pi/180d0
	Phi_Cut        = Params(Phi_Cut_Pos) * Pi / 180d0
	Cube_Tilt_H    = Params(Cube_Tilt_H_Pos) * Pi / 180d0
	Cube_Tilt_V    = Params(Cube_Tilt_V_Pos) * Pi / 180d0
	IF (Params_Used(Phi_Idlr_Pos) .NE. 0) THEN
		Phi_Idlr   = Params(Phi_Idlr_Pos) * Pi/180d0
	ELSE
		Phi_Idlr   = Phi_Sgnl + Pi
	ENDIF
	Lambda_Idlr    = 1/(1/Lambda_Pump - 1/Lambda_Sgnl)
	TargetPMFVal = Params(Target_PMF_Pos)

	XMin = Params(XMIN_POS)
	XMax = Params(XMAX_POS)
	IF (XAxisIsAngle) THEN
		XMin = XMin * Pi / 180d0
		XMax = XMax * Pi / 180d0
	END IF
	YMin = Params(YMIN_POS)
	YMax = Params(YMAX_POS)
	IF (YAxisIsAngle) THEN
		YMin = YMin * Pi / 180d0
		YMax = YMax * Pi / 180d0
	END IF
	XRes = CEILING(ABS(Params(XRes_POS)))
	YRes = CEILING(ABS(Params(YRes_POS)))
	ZRes = CEILING(ABS(Params(ZRes_POS)))

	AutoRange      = (Params(AutoRange_POS) .NE. 0)
	Angle_External = (Params(ANGLE_EXTERNAL_POS) .NE. 0)
	Stop_On_Err   = (Params(STOP_ON_ERR_POS) .NE. 0)
	Use_Previous   = (Params(USE_PREVIOUS_POS) .NE. 0)


!	Allocate a console and Check for Problems
	StatusOK = AllocConsole() 

	WRITE(Title,"(A,A)") 'Calculating Data...', ACHAR(0)
	StatusOK = StatusOK .AND. SetConsoleTitle(Title) 

	IF (StatusOK) THEN		

		IF ((XRes .LE. 0) .OR. (YRES .LE. 0)) THEN
			StatusOK = .FALSE.
			write (*,*) 'Plot resolution must be greater than zero.'
		ENDIF
		IF ((Crystal .LT. 0) .OR. (Crystal .GE. NumberOfCrystals)) THEN
			StatusOK = .FALSE.
			write (*,*) 'Crystal number is out of range.'
		ENDIF

		IF (StatusOK) THEN		
			CALL SET_CRYSTAL(Crystal)
			CALL INIT_NPUMP
			Wavelength_Violation = .FALSE.
		
			SELECT CASE (Number_In)
			CASE (INDEX_WAVELENGTH)
				CALL Plot_Index_Wavelength(XAxis_Out,Data_Out)
			CASE (INDEX_TEMPERATURE)
				CALL Plot_Index_TEMPERATURE(XAxis_Out,Data_Out)
			CASE (DIFFN_PHI_THETA)
				CALL Plot_DIFFN_PHI_THETA(XAxis_Out,YAxis_Out,Data_Out)
			CASE (MAXPMF_PHI_THETA)
				CALL Plot_MAXPMF_PHI_THETA(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMFCOL_LAMBDA_THETA)
				CALL Plot_PMFCOL_LAMBDA_THETA(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMFCOL_PHI_THETA)
				CALL Plot_PMFCOL_PHI_THETA(XAxis_Out,YAxis_Out,Data_Out)
			CASE (THETAOPT_LAMBDA)
				CALL Plot_THETAOPT_LAMBDA(XAxis_Out,Data_Out)
			CASE (PMF_LAMBDA_THETA)
				CALL Plot_PMF_LAMBDA_THETA(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMFINT_LAMBDA_THETA)
				CALL PLOT_PMFINT_LAMBDA_THETA(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMF_LAMBDA_PHI)
				CALL Plot_PMF_LAMBDA_PHI(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMFSPREAD_LAMBDA)
				CALL PLOT_PMFSPREAD_LAMBDA(XAxis_Out,Data_Out)
			CASE (DEFF_LAMBDA_PHI)
				CALL PLOT_DEFF_LAMBDA_PHI(XAxis_Out,YAxis_Out,Data_Out)
			CASE(OPTPMF_THETA_PHI)
				CALL PLOT_OPTPMF_THETA_PHI(XAxis_Out, Data_Out)
			CASE (OPTPMF_X_Y)
				CALL Plot_OPTPMF_X_Y(XAxis_Out, Data_Out)
			CASE (PMF_X_Y) 
				CALL Plot_PMF_X_Y(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMF_THETASIG_THETAIDL)
				CALL Plot_PMF_THETASIG_THETAIDL(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMF_THETAIDL_PHIIDL) 
				CALL Plot_PMF_THETAIDL_PHIIDL(XAxis_Out,YAxis_Out,Data_Out)
				Params(Theta_Sgnl_Pos) = Theta_Sgnl * 180 / Pi
			CASE (THETAOPT_THETAPUMP)
				CALL PLOT_THETAOPT_THETAPUMP(XAxis_Out, Data_Out)
			CASE (PMF_THETAPUMP_THETASGNL)
				CALL PLOT_PMF_THETAPUMP_THETASGNL(XAxis_Out,YAxis_Out,Data_Out)
			CASE (PMFSPREAD_THETA_PUMP)
				CALL PLOT_PMFSPREAD_THETA_PUMP(XAxis_Out, Data_Out)
			CASE (ANGLE_TEST)
				CALL PLOT_ANGLE_TEST(XAxis_Out, Data_Out)
			CASE (REFLECTANCE_LAMBDA_PHI)
				CALL PLOT_REFLECTANCE_LAMBDA_PHI(XAxis_Out,YAxis_Out,Data_Out)
			CASE (REFLECTANCE_PHI)
				CALL PLOT_REFLECTANCE_PHI(XAxis_Out, Data_Out)
				Params(Theta_Pump_Pos) = Theta_Pump * 180d0 / Pi
				Params(Phi_Pump_Pos) = Phi_Pump * 180d0 / Pi
			END SELECT

!			Update the output parameters

			If (AutoRange) THEN
				IF (XAxisIsAngle) THEN
					XMin = XMin * 180d0 / Pi	
					XMax = XMax * 180d0 / Pi
				END IF
				IF (YAxisIsAngle) THEN
					YMin = YMin * 180d0 / Pi
					YMax = YMax * 180d0 / Pi
				END IF
				Params(XMIN_POS) = XMin
				Params(XMAX_POS) = XMax
				Params(YMIN_POS) = YMin
				Params(YMAX_POS) = YMax
			ENDIF
			Params(XRES_POS) = XRes
			Params(YRES_POS) = YRes
			IF (Wavelength_Violation) THEN
				WRITE(*,*) 'WARNING: Wavelengths were out of range.  Results may not be valid' 
				StatusOK = .FALSE.
			ENDIF	
	
			IF ((.NOT. StatusOK) .AND. (Stop_On_Err)) THEN
				Pause
			ENDIF
		ENDIF
	ENDIF

	StatusOK = FreeConsole()

END SUBROUTINE MAKE_PLOT

