예제 #1
0
def noOption(ListOpt):
    List = ListOpt.copy()
    if '--log' in List:
        logFlag = True
        List.remove('--log')
    else:
        logFlag = False

    if '--noCal' in List:
        noCalFlag = True
        List.remove('--noCal')
    else:
        noCalFlag = False
    
    ListAux = List
    for arg in ListAux:
        if arg[0] == '-':
            List.remove(arg)

    plotFlag = False
    exitcode = 0

    if len(ListOpt) == 0:
        exitcode = helpFun(['None'],['.Txt','.SPA','.mca','.info'],extBool=False)
        return exitcode
    else:
        for arg in List:
            if isValidSpecFile(arg):
                if arg.endswith('.info'):
                    exitcode = energyFun(['--energyRanges',arg])
                else:
                    myFilename = arg
                    myExtension = myFilename.split(".")[-1]
                    mySubsDict = functionDictAdv[myExtension](myFilename)
                    # SPE no funciona con estas condiciones
                    # if not noCalFlag and mySubsDict['calBoolean']:
                    #     mySubsDict = functionDictAdv[myExtension](myFilename,noCalFlag=False)
                    # else:
                    #     mySubsDict = functionDictAdv[myExtension](myFilename,noCalFlag=True)
                    mySubsList = mySubsDict["theList"]
                    plotFlag = True
                    simplePlot(mySubsList,logFlag,mySubsDict['calBoolean'],Label=None,show=False,Title=None)
            else:
                print('WARNING: The file ' + arg + ' is invalid. Nothing to do with it.')
                return 90
    if plotFlag:
        plt.show()
    return 0
예제 #2
0
def SumFun(ListOpt):
    List = ListOpt.copy()
    List.pop(0)
    #functionDict = {"SPE": getDictFromSPE,"mca": getDictFromMCA,"Txt": getDictFromGammaVision,"info":getDictFromInfoFile}
    if '--noCal' in List:
        noCalFlag = True
        List.remove('--noCal')
    else:
        noCalFlag = False

    if '--noPlot' in List:
        noPlotFlag = True
        List.remove('--noPlot')
    else:
        noPlotFlag = False

    if '--wof' in List:
        wofFlag = True
        List.remove('--wof')
    else:
        wofFlag = False

    if '--log' in List:
        logFlag = True
        List.remove('--log')
    else:
        logFlag = False

    if len(List) == 1:
        if isValidSpecFile(List[0]):
            print('----------------------------------------------')
            print(
                'WARNING: At least two files are needed to make the sum between them.'
            )
            print(
                'No sum was performed. If option --noPlot is disabled then plot is shown.'
            )
            print('Otherwise, nothing is shown.')
            print('----------------------------------------------')
            myFilename = List[0]
            myExtension = myFilename.split(".")[-1]
            if noCalFlag:
                mySubsDict = functionDict[myExtension](myFilename, False)
            else:
                mySubsDict = functionDict[myExtension](myFilename)
            mySubsList = mySubsDict["theList"]
            if noPlotFlag:
                print('----------------------------------------------')
                print('--noPlot is activated, so there is no figure display.')
            else:
                simplePlot(mySubsList,
                           logFlag,
                           noCalFlag,
                           Label=None,
                           show=True,
                           Title=myFilename.split('/')[-1])
        else:
            print('----------------------------------------------')
            print('WARNING: No .mca, .Txt or .SPE files was indicated.')
            print('Please, introduce two or more valid files.')
            return 41

    elif len(List) > 1:
        count = 0
        Title = ''
        for myFilename in List:
            count += 1
            Title += myFilename.split('/')[-1] + '+'
            if isValidSpecFile(myFilename):
                myExtension = myFilename.split(".")[-1]
            if noCalFlag:
                mySubsDict = functionDict[myExtension](myFilename, False)
                if count == 1:
                    mySubsDictOut = mySubsDict.copy()
            else:
                mySubsDict = functionDict[myExtension](myFilename)
                if count == 1:
                    mySubsDictOut = mySubsDict.copy()
            if count != 1:
                mySubsDictOut = SumSpectra(mySubsDictOut, mySubsDict)

        if not noPlotFlag:
            simplePlot(mySubsDictOut["theList"],
                       logFlag,
                       noCalFlag,
                       Label=None,
                       show=True,
                       Title=Title[:-1])
        if wofFlag:
            myOutFile = myFilename.strip(
                myFilename.split('/')[-1]) + Title + '.txt'
            try:
                WritehgeFile(myOutFile, mySubsDictOut)
                print('-----------------------------------------')
                print('The file was saved as:')
                print(myOutFile)
                print('-----------------------------------------')
            except:
                try:
                    WriteOutputFile(mySubsDictOut, myOutFile, Title)
                    print('-----------------------------------------')
                    print('The file was saved as:')
                    print(myOutFile)
                    print('-----------------------------------------')
                except IOError:
                    print(
                        'An unexpected error ocurred while saving the data to file.'
                    )
                    return 44

    else:
        print('ERROR: Not a valid argument in "Sum" function.')
        return 40

    return 0
