# -*- coding: utf-8 -*- """ Created on Mon Apr 3 17:51:27 2017 @author: sven """ from os import path, chdir, stat # Where the data is: #chdir('/mnt/Projects/ITB/TV 4/2017-04-07 TV4/SRF 160K') #chdir('/mnt/Projects/ITB/TV 3/2017-03-31/TV3_SRF') #chdir('/mnt/Projects/ITB/TV 7/2017-04-20 TV7/TV7 SRF') #chdir('/mnt/Projects/ITB/TV 7/2017-04-21 TV7/TV7 SRF aligned') #chdir('/mnt/Projects/ITB/Rework_TV5/SRF') chdir('/opt/data/CWIS2/TV5/SRF SWIR') # Name of the file #file = '20180314t1612_SRF_VNIR_RodB245K.raw' file = 'SRF SWIR.raw' #file = '20170421t1125 SRF VNIR aligned.raw' #file = '20170421t1136 SRFSWIR aligned.raw' #file ='20170420t1424 SRF SWIR.raw' #file = '20170407t1711_SRF_VNIR_light.raw' #file = '20170331t1520_VNIR_light.raw' #file = '20170331t1556_SWIR_light.raw' # Mostly what range to plot: VNIR (380-900nm) or SWIR (850-2500) ptype = 'SWIR' # ========================================= from ngis import rafile, darkfile, writecsv #from glob import glob from numpy import fromfile, where, median, zeros, asarray, nan, arange from matplotlib import pyplot as plt from matplotlib.backends.backend_pdf import PdfPages import tkinter as tk #from tkinter import filedialog shape = (328,1280) l = shape[0]*shape[1]; # length of a frame in pixels wroot = tk.Tk() lbl1 = tk.Label(wroot) lbl1.pack(padx=5,pady=5) frm1 = tk.Frame(wroot,bg='#aa7777',width=404,height=40,bd=2,relief='sunken') frm1.pack(padx=5,pady=5) frm2=tk.Frame(frm1,bg='#55cc55',width=10,height=36,bd=2,relief='raised') frm2.place(relx=0) frm1.update() rootf = path.basename(path.splitext(file)[0]) dfile = darkfile(file) drk = rafile(dfile,shape=shape,keep=True) nframes = stat(file).st_size//l//4 pldat = zeros((shape[0],nframes,5)) f = open(file,'rb') # first frame target = fromfile(f,dtype='f',count=l).reshape(shape[0],shape[1]) - drk spl = target[100,:] cen = [0,0,0,0,0] for jj in range(5): tspl=spl[jj*256:jj*256+255] fact = .95 ll = 2. while (ll/2 == ll//2): ncen = where(tspl > fact*max(tspl))[0] fact /= 2 ll = len(ncen) cen[jj] = int(median(ncen)) +jj*256 f.seek(0) #print(cen) #raise SystemExit lbl1.configure(text='Reading Data: ['+str(nframes)+' frames]') for i in range (nframes-1): frm=fromfile(f,dtype='f',count=l).reshape(shape[0],shape[1]) pldat[:,i,:]=frm[:,cen]-drk[:,cen] if i % 100 == 0: wroot.title('['+str(int(100*i/nframes))+'%] '+file) frm2.configure(width=400/nframes*i) wroot.update() f.close() wroot.destroy() pp = PdfPages('SRFplots '+ptype+'.pdf') page = plt.figure(figsize=(8.2,10.8),tight_layout=1) start,stop,extra, wv1, wv2 = 241,312,50,380,900 trmrow = 100 ; #row NOT used in SRF to find zero-order cutoffs if (ptype == 'SWIR'): start,stop,extra, wv1, wv2 = 26,242,80,850,2500 trmrow = 300 for jj in range(5): plt.subplot(5,1,jj+1) plt.autoscale(axis='x',tight=1) plt.grid(True) mx = max(pldat[trmrow,:,jj])/2 trm = min(where(pldat[trmrow,:,jj] < mx)[0])+2 tr2 = where(pldat[trmrow,trm:,jj] > mx)[0] if (len(tr2) == 0): trm2=nframes-extra else: trm2 = min(tr2)+trm-extra xax = asarray(range(nframes))[trm:trm2] dtr = trm2-trm ; wvl=zeros(dtr) for j in range(dtr): wvl[j]=wv1+j*(wv2-wv1)/(dtr) ;#-2.5*7.5 rmx = pldat[start:stop,trm:trm2,jj].max() # absolute maximum tname = ptype+' SRF - spatial column '+str(cen[jj]) writecsv(pldat[start:stop,trm:trm2,jj].transpose(),tname+'.csv', labels=', '.join(str(x) for x in arange(start,stop))) for i in range(start,stop): pdat=pldat[i,trm:trm2,jj].copy() pdat[where(pdat < rmx*.01)[0]] = nan plt.plot(wvl,pdat,color=plt.cm.rainbow(1-(i-start)/(stop-start)),lw=.5) plt.xlabel("~$\lambda[nm]$") plt.title(tname) plt.ylim(ymin=0) pp.savefig() pp.close() #directly counted fwhm numbers cntr = zeros((shape[0],5)) fwhm = zeros((shape[0],5)) #from matplotlib import pyplot as plt for jj in range(5): mx = max(pldat[trmrow,:,jj])/2 trm = min(where(pldat[trmrow,:,jj] < mx)[0])+2 tr2 = where(pldat[trmrow,trm:,jj] > mx)[0] if (len(tr2) == 0): trm2=nframes-extra else: trm2 = min(tr2)+trm-extra rmx = pldat[start:stop,trm:trm2,jj].max() # absolute maximum for i in range(start,stop): pdat=pldat[i,trm:trm2,jj].copy() sbmx = pdat.max() cntr[i,jj] = where(pdat == sbmx)[0][0] fwhm[i,jj] = len(where(pdat > 0.5*sbmx)[0]) fwhmout = open("FWHM numbers.csv",'w') fwhmout.write('col \\ row,'+', '.join(str(x) for x in arange(start,stop))+'\n') for jj in range(5): fwhmout.write(str(cen[jj])+','+', '.join(str(cntr[x,jj]) for x in arange(start,stop))+'\n') fwhmout.write(','+', '.join(str(fwhm[x,jj]) for x in arange(start,stop))+'\n') fwhmout.close()