forked from alyshareinard/dimmings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_flare_catalog.py
138 lines (110 loc) · 5.76 KB
/
get_flare_catalog.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
import os
from datetime import datetime
import pandas as pd
import numpy as np
import math
def create_datetime(ymd, hm):
date=[]
#unpack ymd and fix year
for item, ihm in zip(ymd, hm):
if item==" ":
date.append(None)
continue
datestr=item.split()
year=int(datestr[0])
month=int(datestr[1])
day=int(datestr[2])
#fix two year dates without messing up 4 year dates
if year<70:
year=year+2000
elif year<100:
year+=1900
if math.isnan(ihm)==False:
hour=math.floor(ihm/100)
minute=math.floor(ihm-hour*100)
#now check to see if the time is past 2400 and adjust
if hour>=24:
hour-=24
day+=1
[day, month, year]=check_daymonth(day, month, year)
try:
date.append(datetime(year, month, day, hour, minute))
except:
date.append(np.nan)
else:
date.append(np.nan)
return date
def check_daymonth(day, month, year):
if (day==32 or (day==31 and (month==9 or month==4 or
month==6 or month==11)) or (day==30 and month==2) or
(month==2 and day==29 and year % 4 ==0)):
day=1
month=month+1
if month==13:
year=year+1
month=1
return [day, month, year]
def download_flare_catalog(start_year, stop_year):
""" program to read in GOES h-alpha and x-ray flare information from web"""
""" usage: [ha, xray]=get_flare_catalog; ha is a pandas dataframe"""
""" ha['location'][300] prints the 300th location"""
count=0
#strange character at the end of teh 2003 file causes trouble
for getyear in range(start_year, stop_year):
web_stem="http://www.ngdc.noaa.gov/stp/space-weather/solar-data/solar-features/solar-flares/x-rays/goes/xrs/"
xray_webpage=web_stem+"goes-xrs-report_"+str(getyear)+".txt"
web_stem="https://www.ngdc.noaa.gov/stp/space-weather/solar-data/solar-features/solar-flares/h-alpha/reports/kanzelhohe/halpha-flare-reports_kanz_"
# ha_webpage=web_stem+str(getyear)+".txt"
print("\nGetting xray flares from: ", xray_webpage, "for year ", getyear)
# print(ha_webpage)
names=["data code", "station code", "year", "month", "day", "init_ind", "init_time", "final_ind", "final_time", "peak_ind",
"peak_time", "location", "optical", "something", "xray_class", "xray_size", "station", "blank", "NOAA_AR", "etc"]
widths=[2, 3, 2, 2, 2, 2, 4, 1, 4, 1, 4, 7, 3, 22, 1, 3, 8, 8, 6, 24]
xray_df_year=pd.read_fwf(xray_webpage, widths=widths, header=None, names=names)#, parse_dates=[[2, 3, 4]])
xray_df_year["year_month_day"]=[str(x)+" "+str(y)+" "+str(z) for x,y,z in zip(xray_df_year["year"], xray_df_year["month"], xray_df_year["day"])]
xray_df_year["init_date"]=create_datetime(xray_df_year["year_month_day"], xray_df_year["init_time"])
xray_df_year["peak_date"]=create_datetime(xray_df_year["year_month_day"], xray_df_year["peak_time"])
xray_df_year["final_date"]=create_datetime(xray_df_year["year_month_day"], xray_df_year["final_time"])
xray_df_year["location"]=[x if str(x)[0]=="N" or str(x)[0]=="S" else None for x in xray_df_year["location"]]
xray_df_year=xray_df_year[["init_date", "peak_date", "final_date", "location", "xray_class", "xray_size", "NOAA_AR"]]
if count==0:
xray_df=xray_df_year
count=1
else:
# dfs=[ha_df, ha_df_year]
# ha_df=pd.concat(dfs)
dfs=[xray_df, xray_df_year]
xray_df=pd.concat(dfs)
ha_df="not yet implemented"
return (xray_df, ha_df)
def get_flare_catalog_fromfile(data_path):
""" program to read in GOES h-alpha and x-ray flare information from file"""
""" usage: [ha, xray]=get_flare_catalog; ha is a dict"""
""" ha['location'][300] prints the 300th location"""
""" keys are ha.keys() -- station_num, group_num, initial_time, final_time"""
""" peak_time, optical_importance, optical_brightness, xray_class, """
""" xray_size, NOAA_AR """
#define data file location
# ha_file=data_path+"/ha.txt"
xray_file=data_path+"/xray.txt"
print("Getting from file, years are only those downloaded")
print("Reading X-ray flares from: ", xray_file)
#code to read in xray data
names=["data code", "station code", "year", "month", "day", "init_ind", "init_time", "final_ind", "final_time", "peak_ind",
"peak_time", "location", "optical", "something", "xray_class", "xray_size", "station", "blank", "NOAA_AR", "etc"]
widths=[2, 3, 2, 2, 2, 2, 4, 1, 4, 1, 4, 7, 3, 22, 1, 3, 8, 8, 6, 24]
xray_df=pd.read_fwf(xray_file, widths=widths, header=None, names=names, parse_dates=[[2, 3, 4]])
#translates dates to datetime
xray_df["location"]=[x if str(x)[0]=="N" or str(x)[0]=="S" else None for x in xray_df["location"]]
xray_df["init_date"]=create_datetime(xray_df["year_month_day"], xray_df["init_time"])
xray_df["peak_date"]=create_datetime(xray_df["year_month_day"], xray_df["peak_time"])
xray_df["final_date"]=create_datetime(xray_df["year_month_day"], xray_df["final_time"])
xray_df=xray_df[["init_date", "peak_date", "final_date", "location", "xray_class", "xray_size", "NOAA_AR"]]
ha_df="not yet implemented"
return (xray_df, ha_df)
def get_flare_catalog(data_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "data"), start_year=2013, stop_year=2014):
try:
(xray, halpha)=download_flare_catalog(start_year, stop_year)
except:
(xray, halpha)=get_flare_catalog_fromfile(data_path)
return (xray, halpha)