fname = io.download_occultation_times(outdir='../data/')
print(fname)

"""
Explanation: Download the list of occultation periods from the MOC at Berkeley.
Note that the occultation periods typically only are stored at Berkeley for the future and not for the past. So this is only really useful for observation planning.
End of explanation
"""


tlefile = io.download_tle(outdir='../data')
print(tlefile)
times, line1, line2 = io.read_tle_file(tlefile)

"""
Explanation: Download the NuSTAR TLE archive.
This contains every two-line element (TLE) that we've received for the whole mission. We'll expand on how to use this later.
The times, line1, and line2 elements are now the TLE elements for each epoch.
End of explanation
"""


tstart = '2021-07-20T00:00:00'
tend = '2021-07-20T15:00:00'
orbits = planning.sunlight_periods(fname, tstart, tend)

orbits

# Get the solar parameter
from sunpy.coordinates import sun

angular_size = sun.angular_radius(t='now')
dx = angular_size.arcsec
print(dx)

sun_pa = planning.get_nustar_roll(tstart, 0.)


pa = planning.get_nustar_roll(tstart, 0*u.deg)
print(tstart)
print("NuSTAR Roll angle for Det0 in NE quadrant: {}".format(pa))




# Orbit 1 (Eastern limb)
offset = [-1050, -350.]*u.arcsec

for ind, orbit in enumerate(orbits):
    midTime = (0.5*(orbit[1] - orbit[0]) + orbit[0])
    sky_pos = planning.get_skyfield_position(midTime, offset, load_path='./data', parallax_correction=True)
    print("Orbit: {}".format(ind))
    print(f"Solar offset: {offset}")
    print("Orbit start: {} Orbit end: {}".format(orbit[0].iso, orbit[1].iso))
    print(f'Aim time: {midTime.iso} RA (deg): {sky_pos[0]:8.4f} Dec (deg): {sky_pos[1]:8.4f}')
    print("")



test1 = SkyCoord(289.3792274160115,  -22.304595055979675, unit = 'deg')
orb1 = SkyCoord(289.3855, -22.3051, unit = 'deg')
orb1.separation(test1)

orbit

import sunpy

sunpy.__version__

test1 = SkyCoord(289.898451566591, -22.158432904027155 , unit = 'deg')
orb1 = SkyCoord(289.9047,  -22.1589, unit = 'deg')
orb1.separation(test1)

sun_pa = planning.get_nustar_roll(tstart, 0.)


pa = planning.get_nustar_roll(tstart, 45*u.deg)



offset = [0, 0.]*u.arcsec

ind = 1
orbit = orbits[0]
midTime = (0.5*(orbit[1] - orbit[0]) + orbit[0])
sun_pos = planning.get_skyfield_position(midTime, offset, load_path='./data', parallax_correction=True)



# Orbit 1 (AR)
offset = [900, -300.]*u.arcsec

ind = 1
orbit = orbits[0]
midTime = (0.5*(orbit[1] - orbit[0]) + orbit[0])
sky_pos = planning.get_skyfield_position(midTime, offset, load_path='./data', parallax_correction=True)

planning.make_test_region(sky_pos[0], sky_pos[1], pa, sun_pos[0], sun_pos[1], sun_pa)
print(pa)

"""
Explanation: Here is where we define the observing window that we want to use.
Note that tstart and tend must be in the future otherwise you won't find any occultation times and sunlight_periods will return an error.
End of explanation
"""


