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 from monthplan.utils import load_monthplan_fits, load_monthplan_dir from astropy.io import fits 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 math def load_data(file): print('Load data (dummy)') fitsfile=INPUT_DATA_DIR+'/'+file table = Table.read(fitsfile) df = table.to_pandas() print("Column headings:") print(df.columns.str.encode('utf-8')) hdul = fits.open(fitsfile) hdul.info() head=Head() head.start=hdul[1].header['START'] head.stop=hdul[1].header['STOP'] head.gentime=hdul[1].header['GENTIME'] head.version=hdul[1].header['VERSION'] head.author=hdul[1].header['AUTHOR'] head.file=file head.nrows=df.shape[0] head.save() for i in df.index: row=(i+1) if(df['TYPE'][i].decode().strip() == 'SEANCE'): print('SEA') print(df['START'][i].decode().strip()) seance=Seance(head=head, row=row) seance.start=df['START'][i].decode().strip() seance.stop=df['STOP'][i].decode().strip() seance.stations=df['STATIONS'][i].decode().strip() seance.guid=df['GUID'][i].decode().strip() seance.save() if(df['TYPE'][i].decode().strip() == 'OBSERVATION'): obs=Observation(head=head, row=row) obs.start=df['START'][i].decode().strip() obs.stop=df['STOP'][i].decode().strip() obs.target=df['TARGET'][i].decode().strip() obs.experiment=df['EXPERIMENT'][i].decode().strip() obs.ra=df['RA'][i] obs.dec=df['DEC'][i] obs.ra=df['RA'][i] obs.roll_angle=df['ROLL_ANGLE'][i] obs.sun_xoz_angle=df['SUN_XOZ_ANGLE'][i] obs.guid=df['GUID'][i].decode().strip() obs.save() if(df['TYPE'][i].decode().strip() == 'CORRECTION'): cor=Correction(head=head, row=row) cor.start=df['START'][i].decode().strip() cor.stop=df['STOP'][i].decode().strip() cor.impstart=df['IMPSTART'][i].decode().strip() cor.guid=df['GUID'][i].decode().strip() cor.save() if(df['TYPE'][i].decode().strip() == 'SCAN'): print('SCAN') scan=Scan(head=head, row=row) scan.start=df['START'][i].decode().strip() scan.stop=df['STOP'][i].decode().strip() scan.target=df['TARGET'][i].decode().strip() scan.experiment=df['EXPERIMENT'][i].decode().strip() scan.ra=df['RA'][i] scan.dec=df['DEC'][i] scan.roll_angle=df['ROLL_ANGLE'][i] scan.sun_xoz_angle=df['SUN_XOZ_ANGLE'][i] scan.template=int(df['TEMPLATE'][i].decode().strip()) scan.guid=df['GUID'][i].decode().strip() scan.save() if(df['TYPE'][i].decode().strip() == 'SURVEY'): sur=Scan(head=head, row=row) sur.start=df['START'][i].decode().strip() sur.stop=df['STOP'][i].decode().strip() sur.target=df['TARGET'][i].decode().strip() sur.experiment=df['EXPERIMENT'][i].decode().strip() sur.ra_p=df['RA_P'][i] sur.dec_p=df['DEC_P'][i] sur.ra_z0=df['RA_Z0'][i] sur.dec_z0=df['DEC_Z0'][i] sur.ra_zk=df['RA_ZK'][i] sur.dec_zk=df['DEC_ZK'][i] sur.z_speed=df['Z_SPEED'][i] sur.guid=df['GUID'][i].decode().strip() sur.save() #myplan = LogBookPlan(daynumber=myday.daynumber, description=df['plan'][i], date=df['date'][i]) #myplan.save() #myday.plan.add(myplan) #myday.save() print(df['TYPE'][i]) 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('RG_MonthPlan_2019-07_v08.fits') #load_data('RG_MonthPlan_2019-08_v14.fits') #load_monthplan_fits('/export/django/srg/data/npol/fits/RG_MonthPlan_2020-06_v07.fits') load_monthplan_dir('/export/django/srg/data/npol/fits') self.stdout.write(self.style.SUCCESS("Done"))