예제 #3
0
def SubFun(ListOpt):
    List = ListOpt.copy()
    List.pop(0)
    if '--noCal' in List:
        noCalFlag = True
        List.remove('--noCal')
    else:
        noCalFlag = False
    if '--noPlot' in List:
        noPlotFlag = True
        List.remove('--noPlot')
    else:
        noPlotFlag = False
    if '--wof' in List:
        wofFlag = True
        List.remove('--wof')
    else:
        wofFlag = False
    if '--log' in List:
        logFlag = True
        List.remove('--log')
    else:
        logFlag = False
    if '--rebin' in List:
        rebinFlag = True
        List.remove('--rebin')
        rebinNum = None
        for Arg in List:
            try:
                rebinNum = float(Arg)
                break
            except:
                continue
        if rebinNum == None:
            return 64

    else:
        rebinFlag = False

    FileName1 = List[0]
    FileName2 = List[1]
    if len(List) == 0:
        sys.stderr.write(
            'ERROR: "-r" option needs 2 arguments: File1 and File2, which will be substracted to File1.'
        )
        return 60

    if not os.path.isfile(FileName1):
        sys.stderr.write("ERROR: %s does not exist.\n" % (FileName1))
        return 61

    if not os.path.isfile(FileName2):
        sys.stderr.write("ERROR: %s does not exist.\n" % (FileName2))
        return 62
    File1Ext = FileName1.split('.')[-1]
    File2Ext = FileName2.split('.')[-1]
    if File1Ext != File2Ext:
        sys.stderr.write(
            "Errror: background substraction needs the same extension as the main file. (for now)"
        )
        return 65

    if noCalFlag:
        File1Dict = functionDict[File1Ext](FileName1, True)
        File2Dict = functionDict[File2Ext](FileName2, True)
    else:
        File1Dict = functionDict[File1Ext](FileName1, False)
        File2Dict = functionDict[File2Ext](FileName2, False)

    if rebinFlag:
        File1Dict["theRebinedList"] = getRebinedList(File1Dict["theList"],
                                                     rebinNum)
        File2Dict["theRebinedList"] = getRebinedList(File2Dict["theList"],
                                                     rebinNum)
        myLen1 = len(File1Dict["theRebinedList"][1])
        myLen2 = len(File2Dict["theRebinedList"][1])
    else:
        myLen1 = len(File1Dict["theList"][1])
        myLen2 = len(File2Dict["theList"][1])

    if myLen1 != myLen2:
        sys.stderr.write(
            "ERROR: histograms do not have the same length and histoGe cannot continue.\n"
        )
        return 63

    time1 = File1Dict["expoTime"]
    time2 = File2Dict["expoTime"]
    tRatio = time1 / time2
    rescaledList = getRescaledList(File2Dict['theList'], tRatio)

    if File1Dict['calBoolean'] == File2Dict['calBoolean']:
        subsTractedL = getSubstractedList(File1Dict['theList'], rescaledList)
    else:
        print('--------------------------------------------')
        sys.stderr.write(
            'ERROR: All files must be calibrated or non-calibrated.\n')
        print('Sub cannot be performed between different types of files')
        print('--------------------------------------------')
        exit(43)
        pass

    allData = subsTractedL
    allData.append(rescaledList[1])
    allData.append(File1Dict['theList'][1])
    Title = FileName1.split('/')[-1].rstrip(
        '.' + File1Ext) + '-' + FileName2.split('/')[-1].rstrip('.' + File2Ext)
    IdFile = FileName1.rfind('/')
    myOutFile = FileName1[:IdFile + 1] + Title + '.txt'

    Labels = [
        FileName1.split('/')[-1].rstrip('.' + File1Ext),
        FileName2.split('/')[-1].rstrip('.' + File1Ext), Title
    ]

    if not noPlotFlag:
        simplePlot(allData,
                   logFlag,
                   File1Dict['calBoolean'] and File2Dict['calBoolean'],
                   Label=Labels,
                   show=True,
                   Title=Title,
                   figTitle=Title)  #simple plot of subtracted
    if wofFlag:
        try:
            WritehgeFile(myOutFile, subsTractedL)
            print('-----------------------------------------')
            print('The file was saved as:')
            print(myOutFile)
            print('-----------------------------------------')
        except:
            try:
                WriteOutputFile(subsTractedL, myOutFile, Title)
                print('-----------------------------------------')
                print('The file was saved as:')
                print(myOutFile)
                print('-----------------------------------------')
            except IOError:
                sys.stderr.write(
                    'An unexpected error ocurred while saving the data to file.\n'
                )
                return 66

    return 0
