def atlantic_maskuv(msku, mskv): # this is pure convenience fmsku = llc.faces(msku) fmskv = llc.faces(mskv) # mask southern ocean first js = 374 nz, ny, nx = fmsku[0].shape fmsku[0][:, :js, :] = 0. fmsku[4][:, :, ny - js:] = 0. fmskv[0][:, :js, :] = 0. fmskv[4][:, :, ny - js:] = 0. # atlantic mask iw = 176 # this rough and removes part of the norwegian sea fmsku[0][:, :, iw:] = 0 # mediterranean sea fmsku[0][:, 599:651, 96:] = 0 fmsku[0][:, 651:662, 125:] = 0 # baltic and north sea fmsku[0][:, 703:756, 140:] = 0 fmsku[0][:, 756:758, 173:] = 0 fmsku[1][:] = 0. fmsku[2][:] = 0. fmsku[3][:] = 0. # north pacific fmsku[4][:, :90, :] = 0 # south pacific fmsku[4][:, :174, 287:] = 0 fmsku[4][:, :133, 263:] = 0 # hudson bay etc fmsku[4][:, :160, :121] = 0 fmsku[4][:, :189, 26:121] = 0 # # this rough and removes part of the norwegian sea fmskv[0][:, :, iw - 1:] = 0 # mediterranean sea fmskv[0][:, 599:651, 96:] = 0 fmskv[0][:, 651:662, 125:] = 0 # baltic and north sea fmskv[0][:, 703:756, 140:] = 0 fmskv[0][:, 756:758, 172:] = 0 fmskv[1][:] = 0. fmskv[2][:] = 0. fmskv[3][:] = 0. # north pacific fmskv[4][:, :90, :] = 0 # south pacific fmskv[4][:, :174, 287:] = 0 fmskv[4][:, :133, 263:] = 0 fmskv[4][:, 90, 262] = 0. fmskv[4][:, 99:102, 262] = 0. # hudson bay etc fmskv[4][:, :160, :121] = 0 fmskv[4][:, :189, 25:121] = 0 return llc.faces2mds(fmsku), llc.faces2mds(fmskv)
def atlantic_mask(mskc): # this is pure convenience fmsk = llc.faces(mskc) # mask southern ocean first js = 374 nz, ny, nx = fmsk[0].shape fmsk[0][:, :374, 172:] = 0. fmsk[4][:, :188, 527:] = 0. # atlantic mask # this rough and removes part of the norwegian sea fmsk[0][:, :, 180:] = 0 fmsk[0][:, :380, 175:] = 0 # mediterranean sea fmsk[0][:, 599:651, 96:] = 0 fmsk[0][:, 651:662, 125:] = 0 # baltic and north sea fmsk[0][:, 703:756, 140:] = 0 fmsk[0][:, 756:768, 172:] = 0 fmsk[1][:] = 0. fmsk[2][:] = 0. fmsk[3][:] = 0. # north pacific fmsk[4][:, :90, :] = 0 # south pacific fmsk[4][:, :174, 287:] = 0 fmsk[4][:, :133, 263:] = 0 fmsk[4][:, :102, 262] = 0 # hudson bay etc fmsk[4][:, :160, :121] = 0 fmsk[4][:, :189, 26:121] = 0 return llc.faces2mds(fmsk)
def move(runoff,moves,mask):#{{{2 '''move runoff from land- to ocean-point runoff: runoff field (on MITgcm grid) moves: list of source and destination points mask: land-ocean-mask ''' area = rdmds(args.gridpath+'RAC') new_runoff = np.zeros_like(runoff) for month in np.arange(0,12): # loop over months # move mds to list of faces and resort faces runoff_copy = np.copy(runoff[month,:,:])*area runoff_f = llc.faces(runoff_copy) runoff_f = resort_faces(runoff_f) for i in np.arange(len(moves)): # loop over points to move # source and destination coordinates for face, y and x source_face=moves[i][0][0] source_y = moves[i][0][1] source_x = moves[i][0][2] dest_face = moves[i][1][0] dest_y = moves[i][1][1] dest_x = moves[i][1][2] # print '###########################################' # print 'source_coord',source_face,source_y,source_x # print 'dest_coord', dest_face,dest_y,dest_x # print 'dest',runoff_f[dest_face][dest_y,dest_x] # add runoff of source cell to destination cell runoff_f[dest_face][dest_y,dest_x] = runoff_f[source_face][source_y,source_x] + runoff_f[dest_face][dest_y,dest_x] # print 'source', runoff_f[source_face][source_y,source_x] # print 'new',runoff_f[dest_face][dest_y,dest_x] # resort faces back and move list to mds array runoff_f = resort_back_faces(runoff_f) runoff_m = llc.faces2mds(runoff_f) runoff_m = runoff_m*mask # delete runoff on land area[area == 0.0] = 1e-33 runoff_m = runoff_m/area new_runoff[month,:,:] = runoff_m # write runoff of month into array return new_runoff
def atlantic_mask(msku, mskv): # this is pure convenience fmsku = llc.faces(msku) fmskv = llc.faces(mskv) # mask southern ocean first fmsku[0][:, :123, :] = 0. fmsku[4][:, :, 148:] = 0. fmskv[0][:, :123, :] = 0. fmskv[4][:, :, 147:] = 0. # atlantic mask fmsku[0][:, :, 57:] = 0 fmsku[0][:, 203:221, 37:] = 0 fmsku[0][:, 208:210, 33:37] = 0 fmsku[1][:] = 0. fmsku[2][:] = 0. fmsku[3][:] = 0. fmsku[4][:, :30, :] = 0 fmsku[4][:, :51, 91:] = 0 fmsku[4][:, :59, 123:] = 0 fmsku[4][:, :42, 85:] = 0 fmsku[4][:, :54, :40] = 0 # fmskv[0][:, :, 57:] = 0 fmskv[0][:, 203:221, 37:] = 0 fmskv[0][:, 209, 33:37] = 0 fmskv[1][:] = 0. fmskv[2][:] = 0. fmskv[3][:] = 0. fmskv[4][:, :55, :40] = 0 fmskv[4][:, :31, :] = 0 fmskv[4][:, :36, 84:] = 0 fmskv[4][:, :51, 92:] = 0 fmskv[4][:, :60, 123:] = 0 fmskv[4][:, :45, 86:] = 0 return llc.faces2mds(fmsku), llc.faces2mds(fmskv)
def mit_div(u, v, hfw, hfs, dxg, dyg, rac): """Compute divergence of vector field Usage: divergence = mit_div(u,v,hfw,hfs,dxg,dyg,rac): """ lenu = len(np.shape(u)) nt = 1 nk = 1 if lenu == 2: nju, niu = np.shape(u) njv, niv = np.shape(v) elif lenu == 3: nk, nju, niu = np.shape(u) nk, njv, niv = np.shape(v) elif lenu == 4: nt, nk, nju, niu = np.shape(u) nt, nk, njv, niv = np.shape(v) else: raise ValueError('Can only handle 2 to 4 dimensions') # check for 2D arrays # in this case nk is the time dimension and we need to have # a separate index for hfw and hfs if len(np.shape(hfw)) == 2 or np.shape(hfw)[0] != nk: nt = nk nk = 1 nj = np.min([nju, njv]) ni = np.min([niu, niv]) cubed_sphere = 0 if niu == 6 * nju: print('shape(u)[-1]=6*shape(u)[-2]: assuming cubed sphere fields') cubed_sphere = 1 llcap = 0 if nju == 13 * niu: print('shape(u)[-2]=13*shape(u)[-1]: assuming lat-lon-cap fields') llcap = 1 if cubed_sphere: n = nju div = np.zeros((nt, nk, 6, n, n), dtype=u.dtype) u = u.reshape(nt, nk, nju, niu) v = v.reshape(nt, nk, njv, niv) hfw = hfw.reshape(nk, nju, niu) hfs = hfs.reshape(nk, njv, niv) for t in range(0, nt): for k in range(0, nk): uflx = u[t, k, :, :] * hfw[k, :, :] * dyg vflx = v[t, k, :, :] * hfs[k, :, :] * dxg uflx = np.transpose(uflx.reshape(n, 6, n), [1, 0, 2]) vflx = np.transpose(vflx.reshape(n, 6, n), [1, 0, 2]) for iface in range(0, 6): ifp1 = np.remainder(iface + 1, 6) ifp2 = np.remainder(iface + 2, 6) if np.remainder(iface + 1, 2): # odd face utmp = np.concatenate( (uflx[iface, :, 1:n], uflx[ifp1, :, 0].reshape( n, 1)), 1) tmp = uflx[ifp2, np.arange(n, 0, -1) - 1, 0].reshape(1, n) vtmp = np.concatenate((vflx[iface, 1:n, :], tmp), 0) else: # even face tmp = vflx[ifp2, 0, np.arange(n, 0, -1) - 1].reshape(n, 1) utmp = np.concatenate((uflx[iface, :, 1:n], tmp), 1) vtmp = np.concatenate( (vflx[iface, 1:n, :], vflx[ifp1, 0, :].reshape( 1, n)), 0) ij = np.arange(0, n) + n * iface # recip_rac = 1./(rac(ij,:).*hf(ij,:,k)); recip_rac = 1. / rac[:, ij] du = utmp - uflx[iface, :, :] dv = vtmp - vflx[iface, :, :] div[t, k, iface, :, :] = (du + dv) * recip_rac div = np.transpose(div, [0, 1, 3, 2, 4]).reshape(nt, nk, n, n * 6) elif llcap: u = u.reshape(nt, nk, nju, niu) v = v.reshape(nt, nk, njv, niv) hfw = hfw.reshape(nk, nju, niu) hfs = hfs.reshape(nk, njv, niv) div = np.zeros(u.shape) from MITgcmutils.llc import faces, faces2mds racf = faces(rac) for t in range(0, nt): for k in range(0, nk): uflx = faces(u[t, k, :, :] * hfw[k, :, :] * dyg) vflx = faces(v[t, k, :, :] * hfs[k, :, :] * dxg) divf = faces(np.zeros((nju, niu))) for iface in range(len(uflx) - 1): du = np.roll(uflx[iface], -1, axis=-1) - uflx[iface] dv = np.roll(vflx[iface], -1, axis=-2) - vflx[iface] # now take care of the connectivity if iface == 0: du[:, -1] = uflx[1][:, 0] - uflx[iface][:, -1] dv[-1, :] = uflx[2][::-1, 0] - vflx[iface][-1, :] if iface == 1: du[:, -1] = vflx[3][0, ::-1] - uflx[iface][:, -1] dv[-1, :] = vflx[2][0, :] - vflx[iface][-1, :] if iface == 2: du[:, -1] = uflx[3][:, 0] - uflx[iface][:, -1] dv[-1, :] = uflx[4][::-1, 0] - vflx[iface][-1, :] if iface == 3: du[:, -1] = 0. # hack dv[-1, :] = vflx[4][0, :] - vflx[iface][-1, :] if iface == 4: du[:, -1] = 0. # hack dv[-1, :] = uflx[0][::-1, 0] - vflx[iface][-1, :] # putting it all together divf[iface] = (du + dv) / racf[iface] div[t, k, :, :] = faces2mds(divf) else: div = np.zeros((nt, nk, nj, ni), dtype=u.dtype) u = u.reshape(nt, nk, nju, niu) v = v.reshape(nt, nk, njv, niv) hfw = hfw.reshape(nk, nju, niu) hfs = hfs.reshape(nk, njv, niv) for t in range(0, nt): for k in range(0, nk): uflx = u[t, k, :, :] * hfw[k, :, :] * dyg vflx = v[t, k, :, :] * hfs[k, :, :] * dxg if niu > niv: du = uflx[:, 1:] - uflx[:, :-2] else: du = np.roll(uflx, 1, 1) - uflx if njv > nju: dv = vflx[1:, :] - vflx[:-2, :] else: dv = np.roll(vflx, 1, 1) - vflx div[t, k, :, :] = (du + dv) / rac # remove singleton dimensions again return np.squeeze(div)