-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyFAVA.py
199 lines (152 loc) · 6.36 KB
/
pyFAVA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
"""
------------------------------------------------------------------------
Script to download FAVA results:
Author: Daniel Kocevski ([email protected])
Date: September 25th, 2022
Usage Examples:
import FAVApy
FAVApy.getData(week=1, sigma=6)
------------------------------------------------------------------------
"""
import urllib.request
import json
import numpy
from astropy import units as u
from astropy.coordinates import SkyCoord
# Define the FAVA url
fava_api_url = 'https://fermi.gsfc.nasa.gov/ssc/data/access/lat/FAVA/queryDB_2FAV.php'
class DataCatalog(dict):
"""
An dictionary object used to store the FAVA data as numpy array that are accessible through key value pairs
"""
def __init__(self):
self['flareID'] = numpy.array([])
self['num'] = numpy.array([])
self['best_ra'] = numpy.array([])
self['best_dec'] = numpy.array([])
self['best_r95'] = numpy.array([])
self['bestPositionSource'] = numpy.array([])
self['fava_ra'] = numpy.array([])
self['fava_dec'] = numpy.array([])
self['lbin'] = numpy.array([])
self['bbin'] = numpy.array([])
self['gall'] = numpy.array([])
self['galb'] = numpy.array([])
self['tmin'] = numpy.array([])
self['tmax'] = numpy.array([])
self['sigma'] = numpy.array([])
self['avnev'] = numpy.array([])
self['nev'] = numpy.array([])
self['he_nev'] = numpy.array([])
self['he_avnev'] = numpy.array([])
self['he_sigma'] = numpy.array([])
self['sundist'] = numpy.array([])
self['varindex'] = numpy.array([])
self['favasrc'] = numpy.array([])
self['fglassoc'] = numpy.array([])
self['assoc'] = numpy.array([])
self['le_ts'] = numpy.array([])
self['le_tssigma'] = numpy.array([])
self['le_ra'] = numpy.array([])
self['le_dec'] = numpy.array([])
self['le_gall'] = numpy.array([])
self['le_galb'] = numpy.array([])
self['le_r95'] = numpy.array([])
self['le_contflag'] = numpy.array([])
self['le_sundist'] = numpy.array([])
self['le_dist2bb'] = numpy.array([])
self['le_ffsigma'] = numpy.array([])
self['le_hightsfrac'] = numpy.array([])
self['le_gtlts'] = numpy.array([])
self['le_flux'] = numpy.array([])
self['le_fuxerr'] = numpy.array([])
self['le_index'] = numpy.array([])
self['le_indexerr'] = numpy.array([])
self['he_ts'] = numpy.array([])
self['he_tssigma'] = numpy.array([])
self['he_ra'] = numpy.array([])
self['he_dec'] = numpy.array([])
self['he_gall'] = numpy.array([])
self['he_galb'] = numpy.array([])
self['he_r95'] = numpy.array([])
self['he_contflag'] = numpy.array([])
self['he_sundist'] = numpy.array([])
self['he_dist2bb'] = numpy.array([])
self['he_ffsigma'] = numpy.array([])
self['he_hightsfrac'] = numpy.array([])
self['he_le_dist'] = numpy.array([])
self['he_gtlts'] = numpy.array([])
self['he_flux'] = numpy.array([])
self['he_fuxerr'] = numpy.array([])
self['he_index'] = numpy.array([])
self['he_indexerr'] = numpy.array([])
self['week'] = numpy.array([])
self['dateStart'] = numpy.array([])
self['dateStop'] = numpy.array([])
def getWeeklySources(week=1, threshold=6, dataCatalog=None, verbose=True, test=False):
"""
Function to download a week's worth of FAVA data.
Returns a DataCatalog object with data arrays accessible through key value pairs
"""
# Construct the full url
requested_url = '%s?typeOfRequest=SourceList&week=%s&threshold=%s' % (fava_api_url, week, threshold)
if verbose == True:
print(requested_url)
if test == True:
return None
# Request the url. The result should be a json file
with urllib.request.urlopen(requested_url) as url:
# Download the json file
data = json.load(url)
if dataCatalog is None:
# Create a new list of sources
dataCatalog = DataCatalog()
# Loop through each of the sources in the data dictionary
for item in data:
# Loop through each key value pair and add them to the data arrays
for key in item.keys():
try:
dataCatalog[key] = numpy.append(dataCatalog[key], float(item[key]))
float(item[key])
except:
dataCatalog[key] = numpy.append(dataCatalog[key], item[key])
return dataCatalog
def downloadCatalog(start_week=1, end_week=721, threshold=6, verbose=True, test=False):
"""
Function to download multiple weeks worth of FAVA data.
Returns a DataCatalog object with data arrays accessible through key value pairs
"""
week = start_week
dataCatalog = None
print("\nDownloading FAVA data...\n")
while week < end_week+1:
# Download the data
dataCatalog = getWeeklySources(week=week, threshold=threshold, dataCatalog=dataCatalog, verbose=verbose)
# Increment the week
week = week + 1
print('\nDownloaded data for %s sources.\n' % len(dataCatalog['flareID']))
return dataCatalog
def selectGalacticSources(dataCatalog, dgalb=10, return_index=False):
"""
Function to produce a subset of galactic sources
Returns a DataCatalog object with data arrays accessible through key value pairs
"""
# Define a new data catalog object
galacticDataCatalog = DataCatalog()
# Find the sources that are within +/- dgalb
best_ra = dataCatalog['best_ra']
best_dec = dataCatalog['best_dec']
skyCoordinate = SkyCoord(ra=best_ra *u.degree, dec=best_dec *u.degree, frame='fk5')
best_gall = skyCoordinate.galactic.l.value
best_galb = skyCoordinate.galactic.b.value
index = numpy.where((best_galb >= (-1*dgalb)) & (best_galb <= dgalb))
# Loop through each of the variables in the dataCatalog and select only the galactic sources
for key in dataCatalog.keys():
# Fill the new data catalog
galacticDataCatalog[key] = numpy.append(galacticDataCatalog[key], dataCatalog[key][index])
print('\nSelected %s sources.\n' % len(galacticDataCatalog['flareID']))
if return_index == True:
return index
else:
return galacticDataCatalog