예제 #4
0
def noOption(ListOpt):
    labels = []
    List = ListOpt.copy()
    if '--log' in List:
        logFlag = True
        List.remove('--log')
    else:
        logFlag = False

    if '--noCal' in List:
        noCalFlag = True
        List.remove('--noCal')
    else:
        noCalFlag = False

    if '--rebin' in List:
        rebinFlag = True
        List.remove('--rebin')
        rebinNum = None
        for Arg in List:
            try:
                rebinNum = int(Arg)
                List.remove(Arg)
                break
            except:
                continue
    else:
        rebinFlag = False

    ListAux = List
    for arg in ListAux:
        if arg[0] == '-':
            List.remove(arg)

    plotFlag = False
    exitcode = 0

    if len(ListOpt) == 0:
        exitcode = helpFun(['None'], ['.Txt', '.SPE', '.mca', '.info'],
                           extBool=False)
        return exitcode
    else:
        for arg in List:
            if isValidSpecFile(arg):
                if arg.endswith('.info'):
                    exitcode = energyFun(['--energyRanges', arg])

                try:
                    if isValidSpecFile(arg):
                        FileExt = arg.split('.')[-1]
                except:
                    sys.stderr.write(
                        'ERROR: Unexpected error. Not a valid file used.')
                    return 110

                ######
                fileName = arg

                if noCalFlag:
                    FileDict = functionDictAdv[FileExt](fileName, True)
                else:
                    FileDict = functionDictAdv[FileExt](fileName)

                if rebinFlag:

                    if isinstance(rebinNum, int) == False:
                        rebinNum = 5
                        sys.stderr.write(
                            "There was no rebin integer detected, the default rebin value used was 5"
                        )

                    if "theRebinedList" not in FileDict:
                        FileDict["theRebinedList"] = getRebinedList(
                            FileDict["theList"], rebinNum)
                        myDataList = FileDict["theRebinedList"]
                        myDataList[0] = list(myDataList[0])
                        myDataList[1] = list(myDataList[1])

                    else:
                        sys.stderr.write(
                            "There was no rebin option detected, the rebin option is --rebin"
                        )

                    figName = os.path.splitext(arg)[0].split('/')[-1]
                    mySubsList = myDataList
                    plotFlag = True
                    labels.append(figName)
                    simplePlot(mySubsList,
                               logFlag,
                               FileDict['calBoolean'],
                               Label=labels,
                               show=False,
                               Title=fileName,
                               ExpoTime=FileDict['expoTime'],
                               figTitle=figName)

                ######
                else:
                    figName = os.path.splitext(arg)[0].split('/')[-1]
                    labels.append(figName)
                    myExtension = fileName.split(".")[-1]
                    if myExtension != 'info':
                        mySubsList = FileDict["theList"]
                        plotFlag = True
                        simplePlot(mySubsList,
                                   logFlag,
                                   FileDict['calBoolean'],
                                   Label=labels,
                                   show=False,
                                   Title=fileName,
                                   ExpoTime=FileDict['expoTime'],
                                   figTitle=figName)
            else:
                sys.stderr.write('WARNING: The file ' + arg +
                                 ' is invalid. Nothing to do with it.')
                return 90
    if plotFlag:
        plt.show()
    return 0
