Example #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()
Example #2
0
def nestedRenameFiles(dir_path, old_filenames, new_filenames, silent_level=0):
    """
        Rename all files in "old_filenames" under "dir_path" to
        "new_filenames". The first file in "old_filenames" will be
        renamed to the first file in "new_filenames", and similar for
        the rest. If "leaf_only" is specified as "True" then only
        files in leaf subdirectories are modified. "silent_level"=0:
        no output on screen; 1: short output; 2: full output
    """
    old_filenames = listR.toList(old_filenames)
    new_filenames = listR.toList(new_filenames)
    for old_name, new_name in zip(old_filenames, new_filenames):
        dirL = dirR.listNestedDirContainsFiles(dir_path, old_name)
        for aDir in dirL:
            if path.exists(path.join(aDir, new_name)):
                print("File "+path.join(aDir, new_name)+" already exists! skipped.")
            else:
                os.rename(path.join(aDir, old_name), path.join(aDir, new_name))
                if silent_level==0:
                    pass
                elif silent_level==1:
                    print("File "+_relativePath(dir_path, full_path)
                          +" renamed to "+_relativePath(dir_path,new_full_path))
                else:
                    print("File "+full_path+" renamed to "+new_full_path)
Example #3
0
def takeRatioWithFirstLineForAll(dir_path, filename, columns=[2], newfilename=""):
    """ For all files under dir_path, apply takeRatioWithFirstLine.
    Note that columns are indexed from 1. """
    if newfilename=="": newfilename = filename;
    for aDir in dirR.listNestedDirContainsFiles(dir_path, filename):
        fullpath_in = path.join(aDir, filename);
        fullpath_out = path.join(aDir, newfilename);
        takeRatioWithFirstLine(fullpath_in, columns, fullpath_out);
Example #4
0
def nestedDeleteFiles(dir_path, filenames, silence_level=0, leaf_only=False):
    """
        Delete all files in the list "filenames" under "dir_path".
    """
    filenames = listR.toList(filenames)
    for name in filenames:
        dirL = dirR.listNestedDirContainsFiles(dir_path, name)
        for aDir in dirL:
            remove(path.join(aDir, name))
            if silence_level>0: print("File "+full_path+" deleted.")
Example #5
0
def collectFile(pathDir, filenames, targetDir=None):
  """ Collect files of name filenames under pathDir to the targetDir,
  then rename them according to the parameters.
  """
  filenames = listR.toList(filenames)
  if targetDir == None: targetDir=getcwd()
  for aDir in dirR.listNestedDirContainsFiles(pathDir, filenames):
    for aFile in filenames:
      paraList = readCSEFullpathD(aDir).items() # read directory structure
      toAdd="-" + ",".join(map(lambda x:"=".join(x), paraList)) # transform the directory strunction into a string
      changedName = path.splitext(aFile)[0] + toAdd + path.splitext(aFile)[1] # add the string to the name of the file
      copyFile(aFile, aDir, targetDir, changedName) # copy file
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();
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()