Пример #1
0
def read_cfgrib(filename, **kwargs):
    """ Read grib files using cfgrib

    Args:
        filename:
        **kwargs:

    Returns:

    """
    import xarray as xr
    try:
        import cfgrib
        import cf2cdm

    except ImportError:
        print("requires eccodes library, and xarray")
        raise ImportError()

    kwargs['engine'] = 'cfgrib'
    kwargs['backend_kwargs'] = kwargs.get('backend_kwargs', {})
    if 'indexpath' not in kwargs['backend_kwargs']:
        kwargs['backend_kwargs'].update({'indexpath': ''})  # no indexfiles

    data = xr.open_dataset(filename, **kwargs)
    return cf2cdm.translate_coords(data, cf2cdm.CDS)
Пример #2
0
def dump(inpaths, variable, cdm, engine):
    # type: (T.List[str], str, str, str) -> None
    import xarray as xr

    import cf2cdm

    # NOTE: noop if no input argument
    if len(inpaths) == 0:
        return

    if len(inpaths) == 1:
        # avoid to depend on dask when passing only one file
        ds = xr.open_dataset(inpaths[0], engine=engine)  # type: ignore
    else:
        ds = xr.open_mfdataset(inpaths, engine=engine,
                               combine="by_coords")  # type: ignore

    if cdm:
        coord_model = getattr(cf2cdm, cdm)
        ds = cf2cdm.translate_coords(ds, coord_model=coord_model)

    if variable:
        ds_or_da = ds[variable]
    else:
        ds_or_da = ds

    print(ds_or_da)
Пример #3
0
def to_netcdf(inpaths, outpath, cdm, engine):
    import cf2cdm
    import xarray as xr

    # NOTE: noop if no input argument
    if len(inpaths) == 0:
        return

    if not outpath:
        outpath = os.path.splitext(inpaths[0])[0] + '.nc'

    ds = xr.open_mfdataset(inpaths, engine=engine)
    if cdm:
        coord_model = getattr(cf2cdm, cdm)
        ds = cf2cdm.translate_coords(ds, coord_model=coord_model)
    ds.to_netcdf(outpath)
Пример #4
0
def to_netcdf(inpaths, outpath, cdm, engine, backend_kwargs_json):
    # type: (T.List[str], str, str, str, str) -> None
    import json

    import xarray as xr

    import cf2cdm

    # NOTE: noop if no input argument
    if len(inpaths) == 0:
        return

    if not outpath:
        outpath = os.path.splitext(inpaths[0])[0] + ".nc"

    if backend_kwargs_json is not None:
        try:
            # Assume a json format string
            backend_kwargs = json.loads(backend_kwargs_json)
        except json.JSONDecodeError:
            # Then a json file
            with open(backend_kwargs_json, "r") as f:
                backend_kwargs = json.load(f)
    else:
        backend_kwargs = {}

    if len(inpaths) == 1:
        # avoid to depend on dask when passing only one file
        ds = xr.open_dataset(
            inpaths[0],
            engine=engine,
            backend_kwargs=backend_kwargs,
        )  # type: ignore
    else:
        ds = xr.open_mfdataset(
            inpaths,
            engine=engine,
            combine="by_coords",
            backend_kwargs=backend_kwargs,
        )  # type: ignore

    if cdm:
        coord_model = getattr(cf2cdm, cdm)
        ds = cf2cdm.translate_coords(ds, coord_model=coord_model)

    ds.to_netcdf(outpath)
Пример #5
0
def to_netcdf(inpaths, outpath, cdm, engine):
    # type: (T.List[str], str, str, str) -> None
    import xarray as xr

    import cf2cdm

    # NOTE: noop if no input argument
    if len(inpaths) == 0:
        return

    if not outpath:
        outpath = os.path.splitext(inpaths[0])[0] + ".nc"

    if len(inpaths) == 1:
        # avoid to depend on dask when passing only one file
        ds = xr.open_dataset(inpaths[0], engine=engine)  # type: ignore
    else:
        ds = xr.open_mfdataset(inpaths, engine=engine,
                               combine="by_coords")  # type: ignore
    if cdm:
        coord_model = getattr(cf2cdm, cdm)
        ds = cf2cdm.translate_coords(ds, coord_model=coord_model)
    ds.to_netcdf(outpath)
Пример #6
0
    def add(self, name, filename=None, directory=None, rename=None, cfunits=False, close=True, xwargs=None, **kwargs):
        """ Add dataset to radiosonde class object [container]

        Args:
            name (str): used as filename and/or as dataset name
            filename (str): filename to read from (netcdf)
            directory (str): directory of radiosonde store, default config rasodir
            rename (dict): rename variables
            cfunits (bool): apply cfunits
            xwargs (dict): xarray open_dataset keywords
        """
        if rename is None:
            rename = {}

        if xwargs is None:
            xwargs = {}

        from .. import config

        if self.directory is not None:
            directory = self.directory

        if 'engine' not in xwargs.keys():
            xwargs['engine'] = 'h5netcdf'

        if directory is None:
            if 'rasodir' in config and os.path.isdir(config.rasodir):
                directory = config.rasodir + '/' + str(self.ident) + '/'
            else:
                directory = './' + str(self.ident) + '/'
            message(directory, **kwargs)

        if filename is not None:
            if '.nc' not in filename:
                print("Warning can read only NetCDF / Xarray open_dataset formats")

            if '*' not in filename:
                if not os.path.isfile(filename):
                    if directory is not None:
                        if not os.path.isfile(directory + '/' + filename):
                            filename = directory + '/' + str(self.ident) + '/' + filename
                        else:
                            filename = directory + '/' + filename

        if filename is None:
            if os.path.isfile(directory + '/' + name + '.nc'):
                filename = directory + '/' + name + '.nc'
            else:
                message("Not found:", name, directory, **kwargs)
                return

        if '*' in filename:
            if 'combine' not in xwargs.keys():
                xwargs['combine'] = 'by_coords'

            try:
                message("Reading ...", filename, **kwargs)
                self.data[name] = xr.open_mfdataset(filename, **xwargs)
            except OSError:
                message("Reading ...", directory + '/' + filename, **kwargs)
                self.data[name] = xr.open_mfdataset(directory + '/' + filename, **xwargs)

        else:
            message("Reading ...", filename, **kwargs)
            self.data[name] = xr.open_dataset(filename, **xwargs)

        if close:
            # add this to make sure that a file is read and closed (some error with h5netcdf, h5py)
            if hasattr(self.data[name], 'close'):
                self.data[name].load()
                self.data[name].close()

        self.data[name] = self.data[name].rename(rename)

        if cfunits:
            import cf2cdm
            for i, j in self.data[name].coords.items():
                if str(j.dtype) == 'datetime64[ns]':
                    if 'standard_name' in j.attrs:
                        del j.attrs['standard_name']
            self.data[name] = cf2cdm.translate_coords(self.data[name], cf2cdm.CDS)

        # merge dictionaries and append common
        self.attrs.__dict__ = dict_add(vars(self.attrs), dict(self.data[name].attrs))
        if 'ident' in self.attrs:
            if self.ident != self.attrs['ident']:
                message("Warning different idents: ", self.ident, ">", self.attrs['ident'],
                        **update_kw('level', -1, **kwargs))
            if self.ident == "":
                self.ident = self.attrs['ident']