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.createTrialString(lst)

Assemble a list of numbers into a compact list using hyphens to identify ranges. This reverses the operation of parseTrialString()

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.

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.parseMcsDir(path, trialNum_only=False)

Parse a path to a trial and scenario’s “exe” directory to extract the trial number and scenario name. If the pathname doesn’t end in “exe”, assume it’s the parent (scenario) directory.

Parameters

path – (str) pathname of an “exe” directory for a trial and scenario

Returns

(tuple) (simId, trialNum, scenario) unless trialNum_only is True, in which case only the trial number is returned.

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. See also createTrialString() below.

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