예제 #1
0
 def FileOpen(self):
     file_types = ";;Mayavi (*.mvi);;xyz (*.xyz);;reg_grid (*.txt);;All (*.*)"
     #self.filedialog.selectFilter(self.curfilter)
     fnames, filters = self.filedialog.getOpenFileNamesAndFilter(self, 'Open file', '', file_types, initialFilter=self.curfilter)
     if (fnames==[]): 
         #for i in range(10):
         #    self.mayaviobj.visualization.scene.camera.azimuth(10)
         #    self.mayaviobj.visualization.scene.render()
         #    self.mayaviobj.visualization.scene.mlab.draw()
         #    sleep(0.5)
             
         # yield
          
         return
     self.filedialog.setDirectory(os.path.dirname(str(fnames[0])))
     self.curfilter = filters
     self.LoadMayavi()
     
     progressfiles = ProgressBar("Generating 3D Graphs from files", len(fnames))
     for fname in fnames:
         progressfiles.setinfo(fname)
         filetype = os.path.splitext(fname)[1][1:]
         if filetype == "mvi":
             self.Read_mvi(fname)
         elif filetype =="txt":
             self.Read_reggrid(fname)
             self.SetMVIObjName(fname)
         elif filetype =="xyz":
             
             self.Read_xyz(fname)
             self.SetMVIObjName(fname)
         if progressfiles.wasCanceled(): break
         progressfiles.step()
     True
예제 #2
0
 def LoadMayavi(self):
     if self.loaded == False:
         progress = ProgressBar("Loading Mayavi Library...", 3)
         
         import MayaviEmbed
         progress.step()
         self.ui.waterfall_layout.removeWidget(self.ui.widget)
         self.mayavicontainer = qt.QWidget()
         self.mayaviobj = MayaviEmbed.MayaviQWidget(self.mayavicontainer)
         progress.step()
         self.mayavicontainer.setLayout(self.mayaviobj.mylayout)
         self.ui.waterfall_layout.addWidget(self.mayavicontainer)
         self.mayavicontainer.show()
         progress.step()
         self.loaded =  True
예제 #3
0
    def ReadPSFile(self, fname):
        f = open(fname, 'r')
        lastdir = os.path.dirname(str(fname))

        filecontent = f.read()
        f.close()

        #Get a list of the directories
        dirStartIdx = filecontent.find("--dir=")
        #detfile = os.path.basename(str(self.ui.filename_edit.text()))

        dirEndIdx = filecontent.find('"', dirStartIdx + 7)
        simdir1 = filecontent[dirStartIdx + 7:dirEndIdx]
        if "/" in simdir1: sep = "/"
        elif "\\" in simdir1: sep = "\\"
        if os.path.exists(simdir1) == False:
            simdir1 = os.path.dirname(fname) + sep + simdir1.split(sep)[-1]

        self.filedialog.setDirectory(simdir1)

        file_types = "MCstas (*.sim *.dat);; All (*.*)"
        detfilelong = self.filedialog.getOpenFileNames(None,
                                                       'Select detector file',
                                                       '', file_types)
        self.filedialog.setDirectory(lastdir)
        if (detfilelong == []): return

        fnames = []
        detfile = os.path.basename(detfilelong[0])
        while dirStartIdx > -1:
            dirStartIdx = dirStartIdx + 7
            dirEndIdx = filecontent.find('"', dirStartIdx)
            detpath = filecontent[dirStartIdx:dirEndIdx] + sep + detfile
            if os.path.exists(detpath) == False:
                detpath = os.path.dirname(fname) + sep + detpath.split(
                    sep)[-2] + sep + detfile
            fnames.append(detpath)
            dirStartIdx = filecontent.find("--dir=", dirEndIdx)

        numfiles = len(fnames)
        progress = ProgressBar("Opening McStas PS files...", numfiles)
        for afile in fnames:
            progress.setinfo(afile)
            if (os.path.exists(afile)):
                self.ReadFile(afile)
            if progress.wasCanceled(): break
            progress.step()
예제 #4
0
 def GetParamValues(self, prm1, prm2):
     try:
         if self.scanman.ui.outputGroupBox.ui.allfiles_check.isChecked():
             flistidx = range(len(self.scanman.ui.sourceGroupBox.filelist))
         else:
             flistidx = [self.scanman.ui.sourceGroupBox.ui.file_tbl.currentRow()]
     except:             #This source is probably not a filesource and therefore only one dataset is present
         flistidx = [1]          
     
     x=np.array([])
     y=np.array([])
     run_number = float(1)
     progressfiles = ProgressBar("Retrieving parameters [%s] and [%s] from files..." %(prm1,prm2), len(flistidx))
     for filei in flistidx:
         progressfiles.setinfo(self.scanman.ui.sourceGroupBox.filelist[filei].location)
         try:
             self.scanman.ui.sourceGroupBox.SelectDataFile(filei)
         except:
             True
         
         True
         src = self.scanman.datasrc
         self.setstart = 1
         self.setend = len(src.dataset)
         try:    #Only works with numbers but the parameter value can also be text in which case this will fail
             for setnr in range(self.setstart,self.setend):
                 self.scanman.SelectData(setnr,display=False)
                 src = self.scanman.datasrc
                 x=np.append(x,[float(src.prm[prm1])])
                 y_new = run_number if prm2=="" else float(src.prm[prm2])
                 y=np.append(y,y_new)
                 run_number = run_number + 1
                 True
         except:
             x=[0,0]
             y=[0,0]
         
         if progressfiles.wasCanceled(): break
         progressfiles.step()
     return x,y
