Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing while loops to for enumerate() loops #18

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions mop/brokers/asassn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(':')
Expand All @@ -152,16 +151,14 @@ 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))

'''
Reads links with photometry data
'''
k = 0
for link in lightcurvelinks:
for k,link in enumerate(lightcurvelinks):
running = True
hjd = []
ut_date = []
Expand All @@ -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)):
Expand Down Expand Up @@ -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])
Expand All @@ -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
39 changes: 39 additions & 0 deletions mop/toolbox/plot_time_allocation.py
Original file line number Diff line number Diff line change
@@ -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}
37 changes: 37 additions & 0 deletions mop/toolbox/time_allocation.py
Original file line number Diff line number Diff line change
@@ -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