Пример #1
0
def combineFilesWithParas(dir_path, filename, targetDir=None, connector="="):
    """ Combine all files with name filename. Also, parameters will be added as seperated
    columns in the combined file. The order of parameters of corresponding to the inserted
    columns is indicated by the file name. """
    filenameL = listR.toList(filename)

    for filename in filenameL: # do the following for each file:
        # get list of common parameters:
        allParas = []
        for aDir in dirR.listNestedDirContainsFiles(dir_path, filename):
            allParas.append(listR.readCSESD(path.join(aDir, filename)).keys())
        allParas = listR.removeDuplicates(listR.intersect(allParas))

        # generate modified file name:
        if targetDir == None: targetDir=getcwd()
        new_filename = path.join(targetDir,path.splitext(filename)[0]+"("+",".join(listR.stringizeL(allParas))+")"+path.splitext(filename)[1])

        # generate the combined data file:
        outFile = open(new_filename, "w")
        for aDir in dirR.listNestedDirContainsFiles(dir_path, filename):
            to_read = path.join(aDir, filename) # target file: path+name
            parasD = listR.readCSESD(to_read) # get a dictionary of parameter values
            para_str = " ".join(listR.stringizeL(listR.getValueListFromDict(allParas, parasD))) # get the associated parameter list in string format
            inFile = open(to_read, "r")
            buffer = inFile.readlines() # read target file
            for aLine in buffer: # concarnate target file with parameter list:
                outFile.write(para_str+" "+aLine)
            inFile.close()
        outFile.close()
Пример #2
0
def generatePlotFileCore(fList, plotFileName="plot.bat", preCmds="", postCmds="", labels=[], dirName="", useRelativePath=True):
  """ Generate a grace batch file for all files in subdirectory with names
  given in fList with suggested title and labels according to the
  difference of names of the subdirectories.
  fList has the structure [[filename, plotCommand, partialLabel],...].
  This is the core function with most parameters.
  parameter is a long control string that will be written in the plt file.
  labels is a list of strings that will be used as labels with plotIndex
  (see code) if non-empty."""
  # partial label is a label that is shared by all files with the same "filename"
  # "labels" list holds labels that are different among files with the same "filename"

  if dirName == "": dirName = getcwd();

  outFile = open(path.join(dirName,plotFileName), "w");
  # write header
  outFile.write("# Grace batch file generated by generateGraceBatchFile\n");
  outFile.write("#              generated on "+strftime("%Y-%m-%d@%H:%M:%S")+"\n\n\n");

  outFile.write("\n")
  if preCmds != "": outFile.write(preCmds);
  outFile.write("\n")


  plotBuffer = []; # store those plot commands without title content
  legendBuffer = []; # store all the raw titles, only the difference between them should go into the plot file
  partialLabels = []; # store all partial labels

  plotIndex = 0; # unique index that separate graphs apart
  # declare files to be plotted
  for fileName, plotCmd, partialLabel in fList:
    fileIndex = 0; # used for indexing labels
    for aDir in dirR.listNestedDirContainsFiles(dirName,fileName):
      if useRelativePath == True: # final form of "path" can be relative to cwd or absolute
        aDirFinal = aDir.replace(dirName,".")
      else:
        aDirFinal = aDir
      finalPath = path.join(aDirFinal, fileName); # final form of the path to the file
      # write plot commands buffer:
      gracePlotCmd = gnuplotToGrace(plotCmd,plotIndex); # change from gnuplot format to grace format
      plotCmdTmp = fillAll(gracePlotCmd, fileIndex) # if "%d" is in plotCmd, then plotIndex is being used as argument
      plotBuffer.append("read block "+'"'+finalPath+'"'+"\n"+plotCmdTmp+"\n"+"s%d legend "%plotIndex);
      if len(labels) < fileIndex+1:
        legendBuffer.append(processDirTreeName(aDirFinal));
      else:
        legendBuffer.append(labels[fileIndex]);
      partialLabels.append(partialLabel);
      plotIndex = plotIndex + 1;
      fileIndex = fileIndex + 1;

  # process titles: find parameters in common
  if labels == []: titleCommon = intersect(legendBuffer)

  # write plot commands
  upperB = len(plotBuffer)
  for ii in range(upperB):
    if labels == []:
      outFile.write(plotBuffer[ii]+'"'+partialLabels[ii]+" "+" ".join(list(biSetDifference(legendBuffer[ii],titleCommon)))+'"'+"\n\n");
    else:
      outFile.write(plotBuffer[ii]+'"'+partialLabels[ii]+" "+legendBuffer[ii]+'"'+"\n\n");


  outFile.write("autoscale"); # note that thie comes before postCmd since postCmd could redefine world coordinates

  outFile.write("\n")
  if postCmds != "": outFile.write(postCmds);
  outFile.write("\n")

  # save and write to ps file
  outFile.write('saveall "' + plotFileName.split(".")[0]+'.agr" \n');
  outFile.write('print to "' + plotFileName.split(".")[0]+'.ps" \n');
  outFile.write("print \n");

  outFile.write("exit\n");

  outFile.close();
