''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 app ' "The" Application 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
Dim Drive_exists,Drives_Object
Dim Open_Image,sto
Dim objNetwork
Dim strZ_Drive,strRemotePath,Drive_Array(2)

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"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Values
cdelt1=CDbl(0.002002)
cdelt2=CDbl(0.002002)
ctype1="RA---TAN"
ctype2="Dec--TAN"
crpix1=CDbl(512)
crpix2=CDbl(512)
sto=CDbl(1)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Check Here to see if there is Z:\ is mapped to Telescope

Set Drives_Object=CreateObject("Scripting.FileSystemObject")
Drive_exists=Drives_Object.DriveExists("Z:")

If Not Drive_exists Then
	
	strZ_Drive="Z:"
	strRemotePath="\\Old_duder\USER"
	Drive_Array(0)=strZ_Drive
	Drive_Array(1)=strRemotePath
	'strOutputPath=Join(Drive_Array,"")

	Set objNetwork=WScript.CreateObject("WScript.Network")
	objNetwork.MapNetworkDrive strZ_Drive,strRemotePath
	
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Files

strInputFile="Z:\Telescope Position.txt"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Create Maxim objects with methods and properties of their own
'All these PIDs are listed in HKEY_CLASSES_ROOT 

'Set cam = CreateObject("MaxIm.CCDCamera")
'Set doc= GetObject("","MaxIm.Document")
Set App= GetObject("","MaxIm.Application")

'Create an object that is the current document, .ie. the image that is at
'the front of the screen in Maxim

Set Open_Image=app.CurrentDocument

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Utilize WSH objFileSystem for reading Telescope_Position.txt
Set objFileSystem=CreateObject("Scripting.fileSystemObject")
Set objTelePositionFile=objFileSystem.OpenTextFile(strInputFile,ForReading)
Set oRegExp = New RegExp


Set objTelePositionFile=objFileSystem.OpenTextFile(strInputFile,ForReading)
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
 
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" 'Concatenate Strings for RA
	Dec_text=Plus_Minus(0)&Db_Digits(4)&" "&Db_Digits(5)&" "&Db_Digits(6)&"."&Db_Digits(7)	'Concatenate Strings for Dec
	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=Open_Image.SetFITSKey(strCRVAL1,crval1)	'RA Value of Center pixel (Int)
Suc2=Open_Image.SetFITSKey(strCRVAL2,crval2)	'Dec Value of Center pixe (Int)
Suc3=Open_Image.SetFITSKey(strCTYPE1,ctype1)	'Label of X axis
Suc4=Open_Image.SetFITSKey(strCTYPE2,ctype2)	'Label of Y axis
Suc5=Open_Image.SetFITSKey(strCDELT1,cdelt1)	'ArcSeconds/Pixel in X
Suc6=Open_Image.SetFITSKey(strCDELT2,cdelt2)	'ArcSeconds/Pixel in Y
Suc7=Open_Image.SetFITSKey(strCRPIX1,crpix1)	'Center Pixel Number in X
Suc8=Open_Image.SetFITSKey(strCRPIX2,crpix2)	'Center Pixel Number in Y
Suc9=Open_Image.SetFITSKEY(strRA,RA_text)
Suc10=Open_Image.SetFITSKEY(strDec,Dec_text)

Set objFileSystem=Nothing
WScript.Quit