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)
Exemple #2
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())
Exemple #3
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)
Exemple #4
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)
Exemple #5
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,
                          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)
Exemple #7
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())
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)