pygcam.mcs.util

API

pygcam.mcs.util.activeYears(asInt=False)

Convert a string identifying active years into a list of ints or strs. Values must be comma-separated integers or expressions of the form xxxx-yyyy or xxxx-yyyy:z. The two expressions indicate ranges of years, with a default timestep of 5 years. If given, the final value after the colon indicates an alternative timestep.

pygcam.mcs.util.copyfiles(files, dstdir, removeDst=True)
Parameters:
  • files – a list of files to copy
  • dstdir – the directory to copy to
  • removeDst – if True-like, remove destination file before copying
Returns:

nothing

pygcam.mcs.util.dirFromNumber(n, prefix='', create=False)

Compute a directory name using a 2-level directory structure that allows 1000 nodes at each level, accommodating up to 1 million files (0 to 999,999) in two levels.

pygcam.mcs.util.fileReader(filename, error=<class 'pygcam.mcs.error.BaseSpecError'>, fileExtension=None, split=True)

Generator for reading a file line by line.

error is an exception class that subclasses BaseSpecError. lineNum is changed automatically as the generator reads the file to ensure that any error thrown has the correct line number printed.

fileExtension is the expected extension of the file. If the extension doesn’t match, an exception is thrown.

split is a boolean indicating whether the line should be tokenized or not.

Returns either a list of tokens in a line (if split is True) or a single string.

pygcam.mcs.util.filecopy(src, dst, removeDst=True)

Copy src file to dst, optionally removing dst first to avoid writing through symlinks

pygcam.mcs.util.findParamData(paramList, name)

Convenience method mostly used in testing. Finds a param from a given list of Parameter instances with given name.

pygcam.mcs.util.getOptionalArgs(func)

Inspects a function to find the names of optional arguments. Used in distrogen to generate distrogens from a single function.

func is a function object. Returns a tuple of strings of the optional argument names.

pygcam.mcs.util.getRunQueryDir()

Returns the path to sim’s copy of the scenarios.xml file.

pygcam.mcs.util.getSimLocalXmlDir(simId)

Returns the path to sim’s local-xml dir.

pygcam.mcs.util.getSimParameterFile(simId)

Returns the path to sim’s copy of the parameters.xml file.

pygcam.mcs.util.getSimResultFile(simId)

Returns the path to sim’s copy of the results.xml file.

pygcam.mcs.util.getSimScenarioFile(simId)

Returns the path to sim’s copy of the scenarios.xml file.

pygcam.mcs.util.getSimXmlFile(simId, filename)

Returns the path to a file in the sim’s XML dir, e.g., {simDir}/app-xml/foo.xml

Recursively hard link all files in the source directory src_dir in the destination directory dst_dir. Subdirectories are created in the same relative location in dst_dir, and all files are linked.

Parameters:
  • src_dir – (str or pathlib.Path) the directory to create links to
  • dst_dir – (str or pathlib.Path) the directory in which to create links
Returns:

none

pygcam.mcs.util.loadModuleFromPath(modulePath)

The load a module from a ‘.py’ or ‘.pyc’ file from a path that ends in the module name, i.e., from “foo/bar/Baz.py”, the module name is ‘Baz’.

pygcam.mcs.util.loadObjectFromPath(objName, modulePath, required=True)

Load a module and return a reference to a named object in that module. If ‘required’ and the object is not found, an error is raised, otherwise, None is returned if the object is not found.

pygcam.mcs.util.parseTrialString(string)

Converts a comma-separated list of ranges into a list of numbers. Ex. 1,3,4-6,2 becomes [1,3,4,5,6,2]. Duplicates are deleted.

pygcam.mcs.util.readTrialDataFile(simId)

Load trial data (e.g., saved by writeTrialDataFile) and return a DataFrame

pygcam.mcs.util.stripYearPrefix(s)

If s is of the form y + 4 digits, remove the y and convert the rest to integer

pygcam.mcs.util.tail(filename, count)

Return the last count lines from a text file by running the tail command as a subprocess. (Faster than native Python…)

Parameters:
  • filename – a filename
  • count – number of lines to read from end of file
Returns:

a list of the lines read

pygcam.mcs.util.writeTrialDataFile(simId, df)

Save the trial DataFrame in the file ‘trialData.csv’ in the simDir.