# -*- coding: utf-8 -*-
"""
Created on Thursday, 2020-04-09
Read FITS-file from RaspBerry Pi 4G and SDRplay

@author: Christian
"""

import numpy as np
import astropy.io.fits as fits

import matplotlib.pyplot as plt

#-------------------------------------------------------------------------------------------
plt.figure(figsize=(10,7))

myfile    = 'Dish1mD_TRANSIT_3401_spec.fits'
hdu       = fits.open(myfile)
flux1     = hdu[0].data
date      = hdu[0].header['DATE-OBS']
time1     = hdu[0].header['UT']
df        = hdu[0].header['CDELT1']  / 1.e6
Nbins     = hdu[0].header['NAXIS1']
telescope = hdu[0].header['TELESCOP']
shift     = hdu[0].header['CRPIX1']
Frest     = hdu[0].header['RESTFREQ'] / 1.e6
hdu.close()
print (hdu.info()) # FIT-file structure



f1 =    - shift*df
f2 = f1 + Nbins*df
faxis = np.arange(f1,f2,df) # generate frequency axis [MHz]

faxis2 = [] # convert delta-frequency in vlsr
for f in faxis:
    ff = f/Frest*3e5 # MHz -> km/s
    faxis2.append(ff)
    
plt.plot(faxis2,flux1,color='blue')

plt.xlabel('VLSR [Km/s]')
plt.ylabel('Flux [adu]')
#plt.yscale('log')
# plt.ylim(0.04,0.13)
# plt.xlim(0.9,1.5)
plt.grid()
plt.title('Transit 0.9 m dish of '+date+' at '+time1)

plt.savefig('Dipoles.png', bbox_inches=0, dpi=600)
