<jupyter_start><jupyter_text>Plots to check channels etc- took from old checkbathy and plotgrids files<jupyter_code>import scipy.io as sio
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important; }</style>"))

from helpers import expandf, grid_angle

# grid
def load1(f):
    with nc.Dataset(f) as ncid:
        glamt = ncid.variables["glamt"][0, :, :].filled()
        gphit = ncid.variables["gphit"][0, :, :].filled()
        glamu = ncid.variables["glamu"][0, :, :].filled()
        gphiu = ncid.variables["gphiu"][0, :, :].filled()
        glamv = ncid.variables["glamv"][0, :, :].filled()
        gphiv = ncid.variables["gphiv"][0, :, :].filled()
        glamf = ncid.variables["glamf"][0, :, :].filled()
        gphif = ncid.variables["gphif"][0, :, :].filled()
    return glamt, glamu, glamv, glamf, gphit, gphiu, gphiv, gphif

#
def load2(f):
    with nc.Dataset(f) as ncid:
        e1t = ncid.variables["e1t"][0, :, :].filled()
        e1u = ncid.variables["e1u"][0, :, :].filled()
        e1v = ncid.variables["e1v"][0, :, :].filled()
        e1f = ncid.variables["e1f"][0, :, :].filled()
        e2t = ncid.variables["e2t"][0, :, :].filled()
        e2u = ncid.variables["e2u"][0, :, :].filled()
        e2v = ncid.variables["e2v"][0, :, :].filled()
        e2f = ncid.variables["e2f"][0, :, :].filled()
    return e1t,e1u,e1v,e1f,e2t,e2u,e2v,e2f

def load3(f):
    with nc.Dataset(f) as ncid:
        depth = ncid.variables["Bathymetry"][:, :].filled()
        latt = ncid.variables["nav_lat"][:, :].filled()
        lont = ncid.variables["nav_lon"][:, :].filled()

    return depth, latt, lont

# for rivers - GO
def load4(f):
    with nc.Dataset(f) as ncid:
        rorunoff = ncid.variables["rorunoff"][6, :, :].filled()
        latt = ncid.variables["nav_lat"][:, :].filled()
        lont = ncid.variables["nav_lon"][:, :].filled()

    return rorunoff, latt, lont

# grid
def plotgrid1(f):
    glamt, glamu, glamv, glamf, gphit, gphiu, gphiv, gphif = load1(f)

    plt.figure(figsize=(7,5)); plt.clf()

    # Draw sides of every box
    glamfe, gphife = expandf(glamf, gphif)
    NY,NX = glamfe.shape
    print(glamt.shape)
    print(glamu.shape)
    print(glamf.shape)
    for j in range(NY):
        plt.plot(glamfe[j,:],gphife[j,:], 'k')
    for i in range(NX):
        plt.plot(glamfe[:,i],gphife[:,i], 'k')

    # Plot t, u, v, f points in red, green, blue, magenta    
    plt.plot(glamt, gphit, 'r.')
    plt.plot(glamu, gphiu, 'g.')
    plt.plot(glamv, gphiv, 'b.')
    plt.plot(glamf, gphif, 'm.')

    plt.tight_layout()
    plt.xlim([-123.5,-123.3])
    plt.ylim([46.84,46.95])

    #plt.savefig(f.replace(".nc","_gridpts.png"))

# grid
def plotgrid2(f):
    glamt, glamu, glamv, glamf, gphit, gphiu, gphiv, gphif = load1(f)
    e1t,e1u,e1v,e1f,e2t,e2u,e2v,e2f = load2(f)
    glamfe, gphife = expandf(glamf, gphif)
    A = grid_angle(f)
    
    plt.figure(figsize=(12,4))

    plt.subplot(1,3,1)
    plt.pcolormesh(glamfe,gphife,e1t); plt.colorbar(); plt.title("e1t (m)")
    plt.subplot(1,3,2)
    plt.pcolormesh(glamfe,gphife,e2t); plt.colorbar(); plt.title("e2t (m)")
    plt.subplot(1,3,3)
    plt.pcolormesh(glamf,gphif,A); plt.colorbar(); plt.title("angle (deg)")
   
    plt.tight_layout()
    plt.savefig(f.replace(".nc","_resolution_angle.png"))
    
