diff --git a/SAS/TMSS/src/tmss/tmssapp/conversions.py b/SAS/TMSS/src/tmss/tmssapp/conversions.py index ee8f35b3770cfba1683ad3a2822949a6f6dabe60..8666a8bc54e96bbb166ea7facb4f23672a034c83 100644 --- a/SAS/TMSS/src/tmss/tmssapp/conversions.py +++ b/SAS/TMSS/src/tmss/tmssapp/conversions.py @@ -3,6 +3,22 @@ import astropy.units from lofar.lta.sip import station_coordinates from datetime import datetime from astropy.coordinates.earth import EarthLocation +from astroplan.observer import Observer + + +# astropy/astroplan constants for lofar +LOFAR_CENTER_COORDS = station_coordinates.parse_station_coordinates()["CS002_LBA"] +LOFAR_CENTER_LOCATION = EarthLocation.from_geocentric(x=LOFAR_CENTER_COORDS['x'], y=LOFAR_CENTER_COORDS['y'], z=LOFAR_CENTER_COORDS['z'], unit=astropy.units.m) +LOFAR_CENTER_OBSERVER = Observer(LOFAR_CENTER_LOCATION, name="LOFAR", timezone="UTC") + + +def sun_rise_and_set_at_lofar_center(timestamp: datetime) -> (datetime, datetime): + '''compute the sunrise and sunset at the lofar center''' + sun_rise = LOFAR_CENTER_OBSERVER.sun_rise_time(time=Time(timestamp), which='previous') + if sun_rise.to_datetime().date() < timestamp.date(): + sun_rise = LOFAR_CENTER_OBSERVER.sun_rise_time(time=Time(timestamp), which='next') + sun_set = LOFAR_CENTER_OBSERVER.sun_set_time(time=sun_rise, which='next') + return sun_rise.to_datetime(), sun_set.to_datetime() def local_sidereal_time_for_utc_and_station(timestamp: datetime = None, diff --git a/SAS/TMSS/src/tmss/tmssapp/views.py b/SAS/TMSS/src/tmss/tmssapp/views.py index cf57dc6832f0f7340d7483d8789b2f0b2b2e12b8..aa42ae550f0df0636249c2a04c28cb11df0ee36d 100644 --- a/SAS/TMSS/src/tmss/tmssapp/views.py +++ b/SAS/TMSS/src/tmss/tmssapp/views.py @@ -4,6 +4,7 @@ from django.http import HttpResponse, JsonResponse, Http404 from django.shortcuts import get_object_or_404, render from lofar.sas.tmss.tmss.tmssapp import models from lofar.common.json_utils import get_default_json_object_for_schema +from lofar.common.datetimeutils import formatDatetime from lofar.sas.tmss.tmss.tmssapp.adapters.parset import convert_to_parset from drf_yasg.utils import swagger_auto_schema from rest_framework.permissions import AllowAny @@ -12,7 +13,7 @@ from django.apps import apps from datetime import datetime import dateutil.parser -from lofar.sas.tmss.tmss.tmssapp.conversions import local_sidereal_time_for_utc_and_station, local_sidereal_time_for_utc_and_longitude +from lofar.sas.tmss.tmss.tmssapp.conversions import local_sidereal_time_for_utc_and_station, local_sidereal_time_for_utc_and_longitude, sun_rise_and_set_at_lofar_center def subtask_template_default_specification(request, subtask_template_pk:int): subtask_template = get_object_or_404(models.SubtaskTemplate, pk=subtask_template_pk) @@ -109,4 +110,15 @@ def lst(request): lst_lon = local_sidereal_time_for_utc_and_station(timestamp) # todo: do we want to return a dict, so users can make sure their parameters were parsed correctly instead? - return HttpResponse(str(lst_lon), content_type='text/plain') \ No newline at end of file + return HttpResponse(str(lst_lon), content_type='text/plain') + +def get_sun_rise_and_set_at_lofar_center(request, timestamp:str=None): + if timestamp is None: + timestamp = datetime.utcnow() + else: + timestamp = dateutil.parser.parse(timestamp) # isot to datetime + + sun_rise, sun_set = sun_rise_and_set_at_lofar_center(timestamp) + return JsonResponse({'sun_rise': formatDatetime(sun_rise), + 'sun_set': formatDatetime(sun_set)}) + diff --git a/SAS/TMSS/src/tmss/urls.py b/SAS/TMSS/src/tmss/urls.py index 376c1c83ba457c81f98901a262b286e8db94c52b..ed2f9112b21d72354d3f06fa94cf30c77271ac31 100644 --- a/SAS/TMSS/src/tmss/urls.py +++ b/SAS/TMSS/src/tmss/urls.py @@ -65,6 +65,7 @@ urlpatterns = [ path('schemas/<str:template>/<str:name>/<str:version>/', views.get_template_json_schema, name='get_template_json_schema'), path('station_groups/<str:template_name>/<str:template_version>/<str:station_group>', views.get_stations_in_group, name='get_stations_in_group'), #TODO: how to make trailing slash optional? path('station_groups/<str:template_name>/<str:template_version>/<str:station_group>/', views.get_stations_in_group, name='get_stations_in_group'), + path('sun_rise_and_set/<str:timestamp>', views.get_sun_rise_and_set_at_lofar_center, name='get_sun_rise_and_set_at_lofar_center'), path(r'util/utc', views.utc, name="system-utc"), path(r'util/lst', views.lst, name="conversion-lst"), ]