Index: /issm/trunk/src/m/classes/public/plugdata.m
===================================================================
--- /issm/trunk/src/m/classes/public/plugdata.m	(revision 278)
+++ /issm/trunk/src/m/classes/public/plugdata.m	(revision 278)
@@ -0,0 +1,227 @@
+function md=plugdata(md,field,filename)
+%PLUGDATA - load data on a model
+%
+%   load a matlab file (extension .mat) which holds 3 or 4 variables
+%   and interpolate the data on the mesh and plug it onto the model.
+%
+%   o 3 variables
+%     - a vector x (if the name of the variable do not begin with "x", an error can appear)
+%     - a vector y (if the name of the variable do not begin with "y", an error can appear)
+%     - a vector or matrix data (if the name of the variable do not begin with the field name, an error can appear)
+%   o 4 variables
+%     - a vector x (if the name of the variable do not begin with "x", an error can appear)
+%     - a vector y (if the name of the variable do not begin with "y", an error can appear)
+%     - a matrix with 3 columns (if the name of the variable do not begin with "index" or "elements", an error can appear)
+%     - a vector data (if the name of the variable do not begin with the field name, an error can appear)
+%
+%   Usage:
+%      md=plugdata(md,field,filename)
+%
+%   Example:
+%      md=plugdata(md,'surface','surfacefile.mat');
+%
+%   See also: PLUGVELOCITIES, GRIDDATA, GRIDDATA_MESH_TO_MESH, DATAINTERP
+
+%some checks
+if nargin~=3 | nargout~=1
+	help plugdata
+	error('plugdata error message: bad usage');
+end
+if ~ismember(field,fieldnames(md))
+	error(['plugdata error message: ' field ' is not a field of the model']);
+end
+if ~exist(filename)
+	error(['plugdata error message: file ' filename  ' does not exist']);
+end
+
+%Get variables
+A=whos('-file',filename);
+
+%check the number of variables
+if (length(A)~=4 & length(A)~=3)
+	error(['plugdata error message: file ' filename  ' not supported yet (it should hold 3 or 4 variables)']);
+end
+
+%1: find data using their names
+xenum=NaN; yenum=NaN; indexenum=NaN; dataenum=NaN;
+for i=1:length(A),
+
+	%x -> name begins with "x" and is a vector
+	if (strcmpi(A(i).name(1),'x') & min(A(i).size)==1),
+		xenum=i;
+
+	%y -> name begins with "y" and is a vector
+	elseif (strcmpi(A(i).name(1),'y') & min(A(i).size)==1),
+		yenum=i;
+
+	%index -> name begins with "index" or "elements" and 3 columns
+	elseif length(A)==4
+		if (( (length(A(i).name)>=5 & strcmpi(A(i).name(1:5),'index')) ...
+				| (length(A(i).name)>=8 & strcmpi(A(i).name(1:8),'elements')) )...
+				& A(i).size(2)==3),
+			indexenum=i;
+		end
+
+	%data -> name begins with "field" and is a vector or a matrix
+	elseif (strcmpi(A(i).name,field) & max(A(i).size)>1),
+		dataenum=i;
+	end
+end
+
+%2: if only one item is missing, find it by elimination
+if length(A)==4
+	pos=find(isnan([xenum yenum indexenum dataenum]));
+	if length(pos)==1,
+		list=[xenum yenum indexenum dataenum]; list(pos)=[];
+		if pos==1,
+			xenum=setdiff(1:4,list);
+		elseif pos==2,
+			yenum=setdiff(1:4,list);
+		elseif pos==3,
+			indexenum=setdiff(1:4,list);
+		elseif pos==4,
+			dataenum=setdiff(1:4,list);
+		end
+	end
+else
+	pos=find(isnan([xenum yenum dataenum]));
+	if length(pos)==1,
+		list=[xenum yenum indexenum dataenum]; list(pos)=[];
+		if pos==1,
+			xenum=setdiff(1:3,list);
+		elseif pos==2,
+			yenum=setdiff(1:3,list);
+		elseif pos==3,
+			dataenum=setdiff(1:3,list);
+		end
+	end
+end
+
+%3: if one or several variables are missing, use sizes instaed of names (works only if data is a matrix...)
+if (isnan(xenum) | isnan(yenum) |  isnan(dataenum) | (length(A)==4 & isnan(indexenum)))
+
+	if (length(A)==4 & isnan(indexenum)),
+		for i=1:4
+			if A(i).size(2)==3,
+				indexenum=i;
+			end
+		end
+		if isnan(indexenum)
+			error(['plugdata error message: file ' filename  ' not supported yet (index not found)']);
+		end
+	end
+
+	if isnan(dataenum),
+		maxsize1=1;
+		maxsize2=1;
+
+		%find dataenum
+		for i=1:length(A)
+			sizei=A(i).size;
+			if (sizei(1)>=maxsize1-1 & sizei(2)>=maxsize2-1),
+				dataenum=i
+				maxsize1=sizei(1);
+				maxsize2=sizei(2);
+			end
+		end
+		if isnan(dataenum)
+			error(['plugdata error message: file ' filename  ' not supported yet (' field ' not found)']);
+		end
+		if (A(dataenum).size(1)==A(dataenum).size(2) & isnan(xenum) & isnan(yenum)),
+			error(['plugdata error message: file ' filename  ' not supported (' field ' is a square matrix, save x and y with another name)']);
+		end
+	end
+
+	%find xenum and yenum
+	if isnan(xenum) | isnan(yenum)
+		for i=1:length(A),
+			lengthi=max(A(i).size);
+			if isnan(yenum) & ((i~=dataenum) & (lengthi==A(dataenum).size(1) | lengthi==A(dataenum).size(1)+1)),
+				yenum=i;
+			elseif isnan(xenum) & ((i~=dataenum) & (lengthi==A(dataenum).size(2) | lengthi==A(dataenum).size(2)+1)),
+				xenum=i;
+			end
+		end
+	end
+
+	%last chance: if only one item is missing, find it by elimination
+	if length(A)==4
+		pos=find(isnan([xenum yenum indexenum dataenum]));
+		if length(pos)==1,
+			list=[xenum yenum indexenum dataenum]; list(pos)=[];
+			if pos==1,
+				xenum=setdiff(1:4,list);
+			elseif pos==2,
+				yenum=setdiff(1:4,list);
+			elseif pos==3,
+				indexenum=setdiff(1:4,list);
+			elseif pos==4,
+				dataenum=setdiff(1:4,list);
+			end
+		end
+	else
+		pos=find(isnan([xenum yenum dataenum]));
+		if length(pos)==1,
+			list=[xenum yenum indexenum dataenum]; list(pos)=[];
+			if pos==1,
+				xenum=setdiff(1:3,list);
+			elseif pos==2,
+				yenum=setdiff(1:3,list);
+			elseif pos==3,
+				dataenum=setdiff(1:3,list);
+			end
+		end
+	end
+
+	%last check
+	if (isnan(xenum) | isnan(yenum) | isnan(dataenum) | (length(A)==4 & isnan(indexenum)))
+		error(['plugdata error message: file ' filename  ' not supported yet (not recognzed variable names or sizes)']);
+	end
+end
+
+%OK, interpolate
+if length(A)==4,
+
+	%create names:
+	xname=A(xenum).name;
+	yname=A(yenum).name;
+	indexname=A(indexenum).name;
+	dataname=A(dataenum).name;
+
+	%load data
+	B=load(filename);
+
+	%get x y and data
+	eval(['x=B.' xname ';'])
+	eval(['y=B.' yname ';'])
+	eval(['index=B.' indexname ';'])
+	eval(['data=B.' dataname ';'])
+
+	%interpolate
+	if md.debug, disp('   Interpolation using griddata_mesh_to_mesh (x, y and data are vectors, index has 3 columns)'); end
+	eval(['md.' field '=griddata_mesh_to_mesh(index,x,y,data,md.x,md.y);'])
+
+else
+
+	%create names:
+	xname=A(xenum).name;
+	yname=A(yenum).name;
+	dataname=A(dataenum).name;
+
+	%load data
+	B=load(filename);
+
+	%get x y and data
+	eval(['x=B.' xname ';'])
+	eval(['y=B.' yname ';'])
+	eval(['data=B.' dataname ';'])
+
+	%interpolate
+	if length(x)==size(data,2)+1
+		if md.debug, disp('   Interpolation using DataInterp (x and y are vectors, data a matrix)'); end
+		eval(['md.' field '=DataInterp(x,y,data,md.x,md.y);'])
+	else
+		if md.debug, disp('   Interpolation using griddata (x, y and data are vectors)'); end
+		eval(['md.' field '=griddata(x,y,data,md.x,md.y);'])
+	end
+end
Index: /issm/trunk/src/m/classes/public/plugvelocities.m
===================================================================
--- /issm/trunk/src/m/classes/public/plugvelocities.m	(revision 278)
+++ /issm/trunk/src/m/classes/public/plugvelocities.m	(revision 278)
@@ -0,0 +1,107 @@
+function md=plugvelocities(md,filename)
+%PLUGVELOCITIES - load velocities on a model
+%
+%   load a matlab file (extension .mat) which holds 4 variables
+%   x,y,vx,vy to be plugged onto the model (or similar names)
+%   x and y must be vectors, vx, vy matrices
+%
+%   Usage:
+%      md=plugvelocities(md,filename)
+%
+%   Example:
+%      md=plugvelocities(md,'velocityfile.mat');
+%
+%   See also: PLUGDATA, GRIDDATA, GRIDDATA_MESH_TO_MESH, DATAINTERP
+
+%some checks
+if nargin~=2 | nargout~=1
+	help plugvelocities
+	error('plugvelocities error message: bad usage');
+end
+if ~exist(filename)
+	error(['plugvelocities error message: file ' filename  ' does not exist']);
+end
+
+%Get variables
+A=whos('-file',filename);
+
+%find x,y,vx and vy
+xenum=NaN; yenum=NaN; vxenum=NaN; vyenum=NaN;
+if length(A)==4,
+	for i=1:4
+		if strcmpi(A(i).name(1),'x');
+			xenum=i;
+		elseif strcmpi(A(i).name(1),'y');
+			yenum=i;
+		else
+			if strcmpi(A(i).name(end),'x');
+				vxenum=i;
+			elseif strcmpi(A(i).name(end),'y');
+				vyenum=i;
+			end
+		end
+	end
+else
+	error(['plugvelocities error message: file ' filename  ' not supported yet (it should hold 4 variables x,y,vx and vy)']);
+end
+
+%assum that we have found at least vxenum and vyenum
+if ( isnan(vxenum) | isnan(vyenum))
+	error(['plugvelocities error message: file ' filename  ' not supported yet (it should hold 4 variables x,y,vx and vy)']);
+end
+
+%find x y
+if (isnan(xenum) | isnan(yenum))
+
+	%check the size
+	if A(vxenum).size(1)==A(vxenum).size(2),
+		error(['plugvelocities error message: file ' filename  ' not supported (velocities is a square matrix, save x and y with another name)']);
+	end
+	if ~(A(vxenum).size(1)==A(vyenum).size(1) & A(vxenum).size(2)==A(vyenum).size(2)),
+		error(['plugvelocities error message: file ' filename  ' not supported (vx and vy matrices do not have the same size)']);
+	end
+
+	%find xenum and yenum
+	for i=1:4
+		lengthi=max(A(i).size);
+		if ((i~=vxenum) & (lengthi==A(vxenum).size(1) | lengthi==A(vxenum).size(1)+1)),
+			yenum=i;
+		elseif ((i~=vxenum) & (lengthi==A(vxenum).size(2) | lengthi==A(vxenum).size(2)+1)),
+			xenum=i;
+		end
+	end
+
+	%last check
+	if (isnan(xenum) | isnan(yenum))
+		error(['plugdata error message: file ' filename  ' not supported yet']);
+	end
+end
+
+%create names:
+xname=A(xenum).name;
+yname=A(yenum).name;
+vxname=A(vxenum).name;
+vyname=A(vyenum).name;
+
+%load data
+B=load(filename);
+
+%get x y vx and vy
+eval(['x=B.' xname ';'])
+eval(['y=B.' yname ';'])
+eval(['vx=B.' vxname ';'])
+eval(['vy=B.' vyname ';'])
+
+%interpolate
+if length(x)==size(vx,2)
+	md.vx_obs=griddata(x,y,vx,md.x,md.y);
+	md.vy_obs=griddata(x,y,vy,md.x,md.y);
+
+else
+	md.vx_obs=DataInterp(x,y,vx,md.x,md.y);
+	md.vy_obs=DataInterp(x,y,vy,md.x,md.y);
+end
+md.vx=md.vx_obs;
+md.vy=md.vy_obs;
+md.vel_obs=sqrt(md.vx_obs.^2+md.vy_obs.^2);
+md.vel=md.vel_obs;
