diff --git a/schainpy/model/__init__.py b/schainpy/model/__init__.py index 3d76a6d..ae3ce46 100644 --- a/schainpy/model/__init__.py +++ b/schainpy/model/__init__.py @@ -5,8 +5,8 @@ # from schainpy.model.utils.jroutils import * # from schainpy.serializer import * +from graphics import * from data import * from io import * from proc import * -from graphics import * -from utils import * \ No newline at end of file +from utils import * diff --git a/schainpy/model/graphics/mpldriver.py b/schainpy/model/graphics/mpldriver.py index b84fb1c..c1e31ac 100644 --- a/schainpy/model/graphics/mpldriver.py +++ b/schainpy/model/graphics/mpldriver.py @@ -4,32 +4,36 @@ import sys import matplotlib if 'linux' in sys.platform: - matplotlib.use("TKAgg") + matplotlib.use("TkAgg") if 'darwin' in sys.platform: matplotlib.use('TKAgg') -#Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX' +# Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX' import matplotlib.pyplot from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib.ticker import FuncFormatter, LinearLocator ########################################### -#Actualizacion de las funciones del driver +# Actualizacion de las funciones del driver ########################################### # create jro colormap jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90] -blu_values = matplotlib.pyplot.get_cmap("seismic_r", 20)(numpy.arange(20))[10:15] -ncmap = matplotlib.colors.LinearSegmentedColormap.from_list("jro", numpy.vstack((blu_values, jet_values))) +blu_values = matplotlib.pyplot.get_cmap( + "seismic_r", 20)(numpy.arange(20))[10:15] +ncmap = matplotlib.colors.LinearSegmentedColormap.from_list( + "jro", numpy.vstack((blu_values, jet_values))) matplotlib.pyplot.register_cmap(cmap=ncmap) -def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi = 80): + +def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi=80): matplotlib.pyplot.ioff() - fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(1.0*width/dpi, 1.0*height/dpi)) + fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=( + 1.0 * width / dpi, 1.0 * height / dpi)) fig.canvas.manager.set_window_title(wintitle) # fig.canvas.manager.resize(width, height) matplotlib.pyplot.ion() @@ -39,10 +43,11 @@ def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi = 80 return fig + def closeFigure(show=False, fig=None): -# matplotlib.pyplot.ioff() -# matplotlib.pyplot.pause(0) + # matplotlib.pyplot.ioff() + # matplotlib.pyplot.pause(0) if show: matplotlib.pyplot.show() @@ -60,45 +65,52 @@ def closeFigure(show=False, fig=None): return + def saveFigure(fig, filename): -# matplotlib.pyplot.ioff() + # matplotlib.pyplot.ioff() fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi) # matplotlib.pyplot.ion() + def clearFigure(fig): fig.clf() + def setWinTitle(fig, title): fig.canvas.manager.set_window_title(title) + def setTitle(fig, title): fig.suptitle(title) + def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False): matplotlib.pyplot.ioff() matplotlib.pyplot.figure(fig.number) axes = matplotlib.pyplot.subplot2grid((nrow, ncol), - (xpos, ypos), - colspan=colspan, - rowspan=rowspan, - polar=polar) + (xpos, ypos), + colspan=colspan, + rowspan=rowspan, + polar=polar) matplotlib.pyplot.ion() return axes + def setAxesText(ax, text): ax.annotate(text, - xy = (.1, .99), - xycoords = 'figure fraction', - horizontalalignment = 'left', - verticalalignment = 'top', - fontsize = 10) + xy=(.1, .99), + xycoords='figure fraction', + horizontalalignment='left', + verticalalignment='top', + fontsize=10) + def printLabels(ax, xlabel, ylabel, title): @@ -106,11 +118,11 @@ def printLabels(ax, xlabel, ylabel, title): ax.set_ylabel(ylabel, size=11) ax.set_title(title, size=8) + def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', ticksize=9, xtick_visible=True, ytick_visible=True, nxticks=4, nyticks=10, - grid=None,color='blue'): - + grid=None, color='blue'): """ Input: @@ -119,18 +131,19 @@ def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='' matplotlib.pyplot.ioff() - ax.set_xlim([xmin,xmax]) - ax.set_ylim([ymin,ymax]) + ax.set_xlim([xmin, xmax]) + ax.set_ylim([ymin, ymax]) printLabels(ax, xlabel, ylabel, title) ###################################################### - if (xmax-xmin)<=1: - xtickspos = numpy.linspace(xmin,xmax,nxticks) - xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos]) + if (xmax - xmin) <= 1: + xtickspos = numpy.linspace(xmin, xmax, nxticks) + xtickspos = numpy.array([float("%.1f" % i) for i in xtickspos]) ax.set_xticks(xtickspos) else: - xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) + xtickspos = numpy.arange(nxticks) * \ + int((xmax - xmin) / (nxticks)) + int(xmin) # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin) ax.set_xticks(xtickspos) @@ -168,9 +181,11 @@ def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='' return iplot + def set_linedata(ax, x, y, idline): - ax.lines[idline].set_data(x,y) + ax.lines[idline].set_data(x, y) + def pline(iplot, x, y, xlabel='', ylabel='', title=''): @@ -180,14 +195,15 @@ def pline(iplot, x, y, xlabel='', ylabel='', title=''): set_linedata(ax, x, y, idline=0) + def addpline(ax, x, y, color, linestyle, lw): - ax.plot(x,y,color=color,linestyle=linestyle,lw=lw) + ax.plot(x, y, color=color, linestyle=linestyle, lw=lw) def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, - xlabel='', ylabel='', title='', ticksize = 9, - colormap='jet',cblabel='', cbsize="5%", + xlabel='', ylabel='', title='', ticksize=9, + colormap='jet', cblabel='', cbsize="5%", XAxisAsTime=False): matplotlib.pyplot.ioff() @@ -197,16 +213,16 @@ def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, fig = ax.get_figure() fig.add_axes(ax_cb) - ax.set_xlim([xmin,xmax]) - ax.set_ylim([ymin,ymax]) + ax.set_xlim([xmin, xmax]) + ax.set_ylim([ymin, ymax]) printLabels(ax, xlabel, ylabel, title) z = numpy.ma.masked_invalid(z) - cmap=matplotlib.pyplot.get_cmap(colormap) + cmap = matplotlib.pyplot.get_cmap(colormap) cmap.set_bad('black', 1.) - imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=cmap) - cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) + imesh = ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap) + cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) cb.set_label(cblabel) # for tl in ax_cb.get_yticklabels(): @@ -235,13 +251,15 @@ def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, if XAxisAsTime: - func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) + def func(x, pos): return ('%s') % ( + datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) ax.xaxis.set_major_formatter(FuncFormatter(func)) ax.xaxis.set_major_locator(LinearLocator(7)) matplotlib.pyplot.ion() return imesh + def pcolor(imesh, z, xlabel='', ylabel='', title=''): z = z.T @@ -249,11 +267,14 @@ def pcolor(imesh, z, xlabel='', ylabel='', title=''): printLabels(ax, xlabel, ylabel, title) imesh.set_array(z.ravel()) + def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): printLabels(ax, xlabel, ylabel, title) - ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) + ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, + cmap=matplotlib.pyplot.get_cmap(colormap)) + def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): @@ -263,17 +284,16 @@ def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', col z = numpy.ma.masked_invalid(z) - cmap=matplotlib.pyplot.get_cmap(colormap) + cmap = matplotlib.pyplot.get_cmap(colormap) cmap.set_bad('black', 1.) + ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap) - ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=cmap) def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, - ticksize=9, xtick_visible=True, ytick_visible=True, - nxticks=4, nyticks=10, - grid=None): - + ticksize=9, xtick_visible=True, ytick_visible=True, + nxticks=4, nyticks=10, + grid=None): """ Input: @@ -285,11 +305,12 @@ def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', tit lines = ax.plot(x.T, y) leg = ax.legend(lines, legendlabels, loc='upper right') leg.get_frame().set_alpha(0.5) - ax.set_xlim([xmin,xmax]) - ax.set_ylim([ymin,ymax]) + ax.set_xlim([xmin, xmax]) + ax.set_ylim([ymin, ymax]) printLabels(ax, xlabel, ylabel, title) - xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) + xtickspos = numpy.arange(nxticks) * \ + int((xmax - xmin) / (nxticks)) + int(xmin) ax.set_xticks(xtickspos) for tick in ax.get_xticklabels(): @@ -332,13 +353,13 @@ def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''): for i in range(len(ax.lines)): line = ax.lines[i] - line.set_data(x[i,:],y) + line.set_data(x[i, :], y) -def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, - ticksize=9, xtick_visible=True, ytick_visible=True, - nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None", - grid=None, XAxisAsTime=False): +def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, + ticksize=9, xtick_visible=True, ytick_visible=True, + nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None", + grid=None, XAxisAsTime=False): """ Input: @@ -355,10 +376,11 @@ def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='' leg = ax.legend(lines, legendlabels, loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0) - for label in leg.get_texts(): label.set_fontsize(9) + for label in leg.get_texts(): + label.set_fontsize(9) - ax.set_xlim([xmin,xmax]) - ax.set_ylim([ymin,ymax]) + ax.set_xlim([xmin, xmax]) + ax.set_ylim([ymin, ymax]) printLabels(ax, xlabel, ylabel, title) # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) @@ -393,7 +415,8 @@ def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='' if XAxisAsTime: - func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) + def func(x, pos): return ('%s') % ( + datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) ax.xaxis.set_major_formatter(FuncFormatter(func)) ax.xaxis.set_major_locator(LinearLocator(7)) @@ -401,6 +424,7 @@ def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='' return iplot + def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''): ax = iplot.axes @@ -409,19 +433,20 @@ def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''): for i in range(len(ax.lines)): line = ax.lines[i] - line.set_data(x,y[i,:]) + line.set_data(x, y[i, :]) + def createPolar(ax, x, y, - xlabel='', ylabel='', title='', ticksize = 9, - colormap='jet',cblabel='', cbsize="5%", - XAxisAsTime=False): + xlabel='', ylabel='', title='', ticksize=9, + colormap='jet', cblabel='', cbsize="5%", + XAxisAsTime=False): matplotlib.pyplot.ioff() - ax.plot(x,y,'bo', markersize=5) + ax.plot(x, y, 'bo', markersize=5) # ax.set_rmax(90) - ax.set_ylim(0,90) - ax.set_yticks(numpy.arange(0,90,20)) + ax.set_ylim(0, 90) + ax.set_yticks(numpy.arange(0, 90, 20)) # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11') # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11') # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical') @@ -444,9 +469,9 @@ def createPolar(ax, x, y, matplotlib.pyplot.ion() - return iplot + def polar(iplot, x, y, xlabel='', ylabel='', title=''): ax = iplot.axes @@ -456,6 +481,7 @@ def polar(iplot, x, y, xlabel='', ylabel='', title=''): set_linedata(ax, x, y, idline=0) + def draw(fig): if type(fig) == 'int': @@ -463,6 +489,7 @@ def draw(fig): fig.canvas.draw() + def pause(interval=0.000001): matplotlib.pyplot.pause(interval) diff --git a/schainpy/model/io/MIRAtest.py b/schainpy/model/io/MIRAtest.py index c48bc8b..ea8e94f 100644 --- a/schainpy/model/io/MIRAtest.py +++ b/schainpy/model/io/MIRAtest.py @@ -1,4 +1,5 @@ -import os, sys +import os +import sys import glob import fnmatch import datetime @@ -6,11 +7,9 @@ import time import re import h5py import numpy -import matplotlib.pyplot as plt -import pylab as plb from scipy.optimize import curve_fit -from scipy import asarray as ar,exp +from scipy import asarray as ar, exp from scipy import stats from duplicity.path import Path @@ -31,113 +30,130 @@ from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation from numpy import imag, shape, NaN -startFp = open('/home/erick/Documents/MIRA35C/20160117/20160117_0000.zspc',"rb") - - -FILE_HEADER = numpy.dtype([ #HEADER 1024bytes - ('Hname',numpy.str_,32), #Original file name - ('Htime',numpy.str_,32), #Date and time when the file was created - ('Hoper',numpy.str_,64), #Name of operator who created the file - ('Hplace',numpy.str_,128), #Place where the measurements was carried out - ('Hdescr',numpy.str_,256), #Description of measurements - ('Hdummy',numpy.str_,512), #Reserved space - #Main chunk - ('Msign','=5 - ('SPARrawGate2','=5 + ('SPARrawGate1', ' vertical) + ('BeamAngleZen', ' vertical) - ('AntennaCoord0',' endFp: - sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp) + sys.stderr.write( + "Warning %s: Size value read from System Header is lower than it has to be\n" % fp) return 0 - + if OffRHeader < endFp: - sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp) + sys.stderr.write( + "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp) return 0 - + return 1 - + class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODataReader): - + path = None startDate = None endDate = None @@ -435,28 +465,25 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa endTime = None walk = None isConfig = False - - - fileList= None - - #metadata - TimeZone= None - Interval= None - heightList= None - - #data - data= None - utctime= None - - - + + fileList = None + + # metadata + TimeZone = None + Interval = None + heightList = None + + # data + data = None + utctime = None + def __init__(self, **kwargs): - - #Eliminar de la base la herencia + + # Eliminar de la base la herencia ProcessingUnit.__init__(self, **kwargs) - + #self.isConfig = False - + #self.pts2read_SelfSpectra = 0 #self.pts2read_CrossSpectra = 0 #self.pts2read_DCchannels = 0 @@ -464,60 +491,59 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa self.utc = None self.ext = ".fdt" self.optchar = "P" - self.fpFile=None + self.fpFile = None self.fp = None - self.BlockCounter=0 + self.BlockCounter = 0 self.dtype = None self.fileSizeByHeader = None self.filenameList = [] self.fileSelector = 0 - self.Off2StartNxtRec=0 - self.RecCounter=0 + self.Off2StartNxtRec = 0 + self.RecCounter = 0 self.flagNoMoreFiles = 0 - self.data_spc=None - self.data_cspc=None - self.data_output=None + self.data_spc = None + self.data_cspc = None + self.data_output = None self.path = None - self.OffsetStartHeader=0 - self.Off2StartData=0 + self.OffsetStartHeader = 0 + self.Off2StartData = 0 self.ipp = 0 - self.nFDTdataRecors=0 + self.nFDTdataRecors = 0 self.blocksize = 0 self.dataOut = Spectra() - self.profileIndex = 1 #Always - self.dataOut.flagNoData=False + self.profileIndex = 1 # Always + self.dataOut.flagNoData = False self.dataOut.nRdPairs = 0 self.dataOut.pairsList = [] - self.dataOut.data_spc=None - self.dataOut.noise=[] - self.dataOut.velocityX=[] - self.dataOut.velocityY=[] - self.dataOut.velocityV=[] - - + self.dataOut.data_spc = None + self.dataOut.noise = [] + self.dataOut.velocityX = [] + self.dataOut.velocityY = [] + self.dataOut.velocityV = [] def Files2Read(self, fp): ''' Function that indicates the number of .fdt files that exist in the folder to be read. It also creates an organized list with the names of the files to read. ''' - #self.__checkPath() - - ListaData=os.listdir(fp) #Gets the list of files within the fp address - ListaData=sorted(ListaData) #Sort the list of files from least to largest by names - nFiles=0 #File Counter - FileList=[] #A list is created that will contain the .fdt files - for IndexFile in ListaData : - if '.fdt' in IndexFile: + # self.__checkPath() + + # Gets the list of files within the fp address + ListaData = os.listdir(fp) + # Sort the list of files from least to largest by names + ListaData = sorted(ListaData) + nFiles = 0 # File Counter + FileList = [] # A list is created that will contain the .fdt files + for IndexFile in ListaData: + if '.fdt' in IndexFile: FileList.append(IndexFile) - nFiles+=1 - - #print 'Files2Read' - #print 'Existen '+str(nFiles)+' archivos .fdt' - - self.filenameList=FileList #List of files from least to largest by names - - + nFiles += 1 + + # print 'Files2Read' + # print 'Existen '+str(nFiles)+' archivos .fdt' + + self.filenameList = FileList # List of files from least to largest by names + def run(self, **kwargs): ''' This method will be the one that will initiate the data entry, will be called constantly. @@ -527,341 +553,350 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa if not self.isConfig: self.setup(**kwargs) self.isConfig = True - + self.getData() - #print 'running' - - + # print 'running' + def setup(self, path=None, - startDate=None, - endDate=None, - startTime=None, - endTime=None, - walk=True, - timezone='utc', - code = None, - online=False, - ReadMode=None, - **kwargs): - + startDate=None, + endDate=None, + startTime=None, + endTime=None, + walk=True, + timezone='utc', + code=None, + online=False, + ReadMode=None, + **kwargs): + self.isConfig = True - - self.path=path - self.startDate=startDate - self.endDate=endDate - self.startTime=startTime - self.endTime=endTime - self.walk=walk - self.ReadMode=int(ReadMode) - + + self.path = path + self.startDate = startDate + self.endDate = endDate + self.startTime = startTime + self.endTime = endTime + self.walk = walk + self.ReadMode = int(ReadMode) + pass - - + def getData(self): ''' Before starting this function, you should check that there is still an unread file, If there are still blocks to read or if the data block is empty. - + You should call the file "read". - + ''' - + if self.flagNoMoreFiles: self.dataOut.flagNoData = True print 'NoData se vuelve true' return 0 - self.fp=self.path + self.fp = self.path self.Files2Read(self.fp) self.readFile(self.fp) self.dataOut.data_spc = self.data_spc - self.dataOut.data_cspc =self.data_cspc - self.dataOut.data_output=self.data_output - + self.dataOut.data_cspc = self.data_cspc + self.dataOut.data_output = self.data_output + print 'self.dataOut.data_output', shape(self.dataOut.data_output) - - #self.removeDC() - return self.dataOut.data_spc - - - def readFile(self,fp): + + # self.removeDC() + return self.dataOut.data_spc + + def readFile(self, fp): ''' You must indicate if you are reading in Online or Offline mode and load the The parameters for this file reading mode. - + Then you must do 2 actions: - + 1. Get the BLTR FileHeader. 2. Start reading the first block. ''' - - #The address of the folder is generated the name of the .fdt file that will be read - print "File: ",self.fileSelector+1 - + + # The address of the folder is generated the name of the .fdt file that will be read + print "File: ", self.fileSelector + 1 + if self.fileSelector < len(self.filenameList): - - self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector]) - #print self.fpFile + + self.fpFile = str(fp) + '/' + \ + str(self.filenameList[self.fileSelector]) + # print self.fpFile fheader = FileHeaderBLTR() - fheader.FHread(self.fpFile) #Bltr FileHeader Reading - self.nFDTdataRecors=fheader.nFDTdataRecors - - self.readBlock() #Block reading + fheader.FHread(self.fpFile) # Bltr FileHeader Reading + self.nFDTdataRecors = fheader.nFDTdataRecors + + self.readBlock() # Block reading else: print 'readFile FlagNoData becomes true' - self.flagNoMoreFiles=True + self.flagNoMoreFiles = True self.dataOut.flagNoData = True - return 0 - + return 0 + def getVelRange(self, extrapoints=0): - Lambda= SPEED_OF_LIGHT/50000000 - PRF = self.dataOut.PRF#1./(self.dataOut.ippSeconds * self.dataOut.nCohInt) - Vmax=-Lambda/(4.*(1./PRF)*self.dataOut.nCohInt*2.) - deltafreq = PRF / (self.nProfiles) - deltavel = (Vmax*2) / (self.nProfiles) - freqrange = deltafreq*(numpy.arange(self.nProfiles)-self.nProfiles/2.) - deltafreq/2 - velrange = deltavel*(numpy.arange(self.nProfiles)-self.nProfiles/2.) - return velrange - - def readBlock(self): + Lambda = SPEED_OF_LIGHT / 50000000 + # 1./(self.dataOut.ippSeconds * self.dataOut.nCohInt) + PRF = self.dataOut.PRF + Vmax = -Lambda / (4. * (1. / PRF) * self.dataOut.nCohInt * 2.) + deltafreq = PRF / (self.nProfiles) + deltavel = (Vmax * 2) / (self.nProfiles) + freqrange = deltafreq * \ + (numpy.arange(self.nProfiles) - self.nProfiles / 2.) - deltafreq / 2 + velrange = deltavel * \ + (numpy.arange(self.nProfiles) - self.nProfiles / 2.) + return velrange + + def readBlock(self): ''' It should be checked if the block has data, if it is not passed to the next file. - + Then the following is done: - + 1. Read the RecordHeader 2. Fill the buffer with the current block number. - + ''' - - if self.BlockCounter < self.nFDTdataRecors-2: + + if self.BlockCounter < self.nFDTdataRecors - 2: print self.nFDTdataRecors, 'CONDICION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' - if self.ReadMode==1: - rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter+1) - elif self.ReadMode==0: + if self.ReadMode == 1: + rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter + 1) + elif self.ReadMode == 0: rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter) - - rheader.RHread(self.fpFile) #Bltr FileHeader Reading - - self.OffsetStartHeader=rheader.OffsetStartHeader - self.RecCounter=rheader.RecCounter - self.Off2StartNxtRec=rheader.Off2StartNxtRec - self.Off2StartData=rheader.Off2StartData - self.nProfiles=rheader.nProfiles - self.nChannels=rheader.nChannels - self.nHeights=rheader.nHeights - self.frequency=rheader.TransmitFrec - self.DualModeIndex=rheader.DualModeIndex - - self.pairsList =[(0,1),(0,2),(1,2)] + + rheader.RHread(self.fpFile) # Bltr FileHeader Reading + + self.OffsetStartHeader = rheader.OffsetStartHeader + self.RecCounter = rheader.RecCounter + self.Off2StartNxtRec = rheader.Off2StartNxtRec + self.Off2StartData = rheader.Off2StartData + self.nProfiles = rheader.nProfiles + self.nChannels = rheader.nChannels + self.nHeights = rheader.nHeights + self.frequency = rheader.TransmitFrec + self.DualModeIndex = rheader.DualModeIndex + + self.pairsList = [(0, 1), (0, 2), (1, 2)] self.dataOut.pairsList = self.pairsList - - self.nRdPairs=len(self.dataOut.pairsList) + + self.nRdPairs = len(self.dataOut.pairsList) self.dataOut.nRdPairs = self.nRdPairs - - self.__firstHeigth=rheader.StartRangeSamp - self.__deltaHeigth=rheader.SampResolution - self.dataOut.heightList= self.__firstHeigth + numpy.array(range(self.nHeights))*self.__deltaHeigth + + self.__firstHeigth = rheader.StartRangeSamp + self.__deltaHeigth = rheader.SampResolution + self.dataOut.heightList = self.__firstHeigth + \ + numpy.array(range(self.nHeights)) * self.__deltaHeigth self.dataOut.channelList = range(self.nChannels) - self.dataOut.nProfiles=rheader.nProfiles - self.dataOut.nIncohInt=rheader.nIncohInt - self.dataOut.nCohInt=rheader.nCohInt - self.dataOut.ippSeconds= 1/float(rheader.PRFhz) - self.dataOut.PRF=rheader.PRFhz - self.dataOut.nFFTPoints=rheader.nProfiles - self.dataOut.utctime=rheader.nUtime - self.dataOut.timeZone=0 - self.dataOut.normFactor= self.dataOut.nProfiles*self.dataOut.nIncohInt*self.dataOut.nCohInt - self.dataOut.outputInterval= self.dataOut.ippSeconds * self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles - - self.data_output=numpy.ones([3,rheader.nHeights])*numpy.NaN + self.dataOut.nProfiles = rheader.nProfiles + self.dataOut.nIncohInt = rheader.nIncohInt + self.dataOut.nCohInt = rheader.nCohInt + self.dataOut.ippSeconds = 1 / float(rheader.PRFhz) + self.dataOut.PRF = rheader.PRFhz + self.dataOut.nFFTPoints = rheader.nProfiles + self.dataOut.utctime = rheader.nUtime + self.dataOut.timeZone = 0 + self.dataOut.normFactor = self.dataOut.nProfiles * \ + self.dataOut.nIncohInt * self.dataOut.nCohInt + self.dataOut.outputInterval = self.dataOut.ippSeconds * \ + self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles + + self.data_output = numpy.ones([3, rheader.nHeights]) * numpy.NaN print 'self.data_output', shape(self.data_output) - self.dataOut.velocityX=[] - self.dataOut.velocityY=[] - self.dataOut.velocityV=[] - + self.dataOut.velocityX = [] + self.dataOut.velocityY = [] + self.dataOut.velocityV = [] + '''Block Reading, the Block Data is received and Reshape is used to give it shape. - ''' - - #Procedure to take the pointer to where the date block starts - startDATA = open(self.fpFile,"rb") - OffDATA= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec+self.Off2StartData + ''' + + # Procedure to take the pointer to where the date block starts + startDATA = open(self.fpFile, "rb") + OffDATA = self.OffsetStartHeader + self.RecCounter * \ + self.Off2StartNxtRec + self.Off2StartData startDATA.seek(OffDATA, os.SEEK_SET) - + def moving_average(x, N=2): - return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):] - - def gaus(xSamples,a,x0,sigma): - return a*exp(-(xSamples-x0)**2/(2*sigma**2)) - - def Find(x,value): + return numpy.convolve(x, numpy.ones((N,)) / N)[(N - 1):] + + def gaus(xSamples, a, x0, sigma): + return a * exp(-(xSamples - x0)**2 / (2 * sigma**2)) + + def Find(x, value): for index in range(len(x)): - if x[index]==value: - return index - + if x[index] == value: + return index + def pol2cart(rho, phi): x = rho * numpy.cos(phi) y = rho * numpy.sin(phi) return(x, y) - - - - - if self.DualModeIndex==self.ReadMode: - - self.data_fft = numpy.fromfile( startDATA, [('complex',' 0.0001) : -# -# try: +# +# try: # popt,pcov = curve_fit(gaus,xSamples,yMean,p0=[1,meanGauss,sigma]) -# +# # if numpy.amax(popt)>numpy.amax(yMean)*0.3: # FitGauss=gaus(xSamples,*popt) -# -# else: +# +# else: # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean) # print 'Verificador: Dentro', Height # except RuntimeError: -# +# # try: # for j in range(len(ySamples[1])): # yMean2=numpy.append(yMean2,numpy.average([ySamples[1,j],ySamples[2,j]])) @@ -869,7 +904,7 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # FitGauss=gaus(xSamples,*popt) # print 'Verificador: Exepcion1', Height # except RuntimeError: -# +# # try: # popt,pcov = curve_fit(gaus,xSamples,ySamples[1],p0=[1,meanGauss,sigma]) # FitGauss=gaus(xSamples,*popt) @@ -880,12 +915,12 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # else: # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean) # #print 'Verificador: Fuera', Height -# -# -# +# +# +# # Maximun=numpy.amax(yMean) # eMinus1=Maximun*numpy.exp(-1) -# +# # HWpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1))) # HalfWidth= xFrec[HWpos] # GCpos=Find(FitGauss, numpy.amax(FitGauss)) @@ -894,39 +929,39 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # #Vpos=Find(FactNorm, min(FactNorm, key=lambda value:abs(value- numpy.mean(FactNorm) ))) # #print 'GCpos',GCpos, numpy.amax(FitGauss), 'HWpos',HWpos # '''****** Getting Fij ******''' -# +# # GaussCenter=xFrec[GCpos] # if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0): # Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001 # else: # Fij=abs(GaussCenter-HalfWidth)+0.0000001 -# +# # '''****** Getting Frecuency range of significant data ******''' -# +# # Rangpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10))) -# +# # if Rangpos5 and len(FrecRange) 0.: # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, Vzon) #Vmag # #print 'Vmag',Vmag # else: # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, NaN) -# +# # if abs(Vx)<100 and abs(Vx) > 0.: # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, Vmer) #Vang -# #print 'Vang',Vang +# #print 'Vang',Vang # else: # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, NaN) -# +# # if abs(GaussCenter)<2: # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, xFrec[Vpos]) -# +# # else: # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, NaN) -# -# +# +# # # print '********************************************' # # print 'HalfWidth ', HalfWidth # # print 'Maximun ', Maximun @@ -1033,25 +1068,25 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # print 'PhaseSlope ',PhaseSlope[2] # # print '********************************************' # #print 'data_output',shape(self.dataOut.velocityX), shape(self.dataOut.velocityY) -# +# # #print 'self.dataOut.velocityX', len(self.dataOut.velocityX) # #print 'self.dataOut.velocityY', len(self.dataOut.velocityY) # #print 'self.dataOut.velocityV', self.dataOut.velocityV -# +# # self.data_output[0]=numpy.array(self.dataOut.velocityX) # self.data_output[1]=numpy.array(self.dataOut.velocityY) # self.data_output[2]=numpy.array(self.dataOut.velocityV) -# +# # prin= self.data_output[0][~numpy.isnan(self.data_output[0])] # print ' ' -# print 'VmagAverage',numpy.mean(prin) +# print 'VmagAverage',numpy.mean(prin) # print ' ' # # plt.figure(5) # # plt.subplot(211) # # plt.plot(self.dataOut.velocityX,'yo:') # # plt.subplot(212) # # plt.plot(self.dataOut.velocityY,'yo:') -# +# # # plt.figure(1) # # # plt.subplot(121) # # # plt.plot(xFrec,ySamples[0],'k',label='Ch0') @@ -1060,7 +1095,7 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # # plt.plot(xFrec,FitGauss,'yo:',label='fit') # # # plt.legend() # # plt.title('DATOS A ALTURA DE 2850 METROS') -# # +# # # # plt.xlabel('Frecuencia (KHz)') # # plt.ylabel('Magnitud') # # # plt.subplot(122) @@ -1070,7 +1105,7 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # plt.plot(xFrec,FactNorm) # # plt.axis([-4, 4, 0, 0.15]) # # # plt.xlabel('SelfSpectra KHz') -# # +# # # # plt.figure(10) # # # plt.subplot(121) # # plt.plot(xFrec,ySamples[0],'b',label='Ch0') @@ -1079,7 +1114,7 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # # plt.plot(xFrec,FitGauss,'yo:',label='fit') # # plt.legend() # # plt.title('SELFSPECTRA EN CANALES') -# # +# # # # plt.xlabel('Frecuencia (KHz)') # # plt.ylabel('Magnitud') # # # plt.subplot(122) @@ -1089,19 +1124,19 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # # plt.plot(xFrec,FactNorm) # # # plt.axis([-4, 4, 0, 0.15]) # # # plt.xlabel('SelfSpectra KHz') -# # +# # # # plt.figure(9) -# # -# # +# # +# # # # plt.title('DATOS SUAVIZADOS') # # plt.xlabel('Frecuencia (KHz)') # # plt.ylabel('Magnitud') # # plt.plot(xFrec,SmoothSPC,'g') -# # +# # # # #plt.plot(xFrec,FactNorm) # # plt.axis([-4, 4, 0, 0.15]) # # # plt.xlabel('SelfSpectra KHz') -# # # +# # # # # plt.figure(2) # # # #plt.subplot(121) # # plt.plot(xFrec,yMean,'r',label='Mean SelfSpectra') @@ -1115,7 +1150,7 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # plt.xlabel('Frecuencia (KHz)') # # plt.ylabel('Magnitud') # # plt.legend() -# # # +# # # # # # plt.figure(3) # # # plt.subplot(311) # # # #plt.plot(xFrec,phase[0]) @@ -1125,30 +1160,23 @@ class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODa # # # plt.subplot(313) # # # plt.plot(xFrec,phase[2],'g') # # # #plt.plot(xFrec,phase[2]) -# # # +# # # # # # plt.figure(4) -# # # +# # # # # # plt.plot(xSamples,coherence[0],'b') # # # plt.plot(xSamples,coherence[1],'r') # # # plt.plot(xSamples,coherence[2],'g') # # plt.show() -# # # +# # # # # # plt.clf() # # # plt.cla() -# # # plt.close() -# -# print ' ' - - - - self.BlockCounter+=2 - +# # # plt.close() +# +# print ' ' + + self.BlockCounter += 2 + else: - self.fileSelector+=1 - self.BlockCounter=0 + self.fileSelector += 1 + self.BlockCounter = 0 print "Next File" - - - - - diff --git a/schainpy/model/io/jroIO_mira35c.py b/schainpy/model/io/jroIO_mira35c.py index 632bc93..684b106 100644 --- a/schainpy/model/io/jroIO_mira35c.py +++ b/schainpy/model/io/jroIO_mira35c.py @@ -1,4 +1,5 @@ -import os, sys +import os +import sys import glob import fnmatch import datetime @@ -6,11 +7,9 @@ import time import re import h5py import numpy -import matplotlib.pyplot as plt -import pylab as plb from scipy.optimize import curve_fit -from scipy import asarray as ar,exp +from scipy import asarray as ar, exp from scipy import stats from numpy.ma.core import getdata @@ -30,152 +29,168 @@ from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation from numpy import imag, shape, NaN, empty - class Header(object): - + def __init__(self): raise NotImplementedError - - + def read(self): - + raise NotImplementedError - + def write(self): - + raise NotImplementedError - + def printInfo(self): - - message = "#"*50 + "\n" + + message = "#" * 50 + "\n" message += self.__class__.__name__.upper() + "\n" - message += "#"*50 + "\n" - + message += "#" * 50 + "\n" + keyList = self.__dict__.keys() keyList.sort() - + for key in keyList: - message += "%s = %s" %(key, self.__dict__[key]) + "\n" - + message += "%s = %s" % (key, self.__dict__[key]) + "\n" + if "size" not in keyList: attr = getattr(self, "size") - + if attr: - message += "%s = %s" %("size", attr) + "\n" - - #print message - - -FILE_HEADER = numpy.dtype([ #HEADER 1024bytes - ('Hname','a32'), #Original file name - ('Htime',numpy.str_,32), #Date and time when the file was created - ('Hoper',numpy.str_,64), #Name of operator who created the file - ('Hplace',numpy.str_,128), #Place where the measurements was carried out - ('Hdescr',numpy.str_,256), #Description of measurements - ('Hdummy',numpy.str_,512), #Reserved space - #Main chunk 8bytes - ('Msign',numpy.str_,4), #Main chunk signature FZKF or NUIG - ('MsizeData','=5 - ('SPARrawGate2','=5 + ('SPARrawGate1', ' 1180: - self.fp.seek(self.PointerReader , os.SEEK_SET) + self.fp.seek(self.PointerReader, os.SEEK_SET) self.FirstPoint = self.PointerReader - - else : + + else: self.FirstPoint = 1180 - - - + self.srviHeader = SRVIHeader() - - self.srviHeader.SRVIread(self.fp) #Se obtiene la cabecera del SRVI - - self.blocksize = self.srviHeader.SizeOfDataBlock1 # Se obtiene el tamao del bloque - + + self.srviHeader.SRVIread(self.fp) # Se obtiene la cabecera del SRVI + + self.blocksize = self.srviHeader.SizeOfDataBlock1 # Se obtiene el tamao del bloque + if self.blocksize == 148: print 'blocksize == 148 bug' - jump = numpy.fromfile(self.fp,[('jump',numpy.str_,140)] ,1) - - self.srviHeader.SRVIread(self.fp) #Se obtiene la cabecera del SRVI - + jump = numpy.fromfile(self.fp, [('jump', numpy.str_, 140)], 1) + + # Se obtiene la cabecera del SRVI + self.srviHeader.SRVIread(self.fp) + if not self.srviHeader.SizeOfSRVI1: - self.fileSelector+=1 - self.nextfileflag==True + self.fileSelector += 1 + self.nextfileflag == True self.FileHeaderFlag == True - + self.recordheader = RecordHeader() self.recordheader.RHread(self.fp) self.RadarConst = self.recordheader.RadarConst dwell = self.recordheader.time_t npw1 = self.recordheader.npw1 npw2 = self.recordheader.npw2 - - + self.dataOut.channelList = range(1) - self.dataOut.nIncohInt = self.Num_inCoh + self.dataOut.nIncohInt = self.Num_inCoh self.dataOut.nProfiles = self.Num_Bins self.dataOut.nCohInt = 1 self.dataOut.windowOfFilter = 1 self.dataOut.utctime = dwell - self.dataOut.timeZone=0 - + self.dataOut.timeZone = 0 + self.dataOut.outputInterval = self.dataOut.getTimeInterval() - self.dataOut.heightList = self.SPARrawGate1*self.__deltaHeigth + numpy.array(range(self.Num_Hei))*self.__deltaHeigth - - - - self.HSDVsign = numpy.fromfile( self.fp, [('HSDV',numpy.str_,4)],1) - self.SizeHSDV = numpy.fromfile( self.fp, [('SizeHSDV',' 0. , self.data_spc, 0) - - self.dataOut.COFA = numpy.array([self.COFA_Co , self.COFA_Cx]) - + + self.data_spc = numpy.where(self.data_spc > 0., self.data_spc, 0) + + self.dataOut.COFA = numpy.array([self.COFA_Co, self.COFA_Cx]) + print ' ' - print 'SPC',numpy.shape(self.dataOut.data_spc) - #print 'SPC',self.dataOut.data_spc - + print 'SPC', numpy.shape(self.dataOut.data_spc) + # print 'SPC',self.dataOut.data_spc + noinor1 = 713031680 noinor2 = 30 - - npw1 = 1#0**(npw1/10) * noinor1 * noinor2 - npw2 = 1#0**(npw2/10) * noinor1 * noinor2 + + npw1 = 1 # 0**(npw1/10) * noinor1 * noinor2 + npw2 = 1 # 0**(npw2/10) * noinor1 * noinor2 self.dataOut.NPW = numpy.array([npw1, npw2]) - + print ' ' - - self.data_spc = numpy.transpose(self.data_spc, (2,1,0)) - self.data_spc = numpy.fft.fftshift(self.data_spc, axes = 1) - + + self.data_spc = numpy.transpose(self.data_spc, (2, 1, 0)) + self.data_spc = numpy.fft.fftshift(self.data_spc, axes=1) + self.data_spc = numpy.fliplr(self.data_spc) - - self.data_spc = numpy.where(self.data_spc > 0. , self.data_spc, 0) - self.dataOut_spc= numpy.ones([1, self.Num_Bins , self.Num_Hei]) - self.dataOut_spc[0,:,:] = self.data_spc[0,:,:] - #print 'SHAPE', self.dataOut_spc.shape - #For nyquist correction: - #fix = 20 # ~3m/s + + self.data_spc = numpy.where(self.data_spc > 0., self.data_spc, 0) + self.dataOut_spc = numpy.ones([1, self.Num_Bins, self.Num_Hei]) + self.dataOut_spc[0, :, :] = self.data_spc[0, :, :] + # print 'SHAPE', self.dataOut_spc.shape + # For nyquist correction: + # fix = 20 # ~3m/s #shift = self.Num_Bins/2 + fix #self.data_spc = numpy.array([ self.data_spc[: , self.Num_Bins-shift+1: , :] , self.data_spc[: , 0:self.Num_Bins-shift , :]]) - - - + '''Block Reading, the Block Data is received and Reshape is used to give it shape. ''' - + self.PointerReader = self.fp.tell() - - - - - - - \ No newline at end of file diff --git a/schainpy/model/proc/jroproc_parameters.py b/schainpy/model/proc/jroproc_parameters.py index 8fe1cf2..704bdcd 100644 --- a/schainpy/model/proc/jroproc_parameters.py +++ b/schainpy/model/proc/jroproc_parameters.py @@ -17,7 +17,6 @@ from functools import partial import time #from sklearn.cluster import KMeans -import matplotlib.pyplot as plt from scipy.optimize import fmin_l_bfgs_b #optimize with bounds on state papameters from jroproc_base import ProcessingUnit, Operation