def _fetch(start_time, end_time): """ A helpher method of fetch to actually perform an individual request :param start_time: when list begins :type start_time: datetime :param end_time: when list ends :type end_time: datetime :return: list of flares between start_time and end_time :rtype: pd.DataFrame """ response = get_goes_event_list(TimeRange(start_time, end_time)) if response == []: # no events were found df = pd.DataFrame(columns=[ 'time_start', 'time_end', 'time_peak', 'ar', 'pos_x', 'pos_y' ]) else: df = pd.DataFrame(response) df = df.rename(index=str, columns={ "start_time": "time_start", "end_time": "time_end", "peak_time": "time_peak", "noaa_active_region": "ar" }) df = df.drop(['event_date'], axis=1) df['pos_x'] = [e[0] for e in df['goes_location']] df['pos_y'] = [e[1] for e in df['goes_location']] df = df.drop(['goes_location'], axis=1) return df
def test_goes_event_list(): # Set a time range to search trange = TimeRange('2011-06-07 00:00', '2011-06-08 00:00') # Test case where GOES class filter is applied result = goes.get_goes_event_list(trange, goes_class_filter='M1') assert type(result) == list assert type(result[0]) == dict assert type(result[0]['event_date']) == str assert type(result[0]['goes_location']) == tuple assert isinstance(result[0]['peak_time'], Time) assert isinstance(result[0]['start_time'], Time) assert isinstance(result[0]['end_time'], Time) assert type(result[0]['goes_class']) == str assert type(result[0]['noaa_active_region']) == np.int64 assert result[0]['event_date'] == '2011-06-07' assert result[0]['goes_location'] == (54, -21) # float errror assert is_time_equal(result[0]['start_time'], parse_time((2011, 6, 7, 6, 16))) assert is_time_equal(result[0]['peak_time'], parse_time( (2011, 6, 7, 6, 41))) assert is_time_equal(result[0]['end_time'], parse_time( (2011, 6, 7, 6, 59))) assert result[0]['goes_class'] == 'M2.5' assert result[0]['noaa_active_region'] == 11226 # Test case where GOES class filter not applied result = goes.get_goes_event_list(trange) assert type(result) == list assert type(result[0]) == dict assert type(result[0]['event_date']) == str assert type(result[0]['goes_location']) == tuple assert isinstance(result[0]['peak_time'], Time) assert isinstance(result[0]['start_time'], Time) assert isinstance(result[0]['end_time'], Time) assert type(result[0]['goes_class']) == str assert type(result[0]['noaa_active_region']) == np.int64 assert result[0]['event_date'] == '2011-06-07' assert result[0]['goes_location'] == (54, -21) assert is_time_equal(result[0]['start_time'], parse_time((2011, 6, 7, 6, 16))) assert is_time_equal(result[0]['peak_time'], parse_time( (2011, 6, 7, 6, 41))) assert is_time_equal(result[0]['end_time'], parse_time( (2011, 6, 7, 6, 59))) assert result[0]['goes_class'] == 'M2.5' assert result[0]['noaa_active_region'] == 11226
def test_goes_event_list(): # Set a time range to search trange = TimeRange('2011-06-07 00:00', '2011-06-08 00:00') # Test case where GOES class filter is applied result = goes.get_goes_event_list(trange, goes_class_filter='M1') assert type(result) == list assert type(result[0]) == dict assert type(result[0]['event_date'] == str) assert type(result[0]['goes_location'] == tuple) assert type(result[0]['peak_time'] == datetime.datetime) assert type(result[0]['start_time'] == datetime.datetime) assert type(result[0]['end_time'] == datetime.datetime) assert type(result[0]['goes_class'] == str) assert type(result[0]['noaa_active_region'] == int) assert result[0]['event_date'] == '2011-06-07' assert result[0]['goes_location'] == (54, -21) assert result[0]['start_time'] == datetime.datetime(2011, 6, 7, 6, 16) assert result[0]['peak_time'] == datetime.datetime(2011, 6, 7, 6, 41) assert result[0]['end_time'] == datetime.datetime(2011, 6, 7, 6, 59) assert result[0]['goes_class'] == 'M2.5' assert result[0]['noaa_active_region'] == 11226 # Test case where GOES class filter not applied result = goes.get_goes_event_list(trange) assert type(result) == list assert type(result[0]) == dict assert type(result[0]['event_date'] == str) assert type(result[0]['goes_location'] == tuple) assert type(result[0]['peak_time'] == datetime.datetime) assert type(result[0]['start_time'] == datetime.datetime) assert type(result[0]['end_time'] == datetime.datetime) assert type(result[0]['goes_class'] == str) assert type(result[0]['noaa_active_region'] == int) assert result[0]['event_date'] == '2011-06-07' assert result[0]['goes_location'] == (54, -21) assert result[0]['start_time'] == datetime.datetime(2011, 6, 7, 6, 16) assert result[0]['peak_time'] == datetime.datetime(2011, 6, 7, 6, 41) assert result[0]['end_time'] == datetime.datetime(2011, 6, 7, 6, 59) assert result[0]['goes_class'] == 'M2.5' assert result[0]['noaa_active_region'] == 11226
def test_goes_event_list(): # Set a time range to search trange = TimeRange('2011-06-07 00:00','2011-06-08 00:00') # Test case where GOES class filter is applied result = goes.get_goes_event_list(trange, goes_class_filter='M1') assert type(result) == list assert type(result[0]) == dict assert type(result[0]['event_date'] == str) assert type(result[0]['goes_location'] == tuple) assert type(result[0]['peak_time'] == datetime) assert type(result[0]['start_time'] == datetime) assert type(result[0]['end_time'] == datetime) assert type(result[0]['goes_class'] == str) assert type(result[0]['noaa_active_region'] == int) assert result[0]['event_date'] == '2011-06-07' assert result[0]['goes_location'] == (54, -21) assert result[0]['start_time'] == datetime.datetime(2011,6,7,6,16) assert result[0]['peak_time'] == datetime.datetime(2011,6,7,6,41) assert result[0]['end_time'] == datetime.datetime(2011,6,7,6,59) assert result[0]['goes_class'] == 'M2.5' assert result[0]['noaa_active_region'] == 11226 # Test case where GOES class filter not applied result = goes.get_goes_event_list(trange) assert type(result) == list assert type(result[0]) == dict assert type(result[0]['event_date'] == str) assert type(result[0]['goes_location'] == tuple) assert type(result[0]['peak_time'] == datetime) assert type(result[0]['start_time'] == datetime) assert type(result[0]['end_time'] == datetime) assert type(result[0]['goes_class'] == str) assert type(result[0]['noaa_active_region'] == int) assert result[0]['event_date'] == '2011-06-07' assert result[0]['goes_location'] == (54, -21) assert result[0]['start_time'] == datetime.datetime(2011,6,7,6,16) assert result[0]['peak_time'] == datetime.datetime(2011,6,7,6,41) assert result[0]['end_time'] == datetime.datetime(2011,6,7,6,59) assert result[0]['goes_class'] == 'M2.5' assert result[0]['noaa_active_region'] == 11226
def get_goes_flare_events(start_time, end_time, minimum_flare_size='C1', verbose=False): """Get a list of flare events from NOAA's GOES/XRS. Just a wrapper around sunpy.instr.goes get_goes_event_list. Inputs: start_time [metatime or string]: The beginning of the time window of interest. See jpm_time_conversions.py (https://github.com/jmason86/python_convenience_functions/blob/master/jpm_time_conversions.py) for allowed metatime formats if not using an iso or human like time string. end_time [metatime]: Same as start time but for the end of the time window. Optional Inputs: minimum_flare_size [string]: The minimum flare size to search for. Default is 'C1'. verbose [bool]: Set to log the processing messages to disk and console. Default is False. Outputs: goes_events [list]: The list of GOES flare events corresponding to the input search criteria. Optional Outputs: None Example: goes_events = get_goes_flare_events(pd.Timestamp('2010-05-01 00:00:00'), pd.Timestamp('2018-01-12 00:00:00'), verbose=True) """ # Prepare the logger for verbose if verbose: # TODO: Update the path logger = JpmLogger(filename='get_goes_flare_events_log', path='/Users/jmason86/Desktop/') logger.info("Getting > {0} flares from {1} to {2}.".format( minimum_flare_size, start_time, end_time)) if not isinstance(start_time, str): start_time = metatimes_to_human(np.array([start_time]))[0] if not isinstance(end_time, str): end_time = metatimes_to_human(np.array([end_time]))[0] time_range = TimeRange(start_time, end_time) goes_events = get_goes_event_list(time_range, goes_class_filter='c1') if verbose: logger.info("Found {0} events.".format(len(goes_events))) # Return the slopes return goes_events
def get_goes_event_list_sunpy(): startDate = datetime.strptime('2010.05.01 00:00:00', "%Y.%m.%d %H:%M:%S") while startDate.year < 2020: startDateString = startDate.strftime('%Y/%m/%d %H:%M') startDate = startDate + relativedelta(months=+1) endDateString = startDate.strftime('%Y/%m/%d %H:%M') print(startDateString + ' - ' + endDateString) time_range = TimeRange(startDateString, endDateString) goes_list = get_goes_event_list(time_range) df = pd.DataFrame(data=goes_list) df['start_time'] = pd.to_datetime(df['start_time'], format='%Y-%m-%dT%H:%M:00.000') df['peak_time'] = pd.to_datetime(df['peak_time'], format='%Y-%m-%dT%H:%M:00.000') df['end_time'] = pd.to_datetime(df['end_time'], format='%Y-%m-%dT%H:%M:00.000') # get only classification df['classFlare'] = df['goes_class'].apply(lambda x: x[:1]) # use fuction to convert text in number class df['FlareNumber'] = df.apply(flag_df, axis=1) df[['goes_location_fist', 'goes_location_segund' ]] = pd.DataFrame(df['goes_location'].tolist(), index=df.index) nameFile = "Data set\\00_All_GOES_SUNPY.csv" df.to_csv(nameFile, sep=';', encoding='utf-8', header=False, index=False, mode='a')
def download_goes_data(self, file_name='GOES_data.csv', tstart='2010-05-01', tend='2018-07-01'): if ('noaa_active_region' not in self.goes_attrs): self.goes_attrs += ['noaa_active_region'] print('Warning: \'noaa_active_region\' not in goes_attrs. Added.') file_path = os.path.join(self.main_path, file_name) if (os.path.exists(file_path)): print('Be careful, file {} already exists. It will be replaced.'. format(file_path)) with open(file_path, 'w', newline='') as file: writer = csv.writer(file, delimiter=',') writer.writerow(self.goes_attrs) data = goes_db.get_goes_event_list(TimeRange(tstart, tend)) for row in data: if (row['noaa_active_region'] > 0): writing_row = [] for attrs in self.goes_attrs: writing_row += [row[attrs]] writer.writerow(writing_row)