예제 #5
0
def NeXus_hdf_read(fname, config):
    src = Srcpar.Srcpar(config)
    #src.dataset.pop()       #Remove the {0,0} dataset
    paramdict = {}
    params = {}
    detdict = {}
    detdictcmn = {}
    twodim = False

    f = h5py.File(fname, "r")
    dset = f.get("/")
    #y = dset["/entry1/instrument/detector/hmm_x_corrected"].value.astype('float')
    if "/entry1/instrument/detector/hmm" in dset:
        try:
            #n = dset["/entry1/instrument/detector/hmm"].value.astype('float')
            #CDM .value is depricated: n = dset["/entry1/instrument/detector/hmm"].value
            n = dset["/entry1/instrument/detector/hmm"][()]
            twodim = True
        except ValueError:  #array is too big, try to get the 1D dataset
            #n = dset["/entry1/instrument/detector/hmm_x_corrected"].value.astype('float')
            n = np.array(dset["/entry1/instrument/detector/hmm_x_corrected"])
            twodim = False
    elif "/entry1/instrument/detector/hmm_xy" in dset:
        #n = dset["/entry1/instrument/detector/hmm_xy"].value.astype('float')
        n = dset["/entry1/instrument/detector/hmm_xy"][()].astype('float')
        twodim = True
    elif "/entry1/instrument/detector/hmm_total_xy" in dset:
        #n = dset["/entry1/instrument/detector/hmm_total_xy"].value
        n = dset["/entry1/instrument/detector/hmm_total_xy"][()]
        twodim = True

    numruns = len(n)
    progress = ProgressBar("Loading NeXus hdf datasets...", numruns)

    datakeys = dset["/entry1/data"].keys()
    possiblescanparams = [x for x in datakeys]
    if "run_number" in datakeys:  #Scan was done with Gumtree, therefore use the title to determine the scan variables
        #title = dset["/entry1/experiment/title"].value[0]
        title = str(dset["/entry1/experiment/title"][()])
        possiblescanparams = re.findall(r"[\w']+", title)
    #else:
    #    possiblescanparams = datakeys
    if "time" in possiblescanparams: possiblescanparams.remove("time")

    True
    #x_stth = dset["/entry1/instrument/detector/x_stth"]
    try:
        #stth = dset["/entry1/sample/stth"].value
        stth = dset["/entry1/sample/stth"][()]
    except:
        stth = [90.0] * len(n)
    if "/entry1/instrument/detector/sample_to_detector_distance" in dset:
        #sampletodetector = np.float32(dset["/entry1/instrument/detector/sample_to_detector_distance"].value[0])
        sampletodetector = np.float32(
            dset["/entry1/instrument/detector/sample_to_detector_distance"][0])
    else:
        sampletodetector = np.float32(1000)
    #detwidth = np.float32(dset["/entry1/instrument/detector/active_width"].value[0])
    #detheight = np.float32(dset["/entry1/instrument/detector/active_height"].value[0])
    detwidth = np.float32(dset["/entry1/instrument/detector/active_width"][0])
    detheight = np.float32(
        dset["/entry1/instrument/detector/active_height"][0])
    if "/entry1/instrument/crystal" in dset:
        crystal = dset["/entry1/instrument/crystal"]
    else:
        crystal = dict([])
    if "/entry1/instrument/monochromator" in dset:
        monochromator = dset["/entry1/instrument/monochromator"]
        if "focus" in monochromator:
            monochromator = dset["/entry1/instrument/monochromator/focus"]
    else:
        monochromator = dict([])
    if "/entry1/instrument/slits" in dset:
        slits = dset["/entry1/instrument/slits"]
    else:
        slits = dict([])
    sample = dset["/entry1/sample"]
    #moncounts = [[s,dset["/entry1/monitor/"+s].value] for s in dset["entry1/monitor"] if "_counts" in s]
    #monrates = [[s,dset["/entry1/monitor/"+s].value] for s in dset["entry1/monitor"] if "_event_rate" in s]
    #montime = [[s,dset["/entry1/monitor/"+s].value] for s in dset["entry1/monitor"] if "_time" in s]
    moncounts = [[s, dset["/entry1/monitor/" + s][()]]
                 for s in dset["entry1/monitor"] if "_counts" in s]
    monrates = [[s, dset["/entry1/monitor/" + s][()]]
                for s in dset["entry1/monitor"] if "_event_rate" in s]
    montime = [[s, dset["/entry1/monitor/" + s][()]]
               for s in dset["entry1/monitor"] if "_time" in s]

    detdictcmn["sam_to_det"] = sampletodetector
    detdictcmn["det_xmin"] = -detwidth / 2.0
    detdictcmn["det_xmax"] = detwidth / 2.0
    detdictcmn["det_ymin"] = -detheight / 2.0
    detdictcmn["det_ymax"] = detheight / 2.0
    detdictcmn["lambda"] = config["source"]["detector"]["lambda"]

    flipit = config["source"]["hdf"]["flipxy"]

    #tic=time.time()
    for run in range(numruns):
        progress.setinfo(fname)

        params["RunNr"] = str(run + 1)

        detdict = detdictcmn.copy()
        detdict["stth"] = stth[run]
        #for itemkey in crystal.iterkeys(): DM iterkeys() depricated
        for itemkey in crystal.keys():
            params[itemkey] = str(dset["/entry1/instrument/crystal/%s" %
                                       (itemkey)][run])
        #for itemkey in monochromator.iterkeys():  DM iterkeys() depricated
        for itemkey in monochromator.keys():
            try:
                #params[itemkey] = str(dset["/entry1/instrument/monochromator/%s" %(itemkey)][run])
                params[itemkey] = str(dset[monochromator.name + "/" +
                                           itemkey][run])
            except:
                True
        #for itemkey in slits.iterkeys():  DM iterkeys() depricated
        for itemkey in slits.keys():
            try:
                params[itemkey] = str(dset["/entry1/instrument/slits/%s" %
                                           (itemkey)][run])
            except:
                True
        #for itemkey in sample.iterkeys(): DM iterkeys() depricated
        for itemkey in sample.keys():
            try:  #if type(sample[itemkey]) == h5py._hl.dataset.Dataset:       #Cannot handle groups at the moment, only Datasets
                #theval = sample[itemkey].value
                theval = sample[itemkey][()]
                ivalnr = run
                if theval.size == 1: ivalnr = 0
                params[itemkey] = str(theval[ivalnr])
                #params[itemkey]=theval[ivalnr].astype('str')
                #if sample[itemkey].value.size==1:
                #    params[itemkey]=sample[itemkey].value[0].astype('str')
                #else:
                #    params[itemkey]=sample[itemkey].value[run].astype('str')
            except:
                True
        try:
            #params["time"] = str(dset["/entry1/data/time"].value[run])
            params["time"] = str(dset["/entry1/data/time"][run])
        except:
            True
        for item, value in moncounts:
            params[str(item)] = str(value[run])
        for item, value in monrates:
            params[str(item)] = str(value[run])
        for item, value in montime:
            params[str(item)] = str(value[run])

        paramdict = params.copy()
        #frame = n[run][0]
        #for i in range(len(frame)):
        #    if i != 7:
        #        frame[i]=np.zeros(np.shape(frame[i]))

        #src.AddData(frame, paramdict, detdict, "NeXus_hdf", fname, twod=True)     #the [0] is the time channel

        if len(n.shape) == 4:  # run, time, x, y
            if flipit:
                src.AddData(np.transpose(n[run][0]),
                            paramdict,
                            detdict,
                            "NeXus_hdf",
                            fname,
                            twod=twodim)  #the [0] is the time channel
            else:
                src.AddData(n[run][0],
                            paramdict,
                            detdict,
                            "NeXus_hdf",
                            fname,
                            twod=twodim)  #the [0] is the time channel
            #src.AddData(np.transpose(n[run][0]), paramdict, detdict, "NeXus_hdf", fname, twod=twodim)     #the [0] is the time channel
        else:  # run, x, y
            src.AddData(np.asfarray(n[run]),
                        paramdict,
                        detdict,
                        "NeXus_hdf",
                        fname,
                        twod=twodim)
        True
        if progress.wasCanceled(): break
        progress.step()
    f.close()
    #toc=time.time()
    #print "Time is %f" % (toc-tic)
    src.filename = fname
    paramkeys = [x for x in params.keys()]
    commonparams = list(
        set(paramkeys).intersection(possiblescanparams)
    )  #Determine the scan variables from the title and the actual instrument parameters
    for item in commonparams:
        src.precparams[item] = 0.01
        True
    return src
def FullprofCompat_xy_read(fname,config):
    src = Srcpar.Srcpar(config)
    #src.dataset.pop()       #Remove the {0,0} dataset
    paramdictcmn = {}
    paramdict = {}
    detdict = {}
    
    f = open(fname, 'r')
    filecontent = f.read()
    f.close()
    scanStartIdx = 0
    datasections = filecontent[scanStartIdx:-1].split("\n\n")
    numruns = len(datasections)    
    progress = ProgressBar("Loading Fullprof compatible xy datasets...", numruns)
    #progress.setinfo(fname)
    for section in datasections:
        progress.setinfo(fname)
        #paramdict = paramdictcmn.copy()
        
        sectionsplitlines = section.split("\n")
        
        params = sectionsplitlines[3].split("| ")
        params.pop(0)
        detdict = {params[s].split(":")[0]:float(params[s].split(":")[1]) for s in range(len(params))}
        
        params = sectionsplitlines[4].split("| ")
        params.pop(0)   #This is the empty one, should improve the exporting so that this isnt needed
        paramdict = {params[s].split(":")[0]:params[s].split(":")[1] for s in range(len(params))}
        if "stth" not in paramdict:
            paramdict["stth"]=str(detdict["stth"])
        True
            
        #Get the intensity data
        #y = np.array([])
        #tth = np.array([])
        axisheader = sectionsplitlines[5].split(" ")
        if axisheader[0] == "Channel" : axistype = "ch"
        elif axisheader[0] == "Position" : axistype = "mm"
        elif axisheader[0] == "Angle" : axistype = "2th"
        elif axisheader[0] == "d-spacing" : axistype = "d"
        
        datalines = sectionsplitlines[6:]
        nrpoints = len(datalines)
        y = np.array([0.0]*nrpoints)
        x = np.array([0.0]*nrpoints)
        
        #for aline in sectionsplitlines[6:]:
        
        for i in range(len(datalines)):
            #x, intensity = aline.split()
            #y = np.append(y, float(intensity))
            #mm = np.append(tth, float(x))
            xstr, intensity = datalines[i].split()
            y[i] = float(intensity)
            x[i] = float(xstr)
        
        src.AddData(y,paramdict, detdict, "FullprofCompat_xy", fname, {axistype:x})
        if progress.wasCanceled(): break
        progress.step()
    
    return src
    
        