Пример #3
0
def generatePlotFileCore(fList,
                         plotFileName="plot.bat",
                         preCmds="",
                         postCmds="",
                         labels=[],
                         dirName="",
                         useRelativePath=True):
    """ Generate a grace batch file for all files in subdirectory with names
  given in fList with suggested title and labels according to the
  difference of names of the subdirectories.
  fList has the structure [[filename, plotCommand, partialLabel],...].
  This is the core function with most parameters.
  parameter is a long control string that will be written in the plt file.
  labels is a list of strings that will be used as labels with plotIndex
  (see code) if non-empty."""
    # partial label is a label that is shared by all files with the same "filename"
    # "labels" list holds labels that are different among files with the same "filename"

    if dirName == "": dirName = getcwd()

    outFile = open(path.join(dirName, plotFileName), "w")
    # write header
    outFile.write("# Grace batch file generated by generateGraceBatchFile\n")
    outFile.write("#              generated on " +
                  strftime("%Y-%m-%d@%H:%M:%S") + "\n\n\n")

    outFile.write("\n")
    if preCmds != "": outFile.write(preCmds)
    outFile.write("\n")

    plotBuffer = []
    # store those plot commands without title content
    legendBuffer = []
    # store all the raw titles, only the difference between them should go into the plot file
    partialLabels = []
    # store all partial labels

    plotIndex = 0
    # unique index that separate graphs apart
    # declare files to be plotted
    for fileName, plotCmd, partialLabel in fList:
        fileIndex = 0
        # used for indexing labels
        for aDir in dirR.listNestedDirContainsFiles(dirName, fileName):
            if useRelativePath == True:  # final form of "path" can be relative to cwd or absolute
                aDirFinal = aDir.replace(dirName, ".")
            else:
                aDirFinal = aDir
            finalPath = path.join(aDirFinal, fileName)
            # final form of the path to the file
            # write plot commands buffer:
            gracePlotCmd = gnuplotToGrace(plotCmd, plotIndex)
            # change from gnuplot format to grace format
            plotCmdTmp = fillAll(
                gracePlotCmd, fileIndex
            )  # if "%d" is in plotCmd, then plotIndex is being used as argument
            plotBuffer.append("read block " + '"' + finalPath + '"' + "\n" +
                              plotCmdTmp + "\n" + "s%d legend " % plotIndex)
            if len(labels) < fileIndex + 1:
                legendBuffer.append(processDirTreeName(aDirFinal))
            else:
                legendBuffer.append(labels[fileIndex])
            partialLabels.append(partialLabel)
            plotIndex = plotIndex + 1
            fileIndex = fileIndex + 1

    # process titles: find parameters in common
    if labels == []: titleCommon = intersect(legendBuffer)

    # write plot commands
    upperB = len(plotBuffer)
    for ii in range(upperB):
        if labels == []:
            outFile.write(
                plotBuffer[ii] + '"' + partialLabels[ii] + " " + " ".join(
                    list(biSetDifference(legendBuffer[ii], titleCommon))) +
                '"' + "\n\n")
        else:
            outFile.write(plotBuffer[ii] + '"' + partialLabels[ii] + " " +
                          legendBuffer[ii] + '"' + "\n\n")

    outFile.write("autoscale")
    # note that thie comes before postCmd since postCmd could redefine world coordinates

    outFile.write("\n")
    if postCmds != "": outFile.write(postCmds)
    outFile.write("\n")

    # save and write to ps file
    outFile.write('saveall "' + plotFileName.split(".")[0] + '.agr" \n')
    outFile.write('print to "' + plotFileName.split(".")[0] + '.ps" \n')
    outFile.write("print \n")

    outFile.write("exit\n")

    outFile.close()