Print "+------------------------+"
Print "|  AstroArt Auto Mosaic  |"
Print "| (C) Steve Hill, 2005   |"
Print "| adapted maxcaimmi 2012 |"
Print "+------------------------+"

rac = Telescope.RA * 60 * 15
decc = Telescope.DEC * 60

Print "RA :" + RA$(rac / 60 / 15)
Print "DEC:" + DEC$(decc / 60)
Print ""

size = 0
While size <= 0
	Input "Campo (arcmin)", size
Endwhile

cam = 0
While cam <> 1 And cam <> 2
	Input "CCD (1 = ATIK314L, 2 = MX5C)", cam
Endwhile

'
' Fields of view for cameras on 10 inch F10 C925 r90
'
If cam = 1 Then
  xfov = 9.8
  yfov = 13.1
Endif

If cam = 2 Then
  xfov = 6.6
  yfov = 4.9
Endif

ratio = 0
While ratio <= 0
	Input "Rapporto Focale", ratio
Endwhile

' Natural f-ratio is 10
'
xfov = xfov * 10 / ratio
yfov = yfov * 10 / ratio

' Compensate for declination
xfov = xfov / Cos(DegToRad(decc / 60))

Print "CCD field of view: " + Left$(Str$(xfov),5) + " by " + Left$(Str$(yfov), 5)

base$ = ""
While base$ = ""
	Input "Nome Oggetto", base$
Endwhile

'
' Obtain exposure time
'
exposure = 0
While exposure = 0
	Input "Tempo esposizione (sec)", exposure
Endwhile

'
' Obtain number of exposures
'
numExposures = 0
While numExposures = 0
	Input "Numero Frame", numExposures
Endwhile


'
' Calculate the origin of the mosaic
'
raOrg  = rac - size / 2
decOrg = decc - size / 2

'
' How much of the images overlap
'
overlap = 0
While overlap <= 0 Or overlap >= 100
	Input "Sovrapposizione %", overlap
Endwhile

'
' Calculate the steps required in x an y
'
overlap = overlap / 100
effective = 1 - overlap

fsteps = (size - overlap * xfov) / (xfov - xfov * overlap)
xsteps = Int(fsteps)
If fsteps <> xsteps Then
	xsteps = xsteps + 1
Endif

If xsteps <= 0 Then
	xsteps = 1
Endif

fsteps = (size - overlap * yfov) / (yfov - yfov * overlap)
ysteps = Int(fsteps)
If fsteps <> ysteps Then
	ysteps = ysteps + 1
Endif

If ysteps <= 0 Then
	ysteps = 1
Endif

'
' Contants for delays between movements
'
wheelPause          = 1
shortTelescopePause = 3
longTelescopePause  = 5

'
' Calculate and print details of mosaic
'
mosaicSize = xsteps * ysteps
frames     = mosaicSize * numExposures
time       = frames * exposure

Print "Mosaic sides             : " + Str$(xsteps) + "x" + Str$(ysteps)
Print "Number of frames         : " + Str$(frames)
Print "Exposure time (hh mm ss) : " + RA$(time / 60 / 60)

frameCount = 0
dec = decOrg
ra  = raOrg

'
' Give opportunity to quit proceedings before moving scope
'
ok$ = ""
While ok$ = ""
	Input "Avvio? (Y/N)", ok$
Endwhile

ok$ = Left$(UCase$(ok$), 1)
If ok$ = "N" Then
	Print "Program aborted"
	Goto 1000
Endif 

'
' Everything is set up.  Cycle over each image for the
' number of exposures.  Save results.
'

For x = 1 To ysteps
  ra = raOrg
  For y = 1 To xsteps
	Print "Seeking ..."
    Telescope.Goto(ra / 60 / 15, dec / 60)

	If y = 1 Then
	  Pause(longTelescopePause)
	Else
	  Pause(shortTelescopePause)
	Endif
'
' Start Autoguiding.'
'
Guider.Select(2)
Camera.Start(1)
Guider.StartAutoguide()
Camera.Wait
Print "Start Autoguiding"
'
	  For e = 1 To numExposures
	  	frameCount = frameCount + 1
	  	Print "Taking exposure " + Str$(frameCount) + " of " + Str$(frames)
	  	Camera.Start(exposure)
		Camera.Wait

		file$ = base$ + "_(" + Str$(x) + "," + Str$(y) + ")_" + Str$(e) + ".fit"

		Print "Saving image as " + file$
		Image.Save(file$)
		Image.Close

'
' Stop Autoguiding. '
'
Camera.StopAutoguide()
Guider.Close
Image.Close
Print "Stop Autoguiding"
'
	  Next e

  ra = ra + xfov * effective
  Next y

dec = dec + yfov * effective
Next x

Print "Returning to centre of object"
Telescope.Goto(rac / 60 / 15, decc / 60)

'
' Exit point
'
1000
Print "Sequenza Mosaico Terminata"