Ejemplo n.º 1
0
    def __init__(self, obj):
        def lonlat(lon, lat):
            from pyproj import Geod
            geo = Geod(ellps='WGS84')
            x = [np.min(lon), np.max(lon), np.min(lat), np.max(lat)]
            return {
                'lon_0': .5 * np.sum(x[:2]),
                'lat_0': .5 * np.sum(x[2:]),
                'width': geo.inv(x[0], x[3], x[1], x[3])[2],
                'height': geo.inv(x[0], x[2], x[0], x[3])[2],
                'lat_1': -10,
                'lat_2': -40
            }

        def attshw(file):
            h, w = file.variables['XLONG_M'].shape[-2:]
            return atts(file, w, h)

        # this doesn't work if one of the calls to try_list succeeds but returns the wrong
        # object for the init call to Basemap
        super().__init__(projection='lcc',
                         **hh.try_list(
                             obj, lambda x: lonlat(x['lon'], x['lat']),
                             lambda x: x.to_dict(), lambda x: hh.try_list(
                                 x,
                                 atts,
                                 attshw,
                                 lambda x: lonlat(hh.g2d(x.variables['XLONG']),
                                                  hh.g2d(x.variables['XLAT'])),
                             ), lambda x: lonlat(*x), lambda x: lonlat(
                                 (lambda a, b: b, a)(*x[1].latlons()))))
        try:
            self._lonlat = hh.lonlat(obj)
        except:
            pass
Ejemplo n.º 2
0
def itpl():
    "Interpolate 4D (true) temp and (mass level) geopotential fields to stations."
    nc = Dataset('../../data/WRF/2d/d02_2014-09-10_transf.nc')
    T = nc.variables['temp'][:]
    GP = nc.variables['ghgt'][:]

    ma = basemap(nc)
    x, y = ma.xy()
    ij = ma(*hh.lonlat(sta))
    t = hh.get_time(nc)

    Ti = interp4D((x, y), T, ij, sta.index, t, method='linear')
    Gi = interp4D((x, y), GP, ij, sta.index, t, method='linear')
Ejemplo n.º 3
0
def interp_grib(grb, stations, method='linear', transf=False):
    """
If transf=True, first project geographical coordinates to map, then use map coordinates
to interpolate using an unstructured array of points. Otherwise interpret lat/lon as regular
(enough) grid and use grid interpolation.
	"""
    x, t, lon, lat = hh.ungrib(grb)
    if transf:
        m = mp.basemap((lat, lon))
        return sp.irreg_interp(m(lon, lat),
                               x,
                               m(*hh.lonlat(stations)),
                               stations.index,
                               t,
                               method=method)
    else:
        lon -= 360
        return sp.grid_interp((lon, lat),
                              x,
                              hh.lonlat(stations),
                              stations.index,
                              t,
                              method=method)
Ejemplo n.º 4
0
def interp4D():
    nc = Dataset('data/wrf/d02_2014-09-10.nc')
    m = mp.basemap(nc)
    xy = m(*hh.lonlat(nc))
    ij = m(*hh.lonlat(sta))
    t = hh.get_time(nc) - np.timedelta64(4, 'h')

    z = sp.grid_interp(xy,
                       nc.variables['HGT'][:],
                       ij,
                       sta.index,
                       method='linear')
    TH2 = sp.grid_interp(xy,
                         nc.variables['TH2'][:],
                         ij,
                         sta.index,
                         t,
                         method='linear')
    TSK = sp.grid_interp(xy,
                         nc.variables['TSK'][:],
                         ij,
                         sta.index,
                         t,
                         method='linear')
    GP = sp.interp4D(xy, nc.variables['PH'][:], ij, sta.index, t, 'linear')
    GPB = sp.interp4D(xy, nc.variables['PHB'][:], ij, sta.index, t, 'linear')
    P = sp.interp4D(xy, nc.variables['P'][:], ij, sta.index, t, 'linear')
    PB = sp.interp4D(xy, nc.variables['PB'][:], ij, sta.index, t, 'linear')
    TH = sp.interp4D(xy, nc.variables['T'][:], ij, sta.index, t, 'linear')

    V = pd.HDFStore('data/d02_4D.h5')
    V['GP'] = GPB.add(GP)
    V['P'] = PB.add(P)
    V['T'] = TH
    V['TH2'] = TH2
    V['TSK'] = TSK
    V['z'] = z
Ejemplo n.º 5
0
 def __init__(self, nc, var='slp', timedelta='-4H'):
     self.map = basemap.Basemap(
         projection = 'merc',
         llcrnrlon = -180,
         llcrnrlat = -70,
         urcrnrlon = -60,
         urcrnrlat = 10
     )
     lon, lat = hh.lonlat(nc)
     self._i, self._j = self.map(lon-360, lat)
     slp = nc.variables[var][:]
     s = pd.DataFrame(slp.reshape((slp.shape[0], -1)))
     t = pd.DatetimeIndex(hh.get_time(nc) + pd.Timedelta(timedelta))
     self.slp = s.groupby(t.date).mean()
     self.t = pd.DatetimeIndex(self.slp.index)
Ejemplo n.º 6
0
def interp_nc(nc,
              var,
              stations,
              time=True,
              tz=False,
              method='linear',
              map=None):
    m = mp.basemap(nc) if map is None else map
    xy = m.xy()
    ij = m(*hh.lonlat(stations))
    x = nc.variables[var][:].squeeze()
    if time:
        t = pd.DatetimeIndex(hh.get_time(nc))
        if tz:
            t = t.tz_localize('UTC').tz_convert(hh.CEAZAMetTZ())
        else:
            t -= np.timedelta64(4, 'h')
        return sp.grid_interp(xy, x, ij, stations.index, t, method=method)
    else:
        return sp.grid_interp(xy, hh.g2d(x), ij, stations.index, method=method)