예제 #7
0
    def ExportPeak(self):
        self.Getexportsettings()
        try:
            self.sh = self.wb.Worksheets("Peakfit")
            self.sh.Activate()
        except:
            self.excel = Dispatch("Excel.Application")
            self.excel.Visible = 1
            #excel.ScreenUpdating = False
            self.wb = self.excel.Workbooks.Add()
            self.sh = self.excel.ActiveSheet
            self.sh.Name = "Peakfit"
        rngarray = [[]]
        if self.ui.useRangeCheckBox.isChecked():
            text_file = open(self.ui.rangeFileLineEdit.text(), "r")
            txtrange = text_file.read()
            text_file.close()
            rngarray = np.array([
                rng.split() for rng in txtrange[:-1].split('\n')
            ]).astype('int')
            True

        self.includeEmptyRow = 1
        thicklines = []
        self.colnr = {'File name': 1, 'Mods': 2, 'Records': 3}
        thicklines.append(len(self.colnr))

        #src = self.scanman.datasrc
        rangelist = self.scanman.ui.fitGroupBox.rangeList

        #for i in range(len(self.scanman.datasrc.exportparams)):  self.colnr[self.scanman.datasrc.exportparams[i]] = len(self.colnr) + 1
        for i in range(len(self.scanman.exportparams)):
            self.colnr[self.scanman.exportparams[i]] = len(self.colnr) + 1
        thicklines.append(len(self.colnr))

        if "allparams" in rangelist[-1].__dict__:  #Generic (new) way
            for i in range(len(rangelist)):
                rid = "_" + str(i)
                self.colnr["Channel_range" + rid] = len(self.colnr) + 1
                for iterprm in rangelist[i].iterparams:
                    self.colnr[iterprm + rid] = len(self.colnr) + 1
                    self.colnr["StDev_" + iterprm + rid] = len(self.colnr) + 1
                for miscprm in rangelist[i].miscparams:
                    self.colnr[miscprm + rid] = len(self.colnr) + 1
                for fitprm in rangelist[i].fitparams:
                    self.colnr[fitprm + rid] = len(self.colnr) + 1
                thicklines.append(len(self.colnr))
                True
        else:  #old way
            for i in range(len(rangelist)):
                rid = "_" + str(i)
                self.colnr["Channel_range" + rid] = len(self.colnr) + 1
                self.colnr[self.scanman.axistype + rid] = len(self.colnr) + 1
                self.colnr["StDev_" + self.scanman.axistype +
                           rid] = len(self.colnr) + 1
                self.colnr["FWHM" + rid] = len(self.colnr) + 1
                self.colnr["StDev_FWHM" + rid] = len(self.colnr) + 1
                self.colnr["Intensity" + rid] = len(self.colnr) + 1
                self.colnr["StDev_Intensity" + rid] = len(self.colnr) + 1
                self.colnr["Background" + rid] = len(self.colnr) + 1
                self.colnr["StDev_Background" + rid] = len(self.colnr) + 1
                self.colnr["Chi^2" + rid] = len(self.colnr) + 1
                self.colnr["Intensity_sum" + rid] = len(self.colnr) + 1
                self.colnr["Counts" + rid] = len(self.colnr) + 1
                self.colnr["Err_u_strain" + rid] = len(self.colnr) + 1
                thicklines.append(len(self.colnr))
            True

        if self.scanman.ui.outputGroupBox.ui.allfiles_check.isChecked():
            flistidx = range(len(self.scanman.ui.sourceGroupBox.filelist))
        else:
            flistidx = [
                self.scanman.ui.sourceGroupBox.ui.file_tbl.currentRow()
            ]

        numfiles = len(flistidx)
        progressfiles = ProgressBar("Fitting files...", numfiles)

        lastrow = self.sh.UsedRange.Rows.Count
        for filei in flistidx:
            if self.includeHeader == True:
                if lastrow == 1 and self.sh.Cells(1, 1).Value == None:
                    headerrow = 1
                else:
                    headerrow = lastrow + 1

                #for item in self.colnr.iterkeys():
                for item in self.colnr.keys():
                    self.sh.Cells(headerrow, self.colnr[item]).Value = item

                lastcol = self.sh.UsedRange.Columns.Count
                range_header = self.sh.Range(self.sh.Cells(headerrow, 1),
                                             self.sh.Cells(headerrow, lastcol))
                range_header.Font.FontStyle = "Bold"
                #for col in thicklines:
                #range_header.Cells(1,col).Borders(c.xlEdgeRight).LineStyle = c.xlContinuous

            rowdatabegin = self.sh.UsedRange.Rows.Count + self.includeEmptyRow
            row = rowdatabegin

            self.scanman.ui.sourceGroupBox.SelectDataFile(filei)
            if self.ui.all_radio.isChecked():
                self.setend = len(self.scanman.datasrc.dataset)
            progressfiles.setinfo(self.scanman.datasrc.filename)

            numsets = self.setend - self.setstart
            progresdataset = ProgressBar("Fitting datasets...", numsets)

            #rangelist[0].start = 529
            #rangelist[0].stop = 621

            if len(rngarray[0]) == 0:
                if "allparams" in rangelist[0].__dict__:
                    rngarray = [[
                        rangelist[0].rangeparams["Range_start"].value,
                        rangelist[0].rangeparams["Range_end"].value
                    ]]
                else:  #old way
                    rngarray = [[rangelist[0].start, rangelist[0].stop]]
            if len(rngarray) != 0:
                True

            for setnr in range(self.setstart, self.setend):
                progresdataset.setinfo(self.scanman.datasrc.filename)
                for nrng in rngarray:
                    if "allparams" in rangelist[0].__dict__:
                        rangelist[0].rangeparams["Range_start"].value = nrng[0]
                        rangelist[0].rangeparams["Range_end"].value = nrng[1]
                    else:  #old way
                        rangelist[0].start = nrng[0]
                        rangelist[0].stop = nrng[1]
                #self.scanman.ui.dataset_spin.setValue(setnr)        #Selects the next dataset - Hopefully the callback will be completed before the next statements are executed
                    self.scanman.SelectData(setnr, display=False)
                    src = self.scanman.datasrc
                    self.sh.Cells(row, self.colnr["File name"]
                                  ).Value = src.dataset[setnr].filename
                    self.sh.Cells(
                        row, self.colnr["Mods"]).Value = self.scanman.modext
                    self.sh.Cells(row, self.colnr["Records"]).Value = setnr

                    #for exportprm in src.exportparams:
                    for exportprm in self.scanman.exportparams:
                        try:
                            self.sh.Cells(
                                row, self.colnr[exportprm]
                            ).Value = src.dataset[setnr].prm[exportprm]
                        except:
                            self.sh.Cells(row,
                                          self.colnr[exportprm]).Value = ""

                    for irng in range(len(rangelist)):
                        rid = "_" + str(irng)
                        rng = rangelist[irng]
                        if "allparams" in rangelist[0].__dict__:
                            self.sh.Cells(
                                row,
                                self.colnr["Channel_range" + rid]).Value = str(
                                    rng.rangeparams["Range_start"].value
                                ) + " - " + str(
                                    rng.rangeparams["Range_end"].value)
                            for iterprm in rng.iterparams:
                                self.sh.Cells(
                                    row,
                                    self.colnr[iterprm + rid]).Value = str(
                                        rng.iterparams[iterprm].value)
                                self.sh.Cells(
                                    row, self.colnr[
                                        "StDev_" + iterprm + rid]).Value = str(
                                            rng.iterparams[iterprm].stdev)
                                if rng.iterparams[iterprm].valid == False:
                                    self.sh.Cells(row, self.colnr[
                                        iterprm +
                                        rid]).Font.ColorIndex = 3  #3 is red
                            for miscprm in rangelist[i].miscparams:
                                self.sh.Cells(
                                    row,
                                    self.colnr[miscprm + rid]).Value = str(
                                        rng.miscparams[miscprm].value)
                            for fitprm in rangelist[i].fitparams:
                                self.sh.Cells(
                                    row, self.colnr[fitprm + rid]).Value = str(
                                        rng.fitparams[fitprm].value)
                            True
                        else:  #old way
                            self.sh.Cells(
                                row,
                                self.colnr["Channel_range" + rid]).Value = str(
                                    rng.start) + " - " + str(rng.stop)
                            self.sh.Cells(
                                row, self.colnr[self.scanman.axistype +
                                                rid]).Value = str(rng.position)
                            self.sh.Cells(
                                row,
                                self.colnr["StDev_" + self.scanman.axistype +
                                           rid]).Value = str(
                                               rng.position_stdev)
                            if rng.position_valid == False:
                                self.sh.Cells(
                                    row, self.colnr[
                                        self.scanman.axistype +
                                        rid]).Font.ColorIndex = 3  #3 is red

                            self.sh.Cells(
                                row,
                                self.colnr["FWHM" + rid]).Value = str(rng.fwhm)
                            self.sh.Cells(row, self.colnr["StDev_FWHM" +
                                                          rid]).Value = str(
                                                              rng.fwhm_stdev)
                            if rng.fwhm_valid == False:
                                self.sh.Cells(row, self.colnr[
                                    "FWHM" +
                                    rid]).Font.ColorIndex = 3  #3 is red

                            self.sh.Cells(row, self.colnr["Intensity" +
                                                          rid]).Value = str(
                                                              rng.intensity)
                            self.sh.Cells(row,
                                          self.colnr["StDev_Intensity" +
                                                     rid]).Value = str(
                                                         rng.intensity_stdev)
                            if rng.intensity_valid == False:
                                self.sh.Cells(row, self.colnr[
                                    "Intensity" +
                                    rid]).Font.ColorIndex = 3  #3 is red

                            self.sh.Cells(row, self.colnr["Background" +
                                                          rid]).Value = str(
                                                              rng.background)
                            self.sh.Cells(row,
                                          self.colnr["StDev_Background" +
                                                     rid]).Value = str(
                                                         rng.background_stdev)
                            self.sh.Cells(row, self.colnr["Chi^2" +
                                                          rid]).Value = str(
                                                              rng.chi2)
                            self.sh.Cells(row,
                                          self.colnr["Intensity_sum" +
                                                     rid]).Value = str(
                                                         rng.intensity_sum)
                            self.sh.Cells(row, self.colnr["Counts" +
                                                          rid]).Value = str(
                                                              rng.counts)
                            self.sh.Cells(row, self.colnr["Err_u_strain" +
                                                          rid]).Value = str(
                                                              rng.errustrain)

                            True
                    row = row + 1
                    if progresdataset.wasCanceled(): break
                    progresdataset.step()
                    True
            lastrow = self.sh.UsedRange.Rows.Count
            #for col in thicklines:
            #    range_col = self.sh.Range(self.sh.Cells(rowdatabegin,col),self.sh.Cells(lastrow, col))
            #    range_col.Borders(c.xlEdgeRight).LineStyle = c.xlContinuous
            lastcol = self.sh.UsedRange.Columns.Count
            range_data = self.sh.Range(self.sh.Cells(rowdatabegin, 1),
                                       self.sh.Cells(lastrow, lastcol))
            #range_data.HorizontalAlignment = c.xlHAlignRight

            if self.savetext == True:
                from mylib import get_clipboard_text
                self.sh.Range(self.sh.Cells(headerrow, 1),
                              self.sh.Cells(lastrow, lastcol)).Copy()
                data = get_clipboard_text()
                data = data.replace("\r\n", "\n")
                guessfname = path.basename(
                    src.filename).split(".")[0] + self.scanman.modext
                if self.lastdir == "":
                    self.lastdir = self.lastdir = path.dirname(
                        self.scanman.ui.sourceGroupBox.filelist[
                            flistidx[0]].location)
                fname = self.filedialog.getSaveFileName(
                    self, "Save as *.pkf", path.join(self.lastdir, guessfname),
                    "Peakfit (*.pkf)")
                if fname == "": return
                self.lastdir = path.dirname(str(fname))
                text_file = open(fname, "w")
                text_file.write(data)
                text_file.close()

                if self.reopen == True:
                    self.scanman.ui.sourceGroupBox.OpenFile([fname])

            if progressfiles.wasCanceled(): break
            progressfiles.step()
            True
