def shockedFuel(policy): 'Guess the fuel type from the policy name.' import re policy = policy.lower() if policy.startswith('corn'): return TECH_CORN_ETHANOL if re.match(r'^(sugar|cane)', policy): return TECH_SUGARCANE_ETHANOL if re.match(r'^(cell|biomass|residue|stover|switchgrass|willow|miscan)', policy): return TECH_CELLULOSIC_ETHANOL if re.match(r'^(biod|soy|canola|jatr)', policy): return TECH_BIODIESEL if re.match(r'^(FT|ft|fischer)', policy): return TECH_FT_BIOFUELS # 'grass': 'EnergyGrass cellulosic ethanol', # 'palm': 'palm biodiesel', # 'wood': 'FT WoodyCrops', raise PygcamException("Can't infer shocked fuel fuel from policy name '%s'" % policy)
def valueForRegion(self, paramName, region, default=None, raiseError=True): regionMap = self.values(region) if regionMap is None: if raiseError: raise PygcamException('Region "%s" is not defined in %s' % (region, self.filename)) return None return regionMap.get(paramName, default)
def islinkWindows2(path): if not os.path.lexists(path): return False result = GetFileAttributesW(path) if result == INVALID_FILE_ATTRIBUTES: raise PygcamException("Can't get file attributes for '%s': %s'" % (path, ctypes.WinError())) return bool(result & FILE_ATTRIBUTE_REPARSE_POINT)
def calc_carbon_intensity(self): diffs = self.diffDFs normedDF = self.normedDF # Sum just the LUC CO2 emissions lucCO2 = self.sumDiffs(normedDF.query("sector == '{}'".format(LAND_USE_CHANGE))) nonCO2Diffs = self.diffDFs[Q_GHG_EMISSIONS] kyotoCO2e = self.sumDiffs(normedDF) _logger.debug(" Total GHG: {:8.2f} Tg CO2e (AR5 GWPs)".format(kyotoCO2e)) _logger.debug(" LUC CO2: {:8.2f} Tg CO2e".format(lucCO2)) _logger.debug('') refinedLiquids = diffs[Q_REFINED_LIQUIDS] diffsUSA = refinedLiquids.query('region in {}'.format(RegionsUSA)) diffsROW = refinedLiquids.query('region not in {}'.format(RegionsUSA)) shocked = self.shocked[0] if len(self.shocked) else None if shocked: yearCols = yearColumns(refinedLiquids) # TBD: Review whether to keep this if shocked == TECH_CORN_ETHANOL: df = diffsUSA.query('output == "ethanol" and subsector == "corn ethanol" and technology in ("corn ethanol", "corn ethanol (no constraint)")') cornOil = diffsUSA.query('output == "regional biomassOil" and subsector == "corn ethanol" and technology in ("corn ethanol", "corn ethanol (no constraint)")') if len(cornOil) > 0: # divide total corn oil by conversion coefficient to compute resulting biodiesel in each timestep cornOilBD = cornOil[yearCols] / BioOilToBiodieselCoef df = df[yearCols].append(cornOilBD) finalFuel = CORN_BIOFUELS else: finalFuel = ETOH elif shocked == TECH_BIODIESEL: df = diffsUSA.query('output == "refining" and subsector == "biomass liquids" and technology == "%s"' % shocked) finalFuel = FAME else: raise PygcamException('Unrecognized fuel shock: %s', shocked) self.fuelEJ = fuelEJ = self.sumDiffs(df) _logger.debug(" Shock size: {:.2f} EJ {}".format(fuelEJ, self.shocked)) self.save_carbon_intensity(finalFuel, lucCO2, kyotoCO2e) self.save_rebound_metrics(diffsUSA, diffsROW) if self.fuelShockFile: with open(self.fuelShockFile, 'w') as f: f.write("{:.3f}\n" % fuelEJ)
def verifyResultFiles(self, scenario): def resultFileDoesntExist(queryName): filename = self.queryPathname(queryName) path = os.path.join(self.diffsDir, filename) _logger.debug("Checking for '%s'", path) return (None if os.path.lexists(path) else path) # find missing files, if any names = list(filter(resultFileDoesntExist, self.queryList)) if names: raise PygcamException("Query result files are missing in %s for %s scenario:\n %s" % \ (self.diffsDir, scenario, "\n ".join(names)))
def symlinkWindows(src, dst): src = src.replace('/', '\\') dst = dst.replace('/', '\\') if samefileWindows(src, dst): raise PygcamException( "Attempted to create symlink loop from '%s' to '%s' (same file)" % (dst, src)) csl = ctypes.windll.kernel32.CreateSymbolicLinkW csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32) csl.restype = ctypes.c_ubyte # links to files and dir differ in Windows flags = 1 if os.path.isdir(src) else 0 try: if csl(dst, src, flags) == 0: raise ctypes.WinError() except Exception as e: from pygcam.log import getLogger _logger = getLogger(__name__) if getParamAsBoolean('GCAM.SymlinkWarning'): _logger.error(''' ============================================================================================================ WARNING: The current user does not have permission to create symbolic links, forcing pygcam to copy rather than symlink files. This uses much more file space than using symlinks, but it works. To use pygcam more efficiently, ask your System Administrator to give you permission to Create Symbol Links (using gpedit.msc) See http://superuser.com/questions/104845/permission-to-make-symbolic-links-in-windows-7 for more info. Set "GCAM.SymlinkWarning = False" in ~/.pygcam.cfg to suppress this message. ============================================================================================================ ''') raise PygcamException("Failed to create symlink '%s' to '%s': %s" % (dst, src, e))
def removeSymlink(path): """ On Windows, symlinks to directories must be removed with os.rmdir(), while symlinks to files must be removed with os.remove() """ if not os.path.lexists(path): return if not os.path.islink(path): raise PygcamException("removeSymlink: path '%s' is not a symlink" % path) if IsWindows and os.path.isdir(path): os.rmdir(path) else: os.remove(path)
def run(self, args, tool): # If not passed on command-line, read from config file yearsStr = args.years or getParam('GCAM.Years') years = [int(s) for s in yearsStr.split('-')] if len(years) != 2: raise PygcamException('''Years must be specified as XXXX-YYYY, where XXXX and YYYY are the first and last years to consider, respectively''') firstYear, lastYear = years guess = shockedFuel(args.policy) _logger.info("Inferred shocked fuel '%s' from policy '%s'", guess, args.policy) shocked = [guess] GcamResultProcessor(args.baseline, args.policy, args.diffsDir, shocked, args.fuelShockFile, firstYear=firstYear, lastYear=lastYear)
def get_read_handle(filename): if os.path.isdir(filename): dwFlagsAndAttributes = win32file.FILE_FLAG_BACKUP_SEMANTICS else: dwFlagsAndAttributes = 0 # CreateFile(fileName, desiredAccess, shareMode, attributes, # CreationDisposition, flagsAndAttributes, hTemplateFile) try: handle = win32file.CreateFileW( filename, # with 'W' accepts unicode win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, dwFlagsAndAttributes, None) return handle except Exception as e: raise PygcamException("get_read_handle(%s) failed: %s" % (filename, e))