How to use the salem.wrftools.FakeVariable function in salem

To help you get started, we’ve selected a few salem examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fmaussion / salem / salem / wrftools.py View on Github external
def can_do(nc):
        return np.all([n in nc.variables for n in ['P', 'PB']])

    def __getitem__(self, item):

        with ScaledVar(self.nc.variables['P']) as p, \
                ScaledVar(self.nc.variables['PB']) as pb:
            res = p[item] + pb[item]
            if p.units == 'Pa':
                res /= 100
            elif p.units == 'hPa':
                pass
        return res


class GEOPOTENTIAL(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['PH'])
        self.units = 'm2 s-2'
        self.description = 'Full model geopotential'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['PH', 'PHB']])

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['PH']) as p, \
                ScaledVar(self.nc.variables['PHB']) as pb:
            return p[item] + pb[item]
github fmaussion / salem / salem / wrftools.py View on Github external
setattr(self, attr, getattr(ncvar, attr))
        self.dimensions = ncvar.dimensions
        self.dtype = ncvar.dtype
        self.set_auto_maskandscale = dummy_func
        self.set_auto_scale = dummy_func
        self.shape = ncvar.shape

    def getncattr(self, name):
        # dummy getncattrs
        return getattr(self, name)

    def __getitem__(self, item):
        raise NotImplementedError()


class T2C(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['T2'])
        self.units = 'C'
        self.description = '2m Temperature'

    @staticmethod
    def can_do(nc):
        return 'T2' in nc.variables

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['T2']) as var:
            return var[item] - 273.15


class AccumulatedVariable(FakeVariable):
github fmaussion / salem / salem / wrftools.py View on Github external
self.units = 'm s-1'
        self.description = 'Horizontal wind speed'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['U', 'V']])

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['U']) as var:
            ws = var[item]**2
        with ScaledVar(self.nc.variables['V']) as var:
            ws += var[item]**2
        return np.sqrt(ws)


class PRESSURE(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['P'])
        self.units = 'hPa'
        self.description = 'Full model pressure'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['P', 'PB']])

    def __getitem__(self, item):

        with ScaledVar(self.nc.variables['P']) as p, \
                ScaledVar(self.nc.variables['PB']) as pb:
            res = p[item] + pb[item]
            if p.units == 'Pa':
github fmaussion / salem / salem / wrftools.py View on Github external
return AccumulatedVariable.can_do(nc) and 'RAINNC' in nc.variables


class PRCP_C(AccumulatedVariable):

    def __init__(self, nc):
        AccumulatedVariable.__init__(self, nc, 'RAINC')
        self.units = 'mm h-1'
        self.description = 'Precipitation rate from cumulus physics'

    @staticmethod
    def can_do(nc):
        return AccumulatedVariable.can_do(nc) and 'RAINC' in nc.variables


class PRCP(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['RAINC'])
        self.units = 'mm h-1'
        self.description = 'Total precipitation rate'

    @staticmethod
    def can_do(nc):
        return AccumulatedVariable.can_do(nc) and \
               'RAINC' in nc.variables and 'RAINNC' in nc.variables

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['PRCP_NC']) as p1, \
                ScaledVar(self.nc.variables['PRCP_C']) as p2:
            return p1[item] + p2[item]
github fmaussion / salem / salem / wrftools.py View on Github external
FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['PH'])
        self.units = 'm2 s-2'
        self.description = 'Full model geopotential'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['PH', 'PHB']])

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['PH']) as p, \
                ScaledVar(self.nc.variables['PHB']) as pb:
            return p[item] + pb[item]


class Z(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['PH'])
        self.units = 'm'
        self.description = 'Full model height'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['PH', 'PHB']])

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['GEOPOTENTIAL']) as var:
            return var[item] / 9.81


class SLP(FakeVariable):
github fmaussion / salem / salem / wrftools.py View on Github external
def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['T'])
        self.units = 'K'
        self.description = 'Potential temperature'

    @staticmethod
    def can_do(nc):
        return 'T' in nc.variables

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['T']) as var:
            return var[item] + 300.


class TK(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['T'])
        self.units = 'K'
        self.description = 'Temperature'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['T', 'P', 'PB']])

    def __getitem__(self, item):
        p1000mb = 100000.
        r_d = 287.04
        cp = 7 * r_d / 2.
        with ScaledVar(self.nc.variables['T']) as var:
            t = var[item] + 300.
github fmaussion / salem / salem / wrftools.py View on Github external
def can_do(nc):
        return np.all([n in nc.variables for n in ['T', 'P', 'PB']])

    def __getitem__(self, item):
        p1000mb = 100000.
        r_d = 287.04
        cp = 7 * r_d / 2.
        with ScaledVar(self.nc.variables['T']) as var:
            t = var[item] + 300.
        with ScaledVar(self.nc.variables['P']) as p, \
                ScaledVar(self.nc.variables['PB']) as pb:
            p = p[item] + pb[item]
        return (p/p1000mb)**(r_d/cp) * t


class WS(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['U'])
        self.units = 'm s-1'
        self.description = 'Horizontal wind speed'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['U', 'V']])

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['U']) as var:
            ws = var[item]**2
        with ScaledVar(self.nc.variables['V']) as var:
            ws += var[item]**2
        return np.sqrt(ws)
github fmaussion / salem / salem / wrftools.py View on Github external
def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['PH'])
        self.units = 'm'
        self.description = 'Full model height'

    @staticmethod
    def can_do(nc):
        return np.all([n in nc.variables for n in ['PH', 'PHB']])

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['GEOPOTENTIAL']) as var:
            return var[item] / 9.81


class SLP(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['T2'])
        self.units = 'hPa'
        self.description = 'Sea level pressure'
        dims = list(nc.variables['T'].dimensions)
        self.ds = np.nonzero(['bottom_top' in d for d in dims])[0][0]
        self._ds_shape = nc.variables['T'].shape[self.ds]

    @staticmethod
    def can_do(nc):
        # t2 is for attrs (not elegant)
        need = ['T', 'P', 'PB', 'QVAPOR', 'PH', 'PHB', 'T2']
        return np.all([n in nc.variables for n in need])

    def __getitem__(self, item):
github fmaussion / salem / salem / wrftools.py View on Github external
self._copy_attrs_from(nc.variables['RAINC'])
        self.units = 'mm h-1'
        self.description = 'Total precipitation rate'

    @staticmethod
    def can_do(nc):
        return AccumulatedVariable.can_do(nc) and \
               'RAINC' in nc.variables and 'RAINNC' in nc.variables

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['PRCP_NC']) as p1, \
                ScaledVar(self.nc.variables['PRCP_C']) as p2:
            return p1[item] + p2[item]


class THETA(FakeVariable):
    def __init__(self, nc):
        FakeVariable.__init__(self, nc)
        self._copy_attrs_from(nc.variables['T'])
        self.units = 'K'
        self.description = 'Potential temperature'

    @staticmethod
    def can_do(nc):
        return 'T' in nc.variables

    def __getitem__(self, item):
        with ScaledVar(self.nc.variables['T']) as var:
            return var[item] + 300.


class TK(FakeVariable):