OpenDrift / opendrift

Open source framework for ocean trajectory modelling
https://opendrift.github.io
GNU General Public License v2.0
247 stars 121 forks source link

Problem saving animation as .mp4 or gif #1030

Closed joecabz1985 closed 1 year ago

joecabz1985 commented 1 year ago

I am new in python and opendrift. I want to save the animation of the model. I enable the image but not working. Kindly help me please. image:: /gallery/animations/example_leeway_0.gif

knutfrode commented 1 year ago

Hi, can you pleasy explain more precisely what you are trying to do?

To save to mp4, you provide filename="file.mp4" to animation method.

joecabz1985 commented 1 year ago

I am start learning python. I want to save the animation in this code. Which part should I need to modify and what exactly the code? Thank you for helping me.

!/usr/bin/env python

""" Leeway

"""

from datetime import timedelta import cmocean import xarray as xr from opendrift.readers import reader_netCDF_CF_generic from opendrift.models.leeway import Leeway

lw = Leeway(loglevel=20) # Set loglevel to 0 for debug information

Atmospheric model for wind

reader_arome = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/mepslatest/meps_lagged_6_h_latest_2_5km_latest.nc')

reader_arome = reader_netCDF_CF_generic.Reader(lw.test_data_folder() + '16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc')

Ocean model for current

reader_norkyst = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/mepslatest/meps_lagged_6_h_latest_2_5km_latest.nc')

reader_norkyst = reader_netCDF_CF_generic.Reader(lw.test_data_folder() + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')

%%

Adding readers successively, and specifying which variables they

shall provide. This way, order of adding readers does not matter

lw.add_reader(reader_norkyst, variables=['x_sea_water_velocity', 'y_sea_water_velocity']) lw.add_reader(reader_arome, variables=['x_wind', 'y_wind']) lw.set_config('environment:fallback:x_sea_water_velocity', 0) lw.set_config('environment:fallback:y_sea_water_velocity', 0)

%%

Seed leeway elements at defined position and time

object_type = 26 # 26 = Life-raft, no ballast lw.seed_elements(lon=4.5, lat=59.6, radius=100, number=1000, time=reader_arome.start_time, object_type=object_type)

%%

Running model

lw.run(duration=timedelta(hours=48), time_step=900, time_step_output=3600)

%%

Print and plot results

print(lw)

%%

Animation with current as background.

Note that drift is also depending on wind, which is not shown.

lw.animation(background=['x_sea_water_velocity', 'y_sea_water_velocity'], skip=5, # show every 5th vector cmap=cmocean.cm.speed, vmin=0, vmax=.8, bgalpha=.7, land_color='#666666', fast=True)

%%

.. image:: /gallery/animations/example_leeway_0.gif

lw.plot(fast=True)

%%

Plot density of stranded elements

d, dsub, dstr, lon, lat = lw.get_density_array(pixelsize_m=3000) strand_density = xr.DataArray(dstr[-1,:,:], coords={'lon_bin': lon[0:-1], 'lat_bin': lat[0:-1]}) lw.plot(fast=True, background=strand_density.where(strand_density>0), vmin=0, vmax=20, clabel='Density of stranded elements', show_elements=False, linewidth=0)

joecabz1985 commented 1 year ago

thank you very much. I found how to do it. thank you very much

knutfrode commented 1 year ago

Very good