def test_repeated_run(self): # NOTE: this test fails if outfile is not None #outfile = 'leeway_test.nc' outfile = None o = OceanDrift(loglevel=50) o.set_config('drift:vertical_mixing', False) o.add_readers_from_list(reader_list) o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 12)) o.run(steps=5, outfile=outfile) lon1 = o.get_property('lon')[0] # Repeated run with same object o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 12)) o.run(steps=5, outfile=outfile) lon2 = o.get_property('lon')[0] # Third run, with different config o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 12), wind_drift_factor=.1) o.run(steps=5) lon3 = o.get_property('lon')[0] # Fourth run, with different time o.reset() # Reset is needed due to new start_time o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 13), wind_drift_factor=.1) o.run(steps=5, outfile=outfile) lon4 = o.get_property('lon')[0] # Check results self.assertEqual(lon1[-1][0], lon2[-1][0]) self.assertNotEqual(lon3[-1][0], lon2[-1][0])
def test_repeated_run(self): o = OceanDrift(loglevel=50) o.add_readers_from_list(reader_list) o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 12)) o.run(steps=5) lon1 = o.get_property('lon')[0] # Repeated run with same object o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 12)) o.run(steps=5) lon2 = o.get_property('lon')[0] # Third run, with different config o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 12), wind_drift_factor=.1) o.run(steps=5) lon3 = o.get_property('lon')[0] # Fourth run, with different time o.reset() # Reset is needed due to new start_time o.seed_elements(lon=14, lat=67.85, time=datetime(2016, 2, 2, 13), wind_drift_factor=.1) o.run(steps=5) lon4 = o.get_property('lon')[0] # Check results self.assertEqual(lon1[-1][0], lon2[-1][0]) self.assertNotEqual(lon3[-1][0], lon2[-1][0])
def test_adding_readers(self): o = OceanDrift() landmask = reader_global_landmask.Reader( extent=[-1.5, 7, 59, 64]) r = reader_ROMS_native.Reader(o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc') o.add_reader([r, landmask]) self.assertEqual(o.priority_list['land_binary_mask'], ['roms native', 'global_landmask']) self.assertEqual(o.priority_list['x_sea_water_velocity'], ['roms native']) # Switch order o = OceanDrift() o.add_reader([landmask, r]) self.assertEqual(o.priority_list['land_binary_mask'], ['global_landmask', 'roms native']) self.assertEqual(o.priority_list['x_sea_water_velocity'], ['roms native']) # Test add_readers_from_list o = OceanDrift() o.add_readers_from_list(reader_list, lazy=False) self.assertEqual(o.priority_list['x_sea_water_velocity'], ['roms native']) self.assertEqual(o.priority_list['x_wind'], [o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/AROME_MetCoOp_00_DEF.nc_20160202_subset'])
def test_adding_readers(self): o = OceanDrift() basemap = reader_basemap_landmask.Reader(llcrnrlon=-1.5, llcrnrlat=59, urcrnrlon=7, urcrnrlat=64, resolution='c') r = reader_ROMS_native.Reader( o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/Nordic-4km_SLEVELS_avg_00_subset2Feb2016.nc' ) o.add_reader([r, basemap]) self.assertEqual(o.priority_list['land_binary_mask'], ['roms native', 'basemap_landmask']) self.assertEqual(o.priority_list['x_sea_water_velocity'], ['roms native']) # Switch order o = OceanDrift() o.add_reader([basemap, r]) self.assertEqual(o.priority_list['land_binary_mask'], ['basemap_landmask', 'roms native']) self.assertEqual(o.priority_list['x_sea_water_velocity'], ['roms native']) # Test add_readers_from_list o = OceanDrift() o.add_readers_from_list(reader_list, lazy=False) self.assertEqual(o.priority_list['x_sea_water_velocity'], ['roms native']) self.assertEqual(o.priority_list['x_wind'], [ o.test_data_folder() + '2Feb2016_Nordic_sigma_3d/AROME_MetCoOp_00_DEF.nc_20160202_subset' ])
def test_get_variables_along_trajectory_and_wind_drift_factor_from_trajectory( ): o = OceanDrift(loglevel=50) o.add_readers_from_list([ o.test_data_folder() + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc', o.test_data_folder() + '16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc' ], lazy=False) t = o.get_variables_along_trajectory( variables=[ 'x_sea_water_velocity', 'y_sea_water_velocity', 'x_wind', 'y_wind' ], lons=np.array([3.5, 4, 4.5]), lats=np.array([59.7, 60, 60.3]), times=[ o.readers[list(o.readers)[0]].start_time + i * timedelta(hours=3) for i in range(3) ]) np.testing.assert_array_almost_equal(t['x_sea_water_velocity'], [-0.078685, -0.106489, -0.058386]) np.testing.assert_array_almost_equal(t['x_wind'], [-8.308249, -13.063459, -11.09289]) wdf, azimuth = wind_drift_factor_from_trajectory(t) np.testing.assert_array_almost_equal(wdf, [0.27189012, 0.20492421]) np.testing.assert_array_almost_equal(azimuth, [73.0112213, 82.39749185])
#!/usr/bin/env python """ Thredds (global) ================================== """ from datetime import datetime, timedelta from opendrift.models.oceandrift import OceanDrift # Basic ocean drift module: current + 2% of wind o = OceanDrift() # Adding readers for global Thredds datasets: # - Ocean forecast from global HYCOM model # - Weather forecast from NOAA/NCEP o.add_readers_from_list([ 'https://tds.hycom.org/thredds/dodsC/GLBy0.08/latest', 'https://pae-paha.pacioos.hawaii.edu/thredds/dodsC/ncep_global/NCEP_Global_Atmospheric_Model_best.ncd' ]) o.seed_elements(lat=24, lon=-81, time=datetime.utcnow(), number=5000, radius=10000) o.run(time_step=timedelta(minutes=30), duration=timedelta(days=5)) o.animation()
#!/usr/bin/env python """ Drift at different depths ========================== """ from datetime import datetime, timedelta import numpy as np from opendrift.models.oceandrift import OceanDrift o = OceanDrift(loglevel=20) # Set loglevel to 0 for debug information #%% # Using live data from Thredds o.add_readers_from_list([ 'https://thredds.met.no/thredds/dodsC/sea/norkyst800m/1h/aggregate_be']) #%% # Seed 1000 elements at random depths z = -np.random.rand(2000)*50 o.seed_elements(lon=4.8, lat=60.0, z=z, radius=0, number=2000, time=datetime.utcnow()) print(o) #%% # Adding some diffusion o.set_config('drift:horizontal_diffusivity', 10) # m2/s #%% # Running model
from opendrift.models.physics_methods import wind_drift_factor_from_trajectory, distance_between_trajectories, skillscore_liu_weissberg #%% # A very simple drift model is: current + wind_drift_factor*wind # where wind_drift_factor for surface drift is typically # 0.033 if Stokes drift included, and 0.02 in addition to Stokes drift. # This example illustrates how a best-fit wind_drift_factor # can be calculated from an observed trajectory, using two different methods. #%% # First we simulate a synthetic drifter trajectory ot = OceanDrift(loglevel=50) ot.add_readers_from_list([ ot.test_data_folder() + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc', ot.test_data_folder() + '16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc' ], lazy=False) #%% # Using a wind_drift_factor of 0.33 i.e. drift is current + 3.3% of wind speed ot.seed_elements(lon=4, lat=60, number=1, time=ot.readers[list(ot.readers)[0]].start_time, wind_drift_factor=0.033) #%% # Adding some horizontal diffusivity as "noise" ot.set_config('drift:horizontal_diffusivity', 10)
#%% # Seed information lon = 4.8 lat = 60 # Bergen, Norway lon = -89 lat = 29.8 # New Orleans lon = 107 lat = 10 # Ho Chi Minh lon = 123 lat = -16.3 # Australia o = OceanDrift() o.add_readers_from_list([ 'https://nrt.cmems-du.eu/thredds/dodsC/cmems_mod_glo_phy_anfc_merged-uv_PT1H-i' ]) o.seed_elements(lon=lon, lat=lat, number=5000, radius=1000, time=datetime.utcnow()) o.run(duration=timedelta(days=3)) o.animation(fast=True, clabel='Ocean current [m/s]', background=['x_sea_water_velocity', 'y_sea_water_velocity']) #%% # .. image:: /gallery/animations/example_cmems_0.gif
#!/usr/bin/env python from datetime import datetime, timedelta from opendrift.models.oceandrift import OceanDrift # Basic ocean drift module: current + 2% of wind o = OceanDrift() # Adding readers for global Thredds datasets: # - Ocean forecast from UK Met Office (FOAM model) # - Weather forecast from NOAA/NCEP o.add_readers_from_list([ 'http://data.ncof.co.uk/thredds/dodsC/METOFFICE-GLO-AF-PHYS-HOURLY-CUR', 'http://oos.soest.hawaii.edu/thredds/dodsC/hioos/model/atm/ncep_global/NCEP_Global_Atmospheric_Model_best.ncd']) o.seed_elements(lat=24, lon=-81, time=datetime.now(), number=5000, radius=10000) o.run(time_step=timedelta(minutes=30), duration=timedelta(days=5)) o.animation()
#!/usr/bin/env python from datetime import datetime, timedelta from opendrift.models.oceandrift import OceanDrift # Basic ocean drift module: current + 2% of wind o = OceanDrift() # Adding readers for global Thredds datasets: # - Ocean forecast from UK Met Office (FOAM model) # - Weather forecast from NOAA/NCEP o.add_readers_from_list([ 'http://data.ncof.co.uk/thredds/dodsC/METOFFICE-GLO-AF-PHYS-HOURLY-CUR', 'http://oos.soest.hawaii.edu/thredds/dodsC/hioos/model/atm/ncep_global/NCEP_Global_Atmospheric_Model_best.ncd' ]) o.seed_elements(lat=24, lon=-81, time=datetime.now(), number=5000, radius=10000) o.run(time_step=timedelta(minutes=30), duration=timedelta(days=5)) o.animation()
def test_dateline(self): # Make synthetic netCDF file with currents from 0 to 360 deg longitude fc = 'opendrift_test_current_0_360.nc' lon = np.arange(0, 360) lat = np.arange(-88, 89) start_time = datetime(2021, 1, 1) time = [start_time + i * timedelta(hours=24) for i in range(3)] t, xcurr, ycurr = np.meshgrid(time, np.zeros(lat.shape), np.zeros(lon.shape), indexing='ij') xcurr[:, :, 0:180] = 1 # eastward xcurr[:, :, 180:] = -1 # westward, i.e. current divergence at lon=0 ds = xr.Dataset( { "xcurr": (("time", "lat", "lon"), xcurr, { 'standard_name': 'x_sea_water_velocity' }), "ycurr": (("time", "lat", "lon"), ycurr, { 'standard_name': 'y_sea_water_velocity' }) }, coords={ "lon": lon, "lat": lat, "time": time }) ds.to_netcdf(fc) # Make synthetic netCDF file with winds from -180 to 180 deg longitude fw = 'opendrift_test_winds_180_180.nc' lon = np.arange(-180, 180) t, xwind, ywind = np.meshgrid(time, np.zeros(lat.shape), np.zeros(lon.shape), indexing='ij') ywind[:, :, 0:180] = 1 # northward ywind[:, :, 180:] = -1 # southward, i.e. wind divergence at lon=180 ds = xr.Dataset( { "xwind": (("time", "lat", "lon"), xwind, { 'standard_name': 'x_wind' }), "ywind": (("time", "lat", "lon"), ywind, { 'standard_name': 'y_wind' }) }, coords={ "lon": lon, "lat": lat, "time": time }) ds.to_netcdf(fw) reader_current = reader_netCDF_CF_generic.Reader(fc) reader_wind = reader_netCDF_CF_generic.Reader(fw) # Simulation across 0 meridian o = OceanDrift(loglevel=30) o.add_readers_from_list([fc, fw]) o.seed_elements(lon=[-2, 2], lat=[60, 60], time=start_time, wind_drift_factor=.1) o.run(steps=2) # Check that current give divergence, and that # wind is northwards east of 0 and southwards to the east np.testing.assert_array_almost_equal(o.elements.lon, [-2.129, 2.129], decimal=3) np.testing.assert_array_almost_equal(o.elements.lat, [60.006, 59.994], decimal=3) # Simulation across dateline (180 E/W) o = OceanDrift(loglevel=30) o.add_readers_from_list([fc, fw]) o.seed_elements(lon=[-175, 175], lat=[60, 60], time=start_time, wind_drift_factor=.1) o.run(steps=2) #o.plot(fast=True) # Check that current give convergence, and that # wind is northwards east of 180 and southwards to the west np.testing.assert_array_almost_equal(o.elements.lon, [-175.129, 175.129], decimal=3) np.testing.assert_array_almost_equal(o.elements.lat, [60.006, 59.994], decimal=3) # Cleaning up os.remove(fw) os.remove(fc)
#!/usr/bin/env python from datetime import datetime, timedelta from opendrift.models.oceandrift import OceanDrift # Basic ocean drift module: current + 2% of wind o = OceanDrift() # Adding readers for global Thredds datasets: # - Ocean forecast from UK Met Office (FOAM model) # - Weather forecast from NOAA/NCEP o.add_readers_from_list([ 'http://tds.hycom.org/thredds/dodsC/GLBy0.08/latest', 'http://oos.soest.hawaii.edu/thredds/dodsC/hioos/model/atm/ncep_global/NCEP_Global_Atmospheric_Model_best.ncd' ]) o.seed_elements(lat=24, lon=-81, time=datetime.now(), number=5000, radius=10000) o.run(time_step=timedelta(minutes=30), duration=timedelta(days=5)) o.animation()