# bathy
def plotgrid3(f):
    depth, latt, lont = load3(f)
    
    depth[depth==0]=np.nan
    depth[depth>0]=1
    #print(depth.shape)
    
    # can do edits below 
    # made permanent in the main create bathy above
    # north to south
    #depth[178,128] = 400 #northern fjord
#     depth[296,54] = 60 #northern fjord
#     depth[296,53] = 60 #northern fjord

    plt.figure(figsize=(8,8))

    plt.subplot(1,1,1)
    plt.pcolormesh(depth, cmap=plt.plasma()); plt.colorbar(); plt.title("depth")
    #plt.pcolormesh(depth); plt.colorbar(); plt.title("depth")
    #plt.pcolormesh(ma_rorunoff, cmap=plt.pink()); plt.title("rodepth")
    
    plt.tight_layout()
    plt.savefig(f.replace(".nc","_bathycheck.png"))

# runoff / rivers
def plotgrid4(f):
    depth, latt, lont = load3(f)
    
    # added for river runoff overlay
    rorunoff, latt2, lontt2 = load4('c:/temp/runofftools/rivers_month_202101GO.nc')
    #rorunoff[rorunoff==0]=np.nan
    #print(rorunoff.shape)
    ma_rorunoff = np.ma.masked_array(rorunoff, rorunoff == 0)
    
    depth[depth==0]=np.nan
    depth[depth>0]=1
    
    #print(depth.shape)

    plt.figure(figsize=(8,8))

    plt.subplot(1,1,1)
    plt.pcolormesh(depth, cmap=plt.plasma()); plt.colorbar(); plt.title("depth")
    #plt.pcolormesh(depth); plt.colorbar(); plt.title("depth")
    #plt.pcolormesh(ma_rorunoff, cmap=plt.pink()); plt.title("rodepth")
    
    plt.tight_layout()
    plt.savefig("C:/temp/runofftools/runoffcheck2.png")

# #################################################################
# #################### BASIC PLOT OF BATHY ########################

gridfilename = '..//data//grid//coordinates_salishsea_1500m.nc'
#bathyfilename = 'bathy_salishsea_1500m_before_manual_edits.nc'
#bathyfilename = '..//data//bathymetry//bathy_salishsea_1500m_Dec30.nc'

with nc.Dataset(gridfilename) as ncid:
    glamt = ncid.variables["glamt"][0, :, :].filled()
    gphit = ncid.variables["gphit"][0, :, :].filled()
    glamf = ncid.variables["glamf"][0, :, :].filled()
    gphif = ncid.variables["gphif"][0, :, :].filled()
glamfe,gphife=expandf(glamf,gphif)

with nc.Dataset(bathyout_filename) as nc_b_file:
    bathy = nc_b_file.variables["Bathymetry"][:, :].filled()

bb=np.copy(bathy); bb[bb==0]=np.nan
plt.figure(figsize=(8,8))
plt.subplot(1,1,1)
plt.pcolormesh(glamfe,gphife,bb); plt.colorbar()
# Coastlines
mfile = sio.loadmat('..//data//reference//PNW.mat')
ncst = mfile['ncst']
plt.plot(ncst[:,0],ncst[:,1],'k')

mfile2 = sio.loadmat('..//data//reference//PNWrivers.mat')
ncst2 = mfile2['ncst']
plt.plot(ncst2[:,0],ncst2[:,1],'k')
##########################################################
############### PLOTS TO CHECK BATHY ETC #################

