Esempio n. 1
0
def extract_northward_transport(geofil,vgeofil,fil,fil2,xstart,xend,ystart,yend,zs,ze,meanax):

    keepax = ()
    for i in range(4):
        if i not in meanax:
            keepax += (i,)

    fhgeo = dset(geofil)
    fh = mfdset(fil)
    zi = rdp1.getdims(fh)[2][0]
    dbl = -np.diff(zi)*9.8/1031
    (xs,xe),(ys,ye),dimv = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
            slat=ystart, nlat=yend,zs=zs,ze=ze,yhyq='yq')
    dxcv = rdp1.getgeombyindx(fhgeo,xs,xe,ys,ye)[2][2]
    f = rdp1.getgeombyindx(fhgeo,xs,xe,ys,ye)[-1]
    nt_const = dimv[0].size
    fhgeo.close()
    vh = fh.variables['vh'][0:,zs:ze,ys:ye,xs:xe]/dxcv
    fh2 = mfdset(fil2)
    h = fh2.variables['h_Cv'][0:,zs:ze,ys:ye,xs:xe]
    fh2.close()
    fh.close()
    vh = vh.filled(0)
    vhplus = np.where(vh>0,vh,0)
    vhminus = np.where(vh<0,vh,0)
    R = 6378
    #lr = np.sum(np.sqrt(-h*dbl[:,np.newaxis,np.newaxis])/np.pi/f/1e3,axis=1,keepdims=True)/np.radians(R)
    lr = np.sum(np.sqrt(-h*dbl[:,np.newaxis,np.newaxis])/np.pi/f/1e3,axis=1,keepdims=True)/np.radians(R*np.cos(np.radians(dimv[2][:,np.newaxis])))
    lr = np.mean(lr,axis=0).squeeze()
    terms = np.ma.concatenate(( vhplus[:,:,:,:,np.newaxis],
                                vhminus[:,:,:,:,np.newaxis]),axis=4)

    termsm = np.ma.apply_over_axes(np.mean, terms, meanax)
    termsm = termsm.squeeze()
    X = dimv[keepax[1]]
    Y = dimv[keepax[0]]

    return X,Y, termsm, lr
Esempio n. 2
0
def extract_uv(geofil,
               fil,
               fil2,
               fil3,
               xstart,
               xend,
               ystart,
               yend,
               zs,
               ze,
               savfil=None,
               utwamaxscalefactor=1,
               vtwamaxscalefactor=1):

    fh = mfdset(fil)
    fh2 = mfdset(fil2)
    zi = rdp1.getdims(fh)[2][0]

    fhgeo = dset(geofil)

    (xs, xe), (ys, ye), dimutwa = rdp1.getlatlonindx(
        fh,
        wlon=xstart,
        elon=xend,
        slat=ystart,
        nlat=yend,
        zs=zs,
        ze=ze,
        xhxq='xq',
        yhyq='yh')
    em = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
    elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])
    elm = np.ma.apply_over_axes(np.mean, elm, (0, 2))
    dycu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][1]

    uh = fh.variables['uh'][0:, zs:ze, ys:ye, xs:xe]
    h_cu = fh2.variables['h_Cu'][0:, zs:ze, ys:ye, xs:xe]
    utwa = uh / h_cu / dycu

    fig = plt.figure()
    ax = fig.add_subplot(221)
    X = dimutwa[3]
    X = np.meshgrid(X, dimutwa[1])[0]
    Y = elm.squeeze()
    utwa = np.ma.apply_over_axes(np.nanmean, utwa, (0, 2))
    utwa = utwa.squeeze()
    cmax = np.nanmax(np.absolute(utwa)) * utwamaxscalefactor
    im = m6plot(
        (X, Y, utwa),
        ax=ax,
        ylabel='z (m)',
        txt=r'$\hat{u}$',
        vmin=-cmax,
        vmax=cmax,
        cmap='RdBu_r',
        ylim=(-3000, 0))
    ax.set_xticklabels([])

    (xs, xe), (ys, ye), dimvtwa = rdp1.getlatlonindx(
        fh,
        wlon=xstart,
        elon=xend,
        slat=ystart,
        nlat=yend,
        zs=zs,
        ze=ze,
        xhxq='xh',
        yhyq='yq')
    em = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
    elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])
    elm = np.ma.apply_over_axes(np.mean, elm, (0, 2))
    dxcv = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][2]

    vh = fh.variables['vh'][0:, zs:ze, ys:ye, xs:xe]
    h_cv = fh2.variables['h_Cv'][0:, zs:ze, ys:ye, xs:xe]
    vtwa = vh / h_cv / dxcv

    ax = fig.add_subplot(222)
    X = dimvtwa[3]
    X = np.meshgrid(X, dimvtwa[1])[0]
    Y = elm.squeeze()
    vtwa = np.ma.apply_over_axes(np.nanmean, vtwa, (0, 2))
    vtwa = vtwa.squeeze()
    cmax = np.nanmax(np.absolute(vtwa)) * vtwamaxscalefactor
    im = m6plot(
        (X, Y, vtwa),
        ax=ax,
        txt=r'$\hat{v}$',
        vmin=-cmax,
        vmax=cmax,
        cmap='RdBu_r',
        ylim=(-3000, 0))
    ax.set_xticklabels([])
    ax.set_yticklabels([])

    fh2.close()
    fh.close()
    fhgeo.close()

    fh3 = mfdset(fil3)
    (xs, xe), (ys, ye), dimu = rdp1.getlatlonindx(
        fh3,
        wlon=xstart,
        elon=xend,
        slat=ystart,
        nlat=yend,
        zs=zs,
        ze=ze,
        xhxq='xq',
        yhyq='yh',
        zlzi='zlremap')
    u = fh3.variables['u'][0:, zs:ze, ys:ye, xs:xe]
    ax = fig.add_subplot(223)
    X = dimu[3]
    Y = dimu[1]
    u = np.ma.apply_over_axes(np.nanmean, u, (0, 2))
    u = u.squeeze()
    cmax = np.nanmax(np.absolute(u))
    im = m6plot(
        (X, -Y, u),
        ax=ax,
        txt='u',
        ylabel='z (m)',
        vmin=-cmax,
        vmax=cmax,
        cmap='RdBu_r')
    xdegtokm(ax, 0.5 * (ystart + yend))

    (xs, xe), (ys, ye), dimv = rdp1.getlatlonindx(
        fh3,
        wlon=xstart,
        elon=xend,
        slat=ystart,
        nlat=yend,
        zs=zs,
        ze=ze,
        xhxq='xh',
        yhyq='yq',
        zlzi='zlremap')
    v = fh3.variables['v'][0:, zs:ze, ys:ye, xs:xe]
    ax = fig.add_subplot(224)
    X = dimv[3]
    Y = dimv[1]
    v = np.ma.apply_over_axes(np.nanmean, v, (0, 2))
    v = v.squeeze()
    cmax = np.nanmax(np.absolute(v))
    im = m6plot(
        (X, -Y, v), ax=ax, txt='v', vmin=-cmax, vmax=cmax, cmap='RdBu_r')
    xdegtokm(ax, 0.5 * (ystart + yend))
    ax.set_yticklabels([])
    fh3.close()

    if savfil:
        plt.savefig(
            savfil + '.eps',
            dpi=300,
            facecolor='w',
            edgecolor='w',
            format='eps',
            transparent=False,
            bbox_inches='tight')
    else:
        plt.show()
