ocean current velocity data m_quiver plot on high resolution coast line refreshdata not working

4 views (last 30 days)
I have u & v ocean current forecast velocity data and I need to plot every three hours in a loop using 8 days worth of data, then use this plot to create an animated gif. If I loop the map projection its very slow but works. So I was hoping to only refresh the plot data but refreshdata is either not working or more likely than not, I am not using it correctly. Could someone please review my code and see what I am doing wrong or please point me in the right direction. I updated my code with the suggestion below and was still unable to get it to work
This animated gif below is what I am aiming to accomplish with some more efficient code. Right now its super slow because it loops the code that creates the map and axis with the high resolution coastline (the coastline slows this down significantly) and m_quiver plot each iteration.
get_vel_data
global start_stamp
global tstamp
t = datenum(DateTime, 'yyyy-mm-ddTHH:MM:SSZ');
t = datetime(t,'ConvertFrom','datenum','Format', 'yyyy-MM-dd HH:mm:ss');
% start = '2015-11-14 00:00:00';
start = datetime('2015-12-01 00:00:00', 'Format', 'yyyy-MM-dd HH:mm:ss');
start_stamp = datestr(start);
ds = dataset(t,Lat,Lon,u_vel,v_vel);
%%Animated Plot
figure(99)
filename = 'testnew72.gif';
% set map and grid
axes('Position',[0.1,0.1,0.8,0.8]);
m_proj('albers equal-area','lat',[50 65],'lon',[185 215]);
m_gshhs_h('patch',[.6 .6 .6]);
m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','yaxisloc','right');
tiHan = title(['U and V Ocean Current Velocity Forecast Data Downloaded on ',start_stamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
% axis x, y value of title position
tiPos = get (tiHan, 'position');
xyrange = axis;
% move title up
set(tiHan, 'position', tiPos + [0 0.05 * (xyrange(4) - xyrange(3)) 0]);
% so Title doesn't move on zoom.
set(tiHan, 'units', 'inches');
xlabel(['Forecast Data for ',tstamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
hold on;
lat = 0;
lon = 0;
u = 0;
v = 0;
uv_plot = m_quiver(lon,lat,u,v);
% looping through the 64 different time frames. 24hrs/(3hr fcasts)*8days=64
for n = 1:64
if n==1;
i=0;
else
i=i+3;
end
% update timestamp for axis label
tstamp = start + hours(i);
tstamp = datestr(tstamp);
% uv_plot.UDataSource = 'u';
% uv_plot.VDataSource = 'v';
% uv_plot.XDataSource = 'lat';
% uv_plot.YDataSource = 'lon';
% xlabel([], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
% update temp dataset
t0 = ds(ds.t == start + hours(i),{'t','Lat','Lon','u_vel','v_vel'});
lat = t0.Lat;
lon = t0.Lon;
u = t0.u_vel;
v = t0.v_vel;
uv_plot.UData = 'u';
uv_plot.VData = 'v';
uv_plot.XData = 'lat';
uv_plot.YData = 'lon';
drawnow()
xlabel(['Forecast Data for ',tstamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
% clf;
frame = getframe(99);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end

Answers (2)

Walter Roberson
Walter Roberson on 21 Dec 2015
Instead of refreshdata() in the loop, use
t0 = ds(ds.t == start + hours(i),{'t','Lat','Lon','u_vel','v_vel'});
lat = t0.Lat;
lon = t0.Lon;
u = t0.u_vel;
v = t0.v_vel;
uv_plot.UData = U;
uv_plot.VData = v;
uv_plot.XData = lat;
uv_plot.YData = lon;
and the drawnow() will do the update.
refreshdata() is slower than setting the parameters to their desired values.
You also do not need the
xlabel([], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
as you change the xlabel again a few lines later.
  11 Comments
Jon Rendulic
Jon Rendulic on 10 Jan 2016
I made the change and it still does not draw the vectors.
Keep in mind that this code originally worked hence the animated gif. It runs fine in a loop but is extremely slow.
The reason I made this post and decided to try using drawnow or refreshdata was to make my code more efficient. I was looking for a smarter way to solve this problem.
Jon Rendulic
Jon Rendulic on 10 Jan 2016
This is the code that I have that works but is extremely slow.
get_vel_data
global start_stamp
global tstamp
t = datenum(DateTime, 'yyyy-mm-ddTHH:MM:SSZ');
t = datetime(t,'ConvertFrom','datenum','Format', 'yyyy-MM-dd HH:mm:ss');
% start = '2015-11-14 00:00:00';
start = datetime('2015-12-01 00:00:00', 'Format', 'yyyy-MM-dd HH:mm:ss');
start_stamp = datestr(start);
ds = dataset(t,Lat,Lon,u_vel,v_vel);
%%Animated Plot
figure(99)
filename = 'testnew99.gif';
% set map and grid
% looping through the 64 different time frames. 24hrs/(3hr fcasts)*8days=64
for n = 1:64
if n==1;
i=0;
else
i=i+3;
end
% update timestamp for axis label
tstamp = start + hours(i);
tstamp = datestr(tstamp);
% update temp dataset
t0 = ds(ds.t == start + hours(i),{'t','Lat','Lon','u_vel','v_vel'});
lat0 = t0.Lat; lon0 = t0.Lon;
u0 = t0.u_vel; v0 = t0.v_vel;
clf;
v_plot(lat0,lon0,u0,v0)
% pause(.05);
drawnow
frame = getframe(99);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
The code for v_plot is:
function v_plot(lat0, lon0, u0, v0)
global start_stamp
global tstamp
axes('Position',[0.1,0.1,0.8,0.8]);
m_proj('albers equal-area','lat',[55 60],'lon',[200 210]);
m_gshhs_h('patch',[.6 .6 .6]);
m_grid('linest','none','linewidth',2,'tickdir','out','xaxisloc','top','yaxisloc','right');
tiHan = title(['U and V Ocean Current Velocity Forecast Data Downloaded on ',start_stamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
tiPos = get (tiHan, 'position'); % axis x, y value of title position
xyrange = axis;
set(tiHan, 'position', tiPos + [0 0.05 * (xyrange(4) - xyrange(3)) 0]); % move title up
set(tiHan, 'units', 'inches'); % so Title doesn't move on zoom.
xlabel(['Forecast Data for ',tstamp], 'Interpreter', 'none','FontWeight','bold','FontSize',8);
hold on;
m_quiver(lon0,lat0,u0,v0);

Sign in to comment.


Kyon
Kyon on 12 Feb 2017
i want to do the map in 3d

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!