''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,crpix1,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, RA_match, Dec_match,Off_match
Dim RA_text,Dec_text,Off_text
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

crval1=CDbl(10)
crpix1=CDbl(0.002002)
ctype1="RA---TAN"
ctype2="Dec--TAN"

'''''''''''''''''''''''''''''''
'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.Pattern="\d\dh\s\d\dm\s\d\d.\d\ds"	'Set Search Pattern to Right Ascension
Set RA_match = oRegExp.Execute(Position_text)	'Search String for match
RA_text=RA_match.Item(0)	'Matches Collection Object contains RA

'MsgBox(RA_text)

oRegExp.Pattern="\S\d\d\S\d\d'\d\d.\d\d"		'Set Search Pattern to Dec
Set Dec_match = oRegExp.Execute(Position_text) 'Search String for Match
Dec_text=Dec_match.Item(0)

'MsgBox(Dec_text)

oRegExp.Pattern="(\S+)\S+\2"
Set Off_match= oRegExp.Execute(Position_text)
Off_text=Off_match.Item(1)

MsgBox(Off_text)

Suc1=cam.SetFITSKey(strCRVAL1,crval1)
Suc2=cam.SetFITSKey(strCRVAL2,crval1)
Suc3=cam.SetFITSKey(strCTYPE1,ctype1)
Suc4=cam.SetFITSKey(strCTYPE2,ctype2)
Suc5=cam.SetFITSKey(strCDELT1,crpix1)
Suc6=cam.SetFITSKey(strCDELT2,crpix1)
Suc7=cam.SetFITSKey(strCRPIX1,crpix1)
Suc8=cam.SetFITSKey(strCRPIX2,crpix1)
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