#!/usr/bin/env python
# -*- coding: CP949 -*-
#
# generated by wxGlade 0.7.2 on Tue Apr 05 01:49:01 2016
#
# This is a converting program from thorlabs powermeter datafile taken from thorlab official data acquisition program to radius vs power after fiber taper process.
# IF you want to use this program for your own purpose except learning python and programming, you have to ask me licence policy and copyright.
# snowall@kaist.ac.kr / snowall@gmail.com / Keehwan Nam, Physics department, KAIST. 2016.
#
import wx

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade
import numpy as np
import os
import re
import sys

import struct

f= open('sample.png', 'rb')

w, h = struct.unpack('>LL', f.read()[16:24])
displaysizeX = int(w)
displaysizeY = int(h)
f.close()
reload(sys)
sys.setdefaultencoding('cp949')
#~ displaysizeX=1280
#~ displaysizeY=1024
x,y=0,0
a=0
b=0
c=0
sec=0
minute=0
hours=0
msec=0
class MyDialog(wx.Dialog):
    x00=0
    y00=0
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(displaysizeX,displaysizeY))
        self.canvas = wx.StaticBitmap(self, -1, wx.Bitmap("sample.png", wx.BITMAP_TYPE_ANY))
        self.canvas.Bind(wx.EVT_LEFT_DOWN, self.positionnow)
    
    def positionnow(self, event):
        self.x00=wx.MouseEvent.GetX(event)
        self.y00=wx.MouseEvent.GetY(event)
        print((wx.MouseEvent.GetX(event),wx.MouseEvent.GetY(event)))
        
    def drawTick(self, pl):
        self.mydc = wx.MemoryDC()
        self.canvas2 = wx.Bitmap("sample.png", wx.BITMAP_TYPE_ANY)
        self.mydc.SelectObject(self.canvas2)
        self.mypen=wx.Pen(wx.Colour(255,0,0),5)
        self.mypen.SetWidth(1)
        self.mydc.SetPen(self.mypen)
        self.mydc.DrawLines(pl)
        self.mydc.SelectObject(wx.NullBitmap)
        self.canvas.SetBitmap(self.canvas2)
        self.mydc = None
        self.canvas = wx.StaticBitmap(self, -1, self.canvas2)
        return
        
    def graphroutine(self, myf):
        self.convertedData=[]
        self.pointlist=[]
        src=open(myf, "r")
        src.readline()
        d=src.readline()
        a=d.split(" ")
        self.mini=np.inf
        self.maxi=0.
        while True:
            d=src.readline()
            if d=="":
                break
            a=d.split(" ")
            msec=float(((a[1]).split("."))[1])*0.001
            sec=float(a[2].split(":")[2].split("\t")[0])
            minute=float((a[2]).split(":")[1])*60.
            hours=float((a[2]).split(":")[0])*3600.
            self.time=msec+sec+minute+hours
            self.convertedData+=[[self.time,float(a[2].split(":")[2].split("\t")[1])]]
            if self.convertedData[-1][1]<self.mini:
                self.mini=self.convertedData[-1][1]
            if self.convertedData[-1][1]>self.maxi:
                self.maxi=self.convertedData[-1][1]
        self.time0=self.convertedData[0][0]
        self.time1=self.convertedData[-1][0]
        self.timescale=displaysizeX/(self.time1-self.time0)
        self.vertscale=displaysizeY/(self.maxi-self.mini)
        for cd in self.convertedData:
            self.pointlist+=[[np.ceil((cd[0]-self.time0)*self.timescale),displaysizeY-np.ceil((cd[1]-self.mini)*self.vertscale)]]
        self.drawTick(self.pointlist)
        src.flush()
        src.close()
        return
        
    def findV(self,no):
        logfile=open("log.csv","r")
        try:
            while True:
                d=logfile.readline()
                if d=="":
                    break
                a=d.split(",")
                if a[0]==str(no):
                    logfile.close()
                    return a
        except:
            logfile.close()
            return "0"

    def inverse_ratio(self,logdata):
        return np.sqrt(((2.*float(logdata[15]))-float(logdata[14]))/((2.*float(logdata[15]))+float(logdata[14])))
        
    def converting(self,f):
        self.time0=(self.x00/self.timescale)+self.time0
        tar=open(f[:-4]+"_processed.txt", "w")
        errorlist=open("errorlist.txt","a")
        plot=open(f[:-4]+".gpl", "w")
        shotnumber=re.search("\d+",f.split("\\")[-1]).group()
        expcondition=self.findV(shotnumber)
        shotlog=self.inverse_ratio(expcondition)
        try:
            for cd in self.convertedData:
                tar.writelines(str(cd[0])+"\t"+str(cd[1])+"\t"+str(float(expcondition[9])*np.power(shotlog,(cd[0]-self.time0)/(float(expcondition[11])/float(expcondition[15]))))+"\t"+str(cd[1])+"\n")
            plot.writelines("set logscale x\nplot \""+(f.split("\\")[-1])[:-4]+"_processed.txt\" using 3:4 w l ")
        except:
            errorlist.writelines(f+"\n")
            print "error"
        finally:
            tar.flush()
            tar.close()
            plot.flush()
            plot.close()
        
class MainPlotFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainPlotFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.dig = wx.FileDialog(None, message="Choose data files", style = wx.FD_MULTIPLE)
        self.dia = MyDialog(self, -1, 'Close after click!')
        self.__set_properties()
        self.__do_layout()
        if self.dig.ShowModal() == wx.ID_OK:
            for fn in self.dig.GetPaths():
                self.dia.graphroutine(fn)
                self.dia.ShowModal()
                self.dia.converting(fn)
        self.Close()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MainPlotFrame.__set_properties
        self.SetTitle(("frame_1"))
        self.SetSize((displaysizeX, displaysizeY))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MainPlotFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade
            
# end of class MainPlotFrame
if __name__ == "__main__":
    app = wx.App(0)
    #~ wx.InitAllImageHandlers()
    frame_main = MainPlotFrame(None, -1, "")
    frame_main.Show()
    app.MainLoop()


신고
by snowall 2016.04.06 17:54