182 lines
5.6 KiB
Python
182 lines
5.6 KiB
Python
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from datetime import date
|
|
from django.utils import dateparse
|
|
from logbook.models import LogBookPlan, LogBookDay
|
|
from plan.models import INPUT_DATA_DIR
|
|
from plan.models import LaunchDate
|
|
|
|
from monthplan.models import Head, Observation, Seance, Correction, Scan, Survey, FlightPlan, DataDump, SurveyPath
|
|
|
|
from astropy.io import fits
|
|
|
|
from datetime import datetime
|
|
from django.utils import timezone
|
|
|
|
|
|
from astropy.io import fits
|
|
import pandas as pd
|
|
from pandas import ExcelWriter
|
|
from pandas import ExcelFile
|
|
#import xlrd
|
|
from astropy.table import Table
|
|
|
|
import astropy.units as u
|
|
from astropy.time.formats import erfa
|
|
from astropy.time import Time, TimeDelta, TimezoneInfo, TimeFromEpoch
|
|
|
|
from django.db.models import Q
|
|
from math import pi, cos, sin
|
|
from Quaternion import Quat
|
|
from scipy.spatial.transform import Rotation, Slerp
|
|
import numpy as np
|
|
|
|
MJDREF = 51543.875
|
|
TZ_UTC = TimezoneInfo(utc_offset=0*u.hour)
|
|
TZ_MSK = TimezoneInfo(utc_offset=3*u.hour)
|
|
|
|
TIMESTR_FORMAT = '%Y.%m.%d %H:%M:%S'
|
|
|
|
def str2date(timestr, tzone):
|
|
time_raw = datetime.strptime(timestr, TIMESTR_FORMAT)
|
|
time_zoned = time_raw.replace(tzinfo=tzone)
|
|
return time_zoned
|
|
|
|
|
|
def date2mission(dtime):
|
|
mjdref = Time(MJDREF, format='mjd')
|
|
dtime = Time(dtime , format='datetime')
|
|
#print(dtime.mjd)
|
|
return (dtime - mjdref).sec
|
|
|
|
|
|
def mission2date(timesec, tzone):
|
|
mjdref = Time(MJDREF, format='mjd')
|
|
delta = TimeDelta(timesec, format='sec')
|
|
dtime = mjdref + delta + 3*u.hour
|
|
return dtime.to_datetime()
|
|
|
|
|
|
def print_obsplan_dt_sec(dtstr):
|
|
dt = str2date(dtstr, tzone=TZ_MSK)
|
|
ts = date2mission(dt)
|
|
print(dt, '-->', ts, ts/14400)
|
|
|
|
|
|
def print_pz_dt_sec(dtstr):
|
|
dt = str2date(dtstr, tzone=TZ_UTC)
|
|
ts = date2mission(dt)
|
|
|
|
print(dt, '-->', ts)
|
|
|
|
|
|
def quat_to_pol_and_roll(qfin):
|
|
"""
|
|
it is assumed that quaternion is acting on the sattelite coordinate system
|
|
in order to orient in in icrs coordinates
|
|
opaxis - define dirrection of the main axis (x axis [1, 0, 0] coinside with optical axis)
|
|
we assume that north is oriented along z coordinate, we also name this coordinate
|
|
north for detectors
|
|
"""
|
|
opaxis=[1, 0, 0]
|
|
north=[0, 0, 1]
|
|
opticaxis = qfin.apply(opaxis)
|
|
print(opticaxis)
|
|
dec = np.arctan(opticaxis[2]/np.sqrt(opticaxis[1]**2 + opticaxis[0]**2))
|
|
ra = np.arctan2(opticaxis[1], opticaxis[0])%(2.*pi)
|
|
print(ra/pi*180,dec/pi*180)
|
|
return
|
|
yzprojection = np.cross(opticaxis, north)
|
|
vort = np.cross(north, opaxis)
|
|
|
|
rollangle = np.arctan2(np.sum(yzprojection*qfin.apply(vort), axis=1),
|
|
np.sum(yzprojection*qfin.apply(north), axis=1))
|
|
print(ra, dec, rollangle)
|
|
return ra, dec, rollangle
|
|
|
|
def logic(index,nskip):
|
|
if index % nskip == 0:
|
|
return False
|
|
return True
|
|
|
|
def load_data(file,obsid,nskip):
|
|
print('Load data (dummy)')
|
|
try:
|
|
survey=Survey.objects.get(experiment__exact=obsid)
|
|
except:
|
|
print("This survey is not found")
|
|
return
|
|
|
|
# remove all attached SurveyPath to this survey:
|
|
spath=survey.surveypath_set.all()
|
|
spath.delete()
|
|
|
|
csvfile=INPUT_DATA_DIR+'/ARJ-SCAN/'+file
|
|
df = pd.read_csv(csvfile,names=['date', 'time', 'q1', 'q2','q3','q4','dummy'], header=None, delim_whitespace=True, skipfooter=1, skiprows = lambda x: logic(x, nskip))
|
|
print(df)
|
|
for i in df.index:
|
|
dtstr="%s %s" % (df['date'][i], df['time'][i])
|
|
dt = str2date(dtstr, tzone=TZ_MSK)
|
|
ts = date2mission(dt)
|
|
eroday=ts/14400
|
|
dtime = Time(dt , format='datetime')
|
|
mjd=dtime.mjd
|
|
q1=df['q1'][i]
|
|
q2=df['q2'][i]
|
|
q3=df['q3'][i]
|
|
q4=df['q4'][i]
|
|
quat=Quat(attitude=[q2,q3,q4,q1])
|
|
sp=SurveyPath(survey=survey,dtime=dt,mjd=mjd,obt=ts,eroday=eroday,q1=q1,q2=q2,q3=q3,q4=q4,ra=quat.ra,dec=quat.dec,roll=quat.roll)
|
|
sp.save()
|
|
pass
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Initiates data base'
|
|
|
|
# def add_arguments(self, parser):
|
|
# parser.add_argument('poll_id', nargs='+', type=int)
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
load_data('kvater22_23-09.rez','10000000100',50)
|
|
load_data('kvater23_24-09.rez','10000000200',50)
|
|
|
|
load_data('kvater14-11_13_35_00.rez','10000100100',50)
|
|
load_data('kvater15-11_14_35_00.rez','10000100200',50)
|
|
|
|
|
|
load_data('20191203_150000_10000200100.iki','10000200100',50)
|
|
load_data('20191208_170000_10000300100.iki','10000300100',50)
|
|
load_data('20191210_223000_10000400100.iki','10000400100',50)
|
|
load_data('20191212_003000_11000000100.iki','11000000100',50)
|
|
return
|
|
|
|
#
|
|
# Experiments:
|
|
#
|
|
|
|
# .407947163597554
|
|
# .826585167542578 ! 327.89199999
|
|
# -.077996859000543 ! -34.35000000
|
|
# -.379805953741339 ! .000
|
|
print('327.89199999 -34.35000000')
|
|
arr=[.826585167542578, -.077996859000543, -.379805953741339, .407947163597554]
|
|
#qfin = Rotation.from_quat(arr)
|
|
#qfin = Rotation.from_quat([0, 0, np.sin(np.pi/4), np.cos(np.pi/4)])
|
|
#print(qfin.as_euler('zyx', degrees=True))
|
|
#qadd=Rotation([0, 0, 0, 1])
|
|
#qfin=Rotation(arr)
|
|
#a = quat_to_pol_and_roll(qfin)
|
|
#print(a)
|
|
#print_obsplan_dt_sec('2019.10.16 10:42:04')
|
|
|
|
#print_obsplan_dt_sec('2019.10.17 00:47:42')
|
|
# # print_pz_dt_sec('2019.08.21 23:00:00')
|
|
|
|
print('***')
|
|
quat = Quat(attitude=arr)
|
|
print(quat.ra, quat.dec, quat.roll)
|
|
|
|
#validate_flightplan()
|