def __init__(self, subSuite=".", *, suitePath=None): # set the top-level suite path and remember the skirt path self._suitePath = ut.absPath( suitePath ) if suitePath is not None else ut.projectParentPath() / "Functional9" # find all matching sub-suite paths if subSuite is None or subSuite == "" or "." in subSuite: subSuitePaths = [self._suitePath] else: subSuitePaths = self._suitePath.rglob(subSuite) # find all valid test cases in any of the sub-suite paths and remember the paths to the corresponding ski files skiPathSet = set() for subSuitePath in subSuitePaths: for skiPath in subSuitePath.rglob("[!.]*.ski"): if len(list(skiPath.parent.glob("[!.]*.ski"))) == 1: skiPathSet.add(skiPath) self._skiPaths = sorted(skiPathSet) # abort if there are no valid test cases if len(self._skiPaths) == 0: raise ut.UserError( "No valid test cases found for sub-suite specification: '{}'". format(subSuite))
def instrumentWithName(sim, instrname): instrlist = [ instr for instr in sim.instruments() if instr.name() == instrname ] if len(instrlist) == 0: raise ut.UserError( "simulation '{}' does not have instrument '{}'".format( sim.prefix(), instrname)) return instrlist[0]
def findSubDirectory(rootPath, subDirectory): # if the name of the subdirectory is empty, simply return the root directory if not subDirectory: return rootPath # recursively walk all subdirectories of the parent directory; # the path of the first subdirectory with a name matching the specified name is returned for dirpath, dirnames, filenames in os.walk(rootPath): for dirname in dirnames: # compare the directory name itself, and the directory name prefixed with the name of its parent directory, # to the target name (to allow search strings like "SED/Sun") if (dirname.lower() == subDirectory.lower()) or \ (os.path.join(os.path.basename(dirpath), dirname).lower() == subDirectory.lower()): return os.path.join(dirpath, dirname) # if no match is found, raise an error raise ut.UserError("{} not found in {}".format(subDirectory, rootPath))
def do(infilepath: (str, "filepath pattern of the FSPS files to be converted"), outfilepath: (str, "filepath of the resulting stored table file") ) -> "Convert FSPS-generated SED family to stored table format": import glob import pts.utils as ut from pts.storedtable.convert_sed import convertFSPSSEDFamily as convertFSPSSEDFamily infilepath = str(ut.absPath(infilepath)) if len(glob.glob(infilepath)) == 0: raise ut.UserError( "No input files found for pattern: '{}'".format(infilepath)) outfilepath = ut.absPath(outfilepath) if (outfilepath.is_dir()): outfilepath = ut.savePath(defFilePath="CustomFSPSSEDFamily", outDirPath=outfilepath, suffix=".stab") else: outfilepath = ut.savePath(defFilePath=None, outFilePath=outfilepath, suffix=".stab") convertFSPSSEDFamily([infilepath], [outfilepath])
def do( derived: (int, "backup derived data (specify zero to skip)") = 1, repos: (int, "backup repositories (specify zero to skip)") = 1, name: (str, "backup only project subdirectories starting with this name") = "", ) -> "create backup archives for the SKIRT/PTS parent project": import logging import shutil import subprocess import zipfile import pts.utils as ut # get the path to the project parent directory projectdir = ut.projectParentPath() # create the backup directory backupdir = projectdir / "Backup" / ("Backup" + "--" + ut.timestamp()) logging.info("Creating backup directory: {!s}".format(backupdir)) backupdir.mkdir() # open the backup instruction file and read its non-comment lines with open(projectdir / "Backup" / "create_backup.txt") as insfile: linecount = 0 for line in insfile: if len(line.strip()) > 0 and not line.strip().startswith('#'): # split the line in tokens tokens = line.split() if len(tokens) != 2 and len(tokens) != 4: raise ut.UserError( "Backup instruction line has {} tokens rather than 2 or 4" .format(len(tokens))) datatype = tokens[0] if not datatype in ["original", "derived", "repository"]: raise ut.UserError( "Unsupported backup instruction data characterization: '{}'" .format(datatype)) sourcedir = tokens[1] # skip source directories that don't match the specified name if len(name) > 0 and not sourcedir.lower().startswith( name.lower()): continue # for original and derived, create regular backup archive if (datatype == "original") or (datatype == "derived" and derived != 0): # get exclude specification exclude = "" if len(tokens) == 4: if tokens[2] != "exclude": raise ut.UserError("Expected 'exclude' in backup instruction line, not '{}'" \ .format(tokens[2])) exclude = tokens[3] # create the archive logging.info("Creating backup for {} data {}...".format( datatype, sourcedir)) linecount += 1 zipname = backupdir / "{:02d}-{}.zip".format( linecount, sourcedir.replace("/", "-").lower()) with zipfile.ZipFile(zipname, mode='w', compression=zipfile.ZIP_DEFLATED, compresslevel=6) as ziparchive: # include all eligible files for source in (projectdir / sourcedir).rglob("*"): excluded = source.name.startswith(".") if len(exclude) > 0: excluded |= any([ source.match(excl) for excl in exclude.split(";") ]) if not excluded: logging.info(" Including {}".format(source)) ziparchive.write( source, arcname=source.relative_to(projectdir)) # for repository, clone upstream repository and create archive if datatype == "repository" and repos != 0: # get the upstream repository URL gitout = subprocess.run( ("git", "--git-dir", projectdir / sourcedir / ".git", "remote", "-v"), capture_output=True, text=True, check=True).stdout upstream = "" for line in gitout.splitlines(): if line.startswith("origin") and line.endswith( "(fetch)"): upstream = line.split()[1] if len(upstream) == 0: raise ValueError( "Cannot get upstream repository URL for {}".format( sourcedir)) # clone a bare copy of the repository linecount += 1 barename = backupdir / "{:02d}-{}-repo-bare".format( linecount, sourcedir.replace("/", "-").lower()) gitout = subprocess.run( ("git", "--git-dir", projectdir / sourcedir / ".git", "clone", "--bare", upstream, barename), check=True).stdout # create the archive logging.info("Creating backup for repository {}...".format( sourcedir)) zipname = backupdir / "{:02d}-{}-repo.zip".format( linecount, sourcedir.replace("/", "-").lower()) with zipfile.ZipFile(zipname, mode='w', compression=zipfile.ZIP_DEFLATED, compresslevel=6) as ziparchive: # include the contents of the bare repository for source in barename.rglob("*"): logging.info(" Including {}".format(source)) ziparchive.write(source, arcname=source.relative_to( barename.parent)) # remove the temporary bare repository shutil.rmtree(barename, ignore_errors=True)
def do( simDirPath: (str, "SKIRT simulation output directory"), prefix: (str, "SKIRT simulation prefix") = "", type: (str, "type of SKIRT instrument output files to be handled") = "total", name: (str, "name segment that will be added to the image file names") = "", colors: (str, "three comma-separated wavelength values or broadband names defining the R,G,B colors" ) = "", ) -> "create RGB images for surface brightness maps generated by SKIRT instruments": import pts.band as bnd import pts.simulation as sm import pts.utils as ut import pts.visual as vis # get the simulations to be handled sims = sm.createSimulations(simDirPath, prefix if len(prefix) > 0 else None) # parse the colors and handle accordingly # no colors given if len(colors) == 0: if len(name) > 0: raise ut.UserError( "name argument is not supported when colors are not specified") for sim in sims: vis.makeRGBImages(sim, fileType=type) return # get segments segments = colors.split(',') if len(segments) != 3: raise ut.UserError( "colors argument must have three comma-separated segments") # try wavelengths try: wavelengths = [float(segment) for segment in segments] except ValueError: wavelengths = None if wavelengths is not None: tuples = {name: wavelengths << sm.unit("micron")} for sim in sims: vis.makeRGBImages(sim, wavelengthTuples=tuples, fileType=type) return # try bands try: bands = [bnd.BroadBand(segment) for segment in segments] except ValueError: bands = None if bands is not None: contributions = [(bands[0], 1, 0, 0), (bands[1], 0, 1, 0), (bands[1], 0, 0, 1)] for sim in sims: vis.makeConvolvedRGBImages(sim, contributions=contributions, fileType=type, name=name) return raise ut.UserError( "colors argument must specify three wavelengths in micron or three broadband names" )
parser.add_argument("--galaxy") # name of galaxy args = parser.parse_args() galaxy = args.galaxy colors = "SDSS_Z,SDSS_R,SDSS_G" #colors = "PACS_100,SDSS_R,GALEX_NUV" name = "custom_image" type = "total" path = '/scratch/ntf229/RT_fit/resources/SKIRT/'+galaxy+'/maxLevel13/wavelengths601/numPhotons1e9/inc0/dust/dustFraction0.2/maxTemp16000/' os.chdir(path) # get the simulations to be handled sims = sm.createSimulations(path, None) # get segments segments = colors.split(',') if len(segments) != 3: raise ut.UserError("colors argument must have three comma-separated segments") bands = [ bnd.BroadBand(segment) for segment in segments ] #contributions = [ (bands[0], 1, 0, 0), (bands[1], 0, 1, 0), (bands[1], 0, 0, 1) ] # original contributions = [ (bands[0], 1, 0, 0), (bands[1], 0, 1, 0), (bands[2], 0, 0, 1) ] # altered from original for sim in sims: #vis.makeConvolvedRGBImages(sim, contributions=contributions, fileType=type, name=name, decades=6) makeConvolvedRGBImages(sim, contributions=contributions, fileType=type, name=name, decades=6) os.system("mv sph_i00_total_custom_image.png "+image_path+galaxy+"_image.png") print('made it to the end')