<jupyter_start><jupyter_text>Make rivers constant and rivers monthly fileTemperature in not set in these filesThey have flux, area, depth<jupyter_code>import netCDF4 as NC
import numpy as np
import matplotlib.pyplot as plt

from salishsea_tools import rivertools, nc_tools
from salishsea_tools import river_201803 as rivers
prop_dict_name ='river_201803'

%matplotlib inline
import importlib
importlib.reload(rivers)
#what type of file would you like to create?
#e.g. 'constant' - yearly average used for all months
#     'monthly' - monthly averages used for each month ** CHANGE BOTH THESE LINES **
rivertype = 'monthly'

# what file name do you want to produce?
netcdf_file = 'rivers_month_201803.nc'
# what coordinates are you using?
coords = 'coordinates_seagrid_SalishSea201702.nc'
coords_file = '../../../grid/'+coords
# where is the river information? 
prop_dict = rivers.prop_dict
#get dimensions for netcdf files
fB = NC.Dataset(coords_file, 'r')
lat = fB.variables['nav_lat'][:]
lon = fB.variables['nav_lon'][:]
e1t = fB.variables['e1t'][0,:]
e2t = fB.variables['e2t'][0,:]
horz_area = e1t*e2t
fB.close()
#get watershed fluxes from netcdf file
if rivertype == 'constant':
    fluxfile = NC.Dataset('../../../nemo-forcing/rivers/Salish_allrivers_cnst.nc','r')
    #inialise the runoff and run_depth arrays
    runoff = np.zeros_like(horz_area)
if rivertype == 'monthly':
    fluxfile = NC.Dataset('../../../nemo-forcing/rivers/Salish_allrivers_monthly.nc','r')
    #inialise the runoff and run_depth arrays
    runoff = np.zeros((12, horz_area.shape[0], horz_area.shape[1]))
#list of watersheds we are including
names = ['skagit', 'fraser', 'evi_n', 'howe', 'bute', 'puget', 'jdf', 'evi_s', 'jervis', 'toba']
run_depth = np.ones_like(runoff)
run_temp = np.empty_like(runoff)
for name in range(0,len(names)):
    watershedname = names[name]
    print (watershedname)
    Flux = fluxfile.variables[watershedname][:]
    if rivertype == 'constant':
        Flux = float(Flux)
    runoff_orig = np.copy(runoff)
    runoff, run_depth, run_temp = rivertools.put_watershed_into_runoff(rivertype, horz_area,
                                            Flux, runoff, run_depth, run_temp,
                                            prop_dict[watershedname])
    if rivertype == 'constant':
        rivertools.check_sum(runoff_orig, runoff, Flux, horz_area)
    if rivertype == 'monthly':
        rivertools.check_sum_monthly(runoff_orig, runoff, Flux, horz_area)
# set up titles
netcdf_title = rivertype+' rivers for '+prop_dict_name
netcdf_comment = netcdf_title+' on '+coords
notebook = 'MakeClimatologyRiverFilesWithArea.ipynb'

nemo = NC.Dataset(netcdf_file, 'w')
nc_tools.init_dataset_attrs(nemo, netcdf_title, notebook,
                           'NEMO-forcing/rivers/'+netcdf_file, netcdf_comment)

# dimensions
nemo.createDimension('x', horz_area.shape[1])
nemo.createDimension('y', horz_area.shape[0])
nemo.createDimension('time_counter', None)
    
# variables
# latitude and longitude
nav_lat = nemo.createVariable('nav_lat', 'float32', ('y', 'x'), zlib=True)
nav_lat.setncattr('units', 'degrees_north')
nav_lat.setncattr('long_name', 'latitude')
nav_lat[:] = lat

nav_lon = nemo.createVariable('nav_lon','float32',('y','x'),zlib=True)
nav_lon.setncattr('units', 'degrees_east')
nav_lon.setncattr('long_name', 'longitude')
nav_lon[:] = lon

# area
area = nemo.createVariable('area', 'float32', ('y', 'x'), zlib=True)
area.setncattr('units', 'm2')
area.setncattr('long_name', 'horizontal_grid_cell_area')
area[:] = horz_area

# time
time_counter = nemo.createVariable('time_counter', 'float32', ('time_counter'),zlib=True)
time_counter.setncattr('units', 'month')
time_counter.setncattr('long_name', 'time')
if rivertype == 'monthly':
    time_counter[0:12] = range(1,13)
elif rivertype == 'constant':
    time_counter[0] = 1
else:
    print('problem!!!')

# runoff
rorunoff = nemo.createVariable('rorunoff', 'float32', ('time_counter','y','x'), zlib=True)
rorunoff.setncattr('units', 'kg m-2 s-1')
rorunoff.setncattr('long_name', 'runoff_flux')
if rivertype == 'monthly':
    rorunoff[0:12,:] = runoff
else:
    rorunoff[0, :] = runoff

# depth
rodepth = nemo.createVariable('rodepth','float32',('y','x'),zlib=True)
rodepth.setncattr('units', 'm')
rodepth.setncattr('long_name', 'maximum_runoff_depth')
if rivertype == 'monthly':
    rodepth[:] = run_depth[0,:,:]
else:
    rodepth[:] = run_depth
    
nc_tools.show_dataset_attrs(nemo)
nemo.close()<jupyter_output>file format: NETCDF4
Conventions: CF-1.6
title: monthly rivers for river_201803
institution: Dept of Earth, Ocean & Atmospheric Sciences, University of British Columbia
source: https://bitbucket.org/salishsea/tools/src/tip/MakeClimatologyRiverFilesWithArea.ipynb
references: REQUIRED
history: [2018-02-24 14:15:14] Created netCDF4 zlib=True dataset.
comment: monthly rivers for river_201803 on coordinates_seagrid_SalishSea201702.nc
file format: NETCDF4
Conventions: CF-1.6
title: monthly rivers for river_201803
institution: Dept of Earth, Ocean & Atmospheric Sciences, University of British Columbia
source: https://bitbucket.org/salishsea/tools/src/tip/MakeClimatologyRiverFilesWithArea.ipynb
references: REQUIRED
history: [2018-02-24 14:15:14] Created netCDF4 zlib=True dataset.
comment: monthly rivers for river_201803 on coordinates_seagrid_SalishSea201702.nc
StarcoderdataJupyterStructuredCleanDedup