''This VBScript will open a text file containing the RA and Dec for the observatory telescope
''and write it into the FITS header of whatever image is exposed

Option Explicit 'Will tell us if there are undefined variables
''''''''''''''''''''''''''''''
'Declare Variables

Dim cam ' "The" Camera object
Dim doc ' "The" Image Object
Dim objFileSystem 'File System Object Allowing access to files and folders
Dim crval1,crval2,cdelt1,cdelt2,crpix1,crpix2,ctype1,ctype2  'Values for Fits Header
Dim strTest,Position_text 'Text to be manipulated
Dim strCRVAL1,strCRVAL2,strCTYPE1,strCTYPE2,strCDELT1,strCDELT2,strCRPIX1,strCRPIX2,strRA,strDec 'Fits Keys
Dim Suc1,Suc2,Suc3,Suc4,Suc5,Suc6,Suc7,Suc8,Suc9,Suc10 'Booleans to see if write is successful
Dim strInputFile,objTelePositionFile 'Strings for Files and objects
Dim oRegExp,Num_Matches
Dim RA_text,Dec_text,Off_text,Db_Digits,Plus_Minus
Const ForReading=1

''''''''''''''''''''''''''''''
'FITS KEYS
strCRVAL1="CRVAL1"
strCRVAL2="CRVAL2"
strCTYPE1="CTYPE1"
strCTYPE2="CTYPE2"
strCDELT1="CDELT1"
strCDELT2="CDELT2"
strCRPIX1="CRPIX1"
strCRPIX2="CRPIX2"
strRA="RA"
strDec="Dec"
''''''i''''''''''''''''''''''''
'Values

cdelt1=CDbl(0.002002)
cdelt2=CDbl(0.002002)
ctype1="RA---TAN"
ctype2="Dec--TAN"
crpix1=CDbl(512)
crpix2=CDbl(512)

'''''''''''''''''''''''''''''''
'Files

strInputFile="C:/Telescope Position.txt"

'''''''''''''''''''''''''''''''
'Create objects with methods and properties of their own

Set cam = CreateObject("MaxIm.CCDCamera")
Set doc= CreateObject("MaxIm.Document")
Set objFileSystem=CreateObject("Scripting.fileSystemObject")
Set objTelePositionFile=objFileSystem.OpenTextFile(strInputFile,ForReading)
Set oRegExp = New RegExp

'''''''''''''''''''''''''''''''
'Begin By Enabling camera link and exposure

cam.LinkEnabled = True
if Not cam.LinkEnabled Then
wscript.echo "Failed to start camera."
Quit
End If
wscript.echo "Camera is ready, Exposing."
cam.Expose 1, 1, 0
Do While Not cam.ImageReady
Loop

Position_text=objTelePositionFile.Readline
objTelePositionFile.Close

oRegExp.Global= True
oRegExp.Pattern="\d\d"	'Set Search Pattern for consecutive Double Digits
Set Db_Digits = oRegExp.Execute(Position_text)	'Search String for matches

oRegExp.Pattern="\x2B|\x2D"	'Set Search Pattern for + or - in Declination (Hex ASCII code)
Set Plus_Minus= oRegExp.Execute(Position_text)  'Search String for matches
 
'MsgBox(Plus_Minus(0))

if Db_Digits.Count > 0 Then

	Num_Matches=Db_Digits.Count	'Search for two consecutive digits returns 15
	RA_text=Db_Digits(0)&"h "&Db_Digits(1)&"m "&Db_Digits(2)&"."&Db_Digits(3)&"s"
	Dec_text=Plus_Minus(0)&Db_Digits(4)&" "&Db_Digits(5)&" "&Db_Digits(6)&"."&Db_Digits(7)	 		
	crval1=15*(CDbl(Db_Digits(0))+CDbl(Db_Digits(1))/60+(CDbl(Db_Digits(2))+.01*CDbl(Db_Digits(3)))/3600)
	crval2=CDbl(CDbl(Db_Digits(4))+CDbl(Db_Digits(5))/60+(CDbl(Db_Digits(6))+.1*CDbl(Db_Digits(7)))/3600)
else 
	
	MsgBox("There was an error in obtaining the telescope coordinates")

End if


Suc1=cam.SetFITSKey(strCRVAL1,crval1)	'RA Value of Center pixel (Int)
Suc2=cam.SetFITSKey(strCRVAL2,crval2)	'Dec Value of Center pixe (Int)
Suc3=cam.SetFITSKey(strCTYPE1,ctype1)	'Label of X axis
Suc4=cam.SetFITSKey(strCTYPE2,ctype2)	'Label of Y axis
Suc5=cam.SetFITSKey(strCDELT1,cdelt1)	'ArcSeconds/Pixel in X
Suc6=cam.SetFITSKey(strCDELT2,cdelt2)	'ArcSeconds/Pixel in Y
Suc7=cam.SetFITSKey(strCRPIX1,crpix1)	'Center Pixel Number in X
Suc8=cam.SetFITSKey(strCRPIX2,crpix2)	'Center Pixel Number in Y
Suc9=cam.SetFITSKEY(strRA,RA_text)
Suc10=cam.SetFITSKEY(strDec,Dec_text)

cam.SaveImage "C:/Script.fit"
wscript.echo "Exposure is done, Image saved as Script.fit"

'doc.OpenFile "C:/Script.fit"
'strTest=doc.GetFITSKey(BZERO)
'Suc=doc.SetFITSKey(CRVAL1,crval1)

'doc.SaveFile"C:/Script.fit",mxFITS
'MsgBox(Suc)
'End if
'Loop