From eda9814b0476209454c41878c0b1aa6b6c775bb4 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Thu, 22 Oct 2020 15:45:24 +0000
Subject: [PATCH] TMSS-244: added method to compute sun rise/set at the core
 given a timestamp

(cherry picked from commit 3bec3e06509aa1af91b5a90936b099871e1234c5)
---
 SAS/TMSS/src/tmss/tmssapp/conversions.py | 16 ++++++++++++++++
 SAS/TMSS/src/tmss/tmssapp/views.py       | 16 ++++++++++++++--
 SAS/TMSS/src/tmss/urls.py                |  1 +
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/conversions.py b/SAS/TMSS/src/tmss/tmssapp/conversions.py
index ee8f35b3770..8666a8bc54e 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 cf57dc6832f..aa42ae550f0 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 376c1c83ba4..ed2f9112b21 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"),
 ]
-- 
GitLab