예제 #8
0
    def PostProcess(self):
        nums = 8    #Number of different post process events

        if self.ui.sourceGroupBox.src.data2D == False:
            srcp = self.ui.sourceGroupBox.src.PreparePost()
        else:
            srcp = self.ui.sourceGroupBox.srcsplit.PreparePost()
        currset = srcp.currset
        if len(srcp.dataset[currset].y) ==1: return

        progress = ProgressBar("Post Processing...", nums)
        progress.setinfo("Prepare post processing datastructures")        
        progress.step()

        progress.setinfo("Ofsetting stth")
        srcp = srcp.StthOffset(self.ui.stthoffset_edit.text())
        #srcp.CalcSumSet(["raw"])
        #srcp.SelectFrame("raw")
        progress.step()
        
        progress.setinfo("Performing crop")
        srcp.SelectFrame("raw")
        srcp = srcp.Crop()
        srcp.CalcSumSet(["raw"])
        srcp.SelectFrame("raw")
        progress.step()
        

        
        progress.setinfo("Normalising data")    
        srcp.Normalise(self.ui.normalise_combo.currentText())
        progress.step()
        
        progress.setinfo("Summing by parameter")
        srcp = srcp.SumWith(self.ui.sum_combo.currentText())
        progress.step()
        
        progress.setinfo("Combining two theta sets")
        srcp.preps["just_sum"] = True if self.ui.justsum_check.isChecked() else False 
        srcp = srcp.Combine2thSets()
        srcp.CalcSumSetCommon()
        srcp.CalcSumSet()
        srcp.SelectFrame("raw")
        progress.step()
        
        progress.setinfo("Scaling data")
        srcp.Scale(float(self.ui.scale_edit.text()))
        progress.step()
        
        
        
        progress.setinfo("Wrapping up")
        srcp.preps['postdirty'] = False
        self.ui.sourceGroupBox.srcp = srcp
        self.datasrc = srcp
        if currset > len(self.datasrc.dataset): currset = -1
        for findex in range(len(self.ui.sourceGroupBox.filelist)):
            if self.ui.sourceGroupBox.filelist[findex].location == self.datasrc.dataset[-1].filename:
                fileinfo = self.ui.sourceGroupBox.filelist[findex]
                fileinfo.srcp = srcp
                self.ui.sourceGroupBox.filelist[findex] = fileinfo
                break
        self.ApplyAdditionalParams()    
        progress.step()
        
        True   