# plotgrid1('coordinates_seagrid_SalishSea2.nc')
#plotgrid1('coordinates_salishsea_1km.nc')
#plotgrid1('coordinates_salishsea_1500m.nc')
#plotgrid1('coordinates_salishsea_2km.nc')
#plotgrid2('coordinates_seagrid_SalishSea2.nc')
# plotgrid2('coordinates_salishsea_1km.nc')
#plotgrid2('coordinates_salishsea_2km.nc')
#plotgrid2('coordinates_salishsea_1p5km.nc')
#plotgrid3('bathy_salishsea_1500m_Dec21.nc')
plotgrid3(bathyout_filename)
#plotgrid3('bathy_salishsea_2km.nc')
# junk code below
a = range(24)
b = a[::3]
list(b)
my_list[0] = [_ for _ in 'abcdefghi']
my_list[1] = [_ for _ in 'abcdefghi']
my_list[0:-1]
glamu.shape
a[296,10]
############################################################
### EXPLORE TWO MESHES - NEMO ORAS5 and SS1500 #############
### Apr 2021
import sys
# load mask (tmask)
def loadmask(f):
    with nc.Dataset(f) as ncid:
        tmaskutil = ncid.variables["tmaskutil"][0,:, :].filled()
        latt = ncid.variables["nav_lat"][:, :].filled()
        lont = ncid.variables["nav_lon"][:, :].filled()
        e1t = ncid.variables["e1t"][0,:, :].filled()
        e2t = ncid.variables["e2t"][0,:, :].filled()

    return tmaskutil, latt, lont, e1t, e2t

def plot_two_grids(f,g):

    # load ss1500mask
    tmask, latt, lont, e1t, e2t = loadmask(f)

    # load ORAS5
    tmask2, latt2, lont2, e1t2, e2t2 = loadmask(g)

    #print(tmask[:,])
    #plt.subplot(1,1,1)
    #plt.figure(figsize=(7,5)); plt.clf()
    
    plt.scatter(lont, latt, tmask)
    plt.scatter(lont2, latt2, tmask2)


    # Draw sides of every box
    #glamfe, gphife = expandf(glamf, gphif)
    #NY,NX = glamfe.shape

    #for j in range(NY):
    #    plt.plot(glamfe[j,:],gphife[j,:], 'k')
    #for i in range(NX):
    #    plt.plot(glamfe[:,i],gphife[:,i], 'k')

    # Plot t, u, v, f points in red, green, blue, magenta    
    #plt.plot(glamt, gphit, 'r.')
    #plt.plot(glamu, gphiu, 'g.')
    #plt.plot(glamv, gphiv, 'b.')
    #plt.plot(glamf, gphif, 'm.')

    #plt.plot(glamt_2, gphit_2, 'b.')

    #plt.plot(glamu, gphiu, 'g.')
    #plt.plot(glamv, gphiv, 'b.')
    #plt.plot(glamf, gphif, 'm.')
    
    plt.tight_layout()
    plt.xlim([-126.2,-122.1])
    plt.ylim([46.84,52])

    #plt.savefig(f.replace(".nc","_gridpts.png"))
    
res = "1500m"
ss1500grid = "..//data//grid//coordinates_salishsea_{}.nc".format(res) # in 
datetag = "20210406"
oras5grid = "..//data//reference//ORAS5 Mask and Bathy//mesh_mask.nc"
ss1500meshmask = "..//data//mesh mask//mesh_mask_20210406.nc"

np.set_printoptions(threshold=sys.maxsize)
plot_two_grids(ss1500meshmask, oras5grid)


    tmask, latt, lont, e1t, e2t = load2(f)
      
    plt.figure(figsize=(8,8))
    plt.subplot(1,1,1)
    plt.pcolormesh(tmask[:,:], cmap=plt.pink()); plt.title("model_mask")

    plt.tight_layout()


plt.figure(figsize=(7,5)); plt.clf()
plt.plot(tmaskutil[0,:],tmaskutil[:,0], 'r.')
with nc.Dataset(ss1500meshmask) as ncid:
    print(tmaskutil[:,0])<jupyter_output>[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0]
{'fraction_non_alphanumeric': 0.2054923253628485, 'fraction_numerical': 0.06588702098778323, 'mean_word_length': 3.4097605893186005, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '2778', 'license': 'MIT', 'n_tokens_mistral': 4596, 'n_tokens_neox': 4202, 'n_words': 892, 'path': 'code/.ipynb_checkpoints/Bathymetry and Coord Grid Generator (Py3)-checkpoint.ipynb', 'repo_name': 'goldford/NEMO-Salish-Sea-2021', 'size': '9577'}