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 matlab_pwd_changed(path: os.PathLike, oc: "oct2py.octave"): # type: ignore """ Temporarily change Matlab's Oct2Pyc session's working-dir to (and yield) given `path`. :return: yields the given path as a :class:`pathlib.Path` instance """ from pathlib import Path prev_cwd = Path.cwd().as_posix() path = Path(path) oc.cd(path.as_posix()) try: yield path finally: oc.cd(prev_cwd)
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 sys_path_append(o: os.PathLike) -> str: posix_path: str = o.as_posix() if isinstance(o, Path) else Path(o).as_posix() return 'sys.path.append("{}")'.format(posix_path)
def cast_path_to_str(path: os.PathLike) -> str: if isinstance(path, pathlib.Path): path = path.as_posix() return path