예제 #9
0
    def CalcAbs(self):
        axis = self.ui.exp_graphic.figure.axes[0]

        ssw = self.ssw.GetVal()
        psw = self.psw.GetVal()
        sstth = self.sstth.GetVal()
        psp = self.psp.GetVal()
        ssp = self.ssp.GetVal()
        cor_x = self.cor_x.GetVal()
        cor_y = self.cor_y.GetVal()
        sdd = self.sdd.GetVal()
        s_len_x = self.s_len_x.GetVal()
        s_len_y = self.s_len_y.GetVal()
        omega = self.omega.GetVal()
        psw_sec = self.psw_sec.GetVal()
        ssw_sec = self.ssw_sec.GetVal()
        mu = self.att_coef.GetVal() / 10.0  #now in mm-1

        try:
            stth = self.scanman.datasrc.dataset[
                self.scanman.datasrc.currset].currframe.x_2th
            #stth = np.array([90.0,110.0,120.0])
        except:
            stth = np.linspace(0, 180, 50)

        try:
            sec_beam_x_proj = ssw / np.cos(np.deg2rad(90.0 - sstth))
        except:
            sec_beam_x_proj = 0.0

        randomgrid = 0

        #Sample
        if self.ui.rectRadioButton.isChecked() == True:
            s1 = [cor_x, -cor_y]
            s2 = [cor_x, s_len_y - cor_y]
            s3 = [-(s_len_x - cor_x), s_len_y - cor_y]
            s4 = [-(s_len_x - cor_x), -cor_y]
            sample_points = np.array([s1, s2, s3, s4])
        elif self.ui.cylRadioButton.isChecked() == True:
            s1 = [cor_x, -cor_y]
            cylsample = shgeom.Point(s1).buffer(s_len_x / 2.0)
            cylextpts = cylsample.exterior.xy
            sample_points = np.array(cylextpts).transpose()
            randomgrid = 1
        elif self.ui.polyRadioButton.isChecked() == True:
            sample_points = np.loadtxt(self.ui.polyPointFileEdit.text())
            True
        sample_trans = mpl.transforms.Affine2D().rotate_deg_around(
            0.0, 0.0, omega)
        self.sample = plt.Polygon(sample_trans.transform(sample_points),
                                  alpha=0.3,
                                  color="black")
        sh_sample = shgeom.Polygon(self.sample.get_xy())

        #Gauge volume
        xmin, xmax, ymin, ymax = [
            -0.5 * sec_beam_x_proj, 0.5 * sec_beam_x_proj, -0.5 * psw,
            0.5 * psw
        ]
        sh_gridbound = shgeom.Polygon([[xmin, ymin], [xmax,
                                                      ymin], [xmax, ymax],
                                       [xmin, ymax], [xmin, ymin]])
        sh_gridbound_skew = shapely.affinity.skew(sh_gridbound,
                                                  xs=90 - sstth,
                                                  ys=0,
                                                  origin='center')
        if sh_sample.contains(sh_gridbound_skew) != True: randomgrid = 1
        sh_sgvol = sh_sample.intersection(sh_gridbound_skew)
        gridbound_points = np.array(sh_sgvol.exterior.xy).transpose()
        self.gvol = plt.Polygon(gridbound_points, color="red", linewidth=0.0)

        #Grid points
        pointspatches = []
        r = np.min([psw, sec_beam_x_proj]) / np.max(
            [psw_sec, ssw_sec]) / 3.0  #radius of displayed point
        if False:
            #if randomgrid == True:
            nrgridpoints = int(ssw_sec * psw_sec)
            N = 10 * nrgridpoints
            xmin, ymin = gridbound_points.min(0)
            xmax, ymax = gridbound_points.max(0)
            randomPoints = []
            i = 0
            while i < N:
                newxy = [
                    random.uniform(xmin, xmax),
                    random.uniform(ymin, ymax)
                ]
                if sh_sgvol.contains(shgeom.Point(newxy)):
                    randomPoints.append(newxy)
                    i = i + 1
            X = np.array(randomPoints)
            gridpts = mylib.find_centers(X, nrgridpoints)[0]
            self.sh_gridpoints = shgeom.MultiPoint(gridpts)
            for pt in gridpts:
                pointspatches.append(plt.Circle(np.array(pt), r))
        else:
            xdelta = (sec_beam_x_proj) / (ssw_sec)
            ydelta = (psw) / (psw_sec)
            gridpoints = []
            for xpt in frange(xmin + 0.5 * xdelta, xmax, xdelta):
                for ypt in frange(ymin + 0.5 * ydelta, ymax, ydelta):
                    gridpoints.append([xpt, ypt])
            sh_allgridpoints = shgeom.MultiPoint(gridpoints)
            sh_allgridpoints_skew = shapely.affinity.skew(sh_allgridpoints,
                                                          xs=90 - sstth,
                                                          ys=0,
                                                          origin=[0, 0])
            self.sh_gridpoints = []
            for pt in sh_allgridpoints_skew:
                if sh_sgvol.contains(pt):
                    self.sh_gridpoints.append(pt)
                    xypt = plt.Circle(np.array(pt), r)
                    pointspatches.append(xypt)
        self.gridcollection = mpl.collections.PatchCollection(pointspatches,
                                                              color="yellow",
                                                              alpha=None)

        #primary beam path lengths
        pb_len = []
        for pt in self.sh_gridpoints:
            ptxy = np.array(pt.xy)
            pbproj = shgeom.LineString([ptxy, [psp, ptxy[1]]])
            intpt = sh_sample.boundary.intersection(pbproj)
            pb_len.append(intpt.distance(pt))
            intpt.distance(pt)
        self.pb_len = np.array(pb_len)

        #secondary beam path lengths for each detector angle
        nrgridpts = len(self.sh_gridpoints)
        sample_bounddary = sh_sample.boundary
        stth_pts = np.array(pol2cart(sdd, stth + 180.0)).transpose()
        lenstth = len(stth_pts)
        self.sb_len = np.zeros((lenstth, nrgridpts))
        sblenperstth = np.zeros(nrgridpts)
        #tic()
        if self.ui.drawonly_check.isChecked() == True:
            return [stth, np.array(lenstth * [1.0])]
        progressstth = ProgressBar(
            str(nrgridpts) + " points per angle", lenstth)
        for stth_i in range(lenstth):
            progressstth.setinfo("2-theta = " + str(round(stth[stth_i], 1)))
            stth_pt = stth_pts[stth_i]
            for pti in range(nrgridpts):
                ptxy = np.array(self.sh_gridpoints[pti].xy)
                sbproj = shgeom.LineString([ptxy, stth_pt])
                intpt = sample_bounddary.intersection(sbproj)
                sblenperstth[pti] = intpt.distance(self.sh_gridpoints[pti])
                a = intpt
                pass
            self.sb_len[stth_i] = sblenperstth
            if progressstth.wasCanceled(): break
            progressstth.step()
            #xypt = plt.Circle(stth_pt,r)
            #detectorpts_collection.append(xypt)

        #calculate combined attenuation
        #toc()
        ss_i = []
        pb_i = np.exp(-mu * self.pb_len)
        for k in range(len(stth)):
            ss_i.append(pb_i * np.exp(-mu * self.sb_len[k]))
            pass
        self.ss_i = np.array(ss_i)
        self.comb_atten = np.sum(self.ss_i, 1) / len(pb_i)

        return [stth, self.comb_atten]
예제 #10
0
    def Animate(self):
        selectionsettings = mylib.Getexportsettings(self)
        dirname = self.filedialog.getExistingDirectory(self,
                                                       "Select export dir",
                                                       self.lastdir)
        if dirname == "": return
        self.lastdir = dirname
        imagepathlist = []
        fnamebase = "default"
        progressfiles = ProgressBar("Animating files...",
                                    selectionsettings.numfiles)
        for filei in selectionsettings.flistidx:
            progressfiles.setinfo(self.scanman.datasrc.filename)
            fnamebase = path.join(
                dirname,
                str.split(path.basename(self.scanman.datasrc.filename), ".")[0]
                + self.scanman.modext)
            self.scanman.ui.sourceGroupBox.SelectDataFile(filei)

            f = open(fnamebase + "_a.xy",
                     'w')  #_a indicates attenuation corrected data
            src = self.scanman.datasrc
            self.scanman.exportparams.sort()
            axistype = self.scanman.axistype

            if self.ui.all_radio.isChecked():
                selectionsettings.setend = len(self.scanman.datasrc.dataset)
            numsets = selectionsettings.setend - selectionsettings.setstart
            progressdataset = ProgressBar("Calculate and animate datasets...",
                                          numsets)
            for nr in range(selectionsettings.setstart,
                            selectionsettings.setend):
                progressdataset.setinfo(self.scanman.datasrc.filename)
                fname = fnamebase + "_" + str(nr) + ".png"
                fcorr = open(fnamebase + "_corrcoef_" + str(nr) + ".txt", 'w')
                self.scanman.SelectData(nr, display=False)
                self.ui.exp_graphic.figure.savefig(fname,
                                                   format="png",
                                                   dpi=300,
                                                   transparent=False)
                if axistype == 'Channel':
                    x = np.array(
                        src.dataset[nr].currframe.x_chan).astype(float)
                    y = src.dataset[nr].currframe.y.astype(float)
                elif axistype == 'Position':
                    x = src.dataset[nr].currframe.x_mm.astype(float)
                    y = src.dataset[nr].currframe.y.astype(float)
                elif axistype == 'Angle':
                    x = src.dataset[nr].currframe.x_2th.astype(float)
                    y = src.dataset[nr].currframe.y_2th.astype(float)
                elif axistype == 'd-spacing':
                    x = src.dataset[nr].currframe.x_d.astype(float)
                    y = src.dataset[nr].currframe.y_2th.astype(float)
                nrpoints = len(y)
                f.write('XYDATA\n')
                f.write("File name: %s\n" % (src.dataset[nr].filename))
                f.write("| Dataset: %s| Type: %s\n" % (nr, src.ylabel))
                detstr = ""
                for param in src.dataset[nr].detprm:
                    detstr = detstr + "| " + param + ":" + str(
                        src.dataset[nr].detprm[param])
                f.write("%s\n" % (detstr))
                paramstr = ""
                #for param in src.exportparams: paramstr = paramstr + ", " + param +":" + src.dataset[nr].prm[param]
                for param in self.scanman.exportparams:
                    try:
                        paramstr = paramstr + "| " + param + ":" + src.dataset[
                            nr].prm[param]
                    except:
                        True
                if paramstr == "":
                    paramstr = "| set:" + str(
                        nr
                    )  #No parameters were selected to export, but we need some text in that line
                f.write("%s\n" % (paramstr))
                f.write("%s Intensity\n" % (axistype))
                for i in np.arange(nrpoints):
                    f.write('%f %f\n' % (x[i], y[i] * 1 / self.comb_atten[i]))
                    fcorr.write('%f %f\n' % (x[i], 1.0 / self.comb_atten[i]))
                f.write("\n")
                fcorr.write("\n")
                imagepathlist.append(fname)
                if progressdataset.wasCanceled(): break
                progressdataset.step()

            f.truncate(f.tell() - 2)  #Removes the last extra newline
            fcorr.truncate(f.tell() - 2)  #Removes the last extra newline
            f.close()
            fcorr.close()
            if progressfiles.wasCanceled(): break
            progressfiles.step()
        images = [Image.open(image) for image in imagepathlist]
        gifname = fnamebase + ".gif"
        gif = images[0]
        gif.save(fp=gifname,
                 format='gif',
                 save_all=True,
                 append_images=images[1:])
        try:
            execmd = '"' + os.getcwd(
            ) + '\Attenuation\gif2mp4.bat" "' + gifname + '"'
            os.system('"' + execmd + '"')
        except:
            pass
