def open_dataset(source: os.PathLike, **kwargs) -> xr.Dataset: logger.info("extracting dem from %s\n", source) if isinstance(source, pathlib.Path): source = source.as_posix() if source.lower().startswith("http"): # URL kwargs.update({"engine": "pydap"}) dataset = xr.open_dataset(source, **kwargs) elif source.lower().endswith("tif"): # GeoTiff data_array = xr.open_rasterio(source, parse_coordinates=True, **kwargs) dataset = data_array.to_dataset( name="elevation").squeeze().reset_coords(drop=True) else: # NetCDF dataset = xr.open_dataset(source, **kwargs) return dataset
def open_dataset(source: os.PathLike, **kwargs) -> xr.Dataset: """ A wrapper around `xr.open_dataset` that supports multiple engines """ logger.info("extracting dem from %s\n", source) if isinstance(source, pathlib.Path): source = source.as_posix() if source.lower().endswith("tif"): # GeoTiff data_array = rioxarray.open_rasterio(source, parse_coordinates=True, **kwargs) dataset = data_array.to_dataset( name="elevation").squeeze().reset_coords(drop=True) else: if source.lower().startswith("http"): # URL engine = "pydap" else: engine = "netcdf4" logger.debug("Engine: %s", engine) dataset = xr.open_dataset(source, engine=engine, **kwargs) return dataset
def run_clingo(program: Union[str, TextIOBase], clingo: os.PathLike='clingo', models: int=1, args: Sequence[str]=[], timeout: float = None) -> Dict: is_asprin = clingo.lower().endswith('asprin') clingo_cmd = [clingo] if models != 1: clingo_cmd += ['--models', str(models)] if timeout is not None and not is_asprin: clingo_cmd.append('--time-limit={}'.format(int(timeout))) if is_asprin: # remove arguments not supported by asprin def valid(arg: str) -> bool: return not arg.startswith('--outf=') clingo_cmd += [str(a) for a in args if valid(a)] else: clingo_cmd += [str(a) for a in args] logging.info('running: ' + ' '.join(shlex.quote(str(a)) for a in clingo_cmd)) hard_timeout = timeout * 1.2 if timeout is not None else None timedout = False try: if isinstance(program, TextIOBase): cp = subprocess.run(clingo_cmd, timeout=hard_timeout, capture_output=True, text=True, stdin=program) else: cp = subprocess.run(clingo_cmd, timeout=hard_timeout, capture_output=True, text=True, input=program) except subprocess.TimeoutExpired as e: logging.warning('Clingo process timed out') return { 'cmd': e.cmd, 'timeout': e.timeout, 'stdout': e.stdout.decode(), 'stderr': e.stderr.decode() } try: clingo_out = json.loads(cp.stdout) except json.JSONDecodeError as e: logging.info(f'Error parsing clingo JSON output: {e}') clingo_out = {'stdout': cp.stdout} clingo_out['stderr'] = cp.stderr clingo_out['cmd'] = cp.args clingo_out['returnstatus'] = cp.returncode return clingo_out