From ebba2ae4a0c491196a7b29656e24c02dac6d6733 Mon Sep 17 00:00:00 2001 From: new2128 <81988673+new2128@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:40:02 -0500 Subject: [PATCH 1/3] Changing while loops to for enumerate() loops --- mop/brokers/asassn.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/mop/brokers/asassn.py b/mop/brokers/asassn.py index 38b94c0d..3df6ef57 100644 --- a/mop/brokers/asassn.py +++ b/mop/brokers/asassn.py @@ -128,14 +128,13 @@ def find_and_ingest_photometry(self, events, targets): Creates and saves a ReducedDatum object of the given Target and its associated photometry data ''' targets = targets - i = 0 lightcurvelinks = [] lightcurvepartlinks = [] indices_with_photometry_data = [] rd_list = [] events = events - while(i < len(events)): + for i,event in enumerate(events): samplera = events[i][2] sampledec = events[i][3] sampleralist = samplera.split(':') @@ -152,7 +151,6 @@ def find_and_ingest_photometry(self, events, targets): if('/photometry/' in s): lightcurvepartlinks.append(link.get('href')) indices_with_photometry_data.append(i) - i = i + 1 for partlink in lightcurvepartlinks: lightcurvelinks.append(os.path.join('https://asas-sn.osu.edu' + partlink)) @@ -160,8 +158,7 @@ def find_and_ingest_photometry(self, events, targets): ''' Reads links with photometry data ''' - k = 0 - for link in lightcurvelinks: + for k,link in enumerate(lightcurvelinks): running = True hjd = [] ut_date = [] @@ -183,9 +180,7 @@ def find_and_ingest_photometry(self, events, targets): page = requests.get(functional_link) doc = lh.fromstring(page.content) tr_elements = doc.xpath('//tr') - h = 0 for t in tr_elements[0]: - h += 1 content = t.text_content() table.append((content, [])) for m in range(1, len(tr_elements)): @@ -226,8 +221,7 @@ def find_and_ingest_photometry(self, events, targets): running == False break i = i + 1 - n = 0 - while(n < len(hjd)): + for n, HJD in enumerate(hjd): # repeats for all of the data points on the link for a specific target data = {'magnitude': mag[n], 'filter': myfilter[n], 'error': mag_error[n]} time_to_float = float(hjd[n]) @@ -248,10 +242,7 @@ def find_and_ingest_photometry(self, events, targets): data_type='photometry', target=target) rd.save() + rd_list.append(rd) else: pass - - rd_list.append(rd) - n = n + 1 # repeats for all of the data points on the link for a specific target - k = k + 1 # repeats for all targets return rd_list From 13e4f8f18dee0ada6373c97cbe014693ce26131f Mon Sep 17 00:00:00 2001 From: new2128 <81988673+new2128@users.noreply.github.com> Date: Fri, 30 Jul 2021 16:31:23 -0500 Subject: [PATCH 2/3] Create time_allocation.py --- mop/toolbox/time_allocation.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 mop/toolbox/time_allocation.py diff --git a/mop/toolbox/time_allocation.py b/mop/toolbox/time_allocation.py new file mode 100644 index 00000000..275f6352 --- /dev/null +++ b/mop/toolbox/time_allocation.py @@ -0,0 +1,37 @@ +import requests + +from django.core.management.base import BaseCommand + + +from tom_dataproducts.models import ReducedDatum +from tom_observations.models import ObservationRecord +import tom_observations.templatetags.observation_extras as obslist +from tom_targets.models import Target, TargetExtra, TargetList + +#change to BaseCommand when uploading the code, this is just for testing purposes +class ReviewObservation(): + + help = 'Retrieve time allocation variables for different proposals to add new functionality to MOP' + + def review(self): + list_of_observations = [] + headers={'Authorization': 'Token {0}'.format(LCO_SETTINGS['api_key'])} + response = requests.get('https://observe.lco.global/api/proposals/', headers=headers,timeout=20) + results_dictionary = response.json().get('results') + instruments_dictionaries = results_dictionary[0].get('timeallocation_set') + for instrument_dictionary in instruments_dictionaries: + std_allocation = instrument_dictionary.get('std_allocation') + std_time_used = instrument_dictionary.get('std_time_used') + ipp_limit = instrument_dictionary.get('ipp_limit') + ipp_time_available = instrument_dictionary.get('ipp_time_available') + rr_allocation = instrument_dictionary.get('rr_allocation') + rr_time_used = instrument_dictionary.get('rr_time_used') + tc_allocation = instrument_dictionary.get('tc_allocation') + tc_time_used = instrument_dictionary.get('tc_time_used') + instrument_type = instrument_dictionary.get('instrument_type') + semester = instrument_dictionary.get('semester') + variable_list = [std_allocation, std_time_used, ipp_limit, ipp_time_available, + rr_allocation, rr_time_used, tc_allocation, tc_time_used, + instrument_type, semester] + list_of_observations.append(variable_list) + return list_of_observations From e3ca2c6e82e09d805831bc800c82df7f2fdcb24b Mon Sep 17 00:00:00 2001 From: new2128 <81988673+new2128@users.noreply.github.com> Date: Fri, 30 Jul 2021 16:31:48 -0500 Subject: [PATCH 3/3] Create plot_time_allocation.py --- mop/toolbox/plot_time_allocation.py | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 mop/toolbox/plot_time_allocation.py diff --git a/mop/toolbox/plot_time_allocation.py b/mop/toolbox/plot_time_allocation.py new file mode 100644 index 00000000..6a0f54ee --- /dev/null +++ b/mop/toolbox/plot_time_allocation.py @@ -0,0 +1,39 @@ +from django import template +import pandas as pd +import numpy as np +import matplotlib.pyplot as plt,mpld3 + +from mop.time_allocation import ReviewObservation +from tom_observations.models import ObservationRecord +from tom_targets.models import Target + +register = template.Library() +Review = ReviewObservation() +review = Review.review() +class Plot(): + + def plot(self, index): + fig, axis = plt.subplots(1,1,figsize=(10,5)) + x_values = [0,1,2] + y_values = [review[index][1]/review[index][0]] + if(review[index][4]==0): + y_values.append(0) + else: + y_values.append(review[index][5]/review[index][4]) + if(review[index][6]==0): + y_values.append(0) + else: + y_values.append(review[index][7]/review[index][6]) + axis.barh(x_values, y_values,color="red",height=0.4) + axis.set_xticks(np.arange(0, 1.2, 0.2)) + axis.set_yticks([0,1,2]) + axis.set_yticklabels(['Standard','Rapid Response','Time Critical']) # isn't currently working + axis.title.set_text(review[index][9] + " " + review[index][8]) + axis.text(0.01, 0, str(review[index][1])+"/"+str(review[index][0]), color='black',fontweight='bold') + axis.text(0.01, 1, str(review[index][5])+"/"+str(review[index][4]), color='black',fontweight='bold') + axis.text(0.01, 2, str(review[index][7])+"/"+str(review[index][6]), color='black',fontweight='bold') + plt.figtext(0.1, 0.01, "IPP", fontsize=18, bbox={"facecolor":"orange", "alpha":0.5, "pad":5}) + plt.figtext(0.3, 0.01, "Available: " + str(review[index][3]) + " Limit: " + str(review[index][2]), fontsize=14, bbox={"facecolor":"white", "alpha":0.5, "pad":5}) + plt.show() + mpld3.show() + return {'figure': fig}