예제 #11
0
    def OpenFile(self, fnames="", finfo=None):
        if fnames == False:
            file_types = "NeXus (*.hdf);;Fullprof (*.xy);;ASCII Two Column (*.txt);;Gumtree Exported (*.xyd);;MCstas (*.sim);;MCStasPS (*.cmds);;PeakFit (*.pkf);; All (*.*)"
            #self.filedialog.selectFilter(self.curfilter)
            fnames, filters = self.filedialog.getOpenFileNamesAndFilter(
                self,
                'Open file',
                '',
                file_types,
                initialFilter=self.curfilter)
            if (fnames == []): return
            self.filedialog.setDirectory(os.path.dirname(str(fnames[0])))
            self.curfilter = filters

        ftbl = self.ui.file_tbl

        wedisconnected = False
        try:
            ftbl.cellChanged.disconnect(
            )  #otherwise this function might be called recursively
            wedisconnected = True
        except:
            True
        #self.src.Clear()

        numfiles = len(fnames)
        progress = ProgressBar("Opening files...", numfiles)

        for afile in fnames:
            progress.setinfo(afile)
            if finfo == None:
                finfo = FileInfo(afile, self.config)
            if finfo.src == None:
                progress.close()
                break

            ftbl.setRowCount(ftbl.rowCount() + 1)
            duplicate = ftbl.findItems(
                finfo.location,
                qtCore.Qt.MatchExactly)  #the file is already in the table
            if len(duplicate) > 0:
                row = duplicate[0].row()
                ftbl.removeRow(row)  #now remove it from the table
                if row == self.getfileindex(finfo.location):
                    self.filelist.pop(
                        row)  #if it exists in the filelist as well
                #self.src == self.filelist[findex].src

            if len(self.filelist) > 0:
                finfo.src.lowerchan = self.filelist[-1].src.lowerchan
                finfo.src.upperchan = self.filelist[-1].src.upperchan
                finfo.srcsplit.lowerchan = self.filelist[-1].srcsplit.lowerchan
                finfo.srcsplit.upperchan = self.filelist[-1].srcsplit.upperchan
            self.filelist.append(finfo)  #append it to the filelist
            #for key in self.colnr.iterkeys():
            for key in self.colnr.keys():
                someitem = qt.QTableWidgetItem()
                someitem.setTextAlignment(qtCore.Qt.AlignRight)
                someitem.setFlags(someitem.flags() & ~qtCore.Qt.ItemIsEditable)
                if key == 'Filename':
                    someitem.setFlags(someitem.flags()
                                      | qtCore.Qt.ItemIsUserCheckable)
                    someitem.setCheckState(qtCore.Qt.Checked)
                    someitem.setText(finfo.filename)
                elif key == 'Ext':
                    someitem.setText(finfo.ext)
                elif key == 'Type':
                    someitem.setText(finfo.type)
                elif key == 'Location':
                    someitem.setText(finfo.location)
                ftbl.setItem(ftbl.rowCount() - 1, self.colnr[key], someitem)
            if progress.wasCanceled(): break
            finfo = None
            progress.step()
            True

        if (wedisconnected):
            ftbl.cellChanged.connect(
                self.CellValueChanged)  #Reconnect the signal
        self.ui.nrfiles_label.setText(str(len(self.filelist)))

        try:
            guessexport = self.filelist[-1].src.precparams.keys()
            for parname in guessexport:
                if (parname not in self.scanman.exportparams):
                    self.scanman.exportparams.append(parname)

            for i in range(self.scanman.ui.prm_table.rowCount()):
                self.scanman.PostPrecChanged(
                    i, 2
                )  #Let the newly opened file have the same values for post sets precicion
            self.SelectDataFile(-1)
            ftbl.setCurrentCell(ftbl.rowCount() - 1, 1)
        except:
            True
