diff --git a/tom_dataproducts/forced_photometry/atlas.py b/tom_dataproducts/forced_photometry/atlas.py index b0b3e6676..5bb327f73 100644 --- a/tom_dataproducts/forced_photometry/atlas.py +++ b/tom_dataproducts/forced_photometry/atlas.py @@ -1,21 +1,28 @@ from django import forms from django.conf import settings -from django.utils import timezone -from django.core.files.base import ContentFile from crispy_forms.layout import Div, HTML from astropy.time import Time -from tom_dataproducts.forced_photometry.forced_photometry_service import BaseForcedPhotometryQueryForm, BaseForcedPhotometryService, ForcedPhotometryServiceException -from tom_dataproducts.models import ReducedDatum, DataProduct -from tom_dataproducts.data_processor import run_data_processor -from tom_dataproducts.exceptions import InvalidFileFormatException +import tom_dataproducts.forced_photometry.forced_photometry_service as fps from tom_dataproducts.tasks import atlas_query from tom_targets.models import Target -class AtlasForcedPhotometryQueryForm(BaseForcedPhotometryQueryForm): - min_date = forms.CharField(label='Min date:', required=False, widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'})) - max_date = forms.CharField(label='Max date:', required=False, widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'})) - min_date_mjd = forms.FloatField(label='Min date (mjd):', required=False, widget=forms.NumberInput(attrs={'class': 'ml-2'})) - max_date_mjd = forms.FloatField(label='Max date (mjd):', required=False, widget=forms.NumberInput(attrs={'class': 'ml-2'})) +class AtlasForcedPhotometryQueryForm(fps.BaseForcedPhotometryQueryForm): + min_date = forms.CharField( + label='Min date:', required=False, + widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'}) + ) + max_date = forms.CharField( + label='Max date:', required=False, + widget=forms.TextInput(attrs={'class': 'ml-2', 'type': 'datetime-local'}) + ) + min_date_mjd = forms.FloatField( + label='Min date (mjd):', required=False, + widget=forms.NumberInput(attrs={'class': 'ml-2'}) + ) + max_date_mjd = forms.FloatField( + label='Max date (mjd):', required=False, + widget=forms.NumberInput(attrs={'class': 'ml-2'}) + ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -65,12 +72,14 @@ def clean(self): return cleaned_data -class AtlasForcedPhotometryService(BaseForcedPhotometryService): +class AtlasForcedPhotometryService(fps.BaseForcedPhotometryService): name = 'Atlas' def __init__(self): super().__init__ - self.success_message = 'Asynchronous Atlas query is processing. Refresh the page once complete it will show up as a dataproduct in the "Manage Data" tab.' + self.success_message = ('Asynchronous Atlas query is processing. ' + 'Refresh the page once complete it will show ' + 'up as a dataproduct in the "Manage Data" tab.') def get_form(self): """ @@ -91,21 +100,31 @@ def query_service(self, query_parameters): if not max_date_mjd and query_parameters.get('max_date'): max_date_mjd = Time(query_parameters.get('max_date')).mjd if not Target.objects.filter(pk=query_parameters.get('target_id')).exists(): - raise ForcedPhotometryServiceException(f"Target {query_parameters.get('target_id')} does not exist") + raise fps.ForcedPhotometryServiceException(f"Target {query_parameters.get('target_id')} does not exist") if 'atlas' not in settings.FORCED_PHOTOMETRY_SERVICES: - raise ForcedPhotometryServiceException("Must specify 'atlas' settings in FORCED_PHOTOMETRY_SERVICES") + raise fps.ForcedPhotometryServiceException("Must specify 'atlas' settings in FORCED_PHOTOMETRY_SERVICES") if not settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('url'): - raise ForcedPhotometryServiceException("Must specify a 'url' under atlas settings in FORCED_PHOTOMETRY_SERVICES") + raise fps.ForcedPhotometryServiceException( + "Must specify a 'url' under atlas settings in FORCED_PHOTOMETRY_SERVICES" + ) if not settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('api_key'): - raise ForcedPhotometryServiceException("Must specify an 'api_key' under atlas settings in FORCED_PHOTOMETRY_SERVICES") + raise fps.ForcedPhotometryServiceException( + "Must specify an 'api_key' under atlas settings in FORCED_PHOTOMETRY_SERVICES" + ) if 'django_dramatiq' in settings.INSTALLED_APPS: - atlas_query.send(min_date_mjd, max_date_mjd, query_parameters.get('target_id'), self.get_data_product_type()) + atlas_query.send(min_date_mjd, max_date_mjd, + query_parameters.get('target_id'), + self.get_data_product_type()) else: - query_succeeded = atlas_query(min_date_mjd, max_date_mjd, query_parameters.get('target_id'), self.get_data_product_type()) + query_succeeded = atlas_query(min_date_mjd, max_date_mjd, + query_parameters.get('target_id'), + self.get_data_product_type()) if not query_succeeded: - raise ForcedPhotometryServiceException("Atlas query failed, check the server logs for more information") + raise fps.ForcedPhotometryServiceException( + "Atlas query failed, check the server logs for more information" + ) self.success_message = "Atlas query completed. View its data product in the 'Manage Data' tab" return True diff --git a/tom_dataproducts/forced_photometry/forced_photometry_service.py b/tom_dataproducts/forced_photometry/forced_photometry_service.py index 8ab2b2a9a..806345d90 100644 --- a/tom_dataproducts/forced_photometry/forced_photometry_service.py +++ b/tom_dataproducts/forced_photometry/forced_photometry_service.py @@ -1,18 +1,12 @@ from abc import ABC, abstractmethod -import copy import logging -import requests from crispy_forms.helper import FormHelper -from crispy_forms.layout import ButtonHolder, Layout, Submit, Div, HTML +from crispy_forms.layout import ButtonHolder, Layout, Submit from django import forms from django.conf import settings -from django.contrib.auth.models import Group -from django.core.files.base import ContentFile from django.utils.module_loading import import_string -from tom_targets.models import Target - logger = logging.getLogger(__name__) @@ -37,7 +31,9 @@ def get_service_class(name): try: return available_classes[name] except KeyError: - raise ImportError(f'Could not a find a forced photometry service with the name {name}. Did you add it to TOM_FORCED_PHOTOMETRY_CLASSES?') + raise ImportError(( + f'Could not a find a forced photometry service with the name {name}. ' + 'Did you add it to TOM_FORCED_PHOTOMETRY_CLASSES?')) class ForcedPhotometryServiceException(Exception): diff --git a/tom_dataproducts/processors/atlas_processor.py b/tom_dataproducts/processors/atlas_processor.py index 9e90ed1c9..9d452c932 100644 --- a/tom_dataproducts/processors/atlas_processor.py +++ b/tom_dataproducts/processors/atlas_processor.py @@ -39,7 +39,7 @@ def _process_photometry_from_plaintext(self, data_product): text file, as produced by the ATLAS forced photometry service at https://fallingstar-data.com/forcedphot The header looks like this: - ###MJD m dm uJy duJy F err chi/N RA Dec x y maj min phi apfit mag5sig Sky Obs + ###MJD m dm uJy duJy F err chi/N RA Dec x y maj min phi apfit mag5sig Sky Obs :param data_product: ATLAS Photometric DataProduct which will be processed into a list of dicts :type data_product: DataProduct diff --git a/tom_dataproducts/tasks.py b/tom_dataproducts/tasks.py index d29ac0b3b..f6ce1b2d9 100644 --- a/tom_dataproducts/tasks.py +++ b/tom_dataproducts/tasks.py @@ -23,12 +23,13 @@ def atlas_query(min_date_mjd, max_date_mjd, target_id, data_product_type): print("Calling atlas query!") target = Target.objects.get(pk=target_id) - headers = {"Authorization": f"Token {settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('api_key')}", "Accept": "application/json"} + headers = {"Authorization": f"Token {settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('api_key')}", + "Accept": "application/json"} base_url = settings.FORCED_PHOTOMETRY_SERVICES.get('atlas', {}).get('url') task_url = None while not task_url: with requests.Session() as s: - task_data = {"ra":target.ra, "dec": target.dec, "mjd_min": min_date_mjd, "send_email": False} + task_data = {"ra": target.ra, "dec": target.dec, "mjd_min": min_date_mjd, "send_email": False} if max_date_mjd: task_data['mjd_max'] = max_date_mjd resp = s.post( @@ -81,11 +82,11 @@ def atlas_query(min_date_mjd, max_date_mjd, target_id, data_product_type): dp_name = f"atlas_{Time(min_date_mjd, format='mjd').strftime('%Y_%m_%d')}" if max_date_mjd: dp_name += f"-{Time(max_date_mjd, format='mjd').strftime('%Y_%m_%d')}" - dp_name += f"_{urlparse(result_url)[2].rpartition('/')[2]}" + dp_name += f"_{urlparse(result_url)[2].rpartition('/')[2]}" file = ContentFile(results.content, name=dp_name) dp = DataProduct.objects.create( - product_id = dp_name, + product_id=dp_name, target=target, data=file, data_product_type=data_product_type, @@ -99,4 +100,4 @@ def atlas_query(min_date_mjd, max_date_mjd, target_id, data_product_type): logger.error(f"Error processing returned Atlas data into ReducedDatums: {repr(e)}") return False - return True \ No newline at end of file + return True diff --git a/tom_dataproducts/templatetags/dataproduct_extras.py b/tom_dataproducts/templatetags/dataproduct_extras.py index d57ce06d6..107670f5b 100644 --- a/tom_dataproducts/templatetags/dataproduct_extras.py +++ b/tom_dataproducts/templatetags/dataproduct_extras.py @@ -95,12 +95,14 @@ def dataproduct_list_all(context): 'products': products, } + @register.inclusion_tag('tom_dataproducts/partials/query_forced_photometry.html') def query_forced_photometry(target): services = get_service_classes().keys() - return {'forced_photometry_services': services, - 'target': target - } + return { + 'forced_photometry_services': services, + 'target': target + } @register.inclusion_tag('tom_dataproducts/partials/upload_dataproduct.html', takes_context=True) diff --git a/tom_dataproducts/urls.py b/tom_dataproducts/urls.py index b47124344..1f9b7ae73 100644 --- a/tom_dataproducts/urls.py +++ b/tom_dataproducts/urls.py @@ -23,7 +23,8 @@ path('data/group//delete/', DataProductGroupDeleteView.as_view(), name='group-delete'), path('data/upload/', DataProductUploadView.as_view(), name='upload'), path('data/reduced/update/', UpdateReducedDataView.as_view(), name='update-reduced-data'), - path('data/forced_photometry//query/', ForcedPhotometryQueryView.as_view(), name='forced-photometry-query'), + path('data/forced_photometry//query/', ForcedPhotometryQueryView.as_view(), + name='forced-photometry-query'), path('data//delete/', DataProductDeleteView.as_view(), name='delete'), path('data//feature/', DataProductFeatureView.as_view(), name='feature'), path('data//share/', DataShareView.as_view(), name='share'), diff --git a/tom_dataproducts/views.py b/tom_dataproducts/views.py index 85ce0a2b5..0423103a8 100644 --- a/tom_dataproducts/views.py +++ b/tom_dataproducts/views.py @@ -37,7 +37,7 @@ from tom_observations.models import ObservationRecord from tom_observations.facility import get_service_class from tom_dataproducts.serializers import DataProductSerializer -from tom_dataproducts.forced_photometry.forced_photometry_service import ForcedPhotometryServiceException, get_service_class +import tom_dataproducts.forced_photometry.forced_photometry_service as fps import requests @@ -122,7 +122,7 @@ def get_service_class(self): """ Gets the forced photometry service class """ - return get_service_class(self.get_service()) + return fps.get_service_class(self.get_service()) def get_form_class(self): """ @@ -157,7 +157,7 @@ def post(self, request, *args, **kwargs): service = self.get_service_class()() try: service.query_service(form.cleaned_data) - except ForcedPhotometryServiceException as e: + except fps.ForcedPhotometryServiceException as e: form.add_error(f"Problem querying forced photometry service: {repr(e)}") return self.form_invalid(form) messages.info(self.request, service.get_success_message())