#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 18 00:00:43 2017

@author: Felix Henningsen
"""

'''
The MetroPy Initialization File:
    Initializes the data to read-in dynamically from reduced FITS files.
'''
#### Initialization
from sys import argv
FITS_file = 'GRAVI.2017-08-15T20:41:32.463.fits'
rmsLength = 500
resolution = 100
img = 'png'

#### Importing
# handle different operating systems
import platform
import os
# define check variables for os handling
oscheckmain = platform.system()
oscheckver = platform.release()

# standard imports for science
import numpy as np
           
# deal with terminal argument inputs
#from sys import argv

# special library dealing with fits files
from astropy.io import fits
from astropy.table import Table
# ignore the units warning of usec
import warnings
from astropy.utils.exceptions import AstropyWarning
warnings.simplefilter('ignore', category=AstropyWarning)

# special plotsyle file for metrology

###### PARAMETERS #############################################################
# color coding for diodes
# blue, pink, green, orange
d_color = ['#0066ff','#ff33cc','#00cc00','#ff9900']
fc_color = 'black'

plotstyle = os.path.join(os.sep, 'tera', '5', 'fhenningsen', 'python_tools', 'gravi_visual', 'MetroPy', 'MetroPy_Style.txt')

def Init(FITS_file):
	###### HEADER READ-IN #########################################################

	# import the header file
	header_file = FITS_file[:-5] + '.hdr.txt'

	# open it
	header = np.loadtxt(header_file, dtype=bytes, delimiter='\n').astype(str)

	###### TIME DATA READ-IN ######################################################

	#time_file = metrology['TIME'] #unit microseconds
	time_file = FITS_file[:-5] + '.Metrology.time.fits'
	time = fits.open(time_file)[0].data
	# convert to seconds
	time = time / 1000 / 1000

	###### RAW VOLTS DATA READ-IN #################################################
	# splicable: access voltages of column i (starting at 0) by volts[:,i]

	# input volts file
	volts_file = FITS_file[:-5] + '.Metrology.volt.fits'

	# read in the fits file as data
	volts = fits.open( volts_file)[0].data

	# different possibility to read in directly from GRAV-File
	# this means different plotnumbering!!!!
	#volts = metrology['VOLT']

	###### PHASE DATA READ-IN #####################################################

	# input phase file
	phase_file = FITS_file[:-5] + '.Metrology.phase.500.fits'

	# read in the fits file as data
	phase = fits.open(phase_file)[0].data
					 
	# everything is stored as numpy arrays so we can use splicing (fast)
	# format is
	# phase[0][:, 4*n + k] == AT[4-n]D[k]
	# phase[0][:, 32 + n] == AT[4-n]FC        
	# with n = 0, 1, 2, 3 and k = 1, 2, 3, 4

	###### PHASOR DATA READ-IN ####################################################

	# input phase file
	phasor_file = FITS_file[:-5] + '.Metrology.phasor.500.fits'

	# read in the fits file as data
	phasor = fits.open(phasor_file)[0].data

	# format for 2D (plotx, ploty) plots is
	# phasor[0][:, n], phasor[0][:, n+1] = AT[x] D[5-x] FT, n = 0, 2, ..., 30
	# x = 4, 3, 2, 1 for n in [0, 6], [8, 14], [16, 22], [24, 30]
	# phasor[0][:, n], phasor[0][:, n+1] = AT[x] D[5-x] SC, n = 32, 34, ..., 62
	# x = 4, 3, 2, 1 for n in [32, 38], [40, 46], [48, 54], [56, 62]
	
	return header, time, volts, phase, phasor
