from sunpy.net import vso
client = vso.VSOClient()

"""
Explanation: Getting at Data
Both SunPy and Astropy have utilities for downloading data for your delectation. They are simple and easy to use, however increasing levels of computing will allow a great deal of personalisation and selection. Let us begin with SunPy
Aquiring Data with SunPy
VS0
This is the main interface which SunPy to search for and find solar data. VSO stands for the Virtual Solar Observatory, a service which presents a homogenous interface to heterogenous data set.
So what do we need?
End of explanation
"""


qr = client.query(vso.attrs.Time('2001/1/1', '2001/1/2'), 
                  vso.attrs.Instrument('eit'))

"""
Explanation: This is your client object. This is effectively the intermediary between yourself and the treasure chest of solar data available. You query VSO, then VSO querys all data providers which fit the limiations you imposed during your search command. The VSO client also handles the particulars of dowloading the data onto your machiene. 
Making a query
Lets kick off with an example, lets ask the veteran of solar imaging, SoHO for some EIS data, between the dates of between January 1st and 2nd, 2001,
End of explanation
"""


print(len(qr))
print(qr)

"""
Explanation: qr is a Python list of response onjects, each one a record that the VSO has found.
End of explanation
"""


qr = client.query(vso.attrs.Time('2001/1/1T12:00:00', '2001/1/2T13:00:00'), vso.attrs.Instrument('eit') | vso.attrs.Instrument('mdi'))
len(qr)

"""
Explanation: Break it down
So we can pass many attributes to the VSO, in this case we started with time
<pre><code> vso.attrs.Time('2001/1/1','2001/1/2')</code></pre>

Start and end times for the query as strings, any date/time function can be understood by SunPy's parse_time function e.g. the datetime onjects we will look at later. Next we give it the instrument we want:
<pre><code> vso.attrs.Instrument('eit') </code></pre>

You don't have to pass it an instrument, the client will find all available missions in the parameter you've defined if you like. Next, wavelength:
<pre><code> vso.attrs.Wave(14.2*u.nm, 12.3*u.nm)</code></pre>

We pass it a min and max wavelength. This has to be an astropy units quantity (in SI for the love of coffee). If you don't you will get an error.
For a full list of attributes that vso can take use:
So we can use multiple instument queries and define get smaller sample times but narrowing down the query:
End of explanation
"""


from sunpy.net import hek
hek_client = hek.HEKClient()

"""
Explanation: HEK
The Heliophysics Event Knowledgebase (HEK) is a repository of feature and event information about the Sun. Entries are generated both by automated algorithms and human observers.
We need to set up HEK in a similar way to VSO
End of explanation
"""


tstart = '2011/08/09 07:23:56'
tend = '2011/08/09 12:40:29'
event_type = 'FL'
result = hek_client.query(hek.attrs.Time(tstart,tend),
                          hek.attrs.EventType(event_type))



"""
Explanation: Creating a very similar client as we saw with VSO above.
Given that HEK is a database of solar events of interest, the query has different requirements to VSO. It needs start and end times, and an event type. Again time objects can be defined as datetime objects or correctly formatted strings.
Event types are specified as uppercase two letter strings found on the HEK website
End of explanation
"""


result[0].keys()

"""
Explanation: Notice that the HEK query is extremely similar to the VSO query style, with our attributes defined accordingly.
Instead of returning a list, HEK returns a list of dictionary objects. Each entry in the dictionary sis a pair of key-value pairs that exactly correspond to the parameters. We can return the key words using:
End of explanation
"""


for elem in result: 
    print(elem["frm_name"])

"""
Explanation: Remember, the HEK query we made returns all the flares in the time-range stored in the HEK, regardless of the feature recognition method. The HEK parameter which stores the the feature recognition method is called “frm_name”. Using list comprehensions (which are very cool), it is easy to get a list of the feature recognition methods used to find each of the flares in the result object, for example:
End of explanation
"""


from astroquery.simbad import Simbad
result_table = Simbad.query_object("m1")
result_table.pprint(show_unit=True)

"""
Explanation: This way we can avoid troublesome doubling up of results. We can do the same help(hek.attrs) command as VSO to fins out further options. 
Aquiring data with AstroQuery
Astroquery supports a plethora of services, all of which follow roughly the same API (application program interface). In its simplest for the API involves queries based on coordinates or object names e.g. using SIMBAD:
End of explanation
"""


from astropy import coordinates
import astropy.units as u
c = coordinates.SkyCoord("05h35m17.3s -05d23m28s", frame='icrs')
r = 5 * u.arcminute
result = Simbad.query_region(c, radius=r)
result.pprint(show_unit=True, max_width=80, max_lines=5)

"""
Explanation: In this case the query is looking at a specific set of coordinates
End of explanation
"""


