# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import matplotlib.pyplot as plt
from numpy import asarray,fromfile,zeros
from os import chdir

date = "20250408"

chdir("/data/CITB/TV4/"+date)
roi = [1000,1500]

## Spectral cal "by eye" from methane absorption features
xx = asarray(range(513))*0.7+2034

shape = [513,3072]
npix = shape[0]*shape[1]
#bck = fromfile(date+" SSP Ge wafer source OPEN",dtype='f4').reshape(shape)
drk = fromfile("solar spectrum fiber DARK",count=npix,dtype='f4').reshape(shape)
#bck -= drk

files = ['solar spectrum fiber 4000',\
         'solar spectrum fiber',\
         'solar spectrum fiber BRIGHT']

for file in files:

    spc1 = fromfile(file,dtype='f4',count=npix).reshape(shape)
    
    img = spc1-drk
    
    pa = plt.figure()
    plt.xlabel("Detector column"); plt.ylabel("spectral row")
    plt.imshow(img,interpolation=None,filternorm=False)
    plt.clim(0,6000); plt.xlim(roi)
    plt.colorbar()
    plt.title("data minus darks")
    plt.show()
    pa.savefig(date+' '+file+'.png')
        
    spc = zeros(513)
#    bgk = zeros(513)
    for i in range(1235,1285):
        spc += img[:,i]
#        bgk += bck[:,i]
        
    spc /= 50
#    bgk /= 50
        
    #sprel = spc/bgk
    p3=plt.figure();plt.plot(xx,spc,linewidth=.5)
    plt.xlabel(r"$\lambda$[nm]"); plt.ylabel("brightness")
    plt.title(file)
    plt.show()
    p3.savefig(date+' '+file+' spc.png')
    
    # io = open(date+" CITB "+file.split()[-1]+".csv",mode="w")
    # for i in range(1,513): 
    #     print("%6.3f, %6.3f" % (xx[i],sprel[i]),file=io)
    # io.close()
