Commit baf0e704 authored by Rory O'Hara Murray's avatar Rory O'Hara Murray

Changes to plot_fvcom_field, read_netcdf_vars and read_netcdf_vars_xy

parent 380b55b7
......@@ -3,21 +3,30 @@
% output files. This function runs an animation if the field includes more
% than one time steps.
%
% plot_fvcom_field(Mobj, PlotField, 'fid', figure_id, 'cli', colour_lims, 'gif',
% filename, 'axi', axis_range, 'pll', 'grd', colour);
%
% INPUT
% Mobj = matlab mesh object
% PlotField = vertex-based field to plot
% [optional] fid = the fid of the figure to plot the field in
% [optional] cli = the colour limits to use
% [optional] fig = make an animated gif - specify filename
% [optional] axi = the axis
% [optional] fid = the fid of the figure to plot the field in - specify figure id
% [optional] cli = the colour limits to use - specify the limits
% [optional] gif = make an animated gif - specify filename
% [optional] axi = the axis - specify axis range
% [optional] pll = the axis
% [optional] grd = add gridlines - specify colour
%
% EXAMPLE USAGE
% plot_fvcom_field(Mobj,Mobj.zeta,'fid', 1, 'clims', [0 100], fig, 'animation.gif', 'axis', [60000 70000 40000 50000])
% plot_fvcom_field(Mobj, Mobj.zeta, 'fid', 1, 'cli', [0 100], 'gif', 'animation.gif', 'axi', [60000 70000 40000 50000])
%
% Author(s)
% Rory O'Hara Murray (Marine Scotland Science)
%
function plot_fvcom_field(M, plot_field, varargin)
% Developments:
% 2014-05-22: Changed the way fig id is checked, not using 'exist' anymore.
% 2014-08-15: Added the axis command in
%
function [a] = plot_fvcom_field(M, plot_field, varargin)
MJD_datenum = datenum('1858-11-17 00:00:00');
% check to see if nv or tri should be used.
......@@ -34,20 +43,17 @@ else
time_flag = false;
end
xE = M.x(nv)';
yE = M.y(nv)';
plot_field = squeeze(plot_field);
if size(plot_field,1)==size(nv,1) % plot on elements
patch_func = @(dummy) patch(xE, yE, dummy', 'linestyle', 'none');
elseif size(plot_field,1)==size(M.x,1) % plot on nodes
patch_func = @(dummy) patch('Vertices',[M.x, M.y], 'Faces',nv, 'Cdata',dummy,'linestyle','none','facecolor','interp');
end
% defaults
clims = [min(plot_field(:)) max(plot_field(:))];
if clims(1)==clims(2) clims(1)=clims(1)-0.1; clims(2)=clims(2)+0.1; end
gif = false;
grd = false;
plot_ll = false;
fig_flag = false;
axis_flag = false;
title_flag = false;
legend_text_flag = false;
quiver_flag = false;
for ii=1:1:length(varargin)
keyword = lower(varargin{ii});
......@@ -55,26 +61,79 @@ for ii=1:1:length(varargin)
switch(keyword(1:3))
case 'fid' % id of a figure
fig = varargin{ii+1};
fig_flag = true;
case 'cli' % colour limits
clims = varargin{ii+1};
case 'gif' % make an animated gif
gif = true;
gif_filename = varargin{ii+1}
case 'axi' % axis
axis_flag = true;
axi = varargin{ii+1};
case 'grd' % grid lines
grd = true;
edgecolor = varargin{ii+1};
case 'pll'
plot_ll = true;
case 'tit'
title_flag = true;
fig_title = varargin{ii+1};
case 'leg'
legend_text_flag = true;
legend_text = varargin{ii+1};
case 'qui'
quiver_flag = true;
quiverData = varargin{ii+1};
end
end
if plot_ll
x = M.lon;
y = M.lat;
else
x = M.x;
y = M.y;
end
if not(axis_flag)
axi = [min(x) max(x) min(y) max(y)];
end
xE = x(nv)';
yE = y(nv)';
plot_field = squeeze(plot_field);
if size(plot_field,1)==size(nv,1) % plot on elements
if grd
patch_func = @(dummy) patch(xE, yE, dummy', 'edgecolor', edgecolor);
else
patch_func = @(dummy) patch(xE, yE, dummy', 'linestyle', 'none');
end
elseif size(plot_field,1)==size(x,1) % plot on nodes
if grd
patch_func = @(dummy) patch('Vertices',[x, y], 'Faces',nv, 'Cdata',dummy,'edgecolor', edgecolor,'facecolor','interp');
else
patch_func = @(dummy) patch('Vertices',[x, y], 'Faces',nv, 'Cdata',dummy,'linestyle','none','facecolor','interp');
end
end
if exist('fig')==0
if not(fig_flag)
fig = figure;
end
for ii=1:size(plot_field,2)
if ishandle(fig)==0 break; end
patch_func(plot_field(:,ii));
colorbar
set(gca, 'clim', clims)
if time_flag title(['time = ' datestr(M.time(ii)+MJD_datenum, 'HH:MM dd/mm/yyyy')]); end
a = patch_func(plot_field(:,ii));
c = colorbar;
if legend_text_flag set(get(c, 'ylabel'), 'string', legend_text); end
set(gca, 'clim', clims);
axis(axi)
if title_flag title(fig_title); elseif time_flag title(['time = ' datestr(double(M.time(ii))+MJD_datenum, 'HH:MM dd/mm/yyyy')]); end
if quiver_flag
hold on
quiver(quiverData.X, quiverData.Y, quiverData.U(:,:,ii), quiverData.V(:,:,ii), 'k');
hold off
end
if gif
axis off
......
......@@ -12,6 +12,8 @@ function [M] = read_netcdf_vars(varargin)
% Pass the variable names that you want to extract
% [optional pair] filename, the netCDF filename
% [optional triple] dimrange, the dimension name, the dimension range
% the dimension range si of the form [start end
% stride], where stride is optional (default 1).
%
% EXAMPLE USAGE
% Extract variables time, x, y
......@@ -33,9 +35,12 @@ function [M] = read_netcdf_vars(varargin)
%
% Revision history
% v0 July 2013
% 2014-05-27 dimension ids are now added to attributes (ROM)
% 2014-06-02 added the ability to specify the stride/sample rate
%==========================================================================
dimrange = false;
extract_all_flag = false;
% look for some keywords with some setting after them and remember which
% index of varargin are 'taken' in freeI.
......@@ -52,8 +57,17 @@ for ii=1:1:length(varargin)
dimrange = true;
subsample_num = subsample_num + 1;
subsample_dim(subsample_num) = {varargin{ii+1}};
subsample_ran(:,subsample_num) = varargin{ii+2};
range_tmp = varargin{ii+2};
subsample_ran(1:2,subsample_num) = range_tmp(1:2);
if length(range_tmp)>2
subsample_ran(3,subsample_num) = range_tmp(3);
else
subsample_ran(3,subsample_num) = 1;
end
freeI([ii ii+1 ii+2]) = 0;
case 'all_vars'
freeI([ii]) = 0;
extract_all_flag = true;
end
end
......@@ -73,6 +87,7 @@ ncid = netcdf.open(netcdf_filename, 'NC_NOWRITE');
[ndims,nvars,ngatts,unlimdimid] = netcdf.inq(ncid);
% get a list of the variables avaliable and check the inputs
variable_names_avaliable = {};
for ii=0:nvars-1
variable_names_avaliable{ii+1} = netcdf.inqVar(ncid, ii);
end
......@@ -82,7 +97,9 @@ for ii=1:size(varnames,2);
if size(test1,1)==0 test = [test ii]; end
end
if sum(test)>0
if extract_all_flag
varnames = variable_names_avaliable;
elseif sum(test)>0
disp([varnames(test) ' could not be found']);
disp(['variables avaliable are: ' variable_names_avaliable]);
M = 0; netcdf.close(ncid);
......@@ -109,7 +126,8 @@ if dimrange
end
end
for ii=1:size(varnames,2) % loop through all the variables to extract
% Loop through all the variables to extract
for ii=1:size(varnames,2)
varid(ii) = netcdf.inqVarID(ncid,varnames{ii});
% Get the attributes of the variables listed
......@@ -129,6 +147,9 @@ for ii=1:size(varnames,2) % loop through all the variables to extract
% get info about the variable in question
[varname xtype dimids atts] = netcdf.inqVar(ncid,varid(ii));
% add dimids to the attributes
eval(['M.' varnames{ii} '_att.dimids = dimids;']);
% Take a subset if variable is dependent on the subsample variable
if dimrange
......@@ -149,16 +170,23 @@ for ii=1:size(varnames,2) % loop through all the variables to extract
% get all the dimension lengths and make a starts one (zeros)
dim_range(2,:) = dimsize(dimids+1); % the sizes
dim_range(1,:) = zeros(1, size(dim_range,2)); % the start positions, i.e. zeros
dim_range(3,:) = ones(1, size(dim_range,2)); % default stride of 1
% redefine the dim_starts and dim array
for jj=1:length(I)
if I(jj) dim_range(:,jj) = [subsample_ran(1,I(jj)); subsample_ran(end,I(jj))-subsample_ran(1,I(jj))]; end
if I(jj)
dim_range(:,jj) = [subsample_ran(1,I(jj)); ...
round([subsample_ran(2,I(jj))-subsample_ran(1,I(jj))]./subsample_ran(3,I(jj))); ...
subsample_ran(3,I(jj))];
end
end
M.(varnames{ii}) = double(netcdf.getVar(ncid, varid(ii), dim_range(1,:), dim_range(2,:)));
%M.(varnames{ii}) = double(netcdf.getVar(ncid, varid(ii), dim_range(1,:), dim_range(2,:), dim_range(3,:)));
M.(varnames{ii}) = (netcdf.getVar(ncid, varid(ii), dim_range(1,:), dim_range(2,:), dim_range(3,:)));
else
% Do not subsample if 'I' doens't exist or we are not subsampling
M.(varnames{ii}) = double(netcdf.getVar(ncid, varid(ii)));
%M.(varnames{ii}) = double(netcdf.getVar(ncid, varid(ii)));
M.(varnames{ii}) = (netcdf.getVar(ncid, varid(ii)));
end
end
......
function M = read_netcdf_vars_xy(varargin)
%
% A function to extract netcdf variables for a number of (x, y) locations
% read_netcdf_vars.m is called repeatidly
%
% help read_netcdf_vars for detailed usage info
%
% INPUT
% Pass the variable names that you want to extract
% [optional pair] filename, the netCDF filename
% [optional triple] dimrange, the dimension name, the dimension range
% Author(s)
% Rory O'Hara Murray, Marine Scotland Science
%
% Revision history
% v0 May 2014
%==========================================================================
% defaults
nodeflag = false;
cellflag = false;
% look for some keywords with some setting after them and remember which
% index of varargin are 'taken' in freeI.
freeI = true(1,size(varargin,2));
subsample_num = 0;
for ii=1:1:length(varargin)
keyword = lower(varargin{ii});
vartype = class(keyword);
if vartype(1)~='c' | length(keyword)<10, continue; end
switch(keyword(1:10))
case 'nodevalues'
nodeflag = true;
nodevalues = varargin{ii+1};
freeI([ii ii+1]) = false;
case 'cellvalues'
cellflag = true;
cellvalues = varargin{ii+1};
freeI([ii ii+1]) = false;
end
end
% error checking
if cellflag & nodeflag
if length(cellvalues) ~= length(nodevalues)
error('The number of elements and nodes are not the same. This functions only works if they are the same :-(');
return
end
end
% Call read_netcdf_vars.m with all the same arguments, appart from those
% used here to define xy_nodes and cells
if nodeflag && cellflag
for count=1:length(nodevalues);
nodes=nodevalues(count);
cells=cellvalues(count);
M1 = read_netcdf_vars(varargin{freeI}, 'dimrange', 'node', nodes+[0 1], 'dimrange', 'nele', cells+[0 1]);
if count==1
M = M1;
elseif count>0
% save all the variables
varnames = fieldnames(M1);
for ii=4:2:length(varnames)
dimids = M1.(varnames{ii-1}).dimids;
if size(dimids,1)>0 && dimids(1)<=1;
M.(varnames{ii})(count,:,:) = M1.(varnames{ii});
end
end
end
end
elseif nodeflag
for count=1:length(nodevalues);
nodes=nodevalues(count);
M1 = read_netcdf_vars(varargin{freeI}, 'dimrange', 'node', nodes+[0 1]);
if count==1
M = M1;
elseif count>0
% save all the variables
varnames = fieldnames(M1);
for ii=4:2:length(varnames)
dimids = M1.(varnames{ii-1}).dimids;
if dimids(1)==1;
M.(varnames{ii})(count,:,:) = M1.(varnames{ii});
end
end
end
end
elseif cellflag
for count=1:length(cellvalues);
cells=cellvalues(count);
M1 = read_netcdf_vars(varargin{freeI}, 'dimrange', 'nele', cells+[0 1]);
if count==1
M = M1;
elseif count>0
% save all the variables
varnames = fieldnames(M1);
for ii=4:2:length(varnames)
dimids = M1.(varnames{ii-1}).dimids;
if dimids(1)==0;
M.(varnames{ii})(count,:,:) = M1.(varnames{ii});
end
end
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment