Skip to content
Snippets Groups Projects
Commit eda9814b authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-244: added method to compute sun rise/set at the core given a timestamp

(cherry picked from commit 3bec3e06)
parent 8c06ccdd
No related branches found
No related tags found
1 merge request!269Resolve TMSS-435
...@@ -3,6 +3,22 @@ import astropy.units ...@@ -3,6 +3,22 @@ import astropy.units
from lofar.lta.sip import station_coordinates from lofar.lta.sip import station_coordinates
from datetime import datetime from datetime import datetime
from astropy.coordinates.earth import EarthLocation 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, def local_sidereal_time_for_utc_and_station(timestamp: datetime = None,
......
...@@ -4,6 +4,7 @@ from django.http import HttpResponse, JsonResponse, Http404 ...@@ -4,6 +4,7 @@ from django.http import HttpResponse, JsonResponse, Http404
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from lofar.sas.tmss.tmss.tmssapp import models from lofar.sas.tmss.tmss.tmssapp import models
from lofar.common.json_utils import get_default_json_object_for_schema 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 lofar.sas.tmss.tmss.tmssapp.adapters.parset import convert_to_parset
from drf_yasg.utils import swagger_auto_schema from drf_yasg.utils import swagger_auto_schema
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
...@@ -12,7 +13,7 @@ from django.apps import apps ...@@ -12,7 +13,7 @@ from django.apps import apps
from datetime import datetime from datetime import datetime
import dateutil.parser 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): def subtask_template_default_specification(request, subtask_template_pk:int):
subtask_template = get_object_or_404(models.SubtaskTemplate, pk=subtask_template_pk) subtask_template = get_object_or_404(models.SubtaskTemplate, pk=subtask_template_pk)
...@@ -109,4 +110,15 @@ def lst(request): ...@@ -109,4 +110,15 @@ def lst(request):
lst_lon = local_sidereal_time_for_utc_and_station(timestamp) 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? # 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') return HttpResponse(str(lst_lon), content_type='text/plain')
\ No newline at end of file
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)})
...@@ -65,6 +65,7 @@ urlpatterns = [ ...@@ -65,6 +65,7 @@ urlpatterns = [
path('schemas/<str:template>/<str:name>/<str:version>/', views.get_template_json_schema, name='get_template_json_schema'), 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'), #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('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/utc', views.utc, name="system-utc"),
path(r'util/lst', views.lst, name="conversion-lst"), path(r'util/lst', views.lst, name="conversion-lst"),
] ]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment