def test_singlerow(self): h = Hek_Event() h.x_min = 10 h.description = "Hello" h.hpc_x = 1.2 blank_format = "{x_min} {description} {hpc_x}" desired = "10 Hello 1.2" new = ut.dbformat(blank_format, h) self.assertEqual(new, desired)
def test_multirow(self): h = Hek_Event() v = Visual_File() h.x_min = 10 h.description = "Hello" h.hpc_x = 1.2 v.visual_generator = "TEST1" v.description = "TEST2" v.im_ll_x = 1.2 blank_format = ( "{x_min} {description} {hpc_x} {visual_generator} {description} {im_ll_x}" ) desired = "10 Hello 1.2 TEST1 Hello 1.2" new = ut.dbformat(blank_format, h, v) self.assertEqual(new, desired)
def _request_one_interval(self, start_time, end_time): """ Make a request to the HEK server for a single time interval :param start_time: Start time :type start_time: datetime.datetime :param end_time: End time :type end_time: datetime.datetime :return: None :rtype: None """ to_pass = self.__parse_attributes(self.params, event_starttime=start_time, event_endtime=end_time) try: response = requests.get(Hek_Service.base_url, params=to_pass) except HTTPError as http_err: print(f"HTTP error occurred: {http_err}") # Python 3.6 except Exception as err: print(f"Other error occurred: {err}") # Python 3.6 else: json_data = response.json() with Hek_Service.event_adder_lock: events = [ Hek_Event.from_hek(x, source="HEK") for x in json_data["result"] ] self.for_testing_data["result"].extend(json_data["result"]) for e in events: if not e in self._data: self.data.append(e) self.status = "completed"
def save_data(self): """ Save the data found from the request to the database """ self._data = [ e if Hek_Event.select().where( Hek_Event.event_id == e.event_id).count() == 0 else Hek_Event.select().where(Hek_Event.event_id == e.event_id).get() for e in self._data ] for e in self.data: try: e.save() except pw.IntegrityError as err: print(err) chat( f"Looks like the event {e} is already in the database, so I am replacing it with the existing one" )
def parse_cutout_event(search, action, save_data=False, save_request=False): event = Hek_Event.select().where( (Hek_Event.id % search) | (Hek_Event.event_id.contains(search)) | (Hek_Event.sol_standard % f"%{search}%") ) if not event: print("Could not find any requests matching the search") return None elif len(event) > 1: print("I found too many requests with this search, please try to narrow it") for x in event: print(x) return None c = Cutout_Service._from_event(event.get()) print("I will use the request") print(c) grab_save(c, action, save_data, save_request)
def test_save_requests(self): h = Hek_Service() events = [ Hek_Event(event_id="1"), Hek_Event(event_id="123123"), Hek_Event(event_id="12031mmjdd"), Hek_Event(event_id="asdfnadfsoos"), ] h._data = events self.assertEqual(len(h._data), 4) h.save_data() self.assertEqual(len(Hek_Event.select()), 4) for x, y in zip(events, Hek_Event.select()): self.assertEqual(x.event_id, y.event_id) h.save_data() self.assertEqual(len(Hek_Event.select()), 4) for x, y in zip(events, Hek_Event.select()): self.assertEqual(x.event_id, y.event_id)
if True: h = Hek_Service(event_starttime="2010-06-01T00:00:00", event_endtime="2010-9-30T00:00:00") h.submit_request() h.save_request() h.save_data() # Now that request is prepared we can submit it. # The list of events found by the search are stored in the data member variable # To save the events to the database, we iterate over the events, and catch when we try to insert an even that already exists in the database # Now let us get the fits files that correspond to these events # Please see http://docs.peewee-orm.com/en/latest/peewee/api.html# for information on constructing queries. c = [Cutout_Service._from_event(h) for h in Hek_Event.select()] multi_cutout(c) bi = Basic_Image("png") for x in Fits_File.select().where(Fits_File.event == 2): Visual_File.create_new_visual(x, bi).save() if True: # vid = Basic_Video("mp4") to_export = ( Visual_File.select().join(Join_Visual_Fits).join(Fits_File).order_by( Fits_File.image_time))[0:100] # v = Visual_File.create_new_visual(list(to_export), vid)
from solar.service.hek import Hek_Service from solar.database.tables.hek_event import Hek_Event from solar.service.cutout import Cutout_Service, multi_cutout from solar.database.tables.fits_file import Fits_File from solar.database.tables.visual_file import Visual_File from solar.database.tables.join_vis_fit import Join_Visual_Fits from solar.visual.img import Basic_Image from solar.visual.vid import Basic_Video from solar.database import create_tables from solar.zooniverse.export import zooniverse_export, prepare_row, split from peewee import fn to_export = [[ y for y in Visual_File.select().join(Join_Visual_Fits).join(Fits_File).join( Hek_Event).where(Hek_Event.id == e).order_by(Fits_File.image_time) ][0:5] for e in Hek_Event.select()] to_export = [x for x in to_export if x] for x in to_export: print(x) zooniverse_export(*[split(x, 2, 0) for x in to_export])