Esempio n. 3
0
def extract_twamomy_terms(geofil,
                          fil,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          alreadysaved=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fh = mfdset(fil)
        (xs, xe), (ys, ye), dimv = rdp1.getlatlonindx(fh,
                                                      wlon=xstart,
                                                      elon=xend,
                                                      slat=ystart,
                                                      nlat=yend,
                                                      zs=zs,
                                                      ze=ze,
                                                      yhyq='yq')
        #        _,(vys,vye),_ = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
        #                slat=ystart, nlat=yend,zs=zs,ze=ze,yhyq='yq')
        #        (xs,xe),(ys,ye),_ = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
        #                slat=ystart, nlat=yend,zs=zs,ze=ze)
        D, (ah, aq) = rdp1.getgeombyindx(geofil, xs, xe, ys, ye)[0:2]
        nt_const = dimv[0].size

        e = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
        hmfu = fh.variables['twa_hmfu'][0:, zs:ze, ys:ye, xs:xe]
        hpfv = fh.variables['twa_hpfv'][0:, zs:ze, ys:ye, xs:xe]
        huvxpt = fh.variables['twa_huvxpt'][0:, zs:ze, ys:ye, xs:xe]
        hvvymt = fh.variables['twa_hvvymt'][0:, zs:ze, ys:ye, xs:xe]
        hvwb = fh.variables['twa_hvwb'][0:, zs:ze, ys:ye, xs:xe]
        hdvdtvisc = fh.variables['twa_hdvdtvisc'][0:, zs:ze, ys:ye, xs:xe]
        hdiffv = fh.variables['twa_hdiffv'][0:, zs:ze, ys:ye, xs:xe]

        fh.close()

        terms = np.ma.concatenate(
            (hmfu[:, :, :, :, np.newaxis], hpfv[:, :, :, :, np.newaxis],
             hvwb[:, :, :, :, np.newaxis], huvxpt[:, :, :, :, np.newaxis],
             hvvymt[:, :, :, :, np.newaxis], hdvdtvisc[:, :, :, :, np.newaxis],
             hdiffv[:, :, :, :, np.newaxis]),
            axis=4)

        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)

        X = dimv[keepax[1]]
        Y = dimv[keepax[0]]
        if 1 in keepax:
            el = 0.5 * (e[:, 0:-1, :, :] + e[:, 1:, :, :])
            e = np.ma.apply_over_axes(np.mean, e, meanax)
            el = np.ma.apply_over_axes(np.mean, el, meanax)
            Y = el.squeeze()
            X = np.meshgrid(X, dimv[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        np.savez('twamomy_terms', X=X, Y=Y, P=P)
    else:
        npzfile = np.load('twamomy_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']

    return (X, Y, P)
def animate(fh,
            var,
            fig=None,
            wlon=-25,
            elon=0,
            slat=10,
            nlat=60,
            zs=0,
            ze=1,
            **plotkwargs):
    """Sets up the initial figure window and 
    returns and animation object."""
    if fig is None:
        fig, ax = plt.subplots()

    (xs, xe), (ys, ye), dimh = rdp1.getlatlonindx(fh,
                                                  wlon=wlon,
                                                  elon=elon,
                                                  slat=slat,
                                                  nlat=nlat,
                                                  zs=zs,
                                                  ze=ze,
                                                  zlzi='zlremap')
    pdims = ()
    for i in range(1, 4):
        if dimh[i].size != 1:
            pdims += (dimh[i], )
    y, x = pdims
    nt = dimh[0].size
    cmap = plotkwargs.get('cmap', 'viridis')
    xlabel = plotkwargs.get('xlabel', '')
    ylabel = plotkwargs.get('ylabel', '')
    title = plotkwargs.get('title', '')
    aspect = plotkwargs.get('aspect', 'auto')
    vmin = plotkwargs.get('vmin', 5)
    vmax = plotkwargs.get('vmax', 20)
    inverty = plotkwargs.get('inverty', False)
    bvnorm = plotkwargs.get('bvnorm', False)

    pvar = np.squeeze(fh.variables[var][0, zs:ze, ys:ye, xs:xe])
    if var == 'Rho_cv':
        pvar = 1 / 0.2 * (1031 - pvar)

    if bvnorm:
        bounds = np.linspace(vmin, vmax, 25)
        norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
        im = ax.pcolormesh(x, y, pvar, norm=norm, cmap=cmap, shading='flat')
    else:
        im = ax.pcolormesh(x,
                           y,
                           pvar,
                           vmax=vmax,
                           vmin=vmin,
                           cmap=cmap,
                           shading='flat')

    plt.colorbar(im)
    tim = date.fromordinal(int(fh.variables['Time'][0])).isoformat()
    txt = ax.text(0.05, 0.025, tim, transform=ax.transAxes)
    ax.set_aspect(aspect)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    if inverty:
        ax.invert_yaxis()

    def animator(i):
        """Updates the figure for each iteration"""
        pvar = np.squeeze(fh.variables[var][i, zs:ze, ys:ye, xs:xe])
        if var == 'Rho_cv':
            pvar = 1 / 0.2 * (1031 - pvar)
        pvar = pvar[:-1, :-1]
        im.set_array(pvar.ravel())
        im = ax.contourf(x, y, pvar, norm=norm, cmap=cmap, shading='flat')
        tim = date.fromordinal(int(fh.variables['Time'][i])).isoformat()
        txt.set_text(tim)
        sys.stdout.write('\r' + str(int((i + 1) / nt * 100)) + '% done...')
        sys.stdout.flush()
        return im, txt

    anim = animation.FuncAnimation(fig,
                                   animator,
                                   frames=nt,
                                   interval=100,
                                   blit=False)
    return anim
Esempio n. 5
0
def extract_pvterms(geofil,
                    fil,
                    xstart,
                    xend,
                    ystart,
                    yend,
                    zs,
                    ze,
                    meanax,
                    savfil=None,
                    alreadysaved=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fh = mfdset(fil)
        (xs, xe), (ys, ye), dimq = rdp1.getlatlonindx(fh,
                                                      wlon=xstart,
                                                      elon=xend,
                                                      slat=ystart,
                                                      nlat=yend,
                                                      zs=zs,
                                                      ze=ze,
                                                      xhxq='xq',
                                                      yhyq='yq')
        _, _, dimqp1 = rdp1.getdimsbyindx(fh,
                                          xs,
                                          xe,
                                          ys - 1,
                                          ye,
                                          zs=zs,
                                          ze=ze,
                                          xhxq='xq',
                                          yhyq='yq')
        D, (ah, aq), (dxcu, dycu, dxcv, dycv, dxbu, dybu, dxt,
                      dyt) = rdp1.getgeombyindx(geofil, xs, xe, ys, ye)[0:3]
        D_u = rdp1.getgeombyindx(geofil, xs, xe, ys - 1, ye)[0]
        D_v = rdp1.getgeombyindx(geofil, xs - 1, xe, ys, ye)[0]
        nt = dimq[0].size
        omega = 2 * np.pi / 24 / 3600 + 2 * np.pi / 24 / 3600 / 365
        R = 6378000
        f_q = 2 * omega * np.sin(dimq[2] * np.pi / 180)
        beta = 2 * omega * np.cos(dimq[2][None, :, None] * np.pi / 180) / R
        t0 = time.time()

        frhatu = fh.variables['frhatu'][0:1, zs:ze, ys - 1:ye, xs:xe]
        frhatv = fh.variables['frhatv'][0:1, zs:ze, ys:ye, xs - 1:xe]
        h_u = frhatu * D_u[np.newaxis, np.newaxis, :, :]
        h_v = frhatv * D_v[np.newaxis, np.newaxis, :, :]
        h_q = 0.25 * (h_u[:, :, 0:-1, :] + h_u[:, :, 1:, :] +
                      h_v[:, :, :, 0:-1] + h_v[:, :, :, 1:])

        h = fh.variables['h'][0:1, zs:ze, ys:ye, xs:xe]
        u = fh.variables['u'][0:1, zs:ze, ys:ye, xs - 1:xe]
        v = fh.variables['v'][0:1, zs:ze, ys - 1:ye, xs:xe]
        uh = fh.variables['uh'][0:1, zs:ze, ys:ye, xs - 1:xe]
        vh = fh.variables['vh'][0:1, zs:ze, ys - 1:ye, xs:xe]
        wd = fh.variables['wd'][0:1, zs:ze, ys:ye, xs:xe]
        cau = fh.variables['CAu'][0:1, zs:ze, ys:ye + 1, xs:xe]
        gkeu = fh.variables['gKEu'][0:1, zs:ze, ys:ye + 1, xs:xe]
        rvxv = fh.variables['rvxv'][0:1, zs:ze, ys:ye + 1, xs:xe]
        pfu = fh.variables['PFu'][0:1, zs:ze, ys:ye + 1, xs:xe]
        dudtvisc = fh.variables['du_dt_visc'][0:1, zs:ze, ys:ye + 1, xs:xe]
        diffu = fh.variables['diffu'][0:1, zs:ze, ys:ye + 1, xs:xe]
        cav = fh.variables['CAv'][0:1, zs:ze, ys:ye, xs:xe + 1]
        gkev = fh.variables['gKEv'][0:1, zs:ze, ys:ye, xs:xe + 1]
        rvxu = fh.variables['rvxu'][0:1, zs:ze, ys:ye, xs:xe + 1]
        pfv = fh.variables['PFv'][0:1, zs:ze, ys:ye, xs:xe + 1]
        diffv = fh.variables['diffv'][0:1, zs:ze, ys:ye, xs:xe + 1]
        dvdtvisc = fh.variables['dv_dt_visc'][0:1, zs:ze, ys:ye, xs:xe + 1]

        uadv = -gkeu - rvxv
        uadv = np.ma.filled(uadv.astype(float), 0)
        uadvym = h_q * (np.diff(uadv, axis=2) / dybu) / nt

        vadv = -gkev - rvxu
        vadv = np.ma.filled(vadv.astype(float), 0)
        vadvxm = h_q * (diffx_bound(vadv, dxbu)) / nt

        fv = cau - gkeu - rvxv
        fv = np.ma.filled(fv.astype(float), 0)
        fvym = h_q * (np.diff(fv, axis=2) / dybu) / nt

        fu = -cav + gkev + rvxu
        fu = np.ma.filled(fu.astype(float), 0)
        fuxm = h_q * (diffx_bound(fu, dxbu)) / nt

        Y1 = diffv
        Y1 = np.ma.filled(Y1.astype(float), 0)
        Y1xm = h_q * (diffx_bound(Y1, dxbu)) / nt

        Y2 = dvdtvisc
        Y2 = np.ma.filled(Y2.astype(float), 0)
        Y2xm = h_q * (diffx_bound(Y2, dxbu)) / nt

        #        hbetavm = (h_v*beta*v)/nt

        u = np.ma.filled(u.astype(float), 0)
        ux = np.diff(u, axis=3) / dxt

        v = np.ma.filled(v.astype(float), 0)
        vy = np.diff(v, axis=2) / dyt

        uh = np.ma.filled(uh.astype(float), 0)
        uh_x = np.diff(uh, axis=3) / ah
        uhxm = f_q[np.newaxis, np.newaxis, :,
                   np.newaxis] * (uh_x - h * ux) / nt

        vh = np.ma.filled(vh.astype(float), 0)
        vh_y = np.diff(vh, axis=2) / ah
        vhym = f_q[np.newaxis, np.newaxis, :,
                   np.newaxis] * (vh_y - h * vy) / nt

        fdiapycm = f_q[np.newaxis, np.newaxis, :, np.newaxis] * np.diff(
            wd, axis=1) / nt

        if 1 in keepax:
            em = fh.variables['e'][0:1, zs:ze, ys:ye, xs:xe] / nt

        for i in range(1, nt):
            frhatu = fh.variables['frhatu'][i:i + 1, zs:ze, ys - 1:ye, xs:xe]
            frhatv = fh.variables['frhatv'][i:i + 1, zs:ze, ys:ye, xs - 1:xe]
            h_u = frhatu * D_u[np.newaxis, np.newaxis, :, :]
            h_v = frhatv * D_v[np.newaxis, np.newaxis, :, :]
            h_q = 0.25 * (h_u[:, :, 0:-1, :] + h_u[:, :, 1:, :] +
                          h_v[:, :, :, 0:-1] + h_v[:, :, :, 1:])

            h = fh.variables['h'][i:i + 1, zs:ze, ys:ye, xs:xe]
            u = fh.variables['u'][i:i + 1, zs:ze, ys:ye, xs - 1:xe]
            v = fh.variables['v'][i:i + 1, zs:ze, ys - 1:ye, xs:xe]
            uh = fh.variables['uh'][i:i + 1, zs:ze, ys:ye, xs - 1:xe]
            vh = fh.variables['vh'][i:i + 1, zs:ze, ys - 1:ye, xs:xe]
            wd = fh.variables['wd'][i:i + 1, zs:ze, ys:ye, xs:xe]
            cau = fh.variables['CAu'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            gkeu = fh.variables['gKEu'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            rvxv = fh.variables['rvxv'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            pfu = fh.variables['PFu'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            dudtvisc = fh.variables['du_dt_visc'][i:i + 1, zs:ze, ys:ye + 1,
                                                  xs:xe]
            diffu = fh.variables['diffu'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            cav = fh.variables['CAv'][i:i + 1, zs:ze, ys:ye, xs:xe + 1]
            gkev = fh.variables['gKEv'][i:i + 1, zs:ze, ys:ye, xs:xe + 1]
            rvxu = fh.variables['rvxu'][i:i + 1, zs:ze, ys:ye, xs:xe + 1]
            pfv = fh.variables['PFv'][i:i + 1, zs:ze, ys:ye, xs:xe + 1]
            diffv = fh.variables['diffv'][i:i + 1, zs:ze, ys:ye, xs:xe + 1]
            dvdtvisc = fh.variables['dv_dt_visc'][i:i + 1, zs:ze, ys:ye,
                                                  xs:xe + 1]

            uadv = -gkeu - rvxv
            uadv = np.ma.filled(uadv.astype(float), 0)
            uadvym += h_q * (np.diff(uadv, axis=2) / dybu) / nt

            vadv = -gkev - rvxu
            vadv = np.ma.filled(vadv.astype(float), 0)
            vadvxm += h_q * (diffx_bound(vadv, dxbu)) / nt

            fv = cau - gkeu - rvxv
            fv = np.ma.filled(fv.astype(float), 0)
            fvym += h_q * (np.diff(fv, axis=2) / dybu) / nt

            fu = -cav + gkev + rvxu
            fu = np.ma.filled(fu.astype(float), 0)
            fuxm += h_q * (diffx_bound(fu, dxbu)) / nt

            Y1 = diffv
            Y1 = np.ma.filled(Y1.astype(float), 0)
            Y1xm += h_q * (diffx_bound(Y1, dxbu)) / nt

            Y2 = dvdtvisc
            Y2 = np.ma.filled(Y2.astype(float), 0)
            Y2xm += h_q * (diffx_bound(Y2, dxbu)) / nt

            #            hbetavm += (h_v*beta*v)/nt

            u = np.ma.filled(u.astype(float), 0)
            ux = np.diff(u, axis=3) / dxt

            v = np.ma.filled(v.astype(float), 0)
            vy = np.diff(v, axis=2) / dyt

            uh = np.ma.filled(uh.astype(float), 0)
            uh_x = np.diff(uh, axis=3) / ah
            uhxm += f_q[np.newaxis, np.newaxis, :,
                        np.newaxis] * (uh_x - h * ux) / nt

            vh = np.ma.filled(vh.astype(float), 0)
            vh_y = np.diff(vh, axis=2) / ah
            vhym += f_q[np.newaxis, np.newaxis, :,
                        np.newaxis] * (vh_y - h * vy) / nt

            fdiapycm += f_q[np.newaxis, np.newaxis, :, np.newaxis] * np.diff(
                wd, axis=1) / nt

            if 1 in keepax:
                em += fh.variables['e'][i:i + 1, zs:ze, ys:ye, xs:xe] / nt

            sys.stdout.write('\r' + str(int((i + 1) / nt * 100)) + '% done...')
            sys.stdout.flush()

        fh.close()
        print('Time taken for reading: {}'.format(time.time() - t0))

        terms = np.ma.concatenate(
            (uadvym[:, :, :, :, np.newaxis], vadvxm[:, :, :, :, np.newaxis],
             Y1xm[:, :, :, :, np.newaxis], Y2xm[:, :, :, :, np.newaxis],
             fdiapycm[:, :, :, :, np.newaxis], uhxm[:, :, :, :, np.newaxis],
             vhym[:, :, :, :, np.newaxis]),
            axis=4)

        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)

        X = dimq[keepax[1]]
        Y = dimq[keepax[0]]
        if 1 in keepax:
            elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X, dimq[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        np.savez('pv_terms', X=X, Y=Y, P=P)
    else:
        npzfile = np.load('pv_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']

    return (X, Y, P)
Esempio n. 6
0
def extract_twapv(geofil,
                  vgeofil,
                  fil,
                  fil2,
                  xstart,
                  xend,
                  ystart,
                  yend,
                  zs,
                  ze,
                  meanax,
                  savfil=None,
                  cmaxscalefactor=None,
                  plotatz=False,
                  Z=None):

    keepax = ()
    for i in range(4):
        if i not in meanax:
            keepax += (i, )

    fhgeo = dset(geofil)
    fh = mfdset(fil)
    fh2 = mfdset(fil2)
    zi = rdp1.getdims(fh)[2][0]
    dbl = np.diff(zi) * 9.8 / 1031
    (xs, xe), (ys, ye), dimq = rdp1.getlatlonindx(fh,
                                                  wlon=xstart,
                                                  elon=xend,
                                                  slat=ystart,
                                                  nlat=yend,
                                                  zs=zs,
                                                  ze=ze,
                                                  xhxq='xq',
                                                  yhyq='yq')
    dxbu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][4]
    dybu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][5]
    dycu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][1]
    dxcv = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][2]
    f = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[-1]
    nt_const = dimq[0].size
    fhgeo.close()

    em = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
    elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])

    uh = fh.variables['uh'][0:, zs:ze, ys:ye + 1, xs:xe]

    h_cu = fh2.variables['h_Cu'][0:, zs:ze, ys:ye + 1, xs:xe]
    utwa = uh / h_cu / dycu
    h_cu = np.where(h_cu > 1e-3, h_cu, np.nan)

    vh = fh.variables['vh'][0:, zs:ze, ys:ye, xs:xe]

    h_cv = fh2.variables['h_Cv'][0:, zs:ze, ys:ye, xs:xe]
    vtwa = vh / h_cv / dxcv
    vtwa = np.concatenate((vtwa, -vtwa[:, :, :, -1:]), axis=3)
    h_cv = np.concatenate((h_cv, -h_cv[:, :, :, -1:]), axis=3)
    h_cv = np.where(h_cv > 1e-3, h_cv, np.nan)

    fh2.close()
    fh.close()

    hq = 0.25 * (h_cu[:, :, :-1, :] + h_cv[:, :, :, :-1] + h_cu[:, :, 1:, :] +
                 h_cv[:, :, :, 1:])

    pv = f - np.diff(utwa, axis=2) / dybu + np.diff(vtwa, axis=3) / dxbu
    pv = pv / (hq / dbl[:, np.newaxis, np.newaxis])

    X = dimq[keepax[1]]
    Y = dimq[keepax[0]]
    if 1 in keepax:
        em = np.ma.apply_over_axes(np.mean, em, meanax)
        elm = np.ma.apply_over_axes(np.mean, elm, meanax)
        Y = elm.squeeze()
        X = np.meshgrid(X, dimq[1])[0]

    if plotatz:
        pv = getvaratz(pv, Z, em)

    pv = np.ma.apply_over_axes(np.nanmean, pv, meanax)
    pv = pv.squeeze()
    cmax = np.nanmax(np.absolute(pv)) * cmaxscalefactor
    im = m6plot((X, Y, pv),
                xlabel=r'x ($^{\circ}$ E)',
                ylabel=r'y ($^{\circ}$ N)',
                vmin=6e-10,
                vmax=cmax,
                aspect='equal',
                bvnorm=True)

    if savfil:
        plt.savefig(savfil + '.eps',
                    dpi=300,
                    facecolor='w',
                    edgecolor='w',
                    format='eps',
                    transparent=False,
                    bbox_inches='tight')
    else:
        plt.show()
def extract_momx_terms(geofil,
                       fil,
                       xstart,
                       xend,
                       ystart,
                       yend,
                       zs,
                       ze,
                       meanax,
                       alreadysaved=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        (xs, xe), (ys, ye), dimu = rdp1.getlatlonindx(fh,
                                                      wlon=xstart,
                                                      elon=xend,
                                                      slat=ystart,
                                                      nlat=yend,
                                                      zs=zs,
                                                      ze=ze,
                                                      xhxq='xq')
        D, (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0:2]
        fhgeo.close()
        nt = dimu[0].size
        t0 = time.time()

        print('Reading data using loop...')
        #dudtm = fh.variables['dudt'][0:1,zs:ze,ys:ye,xs:xe]/nt
        caum = fh.variables['CAu'][0:1, zs:ze, ys:ye, xs:xe] / nt
        pfum = fh.variables['PFu'][0:1, zs:ze, ys:ye, xs:xe] / nt
        dudtviscm = fh.variables['du_dt_visc'][0:1, zs:ze, ys:ye, xs:xe] / nt
        diffum = fh.variables['diffu'][0:1, zs:ze, ys:ye, xs:xe] / nt
        dudtdiam = fh.variables['dudt_dia'][0:1, zs:ze, ys:ye, xs:xe] / nt
        if 1 in keepax:
            em = fh.variables['e'][0:1, zs:ze, ys:ye, xs:xe] / nt
            print(caum.shape, em.shape)

        for i in range(1, nt):
            #dudtm += fh.variables['dudt'][i:i+1,zs:ze,ys:ye,xs:xe]/nt
            caum += fh.variables['CAu'][i:i + 1, zs:ze, ys:ye, xs:xe] / nt
            pfum += fh.variables['PFu'][i:i + 1, zs:ze, ys:ye, xs:xe] / nt
            dudtviscm += fh.variables['du_dt_visc'][i:i + 1, zs:ze, ys:ye,
                                                    xs:xe] / nt
            diffum += fh.variables['diffu'][i:i + 1, zs:ze, ys:ye, xs:xe] / nt
            dudtdiam += fh.variables['dudt_dia'][i:i + 1, zs:ze, ys:ye,
                                                 xs:xe] / nt
            if 1 in keepax:
                em += fh.variables['e'][i:i + 1, zs:ze, ys:ye, xs:xe] / nt

            sys.stdout.write('\r' + str(int((i + 1) / nt * 100)) + '% done...')
            sys.stdout.flush()

        fh.close()
        print('Time taken for data reading: {}s'.format(time.time() - t0))

        terms = np.ma.concatenate(
            (caum[:, :, :, :, np.newaxis], dudtdiam[:, :, :, :, np.newaxis],
             pfum[:, :, :, :, np.newaxis], diffum[:, :, :, :, np.newaxis],
             dudtviscm[:, :, :, :, np.newaxis]),
            axis=4)
        terms = terms.filled(0)

        termsm = np.ma.apply_over_axes(np.mean, terms, meanax)

        X = dimu[keepax[1]]
        Y = dimu[keepax[0]]
        if 1 in keepax:
            elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X, dimu[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        np.savez('momx_terms', X=X, Y=Y, P=P)
    else:
        npzfile = np.load('momx_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']

    return (X, Y, P)
def extractT(geofil,
             fil,
             xstart,
             xend,
             ystart,
             yend,
             zs,
             ze,
             meanax,
             ts=0,
             te=None,
             z=None,
             drhodt=-0.2,
             rho0=1031.0,
             savfil=None,
             plotit=True,
             loop=True):

    keepax = ()
    for i in range(4):
        if i not in meanax:
            keepax += (i, )

    fh = mfdset(fil)
    (xs, xe), (ys, ye), dimh = rdp1.getlatlonindx(fh,
                                                  wlon=xstart,
                                                  elon=xend,
                                                  slat=ystart,
                                                  nlat=yend,
                                                  zs=zs,
                                                  ze=ze)
    fhgeo = dset(geofil)
    D = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0]
    fhgeo.close()
    nt = dimh[0].size
    t0 = time.time()

    zl = rdp1.getdims(fh)[2][1]
    if loop:
        print('Reading data in loop...')
        e = fh.variables['e'][0:1, zs:ze, ys:ye, xs:xe] / nt
        for i in range(nt):
            e += fh.variables['e'][i:i + 1, zs:ze, ys:ye, xs:xe] / nt
            sys.stdout.write('\r' + str(int((i + 1) / nt * 100)) + '% done...')
            sys.stdout.flush()
        print('Time taken for data reading: {}s'.format(time.time() - t0))
    else:
        e = fh.variables['e'][ts:te, zs:ze, ys:ye, xs:xe]

    X = dimh[keepax[1]]
    Y = dimh[keepax[0]]
    if 1 in keepax:
        Y = z
        if z == None:
            z = np.linspace(-np.nanmax(D), -1, num=50)
            Y = z
    T = getTatz(zl, z, e)
    T = (T - rho0) / drhodt
    T = np.ma.apply_over_axes(np.nanmean, T, meanax)

    P = T.squeeze()
    data = (X, Y, P)

    if plotit:
        Pmax = np.nanmax(P)
        Pmin = np.nanmin(P)
        im = m6plot(data,
                    vmax=Pmax,
                    vmin=Pmin,
                    title=r'T at 40N ($^{\circ}$C)',
                    xlabel=r'x ($^{\circ}$)',
                    ylabel='z (m)',
                    bvnorm=True,
                    blevs=15)
        if savfil:
            plt.savefig(savfil + '.eps',
                        dpi=300,
                        facecolor='w',
                        edgecolor='w',
                        format='eps',
                        transparent=False,
                        bbox_inches='tight')
        else:
            plt.show()
    else:
        return data
def plot_eb_transport(geofil,
                      vgeofil,
                      fil,
                      xstart,
                      xend,
                      ystart,
                      yend,
                      zs,
                      ze,
                      meanax,
                      savfil=None):

    keepax = ()
    for i in range(4):
        if i not in meanax:
            keepax += (i, )

    fh = mfdset(fil)
    (xs, xe), (ys, ye), dimv = rdp1.getlatlonindx(fh,
                                                  wlon=xstart,
                                                  elon=xend,
                                                  slat=ystart,
                                                  nlat=yend,
                                                  zs=zs,
                                                  ze=ze,
                                                  yhyq='yq')
    fhgeo = dset(geofil)
    D = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0]
    fhgeo.close()
    nt_const = dimv[0].size
    vh = fh.variables['vh'][0:, zs:ze, ys:ye, xs:xe]
    e = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
    fh.close()
    vh = vh.filled(0)

    X = dimv[keepax[1]]
    Y = dimv[keepax[0]]
    if 1 in keepax:
        z = np.linspace(-np.nanmax([2000]), -1, num=50)
        Y = z
    P = getvaratzc(vh, z, e)
    P = np.ma.apply_over_axes(np.mean, P, meanax)
    P = P.squeeze()
    #P = np.ma.apply_over_axes(np.mean, vh, meanax).squeeze()
    im = m6plot((X, Y, P),
                titl='Transport near EB',
                ylab='z (m)',
                xlab='y (Deg)')

    if savfil:
        plt.savefig(savfil + '.eps',
                    dpi=300,
                    facecolor='w',
                    edgecolor='w',
                    format='eps',
                    transparent=False,
                    bbox_inches='tight')
    else:
        im = m6plot((X, Y, P),
                    titl='Transport near EB',
                    ylab='z (m)',
                    xlab='y (Deg)')
        plt.show()
def extract_twamomx_terms(geofil,
                          vgeofil,
                          fil,
                          fil2,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          alreadysaved=False,
                          xyasindices=False,
                          calledfrompv=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhvgeo = dset(vgeofil)
        db = -fhvgeo.variables['g'][:]
        dbi = np.append(db, 0)
        fhvgeo.close()

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        fh2 = mfdset(fil2)
        zi = rdp1.getdims(fh)[2][0]
        dbl = np.diff(zi) * 9.8 / 1031
        if xyasindices:
            (xs, xe), (ys, ye) = (xstart, xend), (ystart, yend)
            _, _, dimu = rdp1.getdimsbyindx(fh,
                                            xs,
                                            xe,
                                            ys,
                                            ye,
                                            zs=zs,
                                            ze=ze,
                                            ts=0,
                                            te=None,
                                            xhxq='xq',
                                            yhyq='yh',
                                            zlzi='zl')
        else:
            (xs, xe), (ys, ye), dimu = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          xhxq='xq')
        D, (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0:2]
        Dforgetutwaforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[0]
        Dforgetutwaforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                                 ye + 1)[0]
        Dforgethvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye)[0]
        dxt, dyt = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][6:8]
        dxcu, dycu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][0:2]
        dycuforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[2][1:2]
        dycuforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                          ye + 1)[2][1:2]
        dybu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][5]
        dyt1 = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][7]
        nt_const = dimu[0].size
        t0 = time.time()

        em = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
        elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])

        uh = fh2.variables['uh'][0:, zs:ze, ys:ye, xs:xe]
        h_cu = fh.variables['h_Cu'][0:, zs:ze, ys:ye, xs:xe]
        h_um = h_cu
        utwa = uh / h_cu / dycu

        uhforxdiff = fh2.variables['uh'][0:, zs:ze, ys:ye, xs - 1:xe]
        h_cuforxdiff = fh.variables['h_Cu'][0:, zs:ze, ys:ye, xs - 1:xe]
        utwaforxdiff = uhforxdiff / h_cuforxdiff / dycuforxdiff

        uhforydiff = fh2.variables['uh'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        h_cuforydiff = fh.variables['h_Cu'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        utwaforydiff = uhforydiff / h_cuforydiff / dycuforydiff

        utwax = np.diff(utwaforxdiff.filled(0), axis=3) / dxt
        utwax = np.concatenate((utwax, -utwax[:, :, :, [-1]]), axis=3)
        utwax = 0.5 * (utwax[:, :, :, 0:-1] + utwax[:, :, :, 1:])

        utway = np.diff(utwaforydiff, axis=2) / dybu
        utway = 0.5 * (utway[:, :, 0:-1, :] + utway[:, :, 1:, :])

        humx = np.diff(uhforxdiff.filled(0) / dycuforxdiff, axis=3) / dxt
        humx = np.concatenate((humx, -humx[:, :, :, [-1]]), axis=3)
        humx = 0.5 * (humx[:, :, :, 0:-1] + humx[:, :, :, 1:])

        hvm = fh.variables['hv_Cu'][0:, zs:ze, ys:ye, xs:xe]
        hv_cu = fh.variables['hv_Cu'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        hvmy = np.diff(hv_cu, axis=2) / dyt1
        hvmy = 0.5 * (hvmy[:, :, :-1, :] + hvmy[:, :, 1:, :])

        huuxphuvym = fh.variables['twa_huuxpt'][
            0:, zs:ze, ys:ye, xs:xe] + fh.variables['twa_huvymt'][0:, zs:ze,
                                                                  ys:ye, xs:xe]
        huu = fh.variables['huu_Cu'][0:, zs:ze, ys:ye, xs - 1:xe].filled(0)
        huuxm = np.diff(huu, axis=3) / dxt
        huuxm = np.concatenate((huuxm, -huuxm[:, :, :, -1:]), axis=3)
        huuxm = 0.5 * (huuxm[:, :, :, :-1] + huuxm[:, :, :, 1:])
        huvym = huuxphuvym - huuxm

        utwaforvdiff = np.concatenate((utwa[:, [0], :, :], utwa), axis=1)
        utwab = np.diff(utwaforvdiff, axis=1) / db[:, np.newaxis, np.newaxis]
        utwab = np.concatenate(
            (utwab,
             np.zeros([utwab.shape[0], 1, utwab.shape[2], utwab.shape[3]])),
            axis=1)
        utwab = 0.5 * (utwab[:, 0:-1, :, :] + utwab[:, 1:, :, :])

        hwb_u = fh.variables['hwb_Cu'][0:, zs:ze, ys:ye, xs:xe]
        hwm_u = fh.variables['hw_Cu'][0:, zs:ze, ys:ye, xs:xe]

        esq = fh.variables['esq_Cu'][0:, zs:ze, ys:ye, xs - 1:xe].filled(0)
        ecu = fh.variables['e_Cu'][0:, zs:ze, ys:ye, xs - 1:xe].filled(0)
        edlsqm = (esq - ecu**2)
        edlsqmx = np.diff(edlsqm, axis=3) / dxt
        edlsqmx = np.concatenate((edlsqmx, -edlsqmx[:, :, :, [-1]]), axis=3)
        edlsqmx = 0.5 * (edlsqmx[:, :, :, :-1] + edlsqmx[:, :, :, 1:])

        epfu = fh.variables['epfu'][0:, zs:ze, ys:ye, xs:xe]
        ecu = fh.variables['e_Cu'][0:, zs:ze, ys:ye, xs:xe]
        pfum = fh.variables['pfu_masked'][0:, zs:ze, ys:ye, xs:xe]
        edpfudm = epfu - pfum * ecu
        edpfudmb = np.diff(edpfudm, axis=1)
        edpfudmb = np.concatenate(
            (edpfudmb[:, :1, :, :], edpfudmb, edpfudmb[:, -1:, :, :]), axis=1)
        edpfudmb = 0.5 * (edpfudmb[:, :-1, :, :] + edpfudmb[:, 1:, :, :])

        hfvm = fh.variables['twa_hfv'][0:1, zs:ze, ys:ye, xs:xe]
        huwbm = fh.variables['twa_huwb'][0:1, zs:ze, ys:ye, xs:xe]
        hdiffum = fh.variables['twa_hdiffu'][0:1, zs:ze, ys:ye, xs:xe]
        hdudtviscm = fh.variables['twa_hdudtvisc'][0:1, zs:ze, ys:ye, xs:xe]
        fh2.close()
        fh.close()

        advx = utwa * utwax
        advy = hvm * utway / h_um
        advb = hwm_u * utwab / h_um
        cor = hfvm / h_um
        pfum = pfum

        xdivep1 = -huuxm / h_um
        xdivep2 = -advx
        xdivep3 = -utwa * humx / h_um
        xdivep4 = 0.5 * edlsqmx * dbl[:, np.newaxis, np.newaxis] / h_um
        xdivep = (xdivep1 + xdivep2 + xdivep3 + xdivep4)

        ydivep1 = -huvym / h_um
        ydivep2 = -advy
        ydivep3 = -utwa * hvmy / h_um
        ydivep = (ydivep1 + ydivep2 + ydivep3)

        bdivep1 = -huwbm / h_um
        bdivep2 = -advb
        bdivep3 = -utwa * hwb_u / h_um
        bdivep4 = edpfudmb / h_um
        bdivep = (bdivep1 + bdivep2 + bdivep3 + bdivep4)
        X1twa = hdiffum / h_um
        X2twa = hdudtviscm / h_um

        terms = np.ma.concatenate(
            (-advx[:, :, :, :, np.newaxis], -advy[:, :, :, :, np.newaxis],
             -advb[:, :, :, :, np.newaxis], cor[:, :, :, :, np.newaxis],
             pfum[:, :, :, :, np.newaxis], -xdivep[:, :, :, :, np.newaxis],
             -ydivep[:, :, :, :, np.newaxis], -bdivep[:, :, :, :, np.newaxis],
             X1twa[:, :, :, :, np.newaxis], X2twa[:, :, :, :, np.newaxis]),
            axis=4)
        termsep = np.ma.concatenate((
            -xdivep1[:, :, :, :, np.newaxis], -xdivep3[:, :, :, :, np.newaxis],
            -xdivep4[:, :, :, :, np.newaxis], -ydivep1[:, :, :, :, np.newaxis],
            -ydivep3[:, :, :, :, np.newaxis], -bdivep1[:, :, :, :, np.newaxis],
            -bdivep3[:, :, :, :, np.newaxis],
            -bdivep4[:, :, :, :, np.newaxis]),
                                    axis=4)

        terms[np.isinf(terms)] = np.nan
        termsep[np.isinf(termsep)] = np.nan
        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)
        termsepm = np.ma.apply_over_axes(np.nanmean, termsep, meanax)

        X = dimu[keepax[1]]
        Y = dimu[keepax[0]]
        if 1 in keepax:
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X, dimu[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        Pep = termsepm.squeeze()
        Pep = np.ma.filled(Pep.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        if not calledfrompv:
            np.savez('twamomx_complete_terms', X=X, Y=Y, P=P, Pep=Pep)
    else:
        npzfile = np.load('twamomx_complete_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        Pep = npzfile['Pep']

    return (X, Y, P, Pep)
Esempio n. 11
0
def extractvel(geofil,
               fil,
               xstart,
               xend,
               ystart,
               yend,
               zs,
               ze,
               meanax,
               twa=True,
               loop=True):

    keepax = ()
    for i in range(4):
        if i not in meanax:
            keepax += (i, )

    fh = mfdset(fil)
    (uxs, uxe), (ys, ye), dimu = rdp1.getlatlonindx(fh,
                                                    wlon=xstart,
                                                    elon=xend,
                                                    slat=ystart,
                                                    nlat=yend,
                                                    zs=zs,
                                                    ze=ze,
                                                    xhxq='xq')
    (xs, xe), (vys, vye), dimv = rdp1.getlatlonindx(fh,
                                                    wlon=xstart,
                                                    elon=xend,
                                                    slat=ystart,
                                                    nlat=yend,
                                                    zs=zs,
                                                    ze=ze,
                                                    yhyq='yq')
    D = rdp1.getgeombyindx(geofil, xs, xe, ys, ye)[0]
    nt = dimu[0].size
    t0 = time.time()

    if loop:
        print('Reading data using loop...')
        u = fh.variables['u'][0:1, zs:ze, ys:ye, uxs:uxe]
        v = fh.variables['v'][0:1, zs:ze, vys:vye, xs:xe]
        e = fh.variables['e'][0:1, zs:ze, ys:ye, xs:xe]

        if twa:
            print('Using twa..')
            frhatu = fh.variables['frhatu'][0:1, zs:ze, ys:ye, uxs:uxe]
            frhatv = fh.variables['frhatv'][0:1, zs:ze, vys:vye, xs:xe]
            hm_u = frhatu * D[np.newaxis, np.newaxis, :, :]
            hm_v = frhatv * D[np.newaxis, np.newaxis, :, :]
            hm_u = np.ma.masked_array(hm_u, mask=(hm_u <= 1e-3).astype(int))
            hm_v = np.ma.masked_array(hm_v, mask=(hm_v <= 1e-3).astype(int))
            uhm = u * hm_u.filled(0)
            vhm = v * hm_v.filled(0)
            swashu = np.ma.array(np.ones(hm_u.shape),
                                 mask=np.ma.getmaskarray(hm_u))
            swashv = np.ma.array(np.ones(hm_v.shape),
                                 mask=np.ma.getmaskarray(hm_v))
        else:
            um = u / nt
            vm = v / nt

        em = e / nt

        for i in range(1, nt):
            u = fh.variables['u'][i:i + 1, zs:ze, ys:ye, uxs:uxe]
            v = fh.variables['v'][i:i + 1, zs:ze, vys:vye, xs:xe]
            e = fh.variables['e'][i:i + 1, zs:ze, ys:ye, xs:xe]
            if twa:
                frhatu = fh.variables['frhatu'][i:i + 1, zs:ze, ys:ye, uxs:uxe]
                frhatv = fh.variables['frhatv'][i:i + 1, zs:ze, vys:vye, xs:xe]
                h_u = frhatu * D[np.newaxis, np.newaxis, :, :]
                h_v = frhatv * D[np.newaxis, np.newaxis, :, :]
                h_u = np.ma.masked_array(h_u, mask=(h_u <= 1e-3).astype(int))
                h_v = np.ma.masked_array(h_v, mask=(h_v <= 1e-3).astype(int))
                uhm += u * h_u.filled(0)
                vhm += v * h_v.filled(0)
                hm_u += h_u.filled(0)
                hm_v += h_v.filled(0)
                swashu += np.ma.array(np.ones(hm_u.shape),
                                      mask=np.ma.getmaskarray(hm_u))
                swashv += np.ma.array(np.ones(hm_v.shape),
                                      mask=np.ma.getmaskarray(hm_v))
            else:
                um += u / nt
                vm += v / nt

            em += e / nt

            sys.stdout.write('\r' + str(int((i + 1) / nt * 100)) + '% done...')
            sys.stdout.flush()

        fh.close()
        print('Time taken for data reading: {}s'.format(time.time() - t0))

        if twa:
            hm_u = np.ma.apply_over_axes(np.mean, hm_u, meanax[0])
            hm_v = np.ma.apply_over_axes(np.mean, hm_v, meanax[0])
            uhm = np.ma.apply_over_axes(np.mean, uhm, meanax[0])
            vhm = np.ma.apply_over_axes(np.mean, vhm, meanax[0])
            um = uhm / hm_u
            vm = vhm / hm_v

        um = np.ma.apply_over_axes(np.mean, um, meanax)
        vm = np.ma.apply_over_axes(np.mean, vm, meanax)
        swashu = np.ma.apply_over_axes(np.mean, swashu, meanax)
        swashv = np.ma.apply_over_axes(np.mean, swashv, meanax)
        elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])
        elm = np.ma.apply_over_axes(np.mean, elm, meanax)
        em = np.ma.apply_over_axes(np.mean, em, meanax)

    Xu = dimu[keepax[1]]
    Xv = dimv[keepax[1]]
    Yu = dimu[keepax[0]]
    Yv = dimv[keepax[0]]
    if 1 in keepax:
        Yu = elm.squeeze()
        Xu = np.meshgrid(Xu, dimu[1])[0]
        Yv = elm.squeeze()
        Xv = np.meshgrid(Xv, dimv[1])[0]

    Pu = um.squeeze()
    Pv = vm.squeeze()
    swashu = swashu.squeeze().filled(0)
    swashv = swashv.squeeze().filled(0)
    datau = (Xu, Yu, Pu, swashu)
    datav = (Xv, Yv, Pv, swashv)
    return datau, datav
Esempio n. 12
0
def extract_twamomx_terms(geofil,
                          vgeofil,
                          fil,
                          fil2,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          fil3=None,
                          alreadysaved=False,
                          xyasindices=False,
                          calledfrompv=False,
                          htol=1e-3):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhvgeo = dset(vgeofil)
        db = -fhvgeo.variables['g'][:]
        dbi = np.append(db, 0)
        fhvgeo.close()

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        fh2 = mfdset(fil2)
        zi = rdp1.getdims(fh)[2][0]
        dbl = np.diff(zi) * 9.8 / 1031
        if xyasindices:
            (xs, xe), (ys, ye) = (xstart, xend), (ystart, yend)
            _, _, dimu = rdp1.getdimsbyindx(fh,
                                            xs,
                                            xe,
                                            ys,
                                            ye,
                                            zs=zs,
                                            ze=ze,
                                            ts=0,
                                            te=None,
                                            xhxq='xq',
                                            yhyq='yh',
                                            zlzi='zl')
        else:
            (xs, xe), (ys, ye), dimu = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          xhxq='xq')
        sl = np.s_[:, :, ys:ye, xs:xe]
        slmy = np.s_[:, :, ys - 1:ye, xs:xe]
        D, (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0:2]
        Dforgetutwaforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[0]
        Dforgetutwaforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                                 ye + 1)[0]
        Dforgethvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye)[0]
        dxt, dyt = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][6:8]
        dxcu, dycu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][0:2]
        dycuforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[2][1:2]
        dycuforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                          ye + 1)[2][1:2]
        dxbu, dybu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][4:6]
        aq1 = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye)[1][1]
        ah1 = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[1][0]
        dxcu1 = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye + 1)[2][0]
        nt_const = dimu[0].size
        t0 = time.time()
        dt = fh.variables['average_DT'][:]
        dt = dt[:, np.newaxis, np.newaxis, np.newaxis]

        if fil3:
            fh3 = mfdset(fil3)
            slmytn = np.s_[-1:, :, ys - 1:ye, xs:xe]
            #            islayerdeep0 = fh3.variables['islayerdeep'][:,0,0,0].sum()
            #            islayerdeep = (fh3.variables['islayerdeep'][slmy].filled(np.nan)).sum(axis=0,
            #                                                                               keepdims=True)
            islayerdeep0 = fh3.variables['islayerdeep'][-1:, 0, 0, 0]
            islayerdeep = (fh3.variables['islayerdeep'][slmytn].filled(np.nan))
            swash = (islayerdeep0 - islayerdeep) / islayerdeep0 * 100
            swash = 0.5 * (swash[:, :, :-1, :] + swash[:, :, 1:, :])
            fh3.close()
        else:
            swash = None

        em = (fh2.variables['e'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])

        uh = (fh.variables['uh_masked'][0:, zs:ze, ys:ye, xs:xe].filled(np.nan)
              * dt).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cu = (fh.variables['h_Cu'][0:, zs:ze, ys:ye, xs:xe].filled(0) *
                dt).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cu[h_cu < htol] = np.nan
        h_um = h_cu
        utwa = uh / h_cu / dycu

        uhforxdiff = (fh.variables['uh_masked'][0:, zs:ze, ys:ye, xs - 1:xe] *
                      dt).filled(np.nan).sum(axis=0,
                                             keepdims=True) / np.sum(dt)
        h_cuforxdiff = (fh.variables['h_Cu'][0:, zs:ze, ys:ye, xs - 1:xe] *
                        dt).filled(0).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cuforxdiff[h_cuforxdiff < htol] = np.nan
        utwaforxdiff = uhforxdiff / h_cuforxdiff  #/dycuforxdiff

        uhforydiff = (
            fh.variables['uh_masked'][0:, zs:ze, ys - 1:ye + 1, xs:xe] *
            dt).filled(np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cuforydiff = (fh.variables['h_Cu'][0:, zs:ze, ys - 1:ye + 1, xs:xe] *
                        dt).filled(0).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cuforydiff[h_cuforydiff < htol] = np.nan
        utwaforydiff = uhforydiff / h_cuforydiff  #/dycuforydiff

        utwax = np.diff(np.nan_to_num(utwaforxdiff), axis=3) / dxt / dyt
        utwax = np.concatenate((utwax, -utwax[:, :, :, [-1]]), axis=3)
        utwax = 0.5 * (utwax[:, :, :, 0:-1] + utwax[:, :, :, 1:])

        utway = np.diff(utwaforydiff, axis=2) / dxbu / dybu
        utway = 0.5 * (utway[:, :, 0:-1, :] + utway[:, :, 1:, :])

        humx = np.diff(np.nan_to_num(uhforxdiff), axis=3) / dxt / dyt
        humx = np.concatenate((humx, -humx[:, :, :, [-1]]), axis=3)
        humx = 0.5 * (humx[:, :, :, 0:-1] + humx[:, :, :, 1:])

        hvm = (fh.variables['vh_masked'][0:, zs:ze, ys - 1:ye, xs:xe] *
               dt).sum(axis=0, keepdims=True) / np.sum(dt)
        hvm = np.concatenate((hvm, -hvm[:, :, :, -1:]), axis=3)
        hvm = 0.25 * (hvm[:, :, :-1, :-1] + hvm[:, :, :-1, 1:] +
                      hvm[:, :, 1:, :-1] + hvm[:, :, 1:, 1:]) / dxcu

        hv = (fh.variables['vh_masked'][0:, zs:ze, ys - 1:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hvmy = np.diff(hv, axis=2) / dxt / dyt
        hvmy = np.concatenate((hvmy, -hvmy[:, :, :, -1:]), axis=3)
        hvmy = 0.5 * (hvmy[:, :, :, :-1] + hvmy[:, :, :, 1:])

        huuxphuvym = (
            fh.variables['twa_huuxpt'][0:, zs:ze, ys:ye, xs:xe] * dt +
            fh.variables['twa_huvymt'][0:, zs:ze, ys:ye, xs:xe] * dt).filled(
                np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        #u = (fh.variables['u_masked'][0:,zs:ze,ys:ye,xs-1:xe]*dt).filled(np.nan).sum(axis=0,keepdims=True)/np.sum(dt)
        huu = (fh.variables['huu_Cu'][0:, zs:ze, ys:ye, xs - 1:xe] *
               dt).filled(np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        huuxm = np.diff(np.nan_to_num(huu), axis=3) / dxt / dyt
        huuxm = np.concatenate((huuxm, -huuxm[:, :, :, -1:]), axis=3)
        huuxm = 0.5 * (huuxm[:, :, :, :-1] + huuxm[:, :, :, 1:])
        #        huu = (fh.variables['huu_T'][0:,zs:ze,ys:ye,xs:xe]*dt).sum(axis=0,keepdims=True)/np.sum(dt)*dyt
        #        huu = np.concatenate((huu,-huu[:,:,:,-1:]),axis=3)
        #        huuxm = np.diff(huu,axis=3)/dxcu/dycu
        huvym = huuxphuvym + huuxm

        utwaforvdiff = np.concatenate((utwa[:, [0], :, :], utwa), axis=1)
        utwab = np.diff(utwaforvdiff, axis=1) / db[:, np.newaxis, np.newaxis]
        utwab = np.concatenate((utwab, np.zeros(utwab[:, :1, :, :].shape)),
                               axis=1)
        utwab = 0.5 * (utwab[:, 0:-1, :, :] + utwab[:, 1:, :, :])

        hwb = (fh2.variables['wd'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hwb = np.diff(hwb, axis=1)
        hwb = np.concatenate((hwb, -hwb[:, :, :, -1:]), axis=3)
        hwb_u = 0.5 * (hwb[:, :, :, :-1] + hwb[:, :, :, 1:])
        hwb = (fh2.variables['wd'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hwm = 0.5 * (hwb[:, :-1] + hwb[:, 1:]) * dbl[:, np.newaxis, np.newaxis]
        hwm = np.concatenate((hwm, -hwm[:, :, :, -1:]), axis=3)
        hwm_u = 0.5 * (hwm[:, :, :, :-1] + hwm[:, :, :, 1:])

        esq = (fh.variables['esq'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        edlsqm = (esq - elm**2)
        edlsqm = np.concatenate((edlsqm, edlsqm[:, :, :, -1:]), axis=3)
        edlsqmx = np.diff(edlsqm, axis=3) / dxcu

        hpfu = (fh.variables['twa_hpfu'][0:, zs:ze, ys:ye, xs:xe] * dt).filled(
            np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        pfum = (fh2.variables['PFu'][0:, zs:ze, ys:ye, xs:xe] * dt).filled(
            np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        edpfudmb = -hpfu + h_cu * pfum - 0.5 * edlsqmx * dbl[:, np.newaxis,
                                                             np.newaxis]

        hfvm = (fh.variables['twa_hfv'][0:, zs:ze, ys:ye, xs:xe] * dt).filled(
            np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        huwbm = (fh.variables['twa_huwb'][0:, zs:ze, ys:ye, xs:xe] *
                 dt).filled(np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        hdiffum = (fh.variables['twa_hdiffu'][0:, zs:ze, ys:ye, xs:xe] *
                   dt).filled(np.nan).sum(axis=0, keepdims=True) / np.sum(dt)
        hdudtviscm = (fh.variables['twa_hdudtvisc'][0:, zs:ze, ys:ye, xs:xe] *
                      dt).filled(np.nan).sum(axis=0,
                                             keepdims=True) / np.sum(dt)
        fh2.close()
        fh.close()

        advx = utwa * utwax
        advy = hvm * utway / h_um
        advb = hwm_u * utwab / h_um
        cor = hfvm / h_um
        pfum = pfum

        xdivep1 = -huuxm / h_um
        xdivep2 = advx
        xdivep3 = utwa * humx / h_um
        xdivep4 = -0.5 * edlsqmx * dbl[:, np.newaxis, np.newaxis] / h_um
        xdivep = (xdivep1 + xdivep2 + xdivep3 + xdivep4)

        ydivep1 = huvym / h_um
        ydivep2 = advy
        ydivep3 = utwa * hvmy / h_um
        ydivep = (ydivep1 + ydivep2 + ydivep3)

        bdivep1 = huwbm / h_um
        bdivep2 = advb
        bdivep3 = utwa * hwb_u / h_um
        bdivep4 = -edpfudmb / h_um
        bdivep = (bdivep1 + bdivep2 + bdivep3 + bdivep4)
        X1twa = hdiffum / h_um
        X2twa = hdudtviscm / h_um

        terms = np.concatenate(
            (-advx[:, :, :, :, np.newaxis], -advy[:, :, :, :, np.newaxis],
             -advb[:, :, :, :, np.newaxis], cor[:, :, :, :, np.newaxis],
             pfum[:, :, :, :, np.newaxis], xdivep[:, :, :, :, np.newaxis],
             ydivep[:, :, :, :, np.newaxis], bdivep[:, :, :, :, np.newaxis],
             X1twa[:, :, :, :, np.newaxis], X2twa[:, :, :, :, np.newaxis]),
            axis=4)
        termsep = np.concatenate(
            (xdivep1[:, :, :, :, np.newaxis], xdivep3[:, :, :, :, np.newaxis],
             xdivep4[:, :, :, :, np.newaxis], ydivep1[:, :, :, :, np.newaxis],
             ydivep3[:, :, :, :, np.newaxis], bdivep1[:, :, :, :, np.newaxis],
             bdivep3[:, :, :, :, np.newaxis], bdivep4[:, :, :, :, np.newaxis]),
            axis=4)

        termsm = np.nanmean(terms, axis=meanax, keepdims=True)
        termsepm = np.nanmean(termsep, axis=meanax, keepdims=True)

        X = dimu[keepax[1]]
        Y = dimu[keepax[0]]
        if 1 in keepax and not calledfrompv:
            em = np.nanmean(em, axis=meanax, keepdims=True)
            elm = np.nanmean(elm, axis=meanax, keepdims=True)
            z = np.linspace(-3000, 0, 100)
            Y = z
            P = getvaratzc5(termsm.astype(np.float32), z.astype(np.float32),
                            em.astype(np.float32))
            Pep = getvaratzc5(termsepm.astype(np.float32),
                              z.astype(np.float32), em.astype(np.float32))
            if fil3:
                swash = np.nanmean(swash, meanax, keepdims=True)
                swash = getvaratzc(swash.astype(np.float32),
                                   z.astype(np.float32),
                                   em.astype(np.float32)).squeeze()
        else:
            P = termsm.squeeze()
            Pep = termsepm.squeeze()
        if not calledfrompv:
            np.savez('twamomx_complete_terms', X=X, Y=Y, P=P, Pep=Pep)
    else:
        npzfile = np.load('twamomx_complete_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        Pep = npzfile['Pep']

    return (X, Y, P, Pep, swash, em.squeeze())
Esempio n. 13
0
def extract_buoy_terms(geofil,
                       vgeofil,
                       fil,
                       fil2,
                       xstart,
                       xend,
                       ystart,
                       yend,
                       zs,
                       ze,
                       fil3=None,
                       z=np.linspace(-3000, 0, 100),
                       htol=1e-3):

    fhgeo = dset(geofil)
    fh = mfdset(fil)
    fh2 = mfdset(fil2)
    dz = np.diff(z)[0]

    (xs, xe), (ys, ye), dimh = rdp1.getlatlonindx(fh,
                                                  wlon=xstart,
                                                  elon=xend,
                                                  slat=ystart,
                                                  nlat=yend,
                                                  zs=zs,
                                                  ze=ze,
                                                  ts=0,
                                                  te=None)
    sl = np.s_[:, zs:ze, ys:ye, xs:xe]
    sl2d = sl[2:]
    slmx = np.s_[:, zs:ze, ys:ye, xs - 1:xe]
    slmx2d = slmx[2:]
    slmpy = np.s_[:, zs:ze, ys - 1:ye + 1, xs:xe]
    slmpy2d = slmpy[2:]
    slmy = np.s_[:, zs:ze, ys - 1:ye, xs:xe]
    slmy2d = slmy[2:]

    dxcu = fhgeo.variables['dxCu'][slmx2d]
    dycv = fhgeo.variables['dyCv'][slmy2d]

    e = getvaravg(fh2, 'e', sl)
    h = -np.diff(e, axis=1)
    h = np.ma.masked_array(h, mask=(h < htol)).filled(np.nan)
    zi = fh.variables['zi'][:]
    bi = 9.8 * (1 - zi / 1031)
    dbl = np.diff(zi) * 9.8 / 1031
    bz = -(np.diff(bi)[:, np.newaxis, np.newaxis] / h)
    bz = getvaratzc(bz.astype(np.float32), z.astype(np.float32),
                    e.astype(np.float32))
    whash = getwhash(fhgeo, vgeofil, fh, fh2, sl, htol=htol)
    whash = getvaratzc(whash.astype(np.float32), z.astype(np.float32),
                       e.astype(np.float32))
    wbz = whash * bz

    efxd = getvaravg(fh2, 'e', slmx)
    bfxd = getTatzc2(bi.astype(np.float32), z.astype(np.float32),
                     efxd.astype(np.float32))
    bfxd = np.concatenate((bfxd, bfxd[:, :, :, -1:]), axis=3)
    bx = np.diff(bfxd, axis=3) / dxcu
    u = getutwa(fhgeo, fh, fh2, slmx, htol=htol)
    e_cu = np.concatenate((efxd, efxd[:, :, :, -1:]), axis=3)
    e_cu = 0.5 * (e_cu[:, :, :, :-1] + e_cu[:, :, :, 1:])
    u = getvaratzc(u.astype(np.float32), z.astype(np.float32),
                   e_cu.astype(np.float32))
    ubx = u * bx
    ubx = 0.5 * (ubx[:, :, :, :-1] + ubx[:, :, :, 1:])

    efyd = getvaravg(fh2, 'e', slmpy)
    bfyd = getTatzc2(bi.astype(np.float32), z.astype(np.float32),
                     efyd.astype(np.float32))
    by = np.diff(bfyd, axis=2) / dycv
    e_cv = 0.5 * (efyd[:, :, :-1, :] + efyd[:, :, 1:, :])
    v = getvtwa(fhgeo, fh, fh2, slmy, htol=htol)
    v = getvaratzc(v.astype(np.float32), z.astype(np.float32),
                   e_cv.astype(np.float32))
    vby = v * by
    vby = 0.5 * (vby[:, :, :-1, :] + vby[:, :, 1:, :])

    hwb = getvaravg(fh2, 'wd', sl)
    hwb = -np.diff(hwb, axis=1)
    hwm = hwb * dbl[:, np.newaxis, np.newaxis]
    wtwa = hwm / h
    wtwa = getvaratzc(wtwa.astype(np.float32), z.astype(np.float32),
                      e.astype(np.float32))

    terms = np.ma.concatenate(
        (ubx[:, :, :, :, np.newaxis], vby[:, :, :, :, np.newaxis],
         wbz[:, :, :, :, np.newaxis], -wtwa[:, :, :, :, np.newaxis]),
        axis=4)
    return dimh, terms
Esempio n. 14
0
def extract_twamomy_terms(geofil,
                          vgeofil,
                          fil,
                          fil2,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          alreadysaved=False,
                          xyasindices=False,
                          calledfrompv=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhvgeo = dset(vgeofil)
        db = -fhvgeo.variables['g'][:]
        dbi = np.append(db, 0)
        fhvgeo.close()

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        fh2 = mfdset(fil2)
        zi = rdp1.getdims(fh)[2][0]
        dbl = np.diff(zi) * 9.8 / 1031
        if xyasindices:
            (xs, xe), (ys, ye) = (xstart, xend), (ystart, yend)
            _, _, dimv = rdp1.getdimsbyindx(fh,
                                            xs,
                                            xe,
                                            ys,
                                            ye,
                                            zs=zs,
                                            ze=ze,
                                            ts=0,
                                            te=None,
                                            xhxq='xh',
                                            yhyq='yq',
                                            zlzi='zl')
        else:
            (xs, xe), (ys, ye), dimv = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          yhyq='yq')
        D, (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0:2]
        Dforgetutwaforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[0]
        Dforgetutwaforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                                 ye + 1)[0]
        Dforgethvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye)[0]
        dxt, dyt = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][6:8]
        dxcv, dycv = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][2:4]
        dxcvforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[2][2:3]
        dxcvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                          ye + 1)[2][3:4]
        dxbu = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[2][4]
        nt_const = dimv[0].size
        t0 = time.time()

        em = fh.variables['e'][0:, zs:ze, ys:ye, xs:xe]
        elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])

        vh = fh2.variables['vh'][0:, zs:ze, ys:ye, xs:xe]
        h_cv = fh.variables['h_Cv'][0:, zs:ze, ys:ye, xs:xe]
        h_vm = h_cv
        vtwa = vh / h_cv / dxcv

        vhforxdiff = fh2.variables['vh'][0:, zs:ze, ys:ye, xs - 1:xe]
        h_cvforxdiff = fh.variables['h_Cv'][0:, zs:ze, ys:ye, xs - 1:xe]
        vtwaforxdiff = vhforxdiff / h_cvforxdiff / dxcvforxdiff
        vtwaforxdiff = np.concatenate(
            (vtwaforxdiff, vtwaforxdiff[:, :, :, -1:]), axis=3)

        vhforydiff = fh2.variables['vh'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        h_cvforydiff = fh.variables['h_Cv'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        vtwaforydiff = vhforydiff / h_cvforydiff / dxcvforydiff

        vtwax = np.diff(vtwaforxdiff, axis=3) / dxbu
        vtwax = 0.5 * (vtwax[:, :, :, 0:-1] + vtwax[:, :, :, 1:])

        vtway = np.diff(vtwaforydiff, axis=2) / dyt
        vtway = 0.5 * (vtway[:, :, 0:-1, :] + vtway[:, :, 1:, :])

        hvmy = np.diff(vhforydiff / dxcvforydiff, axis=2) / dyt
        hvmy = 0.5 * (hvmy[:, :, :-1, :] + hvmy[:, :, 1:, :])

        hum = fh.variables['hu_Cv'][0:, zs:ze, ys:ye, xs:xe]
        humforxdiff = fh.variables['hu_Cv'][0:, zs:ze, ys:ye, xs - 1:xe]
        humforxdiff = np.concatenate((humforxdiff, -humforxdiff[:, :, :, -1:]),
                                     axis=3)
        humx = np.diff(humforxdiff, axis=3) / dxbu
        humx = 0.5 * (humx[:, :, :, :-1] + humx[:, :, :, 1:])

        huvxphvvym = fh.variables['twa_huvxpt'][
            0:, zs:ze, ys:ye, xs:xe] + fh.variables['twa_hvvymt'][0:, zs:ze,
                                                                  ys:ye, xs:xe]
        hvv = fh.variables['hvv_Cv'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        hvvym = np.diff(hvv, axis=2) / dyt
        hvvym = 0.5 * (hvvym[:, :, :-1, :] + hvvym[:, :, 1:, :])
        huvxm = huvxphvvym - hvvym

        vtwaforvdiff = np.concatenate((vtwa[:, [0], :, :], vtwa), axis=1)
        vtwab = np.diff(vtwaforvdiff, axis=1) / db[:, np.newaxis, np.newaxis]
        vtwab = np.concatenate(
            (vtwab,
             np.zeros([vtwab.shape[0], 1, vtwab.shape[2], vtwab.shape[3]])),
            axis=1)
        vtwab = 0.5 * (vtwab[:, 0:-1, :, :] + vtwab[:, 1:, :, :])

        hwb_v = fh.variables['hwb_Cv'][0:, zs:ze, ys:ye, xs:xe]
        hwm_v = fh.variables['hw_Cv'][0:, zs:ze, ys:ye, xs:xe]

        esq = fh.variables['esq_Cv'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        ecv = fh.variables['e_Cv'][0:, zs:ze, ys - 1:ye + 1, xs:xe]
        edlsqm = (esq - ecv**2)
        edlsqmy = np.diff(edlsqm, axis=2) / dyt
        edlsqmy = 0.5 * (edlsqmy[:, :, :-1, :] + edlsqmy[:, :, 1:, :])

        epfv = fh.variables['epfv'][0:, zs:ze, ys:ye, xs:xe]
        ecv = fh.variables['e_Cv'][0:, zs:ze, ys:ye, xs:xe]
        pfvm = fh.variables['pfv_masked'][0:, zs:ze, ys:ye, xs:xe]
        edpfvdm = epfv - pfvm * ecv
        edpfvdmb = np.diff(edpfvdm, axis=1)
        edpfvdmb = np.concatenate(
            (edpfvdmb[:, :1, :, :], edpfvdmb, edpfvdmb[:, -1:, :, :]), axis=1)
        edpfvdmb = 0.5 * (edpfvdmb[:, :-1, :, :] + edpfvdmb[:, 1:, :, :])

        hmfum = fh.variables['twa_hmfu'][0:1, zs:ze, ys:ye, xs:xe]
        hvwbm = fh.variables['twa_hvwb'][0:1, zs:ze, ys:ye, xs:xe]
        hdiffvm = fh.variables['twa_hdiffv'][0:1, zs:ze, ys:ye, xs:xe]
        hdvdtviscm = fh.variables['twa_hdvdtvisc'][0:1, zs:ze, ys:ye, xs:xe]
        fh2.close()
        fh.close()

        advx = hum * vtwax / h_vm
        advy = vtwa * vtway
        advb = hwm_v * vtwab / h_vm
        cor = hmfum / h_vm
        pfvm = pfvm

        xdivep1 = -huvxm / h_vm
        xdivep2 = -advx
        xdivep3 = -vtwa * humx / h_vm
        xdivep = (xdivep1 + xdivep2 + xdivep3)

        ydivep1 = -hvvym / h_vm
        ydivep2 = -advy
        ydivep3 = -vtwa * hvmy / h_vm
        ydivep4 = 0.5 * edlsqmy * dbl[:, np.newaxis, np.newaxis] / h_vm
        ydivep = (ydivep1 + ydivep2 + ydivep3 + ydivep4)

        bdivep1 = -hvwbm / h_vm
        bdivep2 = -advb
        bdivep3 = -vtwa * hwb_v / h_vm
        bdivep4 = edpfvdmb / h_vm
        bdivep = (bdivep1 + bdivep2 + bdivep3 + bdivep4)
        Y1twa = hdiffvm / h_vm
        Y2twa = hdvdtviscm / h_vm

        terms = np.ma.concatenate(
            (-advx[:, :, :, :, np.newaxis], -advy[:, :, :, :, np.newaxis],
             -advb[:, :, :, :, np.newaxis], cor[:, :, :, :, np.newaxis],
             pfvm[:, :, :, :, np.newaxis], -xdivep[:, :, :, :, np.newaxis],
             -ydivep[:, :, :, :, np.newaxis], -bdivep[:, :, :, :, np.newaxis],
             Y1twa[:, :, :, :, np.newaxis], Y2twa[:, :, :, :, np.newaxis]),
            axis=4)
        termsep = np.ma.concatenate((
            -xdivep1[:, :, :, :, np.newaxis], -xdivep3[:, :, :, :, np.newaxis],
            -ydivep1[:, :, :, :, np.newaxis], -ydivep3[:, :, :, :, np.newaxis],
            -ydivep4[:, :, :, :, np.newaxis], -bdivep1[:, :, :, :, np.newaxis],
            -bdivep3[:, :, :, :, np.newaxis],
            -bdivep4[:, :, :, :, np.newaxis]),
                                    axis=4)

        terms[np.isinf(terms)] = np.nan
        termsep[np.isinf(termsep)] = np.nan
        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)
        termsepm = np.ma.apply_over_axes(np.nanmean, termsep, meanax)

        X = dimv[keepax[1]]
        Y = dimv[keepax[0]]
        if 1 in keepax:
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X, dimv[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        Pep = termsepm.squeeze()
        Pep = np.ma.filled(Pep.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        if not calledfrompv:
            np.savez('twamomy_complete_terms', X=X, Y=Y, P=P, Pep=Pep)
    else:
        npzfile = np.load('twamomy_complete_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        Pep = npzfile['Pep']

    return (X, Y, P, Pep)
Esempio n. 15
0
def extract_cb_terms(geofil,fil,xstart,xend,ystart,yend,zs,ze,meanax):

    keepax = ()
    for i in range(4):
        if i not in meanax:
            keepax += (i,)

    fh = mfdset(fil)
    (xs,xe),(ys,ye),dimh = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
            slat=ystart, nlat=yend,zs=zs,ze=ze)
    fhgeo = dset(geofil)
    D, (ah,aq) = rdp1.getgeombyindx(fhgeo,xs,xe,ys,ye)[0:2]
    fhgeo.close()
    nt = dimh[0].size
    t0 = time.time()

    uh = fh.variables['uh'][0:1,zs:ze,ys:ye,xs-1:xe]
    vh = fh.variables['vh'][0:1,zs:ze,ys-1:ye,xs:xe]
    em = fh.variables['e'][0:1,zs:ze,ys:ye,xs:xe]/nt
    #dhdtm = fh.variables['dhdt'][0:1,zs:ze,ys:ye,xs:xe]/nt
    wd = fh.variables['wd'][0:1,zs:ze,ys:ye,xs:xe]

    uh = np.ma.filled(uh.astype(float), 0)
    uhx = np.diff(uh,axis = 3)/ah
    uhxm = uhx/nt

    vh = np.ma.filled(vh.astype(float), 0)
    vhy = np.diff(vh,axis = 2)/ah
    vhym = vhy/nt

    wdm = np.diff(wd,axis=1)/nt

    for i in range(1,nt):
        uh = fh.variables['uh'][i:i+1,zs:ze,ys:ye,xs-1:xe]
        vh = fh.variables['vh'][i:i+1,zs:ze,ys-1:ye,xs:xe]
        em += fh.variables['e'][i:i+1,zs:ze,ys:ye,xs:xe]/nt
        #dhdtm += fh.variables['dhdt'][i:i+1,zs:ze,ys:ye,xs:xe]/nt
        wd = fh.variables['wd'][i:i+1,zs:ze,ys:ye,xs:xe]

        uh = np.ma.filled(uh.astype(float), 0)
        uhx = np.diff(uh,axis = 3)/ah
        uhxm += uhx/nt

        vh = np.ma.filled(vh.astype(float), 0)
        vhy = np.diff(vh,axis = 2)/ah
        vhym += vhy/nt

        wdm += np.diff(wd,axis=1)/nt

        sys.stdout.write('\r'+str(int((i+1)/nt*100))+'% done...')
        sys.stdout.flush()

    fh.close()
    print('Total reading time: {}s'.format(time.time()-t0))

    terms = np.ma.concatenate(( uhxm[:,:,:,:,np.newaxis],
                                vhym[:,:,:,:,np.newaxis],
                                wdm[:,:,:,:,np.newaxis]),axis=4)

    termsm = np.ma.apply_over_axes(np.mean, terms, meanax)
    elm = 0.5*(em[:,0:-1,:,:]+em[:,1:,:,:])
    em = np.ma.apply_over_axes(np.mean, em, meanax)
    elm = np.ma.apply_over_axes(np.mean, elm, meanax)

    X = dimh[keepax[1]]
    Y = dimh[keepax[0]]
    if 1 in keepax:
        Y = elm.squeeze()
        X = np.meshgrid(X,dimh[1])[0]

    P = termsm.squeeze()
    P = np.ma.filled(P.astype(float), np.nan)
    X = np.ma.filled(X.astype(float), np.nan)
    Y = np.ma.filled(Y.astype(float), np.nan)
    np.savez('cb_terms', X=X,Y=Y,P=P)

    return (X,Y,P)
Esempio n. 16
0
def extract_twamomy_terms(geofil,
                          vgeofil,
                          fil,
                          fil2,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          fil3=None,
                          alreadysaved=False,
                          xyasindices=False,
                          calledfrompv=False,
                          z=np.linspace(-3000, 0, 100),
                          htol=1e-3):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhvgeo = dset(vgeofil)
        db = -fhvgeo.variables['g'][:]
        dbi = np.append(db, 0)
        fhvgeo.close()

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        fh2 = mfdset(fil2)
        zi = rdp1.getdims(fh)[2][0]
        dbl = np.diff(zi) * 9.8 / 1031
        if xyasindices:
            (xs, xe), (ys, ye) = (xstart, xend), (ystart, yend)
            _, _, dimv = rdp1.getdimsbyindx(fh,
                                            xs,
                                            xe,
                                            ys,
                                            ye,
                                            zs=zs,
                                            ze=ze,
                                            ts=0,
                                            te=None,
                                            xhxq='xh',
                                            yhyq='yq',
                                            zlzi='zl')
        else:
            (xs, xe), (ys, ye), dimv = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          yhyq='yq')
        sl = np.s_[:, :, ys:ye, xs:xe]
        slmx = np.s_[:, :, ys:ye, xs - 1:xe]
        D, (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0:2]
        Dforgetutwaforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[0]
        Dforgetutwaforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                                 ye + 1)[0]
        Dforgethvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye)[0]
        dxt, dyt = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][6:8]
        dxcv, dycv = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][2:4]
        dxcvforxdiff, dycvforxdiff = rdp1.getgeombyindx(
            fhgeo, xs - 1, xe, ys, ye)[2][2:4]
        dxcvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                          ye + 1)[2][3:4]
        dxbu, dybu = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[2][4:6]
        nt_const = dimv[0].size
        t0 = time.time()
        dt = fh.variables['average_DT'][:]
        dt = dt[:, np.newaxis, np.newaxis, np.newaxis]

        if fil3:
            fh3 = mfdset(fil3)
            slmxtn = np.s_[-1:, :, ys:ye, xs - 1:xe]
            #            islayerdeep0 = fh3.variables['islayerdeep'][:,0,0,0].sum()
            #            islayerdeep = (fh3.variables['islayerdeep'][slmx].filled(np.nan)).sum(axis=0,
            #                                                                               keepdims=True)
            islayerdeep0 = fh3.variables['islayerdeep'][-1:, 0, 0, 0]
            islayerdeep = (fh3.variables['islayerdeep'][slmxtn].filled(np.nan))
            islayerdeep[:, :, :, -1:] = islayerdeep[:, :, :, -2:-1]
            swash = (islayerdeep0 - islayerdeep) / islayerdeep0 * 100
            swash = 0.5 * (swash[:, :, :, :-1] + swash[:, :, :, 1:])
            fh3.close()
        else:
            swash = None

        em = (fh2.variables['e'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])

        vh = (fh.variables['vh_masked'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        h_cv = (fh.variables['h_Cv'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        h_cv[h_cv < htol] = np.nan
        h_vm = h_cv
        vtwa = vh / h_cv / dxcv

        vhforxdiff = (fh.variables['vh_masked'][0:, zs:ze, ys:ye, xs - 1:xe] *
                      dt).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cvforxdiff = (fh.variables['h_Cv'][0:, zs:ze, ys:ye, xs - 1:xe] *
                        dt).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cvforxdiff[h_cvforxdiff < htol] = np.nan
        vtwaforxdiff = vhforxdiff / h_cvforxdiff  #/dxcvforxdiff
        vtwaforxdiff = np.concatenate(
            (vtwaforxdiff, vtwaforxdiff[:, :, :, -1:]), axis=3)

        vhforydiff = (
            fh.variables['vh_masked'][0:, zs:ze, ys - 1:ye + 1, xs:xe] *
            dt).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cvforydiff = (fh.variables['h_Cv'][0:, zs:ze, ys - 1:ye + 1, xs:xe] *
                        dt).sum(axis=0, keepdims=True) / np.sum(dt)
        h_cvforydiff[h_cvforydiff < htol] = np.nan
        vtwaforydiff = vhforydiff / h_cvforydiff  #/dxcvforydiff

        vtwax = np.diff(vtwaforxdiff, axis=3) / dxbu / dybu
        vtwax = 0.5 * (vtwax[:, :, :, 0:-1] + vtwax[:, :, :, 1:])

        vtway = np.diff(vtwaforydiff, axis=2) / dyt / dxt
        vtway = 0.5 * (vtway[:, :, 0:-1, :] + vtway[:, :, 1:, :])

        hvmy = np.diff(vhforydiff, axis=2) / dyt / dxt
        hvmy = 0.5 * (hvmy[:, :, :-1, :] + hvmy[:, :, 1:, :])

        uh = (fh.variables['uh_masked'][0:, zs:ze, ys:ye + 1, xs - 1:xe] *
              dt).filled(0).sum(axis=0, keepdims=True) / np.sum(dt)
        hum = 0.25 * (uh[:, :, :-1, :-1] + uh[:, :, :-1, 1:] +
                      uh[:, :, 1:, :-1] + uh[:, :, 1:, 1:]) / dycv

        humx = np.diff(np.nan_to_num(uh), axis=3) / dxt / dyt
        humx = 0.5 * (humx[:, :, :-1, :] + humx[:, :, 1:, :])

        huvxphvvym = (
            fh.variables['twa_huvxpt'][0:, zs:ze, ys:ye, xs:xe] * dt +
            fh.variables['twa_hvvymt'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
                axis=0, keepdims=True) / np.sum(dt)
        hvv = (fh.variables['hvv_Cv'][0:, zs:ze, ys - 1:ye + 1, xs:xe] *
               dt).sum(axis=0, keepdims=True) / np.sum(dt)
        hvvym = np.diff(hvv, axis=2) / dxt / dyt
        hvvym = 0.5 * (hvvym[:, :, :-1, :] + hvvym[:, :, 1:, :])
        #        hvv = (fh.variables['hvv_T'][0:,zs:ze,ys:ye+1,xs:xe]*dt).sum(axis=0,keepdims=True)*dxt/np.sum(dt)
        #        hvvym = np.diff(hvv,axis=2)/dycv/dxcv
        huvxm = -(huvxphvvym + hvvym)

        #        hvv = (fh.variables['hvv_Cv'][0:,zs:ze,ys-1:ye+1,xs:xe]*dt).sum(axis=0,keepdims=True)/np.sum(dt)
        #        hvvym = np.diff(hvv,axis=2)/dxt/dyt
        #        hvvym = 0.5*(hvvym[:,:,:-1,:] + hvvym[:,:,1:,:])
        #        huv = (fh.variables['huv_Bu'][0:,zs:ze,ys:ye,xs-1:xe].filled(0)*dt).sum(axis=0,keepdims=True)/np.sum(dt)
        #        huvxm = np.diff(huv,axis=3)/dxcv

        vtwaforvdiff = np.concatenate((vtwa[:, [0], :, :], vtwa), axis=1)
        vtwab = np.diff(vtwaforvdiff, axis=1) / db[:, np.newaxis, np.newaxis]
        vtwab = np.concatenate(
            (vtwab,
             np.zeros([vtwab.shape[0], 1, vtwab.shape[2], vtwab.shape[3]])),
            axis=1)
        vtwab = 0.5 * (vtwab[:, 0:-1, :, :] + vtwab[:, 1:, :, :])

        hwb = (fh2.variables['wd'][0:, zs:ze, ys:ye + 1, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hwb = np.diff(hwb, axis=1)
        hwb_v = 0.5 * (hwb[:, :, :-1, :] + hwb[:, :, 1:, :])
        hwb = (fh2.variables['wd'][0:, zs:ze, ys:ye + 1, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hwm = hwb_v * dbl[:, np.newaxis, np.newaxis]
        hwm = 0.5 * (hwb[:, :-1] + hwb[:, 1:]) * dbl[:, np.newaxis, np.newaxis]
        hwm_v = 0.5 * (hwm[:, :, :-1, :] + hwm[:, :, 1:, :])
        #hwb_v = fh.variables['hwb_Cv'][0:,zs:ze,ys:ye,xs:xe]
        #hwm_v = fh.variables['hw_Cv'][0:,zs:ze,ys:ye,xs:xe]

        esq = (fh.variables['esq'][0:, zs:ze, ys:ye + 1, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        emforydiff = (fh2.variables['e'][0:, zs:ze, ys:ye + 1, xs:xe] *
                      dt).sum(axis=0, keepdims=True) / np.sum(dt)
        elmforydiff = 0.5 * (emforydiff[:, 0:-1, :, :] +
                             emforydiff[:, 1:, :, :])
        edlsqm = (esq - elmforydiff**2)
        edlsqmy = np.diff(edlsqm, axis=2) / dycv

        hpfv = (fh.variables['twa_hpfv'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        pfvm = (fh2.variables['PFv'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        edpfvdmb = -hpfv + h_cv * pfvm - 0.5 * edlsqmy * dbl[:, np.newaxis,
                                                             np.newaxis]

        hmfum = (fh.variables['twa_hmfu'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hvwbm = (fh.variables['twa_hvwb'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
            axis=0, keepdims=True) / np.sum(dt)
        hdiffvm = (fh.variables['twa_hdiffv'][0:, zs:ze, ys:ye, xs:xe] *
                   dt).sum(axis=0, keepdims=True) / np.sum(dt)
        hdvdtviscm = (fh.variables['twa_hdvdtvisc'][0:, zs:ze, ys:ye, xs:xe] *
                      dt).sum(axis=0, keepdims=True) / np.sum(dt)
        fh2.close()
        fh.close()

        advx = hum * vtwax / h_vm
        advy = vtwa * vtway
        advb = hwm_v * vtwab / h_vm
        cor = hmfum / h_vm
        pfvm = pfvm

        xdivep1 = -huvxm / h_vm
        xdivep2 = advx
        xdivep3 = vtwa * humx / h_vm
        xdivep = (xdivep1 + xdivep2 + xdivep3)

        ydivep1 = -hvvym / h_vm
        ydivep2 = advy
        ydivep3 = vtwa * hvmy / h_vm
        ydivep4 = -0.5 * edlsqmy * dbl[:, np.newaxis, np.newaxis] / h_vm
        ydivep = (ydivep1 + ydivep2 + ydivep3 + ydivep4)

        bdivep1 = hvwbm / h_vm
        bdivep2 = advb
        bdivep3 = vtwa * hwb_v / h_vm
        bdivep4 = -edpfvdmb / h_vm
        bdivep = (bdivep1 + bdivep2 + bdivep3 + bdivep4)
        Y1twa = hdiffvm / h_vm
        Y2twa = hdvdtviscm / h_vm

        terms = np.concatenate(
            (-advx[:, :, :, :, np.newaxis], -advy[:, :, :, :, np.newaxis],
             -advb[:, :, :, :, np.newaxis], cor[:, :, :, :, np.newaxis],
             pfvm[:, :, :, :, np.newaxis], xdivep[:, :, :, :, np.newaxis],
             ydivep[:, :, :, :, np.newaxis], bdivep[:, :, :, :, np.newaxis],
             Y1twa[:, :, :, :, np.newaxis], Y2twa[:, :, :, :, np.newaxis]),
            axis=4)
        termsep = np.concatenate(
            (xdivep1[:, :, :, :, np.newaxis], xdivep3[:, :, :, :, np.newaxis],
             ydivep1[:, :, :, :, np.newaxis], ydivep3[:, :, :, :, np.newaxis],
             ydivep4[:, :, :, :, np.newaxis], bdivep1[:, :, :, :, np.newaxis],
             bdivep3[:, :, :, :, np.newaxis], bdivep4[:, :, :, :, np.newaxis]),
            axis=4)

        termsm = np.nanmean(terms, meanax, keepdims=True)
        termsepm = np.nanmean(termsep, meanax, keepdims=True)

        X = dimv[keepax[1]]
        Y = dimv[keepax[0]]
        if 1 in keepax and not calledfrompv:
            em = np.nanmean(em, axis=meanax, keepdims=True)
            elm = np.nanmean(elm, axis=meanax, keepdims=True)
            #z = np.linspace(-3000,0,100)
            Y = z
            P = getvaratzc5(termsm.astype(np.float32), z.astype(np.float32),
                            em.astype(np.float32))
            Pep = getvaratzc5(termsepm.astype(np.float32),
                              z.astype(np.float32), em.astype(np.float32))
            if fil3:
                swash = np.nanmean(swash, meanax, keepdims=True)
                swash = getvaratzc(swash.astype(np.float32),
                                   z.astype(np.float32),
                                   em.astype(np.float32)).squeeze()
        else:
            P = termsm.squeeze()
            Pep = termsepm.squeeze()

        if not calledfrompv:
            np.savez('twamomy_complete_terms', X=X, Y=Y, P=P, Pep=Pep)
    else:
        npzfile = np.load('twamomy_complete_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        Pep = npzfile['Pep']

    return (X, Y, P, Pep, swash, em.squeeze())
Esempio n. 17
0
def extract_twapv_terms(geofil,
                        vgeofil,
                        fil,
                        fil2,
                        xstart,
                        xend,
                        ystart,
                        yend,
                        zs,
                        ze,
                        meanax,
                        fil3=None,
                        calledfromgeteddycoeffs=False,
                        alreadysaved=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        fh2 = mfdset(fil2)
        zi = rdp1.getdims(fh)[2][0]
        dbl = -np.diff(zi) * 9.8 / 1031
        if not calledfromgeteddycoeffs:
            (xs, xe), (ys, ye), dimq = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          xhxq='xq',
                                                          yhyq='yq')
        else:
            (xs, xe), (ys, ye), dimq = rdp1.getdimsbyindx(fh,
                                                          xstart,
                                                          xend,
                                                          ystart,
                                                          yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          ts=0,
                                                          te=None,
                                                          xhxq='xq',
                                                          yhyq='yq')

        sl = np.s_[:, zs:ze, ys:ye, xs:xe]
        nt_const = dimq[0].size

        pvhash, hq = getpv(fhgeo, fh, fh2, xs, xe, ys, ye)
        slpy = np.s_[:, zs:ze, ys:ye + 1, xs:xe]
        dxcu = fhgeo.variables['dxCu'][slpy[2:]]
        dycv = fhgeo.variables['dyCv'][sl[2:]]
        dycv = np.concatenate((dycv, dycv[:, -1:]), axis=1)
        dxbu = fhgeo.variables['dxBu'][sl[2:]]
        dybu = fhgeo.variables['dyBu'][sl[2:]]
        aq = fhgeo.variables['Aq'][sl[2:]]
        f = fhgeo.variables['f'][sl[2:]]

        if fil3:
            fh3 = mfdset(fil3)
            sltn = np.s_[-1:, zs:ze, ys:ye, xs:xe]
            islayerdeep0 = fh3.variables['islayerdeep'][-1:, 0, 0, 0]
            islayerdeep = (fh3.variables['islayerdeep'][sltn].filled(np.nan))
            swash = (islayerdeep0 - islayerdeep) / islayerdeep0 * 100
            fh3.close()
        else:
            swash = None

        xmom = extract_twamomx_terms(geofil,
                                     vgeofil,
                                     fil,
                                     fil2,
                                     xs,
                                     xe,
                                     ys,
                                     ye + 1,
                                     zs,
                                     ze, (0, ),
                                     alreadysaved=False,
                                     xyasindices=True,
                                     calledfrompv=True)[2]
        ymom = extract_twamomy_terms(geofil,
                                     vgeofil,
                                     fil,
                                     fil2,
                                     xs,
                                     xe,
                                     ys,
                                     ye,
                                     zs,
                                     ze, (0, ),
                                     alreadysaved=False,
                                     xyasindices=True,
                                     calledfrompv=True)[2]

        xmom = xmom[np.newaxis, :, :, :, :]
        ymom = ymom[np.newaxis, :, :, :, :]
        ymom = np.concatenate((ymom, ymom[:, :, :, -1:]), axis=3)

        bxppvflx = np.sum(xmom[:, :, :, :, [0, 1, 3, 4]], axis=4)
        pvhash1, _ = getpv(fhgeo, fh, fh2, xs, xe, ys - 1, ye + 1)
        sl1 = np.s_[:, zs:ze, ys - 1:ye + 1, xs:xe]
        vtwa1, h_cv1 = getvtwa(fhgeo, fh, fh2, sl1)
        vtwa1 = 0.5 * (vtwa1[:, :, :, :-1] + vtwa1[:, :, :, 1:])
        pvhashvtwa = pvhash1 * vtwa1
        sl1 = np.s_[:, zs:ze, ys:ye + 1, xs:xe]
        h_cu1 = fh.variables['h_Cu'][sl1].filled(np.nan).mean(axis=0,
                                                              keepdims=True)
        h_cu1 = np.ma.masked_array(h_cu1, mask=(h_cu1 < 1e-3))
        pvflxx = h_cu1 * (pvhashvtwa[:, :, :-1, :] +
                          pvhashvtwa[:, :, 1:, :]) / 2

        byppvflx = np.sum(ymom[:, :, :, :, [0, 1, 3, 4]], axis=4)
        pvhash1, _ = getpv(fhgeo, fh, fh2, xs - 1, xe, ys, ye)
        sl1 = np.s_[:, zs:ze, ys:ye + 1, xs - 1:xe]
        utwa1, h_cu1 = getutwa(fhgeo, fh, fh2, sl1)
        utwa1 = 0.5 * (utwa1[:, :, :-1, :] + utwa1[:, :, 1:, :])
        pvhashutwa = pvhash1 * utwa1
        pvhashutwa[:, :, :, -1] = 0.0
        sl1 = np.s_[:, zs:ze, ys:ye, xs:xe]
        h_cv1 = fh.variables['h_Cv'][sl1].mean(axis=0, keepdims=True)
        h_cv1 = np.ma.masked_array(h_cv1, mask=(h_cv1 < 1e-3))
        pvflxy = h_cv1 * (pvhashutwa[:, :, :, :-1] +
                          pvhashutwa[:, :, :, 1:]) / 2
        pvflxy = np.concatenate((pvflxy, pvflxy[:, :, :, -1:]), axis=3)

        bx = bxppvflx - pvflxx
        by = byppvflx + pvflxy

        xmom1 = xmom[:, :, :, :, [2, 5, 6, 7, 8, 9]]
        xmom1 = np.concatenate(
            (+pvflxx[:, :, :, :, np.newaxis], xmom1, bx[:, :, :, :,
                                                        np.newaxis]),
            axis=4)
        ymom1 = ymom[:, :, :, :, [2, 5, 6, 7, 8, 9]]
        ymom1 = np.concatenate(
            (-pvflxy[:, :, :, :, np.newaxis], ymom1, by[:, :, :, :,
                                                        np.newaxis]),
            axis=4)

        #pv = (-np.diff(xmom*dxcu[:,:,np.newaxis],axis=2) + np.diff(ymom*dycv[:,:,np.newaxis],axis=3))/aq[:,:,np.newaxis]
        pv = -np.diff(xmom, axis=2) / dybu[:, :, np.newaxis] + np.diff(
            ymom, axis=3) / dxbu[:, :, np.newaxis]
        pv1x = -np.diff(xmom1 * dxcu[:, :, np.newaxis],
                        axis=2) / aq[:, :, np.newaxis]
        pv1y = np.diff(ymom1 * dycv[:, :, np.newaxis], axis=3) / aq[:, :,
                                                                    np.newaxis]

        slyp = np.s_[:, :, ys:ye + 1, xs:xe]
        ah1 = fhgeo.variables['Ah'][slyp[2:]]
        slxmyp = np.s_[:, :, ys:ye + 1, xs - 1:xe]
        uh = fh2.variables['uh'][slxmyp].filled(0).mean(axis=0, keepdims=True)
        uhx = np.diff(uh, axis=3) / ah1
        uhx = np.concatenate((uhx, uhx[:, :, :, -1:]), axis=3)
        uhx = 0.25 * (uhx[:, :, :-1, :-1] + uhx[:, :, :-1, 1:] +
                      uhx[:, :, 1:, :-1] + uhx[:, :, 1:, 1:])
        pv1y[:, :, :, :, 0] += pvhash * uhx

        slymp = np.s_[:, :, ys - 1:ye + 1, xs:xe]
        vh = fh2.variables['vh'][slymp].mean(axis=0, keepdims=True)
        vhy = np.diff(vh, axis=2) / ah1
        vhy = np.concatenate((vhy, vhy[:, :, :, -1:]), axis=3)
        vhy = 0.25 * (vhy[:, :, :-1, :-1] + vhy[:, :, :-1, 1:] +
                      vhy[:, :, 1:, :-1] + vhy[:, :, 1:, 1:])
        pv1x[:, :, :, :, 0] += pvhash * vhy

        wd = fh2.variables['wd'][slyp].mean(axis=0, keepdims=True)
        wdb = np.diff(wd, axis=1)
        wdb = np.concatenate((wdb, wdb[:, :, :, -1:]), axis=3)
        wdb = 0.25 * (wdb[:, :, :-1, :-1] + wdb[:, :, :-1, 1:] +
                      wdb[:, :, 1:, :-1] + wdb[:, :, 1:, 1:])
        pv3 = pvhash * wdb
        pv3 = pv3[:, :, :, :, np.newaxis]
        #hq[hq<1] = np.nan
        pvnew = np.concatenate(
            (pv1y[:, :, :, :, :1], pv1x[:, :, :, :, :1], pv3,
             pv1x[:, :, :, :, 1:-1], pv1y[:, :, :, :, 1:-1],
             pv1x[:, :, :, :, -1:] + pv1y[:, :, :, :, -1:]),
            axis=4) / hq[:, :, :, :, np.newaxis]

        pv = np.ma.filled(pv.astype(np.float64), np.nan)
        pvnew = np.ma.filled(pvnew.astype(np.float64), np.nan)
        pvhash = np.ma.filled(pvhash.astype(np.float64), np.nan)

        pv = np.nanmean(pv, meanax, keepdims=True)
        pvnew = np.nanmean(pvnew, meanax, keepdims=True)
        pvhash = np.nanmean(pvhash, meanax, keepdims=True)

        X = dimq[keepax[1]]
        Y = dimq[keepax[0]]
        if 1 in keepax and not calledfromgeteddycoeffs:
            dt = fh.variables['average_DT'][:]
            dt = dt[:, np.newaxis, np.newaxis, np.newaxis]
            em = (fh2.variables['e'][0:, zs:ze, ys:ye, xs:xe] * dt).sum(
                axis=0, keepdims=True) / np.sum(dt)
            em = np.nanmean(em, meanax, keepdims=True)
            z = np.linspace(-3000, 0, 100)
            Y = z
            P = getvaratzc5(pv.astype(np.float32), z.astype(np.float32),
                            em.astype(np.float32)).squeeze()
            Pnew = getvaratzc5(pvnew.astype(np.float32), z.astype(np.float32),
                               em.astype(np.float32)).squeeze()
            pvhash = getvaratzc(pvhash.astype(np.float32),
                                z.astype(np.float32),
                                em.astype(np.float32)).squeeze()
            if fil3:
                swash = np.nanmean(swash, meanax, keepdims=True)
                swash = getvaratzc(swash.astype(np.float32),
                                   z.astype(np.float32),
                                   em.astype(np.float32)).squeeze()
        else:
            P = pv
            Pnew = pvnew

    fhgeo.close()
    fh.close()
    fh2.close()
    return (X, Y, P, pvhash, Pnew, swash)
def extract_twamomx_terms(geofil,fil,xstart,xend,ystart,yend,zs,ze,meanax,
      alreadysaved=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i,)

        fh = mfdset(fil)
        (xs,xe),(ys,ye),dimu = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
                slat=ystart, nlat=yend,zs=zs,ze=ze,xhxq='xq')
#        _,(vys,vye),_ = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
#                slat=ystart, nlat=yend,zs=zs,ze=ze,yhyq='yq')
#        (xs,xe),(ys,ye),_ = rdp1.getlatlonindx(fh,wlon=xstart,elon=xend,
#                slat=ystart, nlat=yend,zs=zs,ze=ze)
        D, (ah,aq) = rdp1.getgeombyindx(geofil,xs,xe,ys,ye)[0:2]
        nt_const = dimu[0].size
        t0 = time.time()

        print('Reading data using loop...')
        u = fh.variables['u'][0:1,zs:ze,ys:ye,xs:xe]
        nt = np.ones(u.shape)*nt_const
        frhatu = fh.variables['frhatu'][0:1,zs:ze,ys:ye,xs:xe]
        h_u = frhatu*D[np.newaxis,np.newaxis,:,:]
        h_u = np.ma.masked_array(h_u,mask=(h_u<=1e-3).astype(int))
        nt[h_u<=1e-3] -= 1

        cau = fh.variables['CAu'][0:1,zs:ze,ys:ye,xs:xe]
        gkeu = fh.variables['gKEu'][0:1,zs:ze,ys:ye,xs:xe]
        rvxv = fh.variables['rvxv'][0:1,zs:ze,ys:ye,xs:xe]
        pfu = fh.variables['PFu'][0:1,zs:ze,ys:ye,xs:xe]
        hagum = h_u*(cau - gkeu - rvxv + pfu)
        hagum = hagum.filled(0)
        
        hdudtviscm = h_u*fh.variables['du_dt_visc'][0:1,zs:ze,ys:ye,xs:xe]
        hdudtviscm = hdudtviscm.filled(0)
        hdiffum = h_u*fh.variables['diffu'][0:1,zs:ze,ys:ye,xs:xe]
        hdiffum = hdiffum.filled(0)

        dudtdia = fh.variables['dudt_dia'][0:1,zs:ze,ys:ye,xs:xe]
        wd = fh.variables['wd'][0:1,zs:ze,ys:ye,xs:xe]
        wd = np.diff(wd,axis=1)
        wd = np.concatenate((wd,wd[:,:,:,-1:]),axis=3)
        huwbm = (u*(wd[:,:,:,0:-1]+wd[:,:,:,1:])/2 - h_u*dudtdia)
        huwbm = huwbm.filled(0)

        uh = fh.variables['uh'][0:1,zs:ze,ys:ye,xs-1:xe]
        uh = np.ma.filled(uh.astype(float), 0)
        uhx = np.diff(uh,axis = 3)/ah
        uhx = np.concatenate((uhx,uhx[:,:,:,-1:]),axis=3)
        huuxpTm = (u*(uhx[:,:,:,0:-1]+uhx[:,:,:,1:])/2 - h_u*gkeu)
        huuxpTm = huuxpTm.filled(0)

        vh = fh.variables['vh'][0:1,zs:ze,ys-1:ye,xs:xe]
        vh = np.ma.filled(vh.astype(float), 0)
        vhy = np.diff(vh,axis = 2)/ah
        vhy = np.concatenate((vhy,vhy[:,:,:,-1:]),axis=3)
        huvymTm = (u*(vhy[:,:,:,0:-1]+vhy[:,:,:,1:])/2 - h_u*rvxv)
        huvymTm = huvymTm.filled(0)

        if 1 in keepax:
            em = fh.variables['e'][0:1,zs:ze,ys:ye,xs:xe]/nt_const

        for i in range(1,nt_const):
            u = fh.variables['u'][i:i+1,zs:ze,ys:ye,xs:xe]
            frhatu = fh.variables['frhatu'][i:i+1,zs:ze,ys:ye,xs:xe]
            h_u = frhatu*D[np.newaxis,np.newaxis,:,:]
            h_u = np.ma.masked_array(h_u,mask=(h_u<=1e-3).astype(int))
            nt[h_u<=1e-3] -= 1

            cau = fh.variables['CAu'][i:i+1,zs:ze,ys:ye,xs:xe]
            gkeu = fh.variables['gKEu'][i:i+1,zs:ze,ys:ye,xs:xe]
            rvxv = fh.variables['rvxv'][i:i+1,zs:ze,ys:ye,xs:xe]
            pfu = fh.variables['PFu'][i:i+1,zs:ze,ys:ye,xs:xe]
            hagum += (h_u*(cau - gkeu - rvxv + pfu)).filled(0)
            
            hdudtviscm += (h_u*fh.variables['du_dt_visc'][i:i+1,zs:ze,
                ys:ye,xs:xe]).filled(0)
            hdiffum += (h_u*fh.variables['diffu'][i:i+1,zs:ze,
                ys:ye,xs:xe]).filled(0)

            dudtdia = fh.variables['dudt_dia'][i:i+1,zs:ze,ys:ye,xs:xe]
            wd = fh.variables['wd'][i:i+1,zs:ze,ys:ye,xs:xe]
            wd = np.diff(wd,axis=1)
            wd = np.concatenate((wd,wd[:,:,:,-1:]),axis=3)
            huwbm += ((u*(wd[:,:,:,0:-1]+wd[:,:,:,1:])/2 -
                h_u*dudtdia)).filled(0)

            uh = fh.variables['uh'][i:i+1,zs:ze,ys:ye,xs-1:xe]
            uh = np.ma.filled(uh.astype(float), 0)
            uhx = np.diff(uh,axis = 3)/ah
            uhx = np.concatenate((uhx,uhx[:,:,:,-1:]),axis=3)
            huuxpTm += ((u*(uhx[:,:,:,0:-1]+uhx[:,:,:,1:])/2 -
                h_u*gkeu)).filled(0)

            vh = fh.variables['vh'][i:i+1,zs:ze,ys-1:ye,xs:xe]
            vh = np.ma.filled(vh.astype(float), 0)
            vhy = np.diff(vh,axis = 2)/ah
            vhy = np.concatenate((vhy,vhy[:,:,:,-1:]),axis=3)
            huvymTm += ((u*(vhy[:,:,:,0:-1]+vhy[:,:,:,1:])/2 -
                h_u*rvxv)).filled(0)
            if 1 in keepax:
                em += fh.variables['e'][i:i+1,zs:ze,ys:ye,xs:xe]/nt_const

            sys.stdout.write('\r'+str(int((i+1)/nt_const*100))+'% done...')
            sys.stdout.flush()
            
        fh.close()
        print('Time taken for data reading: {}s'.format(time.time()-t0))

        terms = np.ma.concatenate(( hagum[:,:,:,:,np.newaxis],
                                    -huwbm[:,:,:,:,np.newaxis],
                                    -huuxpTm[:,:,:,:,np.newaxis],
                                    -huvymTm[:,:,:,:,np.newaxis],
                                    hdudtviscm[:,:,:,:,np.newaxis],
                                    hdiffum[:,:,:,:,np.newaxis]),
                                    axis=4)/nt[:,:,:,:,np.newaxis]

        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)

        X = dimu[keepax[1]]
        Y = dimu[keepax[0]]
        if 1 in keepax:
            elm = 0.5*(em[:,0:-1,:,:]+em[:,1:,:,:])
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X,dimu[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        np.savez('twamomx_terms', X=X,Y=Y,P=P)
    else:
        npzfile = np.load('twamomx_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        
    return (X,Y,P)
def animatezeta(fhgeo,
                fh,
                fig=None,
                wlon=-25,
                elon=0,
                slat=10,
                nlat=60,
                zs=0,
                ze=1,
                **plotkwargs):
    """Sets up the initial figure window and 
    returns and animation object."""
    if fig is None:
        fig, ax = plt.subplots()

    (xs, xe), (ys, ye), dimq = rdp1.getlatlonindx(fh,
                                                  wlon=wlon,
                                                  elon=elon,
                                                  slat=slat,
                                                  nlat=nlat,
                                                  zs=zs,
                                                  ze=ze,
                                                  zlzi='zlremap',
                                                  xhxq='xq',
                                                  yhyq='yq')
    dxbu, dybu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][4:6]
    pdims = ()
    for i in range(1, 4):
        if dimq[i].size != 1:
            pdims += (dimq[i], )
    y, x = pdims
    nt = dimq[0].size
    cmap = plotkwargs.get('cmap', 'viridis')
    xlabel = plotkwargs.get('xlabel', '')
    ylabel = plotkwargs.get('ylabel', '')
    title = plotkwargs.get('title', '')
    aspect = plotkwargs.get('aspect', 'auto')
    vmin = plotkwargs.get('vmin', None)
    vmax = plotkwargs.get('vmax', None)
    inverty = plotkwargs.get('inverty', False)
    bvnorm = plotkwargs.get('bvnorm', False)

    u = fh.variables['u'][:1, zs:ze, ys:ye, xs:xe]
    u = np.concatenate((u, -u[:, :, -1:, :]), axis=2)
    v = fh.variables['v'][:1, zs:ze, ys:ye, xs:xe]
    v = np.concatenate((v, -v[:, :, :, -1:]), axis=3)
    zeta = np.diff(v, axis=3) / dxbu - np.diff(u, axis=2) / dybu
    pvar = zeta.squeeze()

    if vmin != None and vmax != None:
        if bvnorm:
            bounds = np.linspace(vmin, vmax, 25)
            norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
            im = ax.pcolormesh(x,
                               y,
                               pvar,
                               norm=norm,
                               cmap=cmap,
                               shading='flat')
        else:
            im = ax.pcolormesh(x,
                               y,
                               pvar,
                               vmax=vmax,
                               vmin=vmin,
                               cmap=cmap,
                               shading='flat')
    else:
        im = ax.pcolormesh(x, y, pvar, cmap=cmap, shading='flat')

    plt.colorbar(im)
    tim = date.fromordinal(int(fh.variables['Time'][0])).isoformat()
    txt = ax.text(0.05, 0.025, tim, transform=ax.transAxes)
    ax.set_aspect(aspect)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    if inverty:
        ax.invert_yaxis()

    def animator(i):
        """Updates the figure for each iteration"""
        u = fh.variables['u'][i:i + 1, zs:ze, ys:ye, xs:xe]
        u = np.concatenate((u, -u[:, :, -1:, :]), axis=2)
        v = fh.variables['v'][i:i + 1, zs:ze, ys:ye, xs:xe]
        v = np.concatenate((v, -v[:, :, :, -1:]), axis=3)
        zeta = np.diff(v, axis=3) / dxbu - np.diff(u, axis=2) / dybu
        pvar = zeta.squeeze()
        pvar = pvar[:-1, :-1]
        im.set_array(pvar.ravel())
        tim = date.fromordinal(int(fh.variables['Time'][i])).isoformat()
        txt.set_text(tim)
        sys.stdout.write('\r' + str(int((i + 1) / nt * 100)) + '% done...')
        sys.stdout.flush()
        return im, txt

    anim = animation.FuncAnimation(fig,
                                   animator,
                                   frames=nt,
                                   interval=100,
                                   blit=False)
    return anim
def extract_twamomx_terms(geofil,
                          vgeofil,
                          fil,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          alreadysaved=False,
                          xyasindices=False,
                          calledfrompv=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhvgeo = dset(vgeofil)
        db = -fhvgeo.variables['g'][:]
        dbi = np.append(db, 0)
        fhvgeo.close()

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        zi = rdp1.getdims(fh)[2][0]
        dbl = -np.diff(zi) * 9.8 / 1031
        if xyasindices:
            (xs, xe), (ys, ye) = (xstart, xend), (ystart, yend)
            _, _, dimu = rdp1.getdimsbyindx(fh,
                                            xs,
                                            xe,
                                            ys,
                                            ye,
                                            zs=zs,
                                            ze=ze,
                                            ts=0,
                                            te=None,
                                            xhxq='xq',
                                            yhyq='yh',
                                            zlzi='zl')
        else:
            (xs, xe), (ys, ye), dimu = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          xhxq='xq')
        D, (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0:2]
        Dforgetutwaforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[0]
        Dforgetutwaforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                                 ye + 1)[0]
        Dforgethvforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1, ye)[0]
        dxt, dyt = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][6:8]
        dxcu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][0]
        dybu = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][5]
        nt_const = dimu[0].size
        t0 = time.time()

        print('Reading data using loop...')
        h = fh.variables['h'][0:1, zs:ze, ys:ye, xs:xe]
        u = fh.variables['u'][0:1, zs:ze, ys:ye, xs:xe]
        nt = np.ones(u.shape) * nt_const
        frhatu = fh.variables['frhatu'][0:1, zs:ze, ys:ye, xs:xe]
        h_u = frhatu * D[np.newaxis, np.newaxis, :, :]
        h_u = np.ma.masked_array(h_u, mask=(h <= 1e-3).astype(int))
        nt[h <= 1e-3] -= 1
        hum = (h_u * u).filled(0)
        huum = (h_u * u * u).filled(0)
        h_um = h_u.filled(0)

        humforxdiff, h_umforxdiff, huumforxdiff = getutwaforxdiff(
            fh, fhgeo, Dforgetutwaforxdiff, 0, xs - 1, xe, ys, ye, zs, ze)
        humforydiff, h_umforydiff = getutwaforydiff(fh, fhgeo,
                                                    Dforgetutwaforydiff, 0, xs,
                                                    xe, ys - 1, ye + 1, zs, ze)

        cau = fh.variables['CAu'][0:1, zs:ze, ys:ye, xs:xe]
        gkeu = fh.variables['gKEu'][0:1, zs:ze, ys:ye, xs:xe]
        rvxv = fh.variables['rvxv'][0:1, zs:ze, ys:ye, xs:xe]
        hfvm = h_u * (cau - gkeu - rvxv).filled(0)

        pfu = fh.variables['PFu'][0:1, zs:ze, ys:ye, xs:xe]
        pfu = np.ma.masked_array(pfu, mask=(h <= 1e-3).astype(int))
        pfum = pfu.filled(0)

        hdudtviscm = (
            h_u *
            fh.variables['du_dt_visc'][0:1, zs:ze, ys:ye, xs:xe]).filled(0)
        hdiffum = (h_u *
                   fh.variables['diffu'][0:1, zs:ze, ys:ye, xs:xe]).filled(0)

        dudtdia = fh.variables['dudt_dia'][0:1, zs:ze, ys:ye, xs:xe]
        wd = fh.variables['wd'][0:1, zs:ze, ys:ye, xs:xe]
        wd = np.diff(wd, axis=1)
        wd = np.concatenate((wd, wd[:, :, :, -1:]), axis=3)
        wdm = 0.5 * (wd[:, :, :, :-1] + wd[:, :, :, 1:])
        wdm = np.ma.masked_array(wdm, mask=(h <= 1e-3).astype(int))
        wdm = wdm.filled(0)
        huwbm = (u * (wd[:, :, :, 0:-1] + wd[:, :, :, 1:]) / 2 -
                 h_u * dudtdia).filled(0)

        uh = fh.variables['uh'][0:1, zs:ze, ys:ye, xs - 1:xe]
        uh = np.ma.filled(uh.astype(float), 0)
        uhx = np.diff(uh, axis=3) / ah
        uhx = np.concatenate((uhx, uhx[:, :, :, -1:]), axis=3)
        huuxpTm = (u * (uhx[:, :, :, 0:-1] + uhx[:, :, :, 1:]) / 2 -
                   h_u * gkeu).filled(0)

        vh = fh.variables['vh'][0:1, zs:ze, ys - 1:ye, xs:xe]
        vh = np.ma.filled(vh.astype(float), 0)
        vhy = np.diff(vh, axis=2) / ah
        vhy = np.concatenate((vhy, vhy[:, :, :, -1:]), axis=3)
        huvymTm = (u * (vhy[:, :, :, 0:-1] + vhy[:, :, :, 1:]) / 2 -
                   h_u * rvxv).filled(0)

        v = fh.variables['v'][0:1, zs:ze, ys - 1:ye, xs:xe]
        v = np.concatenate((v, -v[:, :, :, [-1]]), axis=3)
        v = 0.25 * (v[:, :, 0:-1, 0:-1] + v[:, :, 1:, 0:-1] +
                    v[:, :, 0:-1, 1:] + v[:, :, 1:, 1:])
        hvm = (h_u * v).filled(0)

        hvmforydiff = gethvforydiff(fh, fhgeo, Dforgethvforydiff, 0, xs, xe,
                                    ys - 1, ye, zs, ze)

        wd = fh.variables['wd'][0:1, zs:ze, ys:ye, xs:xe]
        wd = np.concatenate((wd, -wd[:, :, :, -1:]), axis=3)
        hw = wd * dbi[:, np.newaxis, np.newaxis]
        hw = 0.5 * (hw[:, 0:-1, :, :] + hw[:, 1:, :, :])
        hwm_u = 0.5 * (hw[:, :, :, 0:-1] + hw[:, :, :, 1:])
        hwm_u = np.ma.masked_array(hwm_u, mask=(h <= 1e-3).astype(int))
        hwm_u = hwm_u.filled(0)

        e = fh.variables['e'][0:1, zs:ze, ys:ye, xs:xe]
        em = e / nt_const
        el = 0.5 * (e[:, :-1, :, :] + e[:, 1:, :, :])
        el = np.ma.masked_array(el, mask=(h <= 1e-3).astype(int))
        elm = el.filled(0)

        esq = el**2
        esq = np.ma.masked_array(esq, mask=(h <= 1e-3).astype(int))
        esqm = esq.filled(0)

        el = np.concatenate((el, el[:, :, :, -1:]), axis=3)
        el = 0.5 * (el[:, :, :, :-1] + el[:, :, :, 1:])
        elmatu = el.filled(0)
        epfum = (pfu * el).filled(0)

        for i in range(1, nt_const):
            h = fh.variables['h'][i:i + 1, zs:ze, ys:ye, xs:xe]
            u = fh.variables['u'][i:i + 1, zs:ze, ys:ye, xs:xe]
            frhatu = fh.variables['frhatu'][i:i + 1, zs:ze, ys:ye, xs:xe]
            h_u = frhatu * D[np.newaxis, np.newaxis, :, :]
            h_u = np.ma.masked_array(h_u, mask=(h <= 1e-3).astype(int))
            nt[h <= 1e-3] -= 1
            hum += (h_u * u).filled(0)
            h_um += h_u.filled(0)

            huforxdiff, h_uforxdiff, huuforxdiff = getutwaforxdiff(
                fh, fhgeo, Dforgetutwaforxdiff, i, xs - 1, xe, ys, ye, zs, ze)
            huforydiff, h_uforydiff = getutwaforydiff(fh, fhgeo,
                                                      Dforgetutwaforydiff, i,
                                                      xs, xe, ys - 1, ye + 1,
                                                      zs, ze)

            humforxdiff += huforxdiff
            h_umforxdiff += h_uforxdiff
            huumforxdiff += huuforxdiff
            humforydiff += huforydiff
            h_umforydiff += h_uforydiff

            cau = fh.variables['CAu'][i:i + 1, zs:ze, ys:ye, xs:xe]
            gkeu = fh.variables['gKEu'][i:i + 1, zs:ze, ys:ye, xs:xe]
            rvxv = fh.variables['rvxv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            hfv = h_u * (cau - gkeu - rvxv)
            hfvm += hfv.filled(0)

            pfu = fh.variables['PFu'][i:i + 1, zs:ze, ys:ye, xs:xe]
            pfu = np.ma.masked_array(pfu, mask=(h <= 1e-3).astype(int))
            pfum += pfu.filled(0)

            hdudtvisc = h_u * fh.variables['du_dt_visc'][i:i + 1, zs:ze, ys:ye,
                                                         xs:xe]
            hdudtviscm += hdudtvisc.filled(0)
            hdiffu = h_u * fh.variables['diffu'][i:i + 1, zs:ze, ys:ye, xs:xe]
            hdiffum += hdiffu.filled(0)

            dudtdia = fh.variables['dudt_dia'][i:i + 1, zs:ze, ys:ye, xs:xe]
            wd = fh.variables['wd'][i:i + 1, zs:ze, ys:ye, xs:xe]
            wd = np.diff(wd, axis=1)
            wd = np.concatenate((wd, wd[:, :, :, -1:]), axis=3)
            huwb = (u * (wd[:, :, :, 0:-1] + wd[:, :, :, 1:]) / 2 -
                    h_u * dudtdia)
            huwb = np.ma.masked_array(huwb, mask=(h_u <= 1e-3).astype(int))
            huwbm += huwb.filled(0)
            wd = 0.5 * (wd[:, :, :, :-1] + wd[:, :, :, 1:])
            wd = np.ma.masked_array(wd, mask=(h <= 1e-3).astype(int))
            wd = wd.filled(0)
            wdm += wd

            uh = fh.variables['uh'][i:i + 1, zs:ze, ys:ye, xs - 1:xe]
            uh = np.ma.filled(uh.astype(float), 0)
            uhx = np.diff(uh, axis=3) / ah
            uhx = np.concatenate((uhx, uhx[:, :, :, -1:]), axis=3)
            huuxpT = (u * (uhx[:, :, :, 0:-1] + uhx[:, :, :, 1:]) / 2 -
                      h_u * gkeu)
            huuxpT = np.ma.masked_array(huuxpT, mask=(h_u <= 1e-3).astype(int))
            huuxpTm += huuxpT.filled(0)

            vh = fh.variables['vh'][i:i + 1, zs:ze, ys - 1:ye, xs:xe]
            vh = np.ma.filled(vh.astype(float), 0)
            vhy = np.diff(vh, axis=2) / ah
            vhy = np.concatenate((vhy, vhy[:, :, :, -1:]), axis=3)
            huvymT = (u * (vhy[:, :, :, 0:-1] + vhy[:, :, :, 1:]) / 2 -
                      h_u * rvxv)
            huvymT = np.ma.masked_array(huvymT, mask=(h_u <= 1e-3).astype(int))
            huvymTm += huvymT.filled(0)

            v = fh.variables['v'][i:i + 1, zs:ze, ys - 1:ye, xs:xe]
            v = np.concatenate((v, -v[:, :, :, [-1]]), axis=3)
            v = 0.25 * (v[:, :, 0:-1, 0:-1] + v[:, :, 1:, 0:-1] +
                        v[:, :, 0:-1, 1:] + v[:, :, 1:, 1:])
            hv = h_u * v
            hvm += hv.filled(0)

            hvmforydiff += gethvforydiff(fh, fhgeo, Dforgethvforydiff, i, xs,
                                         xe, ys - 1, ye, zs, ze)

            wd = fh.variables['wd'][i:i + 1, zs:ze, ys:ye, xs:xe]
            wd = np.concatenate((wd, -wd[:, :, :, -1:]), axis=3)
            hw = wd * dbi[:, np.newaxis, np.newaxis]
            hw = 0.5 * (hw[:, 0:-1, :, :] + hw[:, 1:, :, :])
            hw_u = 0.5 * (hw[:, :, :, 0:-1] + hw[:, :, :, 1:])
            hw_u = np.ma.masked_array(hw_u, mask=(h <= 1e-3).astype(int))
            hw_u = hw_u.filled(0)
            hwm_u += hw_u

            e = fh.variables['e'][i:i + 1, zs:ze, ys:ye, xs:xe]
            em += e / nt_const
            el = 0.5 * (e[:, :-1, :, :] + e[:, 1:, :, :])
            el = np.ma.masked_array(el, mask=(h <= 1e-3).astype(int))
            elm += el.filled(0)

            esq = el**2
            esqm += esq.filled(0)

            el = np.concatenate((el, el[:, :, :, -1:]), axis=3)
            el = 0.5 * (el[:, :, :, :-1] + el[:, :, :, 1:])
            elmatu += el.filled(0)
            epfum += (pfu * el).filled(0)

            sys.stdout.write('\r' + str(int((i + 1) / nt_const * 100)) +
                             '% done...')
            sys.stdout.flush()

        fhgeo.close()
        fh.close()
        print('Time taken for data reading: {}s'.format(time.time() - t0))

        utwa = hum / h_um
        utwaforxdiff = humforxdiff / h_umforxdiff
        utwaforydiff = humforydiff / h_umforydiff

        utwaforxdiff[np.isnan(utwaforxdiff)] = 0
        utwax = np.diff(utwaforxdiff, axis=3) / dxt
        utwax = np.concatenate((utwax, -utwax[:, :, :, [-1]]), axis=3)
        utwax = 0.5 * (utwax[:, :, :, 0:-1] + utwax[:, :, :, 1:])

        utway = np.diff(utwaforydiff, axis=2) / dybu
        utway = 0.5 * (utway[:, :, 0:-1, :] + utway[:, :, 1:, :])

        humx = np.diff(humforxdiff, axis=3) / dxt
        humx = np.concatenate((humx, -humx[:, :, :, [-1]]), axis=3)
        humx = 0.5 * (humx[:, :, :, 0:-1] + humx[:, :, :, 1:])

        hvmy = np.diff(hvmforydiff, axis=2) / dyt
        hvmy = np.concatenate((hvmy, -hvmy[:, :, :, [-1]]), axis=3)
        hvmy = 0.5 * (hvmy[:, :, :, 0:-1] + hvmy[:, :, :, 1:])

        huuxphuvym = huuxpTm + huvymTm
        huuxm = np.diff(huumforxdiff, axis=3) / dxt
        huuxm = np.concatenate((huuxm, -huuxm[:, :, :, [-1]]), axis=3)
        huuxm = 0.5 * (huuxm[:, :, :, 0:-1] + huuxm[:, :, :, 1:])
        huvym = huuxphuvym - huuxm

        utwaforvdiff = np.concatenate((utwa[:, [0], :, :], utwa), axis=1)
        utwab = np.diff(utwaforvdiff, axis=1) / db[:, np.newaxis, np.newaxis]
        utwab = np.concatenate(
            (utwab,
             np.zeros([utwab.shape[0], 1, utwab.shape[2], utwab.shape[3]])),
            axis=1)
        utwab = 0.5 * (utwab[:, 0:-1, :, :] + utwab[:, 1:, :, :])

        hwb_u = wdm

        edlsqm = esqm / nt - (elm / nt)**2
        edlsqm = np.concatenate((edlsqm, edlsqm[:, :, :, [-1]]), axis=3)
        edlsqmx = np.diff(edlsqm, axis=3) / dxcu * nt

        edpfudm = (epfum / nt - elmatu / nt * pfum / nt) * nt
        edpfudmb = np.diff(edpfudm, axis=1)
        #        edpfudmb = np.diff(edpfudm,axis=1)/db[1:,np.newaxis,np.newaxis]
        edpfudmb = np.concatenate(
            (edpfudmb[:, :1, :, :], edpfudmb, edpfudmb[:, -1:, :, :]), axis=1)
        edpfudmb = 0.5 * (edpfudmb[:, :-1, :, :] + edpfudmb[:, 1:, :, :])

        advx = utwa * utwax
        advy = hvm * utway / h_um
        advb = hwm_u * utwab / h_um
        cor = hfvm / h_um
        pfum = pfum / nt

        xdivep1 = huuxm / h_um
        xdivep2 = -advx
        xdivep3 = -utwa * humx / h_um
        xdivep4 = 0.5 * edlsqmx * dbl[:, np.newaxis, np.newaxis] / h_um
        xdivep = (xdivep1 + xdivep2 + xdivep3 + xdivep4)

        ydivep1 = huvym / h_um
        ydivep2 = -advy
        ydivep3 = -utwa * hvmy / h_um
        ydivep = (ydivep1 + ydivep2 + ydivep3)

        bdivep1 = huwbm / h_um
        bdivep2 = -advb
        bdivep3 = -utwa * hwb_u / h_um
        bdivep4 = edpfudmb / h_um
        bdivep = (bdivep1 + bdivep2 + bdivep3 + bdivep4)
        X1twa = hdiffum / h_um
        X2twa = hdudtviscm / h_um

        terms = np.ma.concatenate(
            (-advx[:, :, :, :, np.newaxis], -advy[:, :, :, :, np.newaxis],
             -advb[:, :, :, :, np.newaxis], cor[:, :, :, :, np.newaxis],
             pfum[:, :, :, :, np.newaxis], -xdivep[:, :, :, :, np.newaxis],
             -ydivep[:, :, :, :, np.newaxis], -bdivep[:, :, :, :, np.newaxis],
             X1twa[:, :, :, :, np.newaxis], X2twa[:, :, :, :, np.newaxis]),
            axis=4)
        termsep = np.ma.concatenate(
            (xdivep1[:, :, :, :, np.newaxis], xdivep3[:, :, :, :, np.newaxis],
             xdivep4[:, :, :, :, np.newaxis], ydivep1[:, :, :, :, np.newaxis],
             ydivep3[:, :, :, :, np.newaxis], bdivep1[:, :, :, :, np.newaxis],
             bdivep3[:, :, :, :, np.newaxis], bdivep4[:, :, :, :, np.newaxis]),
            axis=4)

        terms[np.isinf(terms)] = np.nan
        termsep[np.isinf(termsep)] = np.nan
        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)
        termsepm = np.ma.apply_over_axes(np.nanmean, termsep, meanax)

        elm = 0.5 * (em[:, :-1, :, :] + em[:, 1:, :, :])
        X = dimu[keepax[1]]
        Y = dimu[keepax[0]]
        if 1 in keepax:
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X, dimu[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        Pep = termsepm.squeeze()
        Pep = np.ma.filled(Pep.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        if not calledfrompv:
            np.savez('twamomx_complete_terms', X=X, Y=Y, P=P, Pep=Pep)
    else:
        npzfile = np.load('twamomx_complete_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        Pep = npzfile['Pep']

    return (X, Y, P, Pep)
def extract_twamomy_terms(geofil,
                          vgeofil,
                          fil,
                          xstart,
                          xend,
                          ystart,
                          yend,
                          zs,
                          ze,
                          meanax,
                          alreadysaved=False,
                          xyasindices=False,
                          calledfrompv=False):

    if not alreadysaved:
        keepax = ()
        for i in range(4):
            if i not in meanax:
                keepax += (i, )

        fhvgeo = dset(vgeofil)
        db = -fhvgeo.variables['g'][:]
        dbi = np.append(db, 0)
        fhvgeo.close()

        fhgeo = dset(geofil)
        fh = mfdset(fil)
        zi = rdp1.getdims(fh)[2][0]
        dbl = -np.diff(zi) * 9.8 / 1031
        if xyasindices:
            (xs, xe), (ys, ye) = (xstart, xend), (ystart, yend)
            _, _, dimv = rdp1.getdimsbyindx(fh,
                                            xs,
                                            xe,
                                            ys,
                                            ye,
                                            zs=zs,
                                            ze=ze,
                                            ts=0,
                                            te=None,
                                            xhxq='xh',
                                            yhyq='yq',
                                            zlzi='zl')
        else:
            (xs, xe), (ys, ye), dimv = rdp1.getlatlonindx(fh,
                                                          wlon=xstart,
                                                          elon=xend,
                                                          slat=ystart,
                                                          nlat=yend,
                                                          zs=zs,
                                                          ze=ze,
                                                          yhyq='yq')
        D = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[0]
        (ah, aq) = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[1]
        Dforgetvtwaforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[0]
        Dforgetvtwaforydiff = rdp1.getgeombyindx(fhgeo, xs, xe, ys - 1,
                                                 ye + 1)[0]
        Dforgethuforxdiff = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys,
                                               ye + 1)[0]
        dxt, dyt = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye + 1)[2][6:8]
        dycv = rdp1.getgeombyindx(fhgeo, xs, xe, ys, ye)[2][3]
        dxbu = rdp1.getgeombyindx(fhgeo, xs - 1, xe, ys, ye)[2][4]
        nt_const = dimv[0].size
        t0 = time.time()

        print('Reading data using loop...')
        v = fh.variables['v'][0:1, zs:ze, ys:ye, xs:xe]
        nt = np.ones(v.shape) * nt_const
        frhatv = fh.variables['frhatv'][0:1, zs:ze, ys:ye, xs:xe]
        h_v = frhatv * D[np.newaxis, np.newaxis, :, :]
        h_v = np.ma.masked_array(h_v, mask=(h_v <= 1e-3).astype(int))
        nt[h_v <= 1e-3] -= 1
        hvm = (h_v * v).filled(0)
        hvvm = (h_v * v * v).filled(0)
        h_vm = h_v.filled(0)

        hvmforxdiff, h_vmforxdiff = getvtwaforxdiff(fh, fhgeo,
                                                    Dforgetvtwaforxdiff, 0,
                                                    xs - 1, xe, ys, ye, zs, ze)
        hvmforydiff, h_vmforydiff, hvvmforydiff = getvtwaforydiff(
            fh, fhgeo, Dforgetvtwaforydiff, 0, xs, xe, ys - 1, ye + 1, zs, ze)

        cav = fh.variables['CAv'][0:1, zs:ze, ys:ye, xs:xe]
        gkev = fh.variables['gKEv'][0:1, zs:ze, ys:ye, xs:xe]
        rvxu = fh.variables['rvxu'][0:1, zs:ze, ys:ye, xs:xe]
        hmfum = (h_v * (cav - gkev - rvxu)).filled(0)

        pfvm = fh.variables['PFv'][0:1, zs:ze, ys:ye, xs:xe]
        #        pfvm = np.ma.masked_array(pfvm,mask=(h_v<=1e-3).astype(int))
        #        pfvm = pfvm.filled(0)

        hdvdtviscm = (
            h_v *
            fh.variables['dv_dt_visc'][0:1, zs:ze, ys:ye, xs:xe]).filled(0)
        hdiffvm = (h_v *
                   fh.variables['diffv'][0:1, zs:ze, ys:ye, xs:xe]).filled(0)

        dvdtdia = fh.variables['dvdt_dia'][0:1, zs:ze, ys:ye, xs:xe]
        wd = fh.variables['wd'][0:1, zs:ze, ys:ye + 1, xs:xe]
        wd = np.diff(wd, axis=1)
        wdm = wd
        hvwbm = (v * (wd[:, :, 0:-1, :] + wd[:, :, 1:, :]) / 2 -
                 h_v * dvdtdia).filled(0)

        uh = fh.variables['uh'][0:1, zs:ze, ys:ye + 1, xs - 1:xe]
        uh = np.ma.filled(uh.astype(float), 0)
        uhx = np.diff(uh, axis=3) / ah
        huvxpTm = (v * (uhx[:, :, 0:-1, :] + uhx[:, :, 1:, :]) / 2 -
                   h_v * rvxu).filled(0)

        vh = fh.variables['vh'][0:1, zs:ze, ys - 1:ye + 1, xs:xe]
        vh = np.ma.filled(vh.astype(float), 0)
        vhy = np.diff(vh, axis=2) / ah
        hvvymTm = (v * (vhy[:, :, 0:-1, :] + vhy[:, :, 1:, :]) / 2 -
                   h_v * gkev).filled(0)

        u = fh.variables['u'][0:1, zs:ze, ys:ye + 1, xs - 1:xe]
        u = 0.25 * (u[:, :, 0:-1, 0:-1] + u[:, :, 1:, 0:-1] +
                    u[:, :, 0:-1, 1:] + u[:, :, 1:, 1:])
        hum = (h_v * u).filled(0)

        humforxdiff = gethuforxdiff(fh, fhgeo, Dforgethuforxdiff, 0, xs - 1,
                                    xe, ys, ye + 1, zs, ze)

        wd = fh.variables['wd'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
        hw = wd * dbi[:, np.newaxis, np.newaxis]
        hw = 0.5 * (hw[:, 0:-1, :, :] + hw[:, 1:, :, :])
        hwm_v = 0.5 * (hw[:, :, 0:-1, :] + hw[:, :, 1:, :])

        emforydiff = fh.variables['e'][0:1, zs:ze, ys:ye + 1, xs:xe] / nt_const
        if 1 in keepax:
            em = fh.variables['e'][0:1, zs:ze, ys:ye, xs:xe] / nt_const

        for i in range(1, nt_const):
            v = fh.variables['v'][i:i + 1, zs:ze, ys:ye, xs:xe]
            frhatv = fh.variables['frhatv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            h_v = frhatv * D[np.newaxis, np.newaxis, :, :]
            h_v = np.ma.masked_array(h_v, mask=(h_v <= 1e-3).astype(int))
            nt[h_v <= 1e-3] -= 1
            hvm += (h_v * v).filled(0)
            h_vm += h_v.filled(0)

            hvforxdiff, h_vforxdiff = getvtwaforxdiff(fh, fhgeo,
                                                      Dforgetvtwaforxdiff, i,
                                                      xs - 1, xe, ys, ye, zs,
                                                      ze)
            hvforydiff, h_vforydiff, hvvforydiff = getvtwaforydiff(
                fh, fhgeo, Dforgetvtwaforydiff, i, xs, xe, ys - 1, ye + 1, zs,
                ze)

            hvmforxdiff += hvforxdiff
            h_vmforxdiff += h_vforxdiff
            hvvmforydiff += hvvforydiff
            hvmforydiff += hvforydiff
            h_vmforydiff += h_vforydiff

            cav = fh.variables['CAv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            gkev = fh.variables['gKEv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            rvxu = fh.variables['rvxu'][i:i + 1, zs:ze, ys:ye, xs:xe]
            hmfu = h_v * (cav - gkev - rvxu)
            hmfum += hmfu.filled(0)

            pfv = fh.variables['PFv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            #            pfv = np.ma.masked_array(pfv,mask=(h_v<=1e-3).astype(int))
            #            pfvm += pfv.filled(0)
            pfvm += pfv

            hdvdtvisc = h_v * fh.variables['dv_dt_visc'][i:i + 1, zs:ze, ys:ye,
                                                         xs:xe]
            hdvdtviscm += hdvdtvisc.filled(0)
            hdiffv = h_v * fh.variables['diffv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            hdiffvm += hdiffv.filled(0)

            dvdtdia = fh.variables['dvdt_dia'][i:i + 1, zs:ze, ys:ye, xs:xe]
            wd = fh.variables['wd'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            wd = np.diff(wd, axis=1)
            wdm += wd
            hvwb = (v * (wd[:, :, 0:-1, :] + wd[:, :, 1:, :]) / 2 -
                    h_v * dvdtdia)
            hvwb = np.ma.masked_array(hvwb, mask=(h_v <= 1e-3).astype(int))
            hvwbm += hvwb.filled(0)

            uh = fh.variables['uh'][0:1, zs:ze, ys:ye + 1, xs - 1:xe]
            uh = np.ma.filled(uh.astype(float), 0)
            uhx = np.diff(uh, axis=3) / ah
            huvxpT = (v * (uhx[:, :, 0:-1, :] + uhx[:, :, 1:, :]) / 2 -
                      h_v * rvxu).filled(0)
            huvxpTm += huvxpT

            vh = fh.variables['vh'][0:1, zs:ze, ys - 1:ye + 1, xs:xe]
            vh = np.ma.filled(vh.astype(float), 0)
            vhy = np.diff(vh, axis=2) / ah
            hvvymT = (v * (vhy[:, :, 0:-1, :] + vhy[:, :, 1:, :]) / 2 -
                      h_v * gkev).filled(0)
            hvvymTm = hvvymT

            u = fh.variables['u'][0:1, zs:ze, ys:ye + 1, xs - 1:xe]
            u = 0.25 * (u[:, :, 0:-1, 0:-1] + u[:, :, 1:, 0:-1] +
                        u[:, :, 0:-1, 1:] + u[:, :, 1:, 1:])
            hum += (h_v * u).filled(0)

            humforxdiff += gethuforxdiff(fh, fhgeo, Dforgethuforxdiff, i,
                                         xs - 1, xe, ys, ye + 1, zs, ze)

            wd = fh.variables['wd'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            hw = wd * dbi[:, np.newaxis, np.newaxis]
            hw = 0.5 * (hw[:, 0:-1, :, :] + hw[:, 1:, :, :])
            hwm_v += 0.5 * (hw[:, :, 0:-1, :] + hw[:, :, 1:, :])

            emforydiff += fh.variables['e'][i:i + 1, zs:ze, ys:ye + 1,
                                            xs:xe] / nt_const
            if 1 in keepax:
                em += fh.variables['e'][i:i + 1, zs:ze, ys:ye,
                                        xs:xe] / nt_const

            sys.stdout.write('\r' + str(int((i + 1) / nt_const * 100)) +
                             '% done...')
            sys.stdout.flush()

        fhgeo.close()
        print('Time taken for data reading: {}s'.format(time.time() - t0))

        elm = 0.5 * (em[:, 0:-1, :, :] + em[:, 1:, :, :])
        elmforydiff = 0.5 * (emforydiff[:, 0:-1, :, :] +
                             emforydiff[:, 1:, :, :])

        vtwa = hvm / h_vm
        vtwaforxdiff = hvmforxdiff / h_vmforxdiff
        vtwaforydiff = hvmforydiff / h_vmforydiff

        vtwaforxdiff = np.concatenate(
            (vtwaforxdiff, -vtwaforxdiff[:, :, :, -1:]), axis=3)
        vtwax = np.diff(vtwaforxdiff, axis=3) / dxbu
        vtwax = 0.5 * (vtwax[:, :, :, 0:-1] + vtwax[:, :, :, 1:])

        vtway = np.diff(vtwaforydiff, axis=2) / dyt
        vtway = 0.5 * (vtway[:, :, 0:-1, :] + vtway[:, :, 1:, :])

        humx = np.diff(humforxdiff, axis=3) / dxt
        humx = 0.5 * (humx[:, :, 0:-1, :] + humx[:, :, 1:, :])

        hvmy = np.diff(hvmforydiff, axis=2) / dyt
        hvmy = 0.5 * (hvmy[:, :, 0:-1, :] + hvmy[:, :, 1:, :])

        huvxphvvym = huvxpTm + hvvymTm
        hvvym = np.diff(hvvmforydiff, axis=2) / dyt
        hvvym = 0.5 * (hvvym[:, :, 0:-1, :] + hvvym[:, :, 1:, :])
        huvxm = huvxphvvym - hvvym

        vtwaforvdiff = np.concatenate((vtwa[:, [0], :, :], vtwa), axis=1)
        vtwab = np.diff(vtwaforvdiff, axis=1) / db[:, np.newaxis, np.newaxis]
        vtwab = np.concatenate(
            (vtwab,
             np.zeros([vtwab.shape[0], 1, vtwab.shape[2], vtwab.shape[3]])),
            axis=1)
        vtwab = 0.5 * (vtwab[:, 0:-1, :, :] + vtwab[:, 1:, :, :])

        hwb_v = 0.5 * (wdm[:, :, 0:-1, :] + wdm[:, :, 1:, :])

        print('Calculating form drag using loop...')
        t0 = time.time()
        e = fh.variables['e'][0:1, zs:ze, ys:ye + 1, xs:xe]
        el = 0.5 * (e[:, 0:-1, :, :] + e[:, 1:, :, :])
        ed = e - emforydiff
        edl = el - elmforydiff
        edlsqm = (edl**2)
        pfv = fh.variables['PFv'][0:1, zs:ze, ys:ye, xs:xe]
        pfvd = pfv - pfvm / nt_const
        geta = 9.8 * ed[:, :1, :, :] / 1031
        getay = np.diff(geta, axis=2) / dycv
        pfvd = np.concatenate(
            (pfvd, np.zeros([pfvd.shape[0], 1, pfvd.shape[2], pfvd.shape[3]])),
            axis=1)
        pfvd = 0.5 * (pfvd[:, 0:-1, :, :] + pfvd[:, 1:, :, :])
        pfvd = np.concatenate((-getay, pfvd), axis=1)
        ed = 0.5 * (ed[:, :, 0:-1, :] + ed[:, :, 1:, :])
        edpfvdm = ed * pfvd
        for i in range(1, nt_const):
            e = fh.variables['e'][i:i + 1, zs:ze, ys:ye + 1, xs:xe]
            el = 0.5 * (e[:, 0:-1, :, :] + e[:, 1:, :, :])
            ed = e - emforydiff
            edl = el - elmforydiff
            edlsqm += (edl**2)
            pfv = fh.variables['PFv'][i:i + 1, zs:ze, ys:ye, xs:xe]
            pfvd = pfv - pfvm / nt_const
            geta = 9.8 * ed[:, :1, :, :] / 1031
            getay = np.diff(geta, axis=2) / dycv
            pfvd = np.concatenate(
                (pfvd,
                 np.zeros([pfvd.shape[0], 1, pfvd.shape[2], pfvd.shape[3]])),
                axis=1)
            pfvd = 0.5 * (pfvd[:, 0:-1, :, :] + pfvd[:, 1:, :, :])
            pfvd = np.concatenate((-getay, pfvd), axis=1)
            ed = 0.5 * (ed[:, :, 0:-1, :] + ed[:, :, 1:, :])
            edpfvdm += ed * pfvd

            sys.stdout.write('\r' + str(int((i + 1) / nt_const * 100)) +
                             '% done...')
            sys.stdout.flush()

        print('Time taken for data reading: {}s'.format(time.time() - t0))
        fh.close()

        edlsqmy = np.diff(edlsqm, axis=2) / dycv
        advx = hum * vtwax / h_vm
        advy = vtwa * vtway
        advb = hwm_v * vtwab / h_vm
        cor = hmfum / h_vm
        pfvm = pfvm / nt_const

        xdivep1 = huvxm / h_vm
        xdivep2 = -advx
        xdivep3 = -vtwa * humx / h_vm
        xdivep = (xdivep1 + xdivep2 + xdivep3)

        ydivep1 = hvvym / h_vm
        ydivep2 = -advy
        ydivep3 = -vtwa * hvmy / h_vm
        ydivep4 = 0.5 * edlsqmy * dbl[:, np.newaxis, np.newaxis] / h_vm
        ydivep = (ydivep1 + ydivep2 + ydivep3 + ydivep4)

        bdivep1 = hvwbm / h_vm
        bdivep2 = -advb
        bdivep3 = -vtwa * hwb_v / h_vm
        bdivep4 = np.diff(edpfvdm,
                          axis=1) / db[:, np.newaxis,
                                       np.newaxis] * dbl[:, np.newaxis,
                                                         np.newaxis] / h_vm
        bdivep = (bdivep1 + bdivep2 + bdivep3 + bdivep4)

        Y1twa = hdiffvm / h_vm
        Y2twa = hdvdtviscm / h_vm

        terms = np.ma.concatenate(
            (-advx[:, :, :, :, np.newaxis], -advy[:, :, :, :, np.newaxis],
             -advb[:, :, :, :, np.newaxis], cor[:, :, :, :, np.newaxis],
             pfvm[:, :, :, :, np.newaxis], -xdivep[:, :, :, :, np.newaxis],
             -ydivep[:, :, :, :, np.newaxis], -bdivep[:, :, :, :, np.newaxis],
             Y1twa[:, :, :, :, np.newaxis], Y2twa[:, :, :, :, np.newaxis]),
            axis=4)
        termsep = np.ma.concatenate(
            (xdivep1[:, :, :, :, np.newaxis], xdivep3[:, :, :, :, np.newaxis],
             ydivep1[:, :, :, :, np.newaxis], ydivep3[:, :, :, :, np.newaxis],
             ydivep4[:, :, :, :, np.newaxis], bdivep1[:, :, :, :, np.newaxis],
             bdivep3[:, :, :, :, np.newaxis], bdivep4[:, :, :, :, np.newaxis]),
            axis=4)

        terms[np.isinf(terms)] = np.nan
        termsep[np.isinf(termsep)] = np.nan
        termsm = np.ma.apply_over_axes(np.nanmean, terms, meanax)
        termsepm = np.ma.apply_over_axes(np.nanmean, termsep, meanax)

        X = dimv[keepax[1]]
        Y = dimv[keepax[0]]
        if 1 in keepax:
            em = np.ma.apply_over_axes(np.mean, em, meanax)
            elm = np.ma.apply_over_axes(np.mean, elm, meanax)
            Y = elm.squeeze()
            X = np.meshgrid(X, dimv[1])[0]

        P = termsm.squeeze()
        P = np.ma.filled(P.astype(float), np.nan)
        Pep = termsepm.squeeze()
        Pep = np.ma.filled(Pep.astype(float), np.nan)
        X = np.ma.filled(X.astype(float), np.nan)
        Y = np.ma.filled(Y.astype(float), np.nan)
        if not calledfrompv:
            np.savez('twamomy_complete_terms', X=X, Y=Y, P=P, Pep=Pep)
    else:
        npzfile = np.load('twamomy_complete_terms.npz')
        X = npzfile['X']
        Y = npzfile['Y']
        P = npzfile['P']
        Pep = npzfile['Pep']

    return (X, Y, P, Pep)