Ejemplo n.º 7
0
def interp2D(method='nearest', zmethod='nearest'):
    T0 = st.interp_grib(pygrib.open('data/fnl/T2.grib2'), sta, method=method)
    T0.sort_index(inplace=True)

    z, lat, lon = pygrib.open('data/fnl/oro.grb2')[1].data()
    lon -= 360
    # cut off +/- 90 latitude
    z0 = sp.grid_interp((lon[1:-1, :], lat[1:-1, :]),
                        z[1:-1, :],
                        hh.lonlat(sta),
                        sta.index,
                        method=zmethod)

    print('d01')
    g1 = Dataset('data/wrf/d01_T2_2014-09-10.nc')
    T1 = st.interp_nc(g1, 'T2', sta, method=method)
    z1 = st.interp_nc(g1, 'HGT', sta, time=False, method=zmethod)

    print('d02')
    g2 = Dataset('data/wrf/d02_2014-09-10.nc')
    T2 = st.interp_nc(g2, 'T2', sta, method=method)
    z2 = st.interp_nc(g2, 'HGT', sta, time=False, method=zmethod)

    print('d03')
    g3 = Dataset('data/orlando/T2.nc')
    T3 = st.interp_nc(g3, 'T2', sta, method=method)
    z3 = st.interp_nc(g3, 'HGT', sta, time=False, method=zmethod)

    Z = pd.DataFrame({'d01': z1, 'd02': z2, 'd03': z3, 'fnl': z0})
    V = pd.Panel.from_dict({
        'd01': T1,
        'd02': T2,
        'd03': T3,
        'fnl': T0
    },
                           orient='minor')
    return V, Z
Ejemplo n.º 8
0
from matplotlib.figure import SubplotParams

S = pd.HDFStore('../data/IGRA/IGRAraw.h5')
sta = S['sta']


def ts(x):
    # need to do it this way, since dropna above doesn't delete the index properly
    # https://github.com/pandas-dev/pandas/issues/2770
    return np.array(x.unstack().index, dtype='datetime64[h]')


nc = Dataset('../data/wrf/d02_2014-09-10_transf.nc')
ma = basemap(nc)
x, y = ma.xy()
ij = ma(*hh.lonlat(sta.iloc[:2]))
t = hh.get_time(nc)
T = nc.variables['temp'][:]
P = nc.variables['press'][:]

Ti = interp4D((x, y), T, ij, sta.iloc[:2].index, t, method='linear')
Pi = interp4D((x, y), P, ij, sta.iloc[:2].index, t, method='linear')

p = [1000, 925, 850, 700, 500, 400, 300, 250, 200, 150, 100, 70, 50]
pl = np.log(p) + np.log(100)


def ints(pl, x):
    try:
        return interp1d(np.log(x.index), x, 'linear',
                        bounds_error=False)(pl) * .1 + hh.K
Ejemplo n.º 9
0
# v = nc.variables['V'][:]
# w = nc.variables['W'][:]
# gp = (nc.variables['PHB']+nc.variables['PH'])/9.81
# 
# uT = .5 * u[:,:,1:-1,1:-1]*(T[:,:,1:-1,:-1]+T[:,:,1:-1,1:])
# vT = .5 * v[:,:,1:-1,1:-1]*(T[:,:,:-1,1:-1]+T[:,:,1:,1:-1])
# wT = .5 * w[:,1:-1,1:-1,1:-1]*(T[:,:-1,1:-1,1:-1]+T[:,1:,1:-1,1:-1])
# 
# ad = np.diff(uT,1,3)/10000 + np.diff(vT,1,2)/10000
# dw = np.diff(wT,1,1)/np.diff(gp[:,1:-1,1:-1,1:-1],1,1)


D = pd.HDFStore('../data/station_data.h5')
S = pd.HDFStore('../data/LinearLinear.h5')

T = hh.extract(D['ta_c'],'prom',1)
Tm = S['T2']['d02']
sta = D['sta']
b = Tm-T

nc = Dataset('../data/wrf/d02_2014-09-10.nc')
t = pd.DatetimeIndex(hh.get_time(nc)) - np.timedelta64(4,'h')

ma = basemap(nc)
ij = ma(*hh.lonlat(sta))
lon,lat = ma.lonlat()
xy = (lon[1:-1,1:-1],lat[1:-1,1:-1])
ad = sp.grid_interp(xy, np.load('adv.npy'), ij, sta.index, t, method='linear')

d = pd.Panel({'adv':ad,'bias':b})
dm = d.groupby(d.major_axis.date,'major').mean().to_frame()
Ejemplo n.º 10
0
# dz = Z['d03_orl']-sta['elev']
# lm = L['d03_orl']

# B = Tm['d03_0_12']-T
# dz = Z['d03_op']-sta['elev']
# lm = L['d03_op']

# B = Tm['d02_0_00']-Tm['d03_0_00']
# dz = Z['d02']-Z['d03_op']
# lm = L['d02']

lr = (B / dz * 1000)

nc = Dataset('../../data/WRF/3d/geo_em.d03.nc')
lm = nc.variables['LANDMASK'][0, :, :]
glon, glat = hh.lonlat(nc)


def landsea(r, d=5000):
    from pyproj import Geod
    from functools import partial
    inv = partial(Geod(ellps='WGS84').inv, r['lon'], r['lat'])

    def dist(x, y):
        return inv(x, y)[2]

    dv = np.vectorize(dist)
    return np.any(1 - lm[np.where(dv(glon, glat) < d)])


l = sta.apply(landsea, 1)