示例#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()
def generatePlotFile(fList,
                     plotFileName="plot.bat",
                     title="",
                     xlabel="",
                     ylabel="",
                     addons1=[],
                     addons2=[],
                     labels=[],
                     dirName="",
                     useRelativePath=True):
    """
  Generate a gnuplot 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, column used as x, column used as y, partialLabel],...],
  or [[filename, plotCommand, partialLable],...] (standard).
  plotCommand is written in the standard of gnuplot.
  Argument addons is of the form [string of commands, string of commands, ...].
  If forPaper is True, no title is generated, and labels will use 20 fontsize. Parameter
  forPaper can also be given a number, in which case it will be interpreted as fontsize.
  """

    # convert to standard format (each sublist has length 2):
    if len(fList[0]) == 4:  # format 5
        fListM = map(
            lambda x:
            [x[0], "($" + str(x[1]) + "):($" + str(x[2]) + ")", x[3]], fList)
    elif len(fList[0]) == 3:  # format 3 (standard)
        fListM = fList
    else:
        print "generatePlotFile: Argument fList has wrong structure."
        return

    preCmds = ""

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

    if xlabel == "": xlabel = 'r (fm)'
    if ylabel == "": ylabel = 'tau (fm/c)'
    xset = 'xaxis label ' + '"' + xlabel + '"'
    yset = 'yaxis label ' + '"' + ylabel + '"'

    if title == "": title = " ".join(stringizeL(fList))
    titleSet = 'title ' + '"' + title + '"'
    preCmds = preCmds + "timestamp on\n"

    preCmds = preCmds + titleSet + "\n" + xset + "\n" + yset + "\n"
    preCmds = preCmds + "\n".join(addons1) + "\n"
    postCmds = "\n".join(addons2) + "\n"

    generatePlotFileCore(fListM, plotFileName, preCmds, postCmds, labels,
                         dirName, useRelativePath)
def generatePlotFile(fList, plotFileName="plot.bat", title="", xlabel="", ylabel="", addons1=[], addons2=[], labels=[], dirName="", useRelativePath=True):
  """
  Generate a gnuplot 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, column used as x, column used as y, partialLabel],...],
  or [[filename, plotCommand, partialLable],...] (standard).
  plotCommand is written in the standard of gnuplot.
  Argument addons is of the form [string of commands, string of commands, ...].
  If forPaper is True, no title is generated, and labels will use 20 fontsize. Parameter
  forPaper can also be given a number, in which case it will be interpreted as fontsize.
  """

  # convert to standard format (each sublist has length 2):
  if len(fList[0]) == 4: # format 5
    fListM = map(lambda x:[x[0],"($"+str(x[1])+"):($"+str(x[2])+")",x[3]], fList);
  elif len(fList[0]) == 3: # format 3 (standard)
    fListM = fList;
  else:
    print "generatePlotFile: Argument fList has wrong structure."
    return;

  preCmds = "";

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

  if xlabel == "": xlabel='r (fm)'
  if ylabel == "": ylabel='tau (fm/c)';
  xset = 'xaxis label ' + '"' + xlabel + '"';
  yset = 'yaxis label ' + '"' + ylabel + '"';

  if title == "": title = " ".join(stringizeL(fList))
  titleSet = 'title ' + '"' + title + '"';
  preCmds = preCmds + "timestamp on\n";

  preCmds = preCmds + titleSet + "\n" + xset + "\n" + yset + "\n"
  preCmds = preCmds + "\n".join(addons1) + "\n"
  postCmds = "\n".join(addons2) + "\n"

  generatePlotFileCore(fListM, plotFileName, preCmds, postCmds, labels, dirName, useRelativePath)