예제 #12
0
    def ExportGSAS(self):
        sep = os.linesep
        self.Getexportsettings()

        if self.scanman.ui.outputGroupBox.ui.files_all_radio.isChecked(
        ):  #should put this in also in Getexportsettings
            flistidx = range(len(self.scanman.ui.sourceGroupBox.filelist))
        elif self.scanman.ui.outputGroupBox.ui.files_current_radio.isChecked():
            flistidx = [
                self.scanman.ui.sourceGroupBox.ui.file_tbl.currentRow()
            ]

        self.filedialog.setAcceptMode(QFileDialog.AcceptSave)
        self.filedialog.setViewMode(QFileDialog.Detail)
        if self.lastdir == "":
            self.lastdir = self.lastdir = os.path.dirname(
                self.scanman.ui.sourceGroupBox.filelist[flistidx[0]].location)
        dirname = self.filedialog.getExistingDirectory(self,
                                                       "Select export dir",
                                                       self.lastdir)
        if dirname == "": return
        self.lastdir = dirname

        numfiles = len(flistidx)
        progress = ProgressBar("Exporting graph data...", numfiles)

        #GSAS only works with angles, therefore force ScanMan to select this (bad hack)
        oldaxistype = self.scanman.axistype
        if oldaxistype != 'Angle':
            self.scanman.ui.angle_radio.click()

        if self.ui.GSASLinkProfileCheckBox.isChecked():
            insfname = self.ui.GSASProfileLineEdit.text()
            #Set up initial instrument file if it does not exist
            instrumentname = self.scanman.config['instrument']
            wavelength = self.scanman.config['source']['detector']['lambda']
            if insfname == "":
                insfnamebase = instrumentname + "_init_lam(" + str(
                    wavelength) + ").prm"
                insfname = os.path.join(str(dirname), insfnamebase)
            if os.path.exists(insfname) == False:
                insf = open(insfname, 'w')
                zoffset = 0.0
                insf.write(
                    "            123456789012345678901234567890123456789012345678901234567890%s"
                    % (sep))
                insf.write("INS   BANK      1%s" % (sep))
                insf.write(
                    "INS   HTYPE      PNCR%s" % (sep)
                )  #Histogram type: Powder, Neutron, Constant wavelength,  R=fixed for powders
                insf.write(
                    "INS  1 ICONS  %f  0.0       0.0         %f      0.0    0       0.0   %s"
                    % (wavelength, zoffset, sep))
                insf.write(
                    "INS  1I HEAD  DUMMY INCIDENT SPECTRUM FOR DIFFRACTOMETER %s%s"
                    % (self.scanman.config['instrument'], sep))
                insf.write(
                    "INS  1I ITYP    0    0.0000  180.0000         1                                 %s"
                    % (sep))
                insf.write(
                    "INS  1PRCF1     1    6      0.01                                                %s"
                    % (sep))
                insf.write(
                    "INS  1PRCF11   0.000000E+00   0.000000E+00   0.000000E+00   0.000000E+00        %s"
                    % (sep))
                insf.write(
                    "INS  1PRCF12   0.000000E+00   0.000000E+00   0.000000E+00   0.000000E+00        %s"
                    % (sep))
                insf.write(
                    "INS  1PRCF2     2    6      0.01                                                %s"
                    % (sep))
                insf.write(
                    "INS  1PRCF21   0.000000E+00   0.000000E+00   0.000000E+00   0.000000E+00        %s"
                    % (sep))
                insf.write(
                    "INS  1PRCF22   0.000000E+00   0.000000E+00        %s" %
                    (sep))
                insf.close()
            else:
                insfnamebase = os.path.basename(insfname)
                try:
                    copy2(insfname, os.path.join(str(dirname), insfnamebase))
                except:
                    True

        #Do this if a single zip file must be created
        if self.singlefile == True:
            src = self.scanman.datasrc
            fnames = sorted([
                self.scanman.ui.sourceGroupBox.filelist[flistidx[i]].filename
                for i in range(len(flistidx))
            ])
            #fnames.sort()
            name1 = str.split(
                os.path.basename(os.path.splitext(str(fnames[0]))[0]), ".")[0]
            name2 = str.split(
                os.path.basename(os.path.splitext(str(fnames[-1]))[0]), ".")[0]
            if len(name1) > 4 and len(name2) > 4:
                try:
                    name2 = str(int(name2[4:]))
                except:
                    True
            fnamezip = name1 + "-" + name2 + self.scanman.modext + "_c" + ".zip"
            zfile = zipfile.ZipFile(os.path.join(str(dirname), fnamezip), "w")
            True

        #Do this for all the datafiles
        for filei in flistidx:
            progress.setinfo(
                self.scanman.ui.sourceGroupBox.filelist[filei].filename)
            self.scanman.ui.sourceGroupBox.SelectDataFile(filei)
            src = self.scanman.datasrc

            self.scanman.exportparams.sort()

            axistype = self.scanman.axistype  #should now be 'Angle'
            sfname = str.split(
                os.path.basename(
                    os.path.splitext(str(src.dataset[1].filename))[0]), ".")[0]
            sfname = sfname + self.scanman.modext

            if self.ui.dset_all_radio.isChecked():
                self.setend = len(src.dataset)
                self.setstart = 1
            for nr in range(self.setstart, self.setend):
                fname = sfname + "_dset(" + str(nr) + ").fxye"
                self.scanman.SelectData(nr, display=False)
                src = self.scanman.datasrc
                x_original = src.dataset[nr].currframe.x_2th.astype(float)
                y_original = src.dataset[nr].currframe.y_2th.astype(float)

                nrpoints = len(y_original)
                x = np.linspace(x_original[0], x_original[-1], nrpoints)
                stepsize = x[1] - x[0]
                y = np.interp(x, x_original, y_original)
                thisfname = os.path.join(str(dirname), fname)
                f = open(thisfname, 'wb')
                headerline = src.dataset[nr].filename + " | Dataset: " + str(
                    nr) + " | Type: " + src.ylabel + sep
                f.write(headerline.encode('utf-8'))

                if self.ui.GSASLinkProfileCheckBox.isChecked():
                    f.write(("Instrument parameter file:%s%s" %
                             (insfnamebase, sep)).encode('utf-8'))

                detstr = "#"
                for param in src.dataset[nr].detprm:
                    detstr = detstr + sep + "# " + param + ":" + str(
                        src.dataset[nr].detprm[param])
                f.write(("%s%s" % (detstr, sep)).encode('utf-8'))
                paramstr = "#"
                for param in self.scanman.exportparams:
                    try:
                        paramstr = paramstr + sep + "# " + param + ":" + src.dataset[
                            nr].prm[param]
                    except:
                        True
                if paramstr == "#":
                    paramstr = sep + "# set:" + str(
                        nr
                    )  #No parameters were selected to export, but we need some text in that line
                f.write(("%s%s" % (paramstr, sep)).encode('utf-8'))
                f.write(("BANK 1 %i %i CONS %f %f 0 0 FXY%s" %
                         (nrpoints, nrpoints, x[0] * 100, stepsize * 100,
                          sep)).encode('utf-8'))
                for i in np.arange(nrpoints):
                    f.write(
                        ('%f %f %s' % (x[i] * 100, y[i], sep)).encode('utf-8'))
                f.close()

                if self.singlefile == True:  #add it to the zip file
                    zfile.write(thisfname)
                    os.remove(thisfname)
                    True
            progress.step()
        if self.singlefile == True:
            #zfile.write(os.path.join(str(dirname),insfnamebase))
            zfile.close()

        #Select the initial axis view again
        if oldaxistype != 'Angle':
            if oldaxistype == 'Channel':
                self.scanman.ui.channel_radio.click()
            elif oldaxistype == 'Position':
                self.scanman.ui.position_radio.click()
            elif oldaxistype == 'd-spacing':
                self.scanman.ui.dspacing_radio.click()

        True
예제 #13
0
    def ExportFullprof(self):
        self.Getexportsettings()

        if self.scanman.ui.outputGroupBox.ui.files_all_radio.isChecked(
        ):  #should put this in also in Getexportsettings
            flistidx = range(len(self.scanman.ui.sourceGroupBox.filelist))
        elif self.scanman.ui.outputGroupBox.ui.files_current_radio.isChecked():
            flistidx = [
                self.scanman.ui.sourceGroupBox.ui.file_tbl.currentRow()
            ]

        self.filedialog.setAcceptMode(QFileDialog.AcceptSave)
        self.filedialog.setViewMode(QFileDialog.Detail)
        if self.lastdir == "":
            self.lastdir = self.lastdir = os.path.dirname(
                self.scanman.ui.sourceGroupBox.filelist[flistidx[0]].location)
        dirname = self.filedialog.getExistingDirectory(self,
                                                       "Select export dir",
                                                       self.lastdir)
        if dirname == "": return
        self.lastdir = dirname

        numfiles = len(flistidx)
        progress = ProgressBar("Exporting graph data...", numfiles)

        if self.singlefile == True:
            src = self.scanman.datasrc
            fnames = sorted([
                self.scanman.ui.sourceGroupBox.filelist[flistidx[i]].filename
                for i in range(len(flistidx))
            ])
            #fnames.sort()
            name1 = str.split(
                os.path.basename(os.path.splitext(str(fnames[0]))[0]), ".")[0]
            name2 = str.split(
                os.path.basename(os.path.splitext(str(fnames[-1]))[0]), ".")[0]
            if len(name1) > 4 and len(name2) > 4:
                try:
                    name2 = str(int(name2[4:]))
                except:
                    True

            fname = name1 + "-" + name2 + self.scanman.modext + "_c" + ".xy"
            f = open(os.path.join(str(dirname), fname), 'w')

            True

        for filei in flistidx:
            progress.setinfo(
                self.scanman.ui.sourceGroupBox.filelist[filei].filename)
            self.scanman.ui.sourceGroupBox.SelectDataFile(filei)
            src = self.scanman.datasrc

            self.scanman.exportparams.sort()
            axistype = self.scanman.axistype
            #filemod = ""
            if self.singlefile == False:
                fname = str.split(
                    os.path.basename(
                        os.path.splitext(str(src.dataset[1].filename))[0]),
                    ".")[0]
                fname = fname + self.scanman.modext + ".xy"
                f = open(os.path.join(str(dirname), fname), 'w')

            if self.ui.dset_all_radio.isChecked():
                self.setend = len(src.dataset)
                self.setstart = 1
            for nr in range(self.setstart, self.setend):
                self.scanman.SelectData(nr, display=False)
                src = self.scanman.datasrc

                if axistype == 'Channel':
                    x = np.array(
                        src.dataset[nr].currframe.x_chan).astype(float)
                    y = src.dataset[nr].currframe.y.astype(float)
                elif axistype == 'Position':
                    x = src.dataset[nr].currframe.x_mm.astype(float)
                    y = src.dataset[nr].currframe.y.astype(float)
                elif axistype == 'Angle':
                    x = src.dataset[nr].currframe.x_2th.astype(float)
                    y = src.dataset[nr].currframe.y_2th.astype(float)
                elif axistype == 'd-spacing':
                    x = src.dataset[nr].currframe.x_d.astype(float)
                    y = src.dataset[nr].currframe.y_2th.astype(float)
                nrpoints = len(y)
                f.write('XYDATA\n')
                f.write("File name: %s\n" % (src.dataset[nr].filename))
                f.write("| Dataset: %s| Type: %s\n" % (nr, src.ylabel))
                detstr = ""
                for param in src.dataset[nr].detprm:
                    detstr = detstr + "| " + param + ":" + str(
                        src.dataset[nr].detprm[param])
                f.write("%s\n" % (detstr))
                paramstr = ""
                #for param in src.exportparams: paramstr = paramstr + ", " + param +":" + src.dataset[nr].prm[param]
                for param in self.scanman.exportparams:
                    try:
                        paramstr = paramstr + "| " + param + ":" + src.dataset[
                            nr].prm[param]
                    except:
                        True
                if paramstr == "":
                    paramstr = "| set:" + str(
                        nr
                    )  #No parameters were selected to export, but we need some text in that line
                f.write("%s\n" % (paramstr))
                f.write("%s Intensity\n" % (axistype))
                for i in np.arange(nrpoints):
                    f.write('%f %f\n' % (x[i], y[i]))
                f.write("\n")

            if self.singlefile == False:
                f.truncate(f.tell() - 2)  #Removes the last extra newline
                f.close()
            progress.step()
        if self.singlefile == True:
            f.truncate(f.tell() - 2)  #Removes the last extra newline
            f.close()
        True
