Repository: sunpy/sunpy

Issue:
Add an example on how to sort the results from HEK
The result from HEK is that it is sorted by date, which make sense as that's what HEK provides. To sort it by magnitude this works:

```python
>>> result = client.search(hek.attrs.Time(tstart, tend), hek.attrs.EventType(event_type), hek.attrs.FL.GOESCls > 'X1', hek.attrs.OBS.Observatory == 'GOES')
>>> mag_sorted = sorted(result, key=lambda x: ord(x['fl_goescls'][0])*100+float(x['fl_goescls'][1:]))
>>> for r in mag_sorted:
...  print('type {} on {}'.format(r['fl_goescls'], r['event_starttime']))

type X1.1 on 2003-10-19T16:29:00
type X1.1 on 2003-10-23T19:50:00
type X1.2 on 2003-10-26T05:57:00
type X1.2 on 2003-10-26T17:21:00
type X2.7 on 2003-11-03T01:09:00
type X3.9 on 2003-11-03T09:43:00
type X5.4 on 2003-10-23T08:19:00
type X8.3 on 2003-11-02T17:03:00
type X10. on 2003-10-29T20:37:00
type X17. on 2003-10-28T09:51:00
type X28. on 2003-11-04T19:29:00
```
This gives the same result and it's more robust (we've got tests):

```python
>>> from sunpy.instr.goes import flareclass_to_flux
>>> mag_sorted = sorted(result, key=lambda x: flareclass_to_flux(x['fl_goescls']))
```

Maybe we could put this as an example on the gallery.  


Discussion:
@dpshelio Interesting, we _should_ be able to fix this on the Python side, maybe with a special `sort` command?

I'm confused with this issue that I've opened... The result it's sorted by date, which make sense as that's what HEK provides. To sort it by magnitud this works:

```python
>>> result = client.search(hek.attrs.Time(tstart, tend), hek.attrs.EventType(event_type), hek.attrs.FL.GOESCls > 'X1', hek.attrs.OBS.Observatory == 'GOES')
>>> mag_sorted = sorted(result, key=lambda x: ord(x['fl_goescls'][0])*100+float(x['fl_goescls'][1:]))
>>> for r in mag_sorted:
...  print('type {} on {}'.format(r['fl_goescls'], r['event_starttime']))

type X1.1 on 2003-10-19T16:29:00
type X1.1 on 2003-10-23T19:50:00
type X1.2 on 2003-10-26T05:57:00
type X1.2 on 2003-10-26T17:21:00
type X2.7 on 2003-11-03T01:09:00
type X3.9 on 2003-11-03T09:43:00
type X5.4 on 2003-10-23T08:19:00
type X8.3 on 2003-11-02T17:03:00
type X10. on 2003-10-29T20:37:00
type X17. on 2003-10-28T09:51:00
type X28. on 2003-11-04T19:29:00
```
This gives the same result and it's more robust (we've got tests):

```python
>>> from sunpy.instr.goes import flareclass_to_flux
>>> mag_sorted = sorted(result, key=lambda x: flareclass_to_flux(x['fl_goescls']))
```

I believe we can close this, maybe we could put this as an example on the gallery.  
More generally it would be good to have an HEK event class that holds event
information. Would the current way map is implemented work for events too?

On Sat, Dec 22, 2018, 13:34 David Pérez-Suárez <notifications@github.com>
wrote:

