<jupyter_start><jupyter_text>This notebook generates forcing files for the 2D domain.Plan: use 3D boundary files, average across the mouth of Juan de Fuca and set uniformly across domain.<jupyter_code>import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
import os

from salishsea_tools import tidetools,nc_tools

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load 3D T+S<jupyter_code>f = nc.Dataset('/data/nsoontie/MEOPAR/NEMO-forcing/open_boundaries/west/SalishSea2_Masson_DC.nc')

T = f.variables['votemper'][:]
S = f.variables['vosaline'][:]
print S.shape
depth = f.variables['deptht'][:]
times = f.variables['time_counter'][:]

nc_tools.show_dimensions(f)<jupyter_output><type 'netCDF4._netCDF4.Dimension'>: name = 'deptht', size = 40

<type 'netCDF4._netCDF4.Dimension'>: name = 'yb', size = 1

<type 'netCDF4._netCDF4.Dimension'>: name = 'xbT', size = 870

<type 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'time_counter', size = 52<jupyter_text>There are 52 weeks, 40 depth levels, 87 points across mouth, and 10 points into domain.  Average across mouth<jupyter_code>ntime=52; ndepth=40; nrim = 10

width_3d=87
Tmean = np.zeros((ntime,ndepth,nrim))
Smean = np.zeros((ntime,ndepth,nrim))
for i in np.arange(nrim):
    ind = i*width_3d
    Tmean[:,:,i] = np.nanmean(T[:,:,0,ind:ind+width_3d], axis=2)
    Smean[:,:,i] = np.nanmean(S[:,:,0,ind:ind+width_3d], axis=2)
    
plt.pcolormesh(Smean[:,:,0].T)
plt.axis([0,ntime,ndepth,0])
plt.colorbar()


plt.pcolormesh(Tmean[:,:,0].T)
plt.axis([0,ntime,ndepth,0])
plt.colorbar()
plt.pcolormesh(Smean[0,:,:])
plt.axis([0,nrim,ndepth,0])
plt.colorbar()
plt.pcolormesh(Tmean[0,:,:])
plt.axis([0,nrim,ndepth,0])
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Looks reasonable. Copy across Y<jupyter_code>Ny=8 #only 8 because of masked edges 
T_y=np.tile(Tmean,Ny)
T_y=T_y.reshape(ntime,ndepth,Ny,nrim)
T_y.shape

plt.pcolormesh(T_y[0,:,:,0])
plt.axis([0,Ny,ndepth,0])
plt.colorbar()

S_y=np.tile(Smean,Ny)
S_y=S_y.reshape(ntime,ndepth,Ny,nrim)
S_y.shape

plt.pcolormesh(S_y[0,:,:,3])
plt.axis([0,Ny,ndepth,0])
plt.colorbar()

print S_y.min()<jupyter_output>29.5011062622<jupyter_text>UntileNeed to order the data from closest to the edge to furthest from the edge<jupyter_code>T_untile = np.zeros((ntime,ndepth,1,Ny*nrim))
S_untile = np.zeros((ntime,ndepth,1,Ny*nrim))
for i in np.arange(nrim):
    ind = i*Ny
    T_untile[:,:,0,ind:ind+Ny] = T_y[:,:,:,i]
    S_untile[:,:,0,ind:ind+Ny] = S_y[:,:,:,i]
plt.pcolormesh(T_untile[0,:,0,:])
plt.axis([0,Ny*nrim,ndepth,0])
plt.colorbar()
plt.pcolormesh(S_untile[0,:,0,:])
plt.axis([0,Ny*nrim,ndepth,0])
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Save to netcdf<jupyter_code>nemo = nc.Dataset('../boundary_conditions/TS_OBC.nc', 'w', zlib=True)

#start and end points
length_rim =nrim
lengthi=Ny*length_rim
#time and depth
depth_levels =ndepth


# dataset attributes
nc_tools.init_dataset_attrs(
    nemo,
    title='Temperature and Salinty Boundary Conditions 2D domain',
    notebook_name='Generate T+S Forcing - NEMO3.6',
    nc_filepath='/data/nsoontie/MEOPAR/2Ddomain/TS_OBC.nc',
    comment='based on average values across mouth of JdF and 3D weekly climatology')

# dimensions 
nemo.createDimension('xb', lengthi)
nemo.createDimension('yb', 1)
nemo.createDimension('time_counter', None)
nemo.createDimension('deptht', depth_levels)
    
# variables
# deptht
deptht = nemo.createVariable('deptht', 'float32', ('deptht',))
deptht.long_name = 'Vertical T Levels'
deptht.units = 'm'
deptht.positive = 'down'
deptht.valid_range = np.array((4., 428.))
deptht[:]=depth
# time_counter
time_counter = nemo.createVariable('time_counter', 'float32', ('time_counter'))
time_counter.long_name = 'Time axis'
time_counter.axis = 'T'
time_counter.units = 'weeks since beginning of year'
time_counter[:]=times
# votemper
votemper = nemo.createVariable('votemper', 'float32', 
                               ('time_counter','deptht','yb','xb'))
votemper.units = 'degC'
votemper.long_name = 'Temperature'   
votemper.grid = 'SalishSea2D'
votemper[:]=T_untile
# vosaline
vosaline = nemo.createVariable('vosaline', 'float32', 
                               ('time_counter','deptht','yb','xb'))
vosaline.units = 1
vosaline.long_name = 'Practical Salinity'  
vosaline.grid = 'SalishSea2D'
vosaline[:]=S_untile
# nbidta, ndjdta, ndrdta
nbidta = nemo.createVariable('nbidta', 'int32' , ('yb','xb'))
nbidta.long_name = 'i grid position'
nbidta.units = 1
nbjdta = nemo.createVariable('nbjdta', 'int32' , ('yb','xb'))
nbjdta.long_name = 'j grid position'
nbjdta.units = 1
nbrdta = nemo.createVariable('nbrdta', 'int32' , ('yb','xb'))
nbrdta.long_name = 'position from boundary'
nbrdta.units = 1
    
for ir in range(length_rim):
    nbidta[0,ir*Ny:(ir+1)*Ny] = ir
    nbjdta[0,ir*Ny:(ir+1)*Ny] = range(Ny)
    nbrdta[0,ir*Ny:(ir+1)*Ny] = ir
    
nemo.close()<jupyter_output>file format: NETCDF4
Conventions: CF-1.6
title: Temperature and Salinty Boundary Conditions 2D domain
institution: Dept of Earth, Ocean & Atmospheric Sciences, University of British Columbia
source: https://bitbucket.org/salishsea/2d-domain/src/tip/Generate T+S Forcing - NEMO3.6.ipynb
references: REQUIRED
history: [2015-08-05 16:23:30] Created netCDF4 zlib=True dataset.
comment: based on average values across mouth of JdF and 3D weekly climatology
{'fraction_non_alphanumeric': 0.161723559037493, 'fraction_numerical': 0.027979854504756575, 'mean_word_length': 5.972691807542263, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '145519', 'license': 'Apache-2.0', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2264, 'n_words': 398, 'path': 'notebooks/Generate T+S Forcing - NEMO3.6.ipynb', 'repo_name': 'SalishSeaCast/2d-domain', 'size': '5361'}