import numpy as np
import xarray as xr
import math
import matplotlib.pyplot as plt
%matplotlib inline
ds = xr.open_dataset('/data/jpetrie/MEOPAR/SalishSea/results/d9f69a38-160e-11e6-9b98-0025909a8461')
print(ds)
<xarray.Dataset> Dimensions: (gridX: 398, gridY: 898) Coordinates: * gridY (gridY) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ... * gridX (gridX) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ... Data variables: longitude (gridY, gridX) float32 ... latitude (gridY, gridX) float32 ... bathymetry (gridY, gridX) float64 ... Attributes: acknowledgement: MEOPAR, ONC, Compute Canada cdm_data_type: Grid comment: Bathymetry, Latitudes and Longitudes Conventions: CF-1.6, COARDS, ACDD-1.3 coverage_content_type: modelResult creator_email: sallen@eos.ubc.ca creator_name: Salish Sea MEOPAR Project Contributors creator_url: https://salishsea-meopar-docs.readthedocs.org/ drawLandMask: over history: [2016-02-05 16:35:19] Created dataset. [2016-03-02 18:08:56] Changed all variables to zlib=True. [2016-03-02 18:08:56] Added least_significant_digit=1 and fill_value=0 to bathymetry variable. [2016-03-03 12:28:37] Added valid_range attribute to all variables. 2016-05-10T17:11:13Z (local files) 2016-05-10T17:11:13Z https://salishsea.eos.ubc.ca/erddap/griddap/ubcSSnBathymetry2V1.das infoUrl: https://salishsea-meopar-tools.readthedocs.org/en/latest/results_server/index.html#salish-sea-model-results institution: UBC EOAS institution_fullname: Earth, Ocean & Atmospheric Sciences, University of British Columbia keywords: bathymetry, bottom, data, model results, depth, floor, latitude, longitude, nemo, ocean, oceans, Oceans > Bathymetry/Seafloor Topography > Bathymetry, salishsea, sea, sea_floor_depth, seafloor, topography keywords_vocabulary: GCMD Science Keywords license: The Salish Sea MEOPAR NEMO model results are copyright 2013-2016 by the Salish Sea MEOPAR Project Contributors and The University of British Columbia. They are licensed under the Apache License, Version 2.0. http://www.apache.org/licenses/LICENSE-2.0 project: Salish Sea MEOPAR NEMO Model references: https://bitbucket.org/salishsea/nemo-forcing/src/tipgrid/mesh_mask_SalishSea2.nc source: https://bitbucket.org/salishsea/tools/src/tip/bathymetry/NEMOBathymetryfromMeshMask.ipynb sourceUrl: (local files) standard_name_vocabulary: CF Standard Name Table v29 summary: Salish Sea NEMO Model Grid, Geo-location and Bathymetry, v1 Longitude, latitude, and bathymetry of the Salish Sea NEMO model grid. The bathymetry values are those calculated by NEMO from the input bathymetry file. NEMO modifies the input bathymetry to remove isolated holes, and too-small partial steps. The model grid includes the Juan de Fuca Strait, the Strait of Georgia, Puget Sound, and Johnstone Strait on the coasts of Washington State and British Columbia. v1: longitude, latitude and b... title: Salish Sea NEMO Model Grid, Geo-location and Bathymetry, v1
print(ds.dims)
print(ds.dims['gridY'])
Frozen(SortedKeysDict({'gridY': 898, 'gridX': 398})) 898
ds.data_vars
Data variables: longitude (gridY, gridX) float32 ... latitude (gridY, gridX) float32 ... bathymetry (gridY, gridX) float64 ...
ds.coords
Coordinates: * gridY (gridY) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... * gridX (gridX) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
ds.attrs
OrderedDict([('acknowledgement', 'MEOPAR, ONC, Compute Canada'), ('cdm_data_type', 'Grid'), ('comment', 'Bathymetry, Latitudes and Longitudes'), ('Conventions', 'CF-1.6, COARDS, ACDD-1.3'), ('coverage_content_type', 'modelResult'), ('creator_email', 'sallen@eos.ubc.ca'), ('creator_name', 'Salish Sea MEOPAR Project Contributors'), ('creator_url', 'https://salishsea-meopar-docs.readthedocs.org/'), ('drawLandMask', 'over'), ('history', '[2016-02-05 16:35:19] Created dataset.\n[2016-03-02 18:08:56] Changed all variables to zlib=True.\n[2016-03-02 18:08:56] Added least_significant_digit=1 and fill_value=0 to bathymetry variable.\n[2016-03-03 12:28:37] Added valid_range attribute to all variables.\n2016-05-10T17:11:13Z (local files)\n2016-05-10T17:11:13Z https://salishsea.eos.ubc.ca/erddap/griddap/ubcSSnBathymetry2V1.das'), ('infoUrl', 'https://salishsea-meopar-tools.readthedocs.org/en/latest/results_server/index.html#salish-sea-model-results'), ('institution', 'UBC EOAS'), ('institution_fullname', 'Earth, Ocean & Atmospheric Sciences, University of British Columbia'), ('keywords', 'bathymetry, bottom, data, model results, depth, floor, latitude, longitude, nemo, ocean, oceans,\nOceans > Bathymetry/Seafloor Topography > Bathymetry, salishsea, sea, sea_floor_depth, seafloor, topography'), ('keywords_vocabulary', 'GCMD Science Keywords'), ('license', 'The Salish Sea MEOPAR NEMO model results are copyright 2013-2016\nby the Salish Sea MEOPAR Project Contributors and The University of British Columbia.\n\nThey are licensed under the Apache License, Version 2.0. http://www.apache.org/licenses/LICENSE-2.0'), ('project', 'Salish Sea MEOPAR NEMO Model'), ('references', 'https://bitbucket.org/salishsea/nemo-forcing/src/tipgrid/mesh_mask_SalishSea2.nc'), ('source', 'https://bitbucket.org/salishsea/tools/src/tip/bathymetry/NEMOBathymetryfromMeshMask.ipynb'), ('sourceUrl', '(local files)'), ('standard_name_vocabulary', 'CF Standard Name Table v29'), ('summary', 'Salish Sea NEMO Model Grid, Geo-location and Bathymetry, v1\n\nLongitude, latitude, and bathymetry of the Salish Sea NEMO model grid.\nThe bathymetry values are those calculated by NEMO from the input bathymetry file.\nNEMO modifies the input bathymetry to remove isolated holes, and too-small partial steps.\nThe model grid includes the Juan de Fuca Strait, the Strait of Georgia, Puget Sound,\nand Johnstone Strait on the coasts of Washington State and British Columbia.\n\nv1: longitude, latitude and bathymetry variables'), ('title', 'Salish Sea NEMO Model Grid, Geo-location and Bathymetry, v1')])
print(ds.bathymetry.values)
(np.logical_not(np.isnan(ds.bathymetry.values))).sum()/ds.bathymetry.values.size # percentage of area that is water (by area)
[[ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan] ..., [ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan] [ nan nan nan ..., nan nan nan]]
0.21566350684379582
# Subset for values that are not NaN (loses dimensionality though)
(ds.bathymetry.values[np.logical_not(np.isnan(ds.bathymetry.values))])
array([ 4. , 5.375 , 8.375 , ..., 133.1875, 8.25 , 7.625 ])
fig, ax = plt.subplots(1, 1,figsize=(15,15))
mesh = ax.pcolormesh(ds.longitude.values, ds.latitude.values,ds.bathymetry.values)
fig.colorbar(mesh)
mesh.set_clim(vmin=0, vmax=200)
ds.latitude.values
array([[ 46.85966492, 46.86154556, 46.86342621, ..., 47.59721375, 47.59906769, 47.60092163], [ 46.86278915, 46.86481476, 46.86677933, ..., 47.60125732, 47.60311127, 47.60496521], [ 46.86606979, 46.86814499, 46.87015915, ..., 47.60529709, 47.60715485, 47.60900879], ..., [ 50.38191605, 50.38397598, 50.38602448, ..., 51.09400177, 51.09560776, 51.09720612], [ 50.38591766, 50.38798523, 50.39004135, ..., 51.09781265, 51.0994072 , 51.10100174], [ 50.38992691, 50.39200592, 50.39406967, ..., 51.10162354, 51.10321808, 51.10480118]], dtype=float32)