예제 #5
0
파일: Sub.py 프로젝트: ffavela/histoGe
def SubFun(ListOpt):
    List = ListOpt.copy()
    List.pop(0)
    if '--noCal' in List:
        noCalFlag = True
        List.remove('--noCal')
    else:
        noCalFlag = False
    if '--noPlot' in List:
        noPlotFlag = True
        List.remove('--noPlot')
    else:
        noPlotFlag = False
    if '--wof' in List:
        wofFlag = True
        List.remove('--wof')
    else:
        wofFlag = False
    if '--log' in List:
        logFlag = True
        List.remove('--log')
    else:
        logFlag = False
    if '--rebin' in List:
        rebinFlag = True
        List.remove('--rebin')
        rebinNum = None
        for Arg in List:
            try:
                rebinNum = float(Arg)
                break
            except:
                continue
        if rebinNum == None:
            return 64

    else:
        rebinFlag = False

    FileName1 = List[0]
    FileName2 = List[1]
    if len(List) == 0:
        print(
            'ERROR: "-r" option needs 2 arguments: File1 and File2, which will be substracted to File1.'
        )
        return 60

    if not os.path.isfile(FileName1):
        print("ERROR: %s does not exist." % (FileName1))
        return 61

    if not os.path.isfile(FileName2):
        print("ERROR: %s does not exist." % (FileName2))
        return 62
    File1Ext = FileName1.split('.')[-1]
    File2Ext = FileName2.split('.')[-1]
    if File1Ext != File2Ext:
        print(
            "Errror: background substraction needs the same extension as the main file. (for now)"
        )
        return 65

    if noCalFlag:
        File1Dict = functionDict[File1Ext](FileName1, False)
        File2Dict = functionDict[File2Ext](FileName2, False)
    else:
        File1Dict = functionDict[File1Ext](FileName1)
        File2Dict = functionDict[File2Ext](FileName2)

    if rebinFlag:
        File1Dict["theRebinedList"] = getRebinedList(File1Dict["theList"],
                                                     rebinNum)
        File2Dict["theRebinedList"] = getRebinedList(File2Dict["theList"],
                                                     rebinNum)
        myLen1 = len(File1Dict["theRebinedList"][1])
        myLen2 = len(File2Dict["theRebinedList"][1])
    else:
        myLen1 = len(File1Dict["theList"][1])
        myLen2 = len(File2Dict["theList"][1])

    if myLen1 != myLen2:
        print(
            "ERROR: histograms do not have the same length and histoGe cannot continue."
        )
        return 63

    time1 = File1Dict["expoTime"]
    time2 = File2Dict["expoTime"]
    tRatio = time1 / time2
    rescaledList = getRescaledList(File2Dict['theList'], tRatio)
    subsTractedL = getSubstractedList(File1Dict['theList'], rescaledList)

    Title = FileName1.split('/')[-1].rstrip(
        '.' + File1Ext) + '-' + FileName2.split('/')[-1].rstrip('.' + File2Ext)
    IdFile = FileName1.rfind('/')
    myOutFile = FileName1[:IdFile + 1] + Title + '.txt'

    if not noPlotFlag:
        simplePlot(subsTractedL,
                   logFlag,
                   noCalFlag,
                   Label=None,
                   show=True,
                   Title=Title)
    if wofFlag:
        try:
            WritehgeFile(myOutFile, subsTractedL)
            print('-----------------------------------------')
            print('The file was saved as:')
            print(myOutFile)
            print('-----------------------------------------')
        except:
            try:
                WriteOutputFile(subsTractedL, myOutFile, Title)
                print('-----------------------------------------')
                print('The file was saved as:')
                print(myOutFile)
                print('-----------------------------------------')
            except IOError:
                print(
                    'An unexpected error ocurred while saving the data to file.'
                )
                return 66

    return 0