예제 #14
0
 def GetWaterFallValues(self):
     
     prm1=str(self.ui.waterfall_scan_combo.currentText())
     if prm1 == "" :
         self.xlabel="Run_number"
     else:
         self.xlabel=prm1
     try:
         if self.scanman.ui.outputGroupBox.ui.allfiles_check.isChecked():
             flistidx = range(len(self.scanman.ui.sourceGroupBox.filelist))
         else:
             flistidx = [self.scanman.ui.sourceGroupBox.ui.file_tbl.currentRow()]
     except:             #This source is probably not a filesource and therefore only one dataset is present
         flistidx = [1]            
     
     x=[]
     y=[]
     #z=[]
     z=np.array([])
     y_err=[]
     run_number = float(1)
     progressfiles = ProgressBar("Retrieving parameters waterfall...", len(flistidx))
     ylabelold = ""
     ylabelnew = ""
     self.ylabel = ""
     for filei in flistidx:
         progressfiles.setinfo(self.scanman.ui.sourceGroupBox.filelist[filei].location)
         try:
             self.scanman.ui.sourceGroupBox.SelectDataFile(filei)
         except:
             True
         
         True
         src = self.scanman.datasrc
         self.setstart = 1
         self.setend = len(src.dataset)-1
         
         if (self.ui.range_radio.isChecked()):
             self.setstart = self.ui.rangemin_spin.value()
             self.setend = self.ui.rangemax_spin.value()
         
         self.scanman.SelectData(self.setstart,display=False)
         if len(y)==0:           #First file
             #y = [src.y]
             y = [src.y.tolist()]
             if (len(src.y)==len(src.y_err)):
                 y_err = [src.y_err.tolist()]
         else:
             y=np.concatenate((y,[src.y]),axis=0)
             if (len(src.y)==len(src.y_err)):
                 y_err=np.concatenate((y_err,[src.y_err]),axis=0)
             
         #x=[src.x]
         x=[src.x.tolist()]
         z_new = run_number if prm1=="" else float(src.prm[prm1])
         #z=np.append(z,z_new)
         z=z+[z_new]
         
         #xp=np.append(xp,[src.x])
         #yp=np.append(yp,[src.y])
         #zp=np.append(zp,[z_new]*len(src.x))
                 
         try:    #Only works with numbers but the parameter value can also be text in which case this will fail
             for setnr in range(self.setstart+1,self.setend+1):
                 run_number = run_number + 1
                 self.scanman.SelectData(setnr,display=False)
                 src = self.scanman.datasrc
                 #if src.prm.has_key("position"):
                 if "position" in src.prm.keys():
                     ylabelnew =src.prm["position"]
                 else:
                     ylabelnew = self.scanman.axistype
                 if  ylabelnew != ylabelold:
                     self.ylabel = self.ylabel + "," + ylabelnew
                     ylabelold = ylabelnew
                 #y=np.concatenate((y,[src.y]),axis=0)
                 y=y+[src.y.tolist()]
                 if (len(src.y)==len(src.y_err)):
                     y_err=y_err+[src.y_err.tolist()]
                 x=x+[src.x.tolist()]
                 z_new = run_number if prm1=="" else float(src.prm[prm1])
                 z=np.append(z,z_new)
                 
                 #xp=np.append(xp,[src.x])
                 #yp=np.append(yp,[src.y])
                 #zp=np.append(zp,[z_new]*len(src.x))
                 True
         except:
             x=[0,0]
             y=[0,0]
             y_err=[0]
         
         if progressfiles.wasCanceled(): break
         progressfiles.step()
     sortedi = z.argsort()
     y=np.array(y)
     y_err=np.array(y_err)
     x=np.array(x)
     self.ylabel = self.ylabel[1:] #remove the ',' at the beginning
     if len(y_err)==0:
         y_err = np.zeros((len(y),len(y[0])))
     try:
         return x[sortedi].tolist(), y[sortedi].tolist(), z[sortedi].tolist(), y_err[sortedi].tolist()
     except:
         
         return z[sortedi], z[sortedi], z[sortedi], z[sortedi]
예제 #15
0
    def Read_xyz(self,fname):
        f = open(fname, 'r')
        filecontent = f.read()
        f.close()
        lines = filecontent.split("\n")
        blockidx=[-1]
        for iline in range(len(lines)):
            if len(lines[iline].rstrip()) == 0:     #Determine where whitespace lines are, these will define new datasets
                blockidx =blockidx + [iline]
        datablocks=[]        
        for blocki in range(1,len(blockidx)):
            datablocks = datablocks + [lines[blockidx[blocki-1]+1:blockidx[blocki]]]
        
        progressgraph = ProgressBar("Creating xyz graphs", len(datablocks))          
        for data in datablocks:
            progressgraph.setinfo(fname)
            header = data[0].split()
            readerr=False
            if len(header)==4:readerr=True
            try:
                a=float(header[0])
                datastart = 0
                self.xlabel = "x"
                self.ylabel = "y"
                self.zlabel = "z"
            except:
                datastart = 1
                self.xlabel = header[0]
                self.ylabel = header[1]
                self.zlabel = header[2]
            x = y = z = z_err = []
            for dataln in data[datastart:]:
                if len(dataln.strip())!=0:
                    if readerr:
                        xval,yval,zval,errval = dataln.split()
                        z_err = z_err +  [[float(errval)]]
                    else:
                        xval,yval,zval = dataln.split()
                        z_err = z_err +  [[0.0]]
                        True
                    x = x + [float(xval)]
                    y = y + [[float(yval)]]
                    z = z + [[float(zval)]]

                True
            x = np.array(x)
            y = np.array(y)
            z = np.array(z)
            z_err = np.array(z_err)
            
            #z_err2 = np.zeros((len(z),len(z[0])))
            pts = [0,0]
            if self.ui.interp_check.isChecked():
                xpts = int(self.ui.xpts_edit.text())
                ypts = int(self.ui.ypts_edit.text())
                pts = [xpts,ypts]
            self.mayaviobj.setWaterfall(x,y,z,z_err,pts)  #Note the difference in x,y,z assignemnts
            self.mayaviobj.visualization.surfaxes.axes.x_label=self.xlabel
            self.mayaviobj.visualization.surfaxes.axes.y_label=self.ylabel
            self.mayaviobj.visualization.surfaxes.axes.z_label=self.zlabel
            
            progressgraph.step()               
            True #To be implemented