> I'm confused with this issue that I've opened... The result it's sorted by
> date, which make sense as that's what HEK provides. To sort it by magnitud
> this works:
>
> >>> result = client.search(hek.attrs.Time(tstart, tend), hek.attrs.EventType(event_type), hek.attrs.FL.GOESCls > 'X1', hek.attrs.OBS.Observatory == 'GOES')>>> mag_sorted = sorted(result, key=lambda x: ord(x['fl_goescls'][0])*100+float(x['fl_goescls'][1:]))>>> for r in mag_sorted:...  print('type {} on {}'.format(r['fl_goescls'], r['event_starttime']))
> type X1.1 on 2003-10-19T16:29:00type X1.1 on 2003-10-23T19:50:00type X1.2 on 2003-10-26T05:57:00type X1.2 on 2003-10-26T17:21:00type X2.7 on 2003-11-03T01:09:00type X3.9 on 2003-11-03T09:43:00type X5.4 on 2003-10-23T08:19:00type X8.3 on 2003-11-02T17:03:00type X10. on 2003-10-29T20:37:00type X17. on 2003-10-28T09:51:00type X28. on 2003-11-04T19:29:00
>
> This gives the same result and it's more robust (we've got tests):
>
> from sunpy.instr.goes import flareclass_to_flux
> mag_sorted = sorted(result, key=lambda x: flareclass_to_flux(x['fl_goescls']))
>
> I believe we can close this, maybe we could put this as an example on the
> gallery.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/sunpy/sunpy/issues/1005#issuecomment-449589772>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AA8CF2WbAIjNV3NyjZIBjXsVVboLczhjks5u7nsagaJpZM4B-e9r>
> .
>

That could be an ROI or EOI factory, that understand HEK or other type of outputs. #164 is about that. I was thinking of closing this issue. 

Resolving patch:
diff --git a/examples/acquiring_data/querying_the_GOES_event_list.py b/examples/acquiring_data/querying_the_GOES_event_list.py
--- a/examples/acquiring_data/querying_the_GOES_event_list.py
+++ b/examples/acquiring_data/querying_the_GOES_event_list.py
@@ -3,8 +3,7 @@
 Querying the GOES flare event list
 ==================================
 
-How to retrieve the GOES flare event list through use of
-sunpy's Heliophysics Event Knowledgebase (HEK) client.
+How to retrieve the GOES flare event list, sort and save the results.
 """
 from sunpy.net import Fido
 from sunpy.net import attrs as a
@@ -33,26 +32,38 @@
 
 # It"s also possible to access the HEK results from the
 # `~sunpy.net.fido_factory.UnifiedResponse` by name.
-
 hek_results = result["hek"]
 
 ###################################################################
-# We can also print the key-values that correspond to the HEK parameters returned
-# in result[0]. The ``.table`` attribute returns an `~astropy.table.Table`.
+# We can also print the key names that correspond to the HEK
+# parameters returned by the query.
 
-print(hek_results.colnames)
+# We only print every 10th key to avoid the output being too long.
+print(hek_results.colnames[::10])
 
 ###################################################################
-# The results returned to the `~sunpy.net.hek.hek.HEKTable`
-# contain a lot of information and we may only want to keep some main
-# results such as start time, end time, peak time, GOES-class, and
-# active region number. This can be done as so:
+# The results returned contain a lot of information and we may
+# only want to keep some main results such as start time,
+# end time, peak time, GOES-class, and active region number.
+# This can be done as so:
+
+filtered_results = hek_results["event_starttime", "event_peaktime",
+                               "event_endtime", "fl_goescls", "ar_noaanum"]
+
+###################################################################
+# ``hek_result`` is already sorted by date, if one wants to sort
+# by magnitude, one can do the following:
+
+# Sorting is done by using the flare class from "fl_goescls"
+# By converting the flare class to a number using ord()
+# and adding the flare strength, we can sort by value
+by_magnitude = sorted(filtered_results, key=lambda x: ord(x['fl_goescls'][0]) + float(x['fl_goescls'][1:]), reverse=True)
 
-new_table = hek_results["event_starttime", "event_peaktime",
-                        "event_endtime", "fl_goescls", "ar_noaanum"]
+for flare in by_magnitude:
+    print(f"Class {flare['fl_goescls']} occurred on {flare['event_starttime']}")
 
 ###################################################################
 # These results can then be saved to a CSV file, or any other file
 # format that `~astropy.table.Table` supports.
 
-new_table.write("october_M1_flares.csv", format="csv")
+filtered_results.write("october_M1_flares.csv", format="csv")


Test patch: