# Play with Beaching #
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
import xarray as xr
from salishsea_tools import viz_tools
mesh = xr.open_dataset('/home/sallen/MEOPAR/grid/mesh_mask201702.nc')
land = mesh.tmask[0, 0]
mesh
<xarray.Dataset> Dimensions: (t: 1, x: 398, y: 898, z: 40) Dimensions without coordinates: t, x, y, z Data variables: (12/43) nav_lon (y, x) float32 ... nav_lat (y, x) float32 ... time_counter (t) datetime64[ns] 1900-01-01 tmask (t, z, y, x) int8 ... umask (t, z, y, x) int8 ... vmask (t, z, y, x) int8 ... ... ... gdepv (t, z, y, x) float32 ... gdepw_0 (t, z, y, x) float32 ... gdept_1d (t, z) float64 0.5 1.5 2.5 3.5 4.5 ... 360.7 387.6 414.5 441.5 gdepw_1d (t, z) float64 0.0 1.0 2.0 3.0 4.0 ... 347.2 374.1 401.1 428.0 e3t_1d (t, z) float64 1.0 1.0 1.0 1.0 1.0 ... 26.93 26.93 26.93 26.93 e3w_1d (t, z) float64 1.0 1.0 1.0 1.0 1.0 ... 26.92 26.93 26.93 26.93 Attributes: (12/18) DOMAIN_number_total: 1 DOMAIN_number: 0 DOMAIN_dimensions_ids: [1 2] DOMAIN_size_global: [398 898] DOMAIN_size_local: [398 898] DOMAIN_position_first: [1 1] ... ... Conventions: CF-1.6 title: Salish Sea NEMO bathymetry_201702 Bathymetry Mes... institution: Dept of Earth, Ocean & Atmospheric Sciences, Uni... source: NEMO-3.6 Salish Sea configuration references: https://salishsea.eos.ubc.ca/erddap/info/\nhttps... history: [2019-03-14 15:00] ncks -4 -L4 -O mesh_mask.nc m...
[357404 values with dtype=float32]
[357404 values with dtype=float32]
array(['1900-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
[14296160 values with dtype=int8]
[14296160 values with dtype=int8]
[14296160 values with dtype=int8]
[14296160 values with dtype=int8]
[357404 values with dtype=int8]
[357404 values with dtype=int8]
[357404 values with dtype=int8]
[357404 values with dtype=int8]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float32]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=float64]
[357404 values with dtype=int16]
[357404 values with dtype=int16]
[357404 values with dtype=float32]
[14296160 values with dtype=float64]
[14296160 values with dtype=float64]
[14296160 values with dtype=float64]
[14296160 values with dtype=float64]
[14296160 values with dtype=float32]
[14296160 values with dtype=float32]
[14296160 values with dtype=float32]
[14296160 values with dtype=float32]
array([[ 0.5 , 1.500003, 2.500012, 3.500031, 4.50007 , 5.500151, 6.50031 , 7.500623, 8.501236, 9.502433, 10.504765, 11.509311, 12.518167, 13.535412, 14.568982, 15.634287, 16.761173, 18.007135, 19.481785, 21.389979, 24.100257, 28.229915, 34.685758, 44.517725, 58.484334, 76.585584, 98.062959, 121.866518, 147.089458, 173.114482, 199.573049, 226.260306, 253.066637, 279.93455 , 306.834197, 333.75017 , 360.674532, 387.603203, 414.534088, 441.46611 ]])
array([[ 0. , 1.000001, 2.000006, 3.000019, 4.000047, 5.000104, 6.000217, 7.000441, 8.000879, 9.001735, 10.003406, 11.006663, 12.013008, 13.025366, 14.049429, 15.096256, 16.187303, 17.364034, 18.705972, 20.363473, 22.613064, 25.937413, 31.101035, 39.118857, 50.963237, 67.052074, 86.96747 , 109.737066, 134.345934, 160.029562, 186.305278, 212.896557, 239.653045, 266.495214, 293.381605, 320.29076 , 347.21162 , 374.138492, 401.068453, 428. ]])
array([[ 1.000001, 1.000005, 1.000013, 1.000027, 1.000056, 1.000111, 1.000219, 1.00043 , 1.000841, 1.00164 , 1.003197, 1.006229, 1.012133, 1.023624, 1.045976, 1.089401, 1.173564, 1.335929, 1.646368, 2.229903, 3.292486, 5.119985, 7.974515, 11.825297, 16.10792 , 19.958703, 22.813233, 24.640732, 25.703315, 26.28685 , 26.597289, 26.759653, 26.843817, 26.887242, 26.909594, 26.921085, 26.926989, 26.930021, 26.931578, 26.932377]])
array([[ 1. , 1.000003, 1.000008, 1.000019, 1.000039, 1.000079, 1.000156, 1.000307, 1.000602, 1.001174, 1.00229 , 1.004463, 1.008694, 1.016931, 1.032959, 1.06412 , 1.1246 , 1.24159 , 1.466437, 1.893272, 2.684857, 4.091313, 6.409889, 9.797678, 13.966609, 18.13554 , 21.523329, 23.841905, 25.248361, 26.039946, 26.46678 , 26.691628, 26.808618, 26.869097, 26.900259, 26.916287, 26.924524, 26.928755, 26.930928, 26.932043]])
# Kits
imin, imax = 448, 460
jmin, jmax = 321, 345
fig, ax = plt.subplots(1, 1)
land[imin:imax, jmin:jmax].plot(ax=ax)
viz_tools.set_aspect(ax);
mymask = np.zeros_like(land)
mymask[imin:imax, jmin:jmax] = 1
# Orcas
imin, imax = 320, 327
jmin, jmax = 284, 308
fig, ax = plt.subplots(1, 1)
land[imin:imax, jmin:jmax].plot(ax=ax)
viz_tools.set_aspect(ax);
mymask = np.zeros_like(land)
mymask[imin:imax, jmin:jmax] = 1
# Lummi
imin, imax = 305, 320
jmin, jmax = 337, 355
fig, ax = plt.subplots(1, 1)
land[imin:imax, jmin:jmax].plot(ax=ax)
viz_tools.set_aspect(ax);
mymask = np.zeros_like(land)
mymask[imin:imax, jmin:jmax] = 1
values = np.ma.masked_array(np.ones_like(land[imin:imax, jmin:jmax]), mask=1-land[imin:imax, jmin:jmax])
np.ma.notmasked_edges(values)
minoil = 5
colours = {'bunker': 'blue',
'other': 'blue',
'akns': 'navy',
'diesel': 'skyblue',
'gas': 'skyblue',
'jet': 'skyblue',
'dilbit': 'navy',
}
colours['bunker']
data.OilType.item()
'dilbit'
month_colours = ['slateblue', 'slateblue', 'lawngreen', 'lawngreen', 'indianred', 'indianred',
'goldenrod', 'goldenrod', 'goldenrod', 'darkorchid', 'darkorchid', 'slateblue']
scale = 5
scount = 0
ncount = 0
fig, axs = plt.subplots(1, 2, figsize=(15, 7))
for ax in axs:
ax.pcolormesh(mesh.nav_lon, mesh.nav_lat, land[:-1,:-1], cmap='BuGn_r')
direct = Path('/data/sallen/results/MIDOSS/beaching_files/')
for file in direct.glob('*'):
data = xr.open_dataset(file)
# month = data.Spilldatetime.values.astype('datetime64[M]').astype(int) % 12
if (np.ma.masked_array(data.Beaching_Volume, mask=data.Beaching_Volume < minoil/1000.) *
mymask[1:-1, 1:-1]).sum(axis=0).sum(axis=0) > 0 :
axs[1].scatter(data.SpillLon, data.SpillLat, marker='o', c=colours[data.OilType.item()],
s=np.log(1000*data.SpillVolume.item())*scale,
edgecolors='k', zorder=3)
scount = scount + 1
else:
axs[0].scatter(data.SpillLon, data.SpillLat, marker='x', c=colours[data.OilType.item()],
s=np.log(1000*data.SpillVolume.item())*scale)
ncount = ncount + 1
data.close()
for ax in axs:
ax.set_xlim(-123.6, -122.35)
ax.set_ylim(48.35, 49.4)
ax.plot([mesh.nav_lon[imin, jmin], mesh.nav_lon[imin, jmax], mesh.nav_lon[imax, jmax],
mesh.nav_lon[imax, jmin], mesh.nav_lon[imin, jmin]],
[mesh.nav_lat[imin, jmin], mesh.nav_lat[imin, jmax], mesh.nav_lat[imax, jmax],
mesh.nav_lat[imax, jmin], mesh.nav_lat[imin, jmin]],
color='orange')
viz_tools.set_aspect(ax, coords='map');
axs[0].set_title(f'{ncount} Spills that do *not* Result in Oil on Coastline in Orange Box')
axs[1].set_title(f'{scount} Spills that do Result in Oil on Coastline in Orange Box');
data
<xarray.Dataset> Dimensions: (grid_x: 396, grid_y: 896) Coordinates: * grid_y (grid_y) int16 0 1 2 3 4 5 6 ... 890 891 892 893 894 895 * grid_x (grid_x) int16 0 1 2 3 4 5 6 ... 390 391 392 393 394 395 Data variables: Beaching_Volume (grid_y, grid_x) float64 ... Beaching_Time (grid_y, grid_x) datetime64[ns] ... OilType <U1 '' SpillVolume float64 6.89e+03 SpillLon float64 -122.8 SpillLat float64 48.75 Spilldatetime datetime64[ns] 2017-06-13T05:30:00 Attributes: acknowledgements: MOHID output creator_email: sallen@eoas.ubc.ca creator_name: Salish Sea MEOPAR Project Contributors creator_url: https://ubc-moad-docs.readthedocs.org/ institution: UBC EOAS institution_fullname: Earth, Ocean & Atmospheric Sciences, University of... summary: Beaching Time and Volume from a Specific Run source: analysis-susan/notebooks/MOHID/SaveBeaching.py history: [2022-04-21] File creation.
array([ 0, 1, 2, ..., 893, 894, 895], dtype=int16)
array([ 0, 1, 2, ..., 393, 394, 395], dtype=int16)
[354816 values with dtype=float64]
[354816 values with dtype=datetime64[ns]]
array('', dtype='<U1')
array(6890.)
array(-122.750926)
array(48.754464)
array('2017-06-13T05:30:00.000000000', dtype='datetime64[ns]')
mydate = data.Spilldatetime.values
mydate
numpy.datetime64('2017-07-04T09:30:00.000000000')
dt.datetime.fromtimestamp(mydate)
--------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-168-1bac75e62b4b> in <module> ----> 1 dt.datetime.fromtimestamp(mydate) OSError: [Errno 75] Value too large for defined data type
data.Spilldatetime.values.astype('datetime64[M]').astype(int) % 12 + 1
7
months
1