function applyoptions(md,data,options)
%APPLYOPTIONS - apply the options to current plot
%
%   Usage:
%      applyoptions(md,data,options)
%
%   See also: PLOTMODEL, PARSE_OPTIONS

%some defaults
if isfield(md.mesh,'hemisphere'),
	if strcmpi(md.mesh.hemisphere,'n'), options=addfielddefault(options,'hemisphere','n'); 
	elseif strcmpi(md.mesh.hemisphere,'s'), options=addfielddefault(options,'hemisphere','s'); 
	end
end

%fontsize
fontsize=getfieldvalue(options,'fontsize',14);

%fontweight
fontweight=getfieldvalue(options,'fontweight','normal');

%title
if exist(options,'title')
	titlevalue=getfieldvalue(options,'title');
	if iscell(titlevalue),
		title(titlevalue,'FontSize',fontsize,'FontWeight',fontweight);
	else
		if ~isnan(titlevalue),
			title(titlevalue,'FontSize',fontsize,'FontWeight',fontweight);
		end
	end
end

%xlabel, ylabel and zlabel
if exist(options,'xlabel');
	xlabel(getfieldvalue(options,'xlabel'),'FontSize',fontsize,'FontWeight',fontweight);
end
if exist(options,'ylabel');
	ylabel(getfieldvalue(options,'ylabel'),'FontSize',fontsize,'FontWeight',fontweight);
end
if exist(options,'zlabel');
	zlabel(getfieldvalue(options,'zlabel'),'FontSize',fontsize,'FontWeight',fontweight);
end

%xticks, yticks and zticks
if exist(options,'xtick'), set(gca,'XTick',getfieldvalue(options,'xtick')); end
if exist(options,'ytick'), set(gca,'YTick',getfieldvalue(options,'ytick')); end
if exist(options,'ztick'), set(gca,'ZTick',getfieldvalue(options,'ztick')); end

%view 
if dimension(md.mesh)==3 & ~exist(options,'layer'),
	view(getfieldvalue(options,'view',3));
else
	view(getfieldvalue(options,'view',2));
end

%axis
set(gca,'FontSize',getfieldvalue(options,'axisfontsize',fontsize));;
if exist(options,'axis')
	eval(['axis ' getfieldvalue(options,'axis')]);
else
	if strcmp(domaintype(md.mesh),'3D'),
		if ~exist(options,'layer'),
			axis auto tight
		else
			axis tight equal
		end
	elseif strcmp(domaintype(md.mesh),'2Dvertical'),
		axis auto tight
	elseif strcmp(domaintype(md.mesh),'2Dhorizontal'),
		axis tight equal;
	else
		error('type of domain not supported');
	end
end

%box
if exist(options,'box')
	eval(['box ' getfieldvalue(options,'box')]);
end

%xlim, ylim and zlim
if exist(options,'xlim');
	xlim(getfieldvalue(options,'xlim'));
end
if exist(options,'ylim');
	ylim(getfieldvalue(options,'ylim'));
end
if exist(options,'zlim');
	zlim(getfieldvalue(options,'zlim'));
end

%latlon
%Must be done here (before xlim and ylim??) so that it uses the same xlim and ylim as plot_overlay
%these are changed by axis that follows
if ~strcmpi(getfieldvalue(options,'latlon','off'),'off')
	latlonoverlay(md,options);
end

%Basinzoom
if exist(options,'basin');
	basinzoom(options);
end

%ShowBasins
if strcmpi(getfieldvalue(options,'showbasins','off'),'on')
	showbasins(options);
end

%Caxis
if exist(options,'caxis'),
	caxis(getfieldvalue(options,'caxis'));
end

%shading
if exist(options,'shading'),
	shading(getfieldvalue(options,'shading'));
end

%grid
if exist(options,'grid'),
	if strcmpi(getfieldvalue(options,'grid'),'on'),
		grid on;
	end
end

%colormap
c = getcolormap(options);
h = colormap(c);

%wrapping
if exist(options,'wrapping'),
	if ~exist(options,'colormap'),
		h=jet;
	end
	colormap(repmat(h,getfieldvalue(options,'wrapping',1),1));
end

%colorbar
if getfieldvalue(options,'colorbar',1)==1,
	if exist(options,'colorbarcornerposition'),
		c=colorbar(getfieldvalue(options,'colorbarcornerposition'),'peer',gca);
	else 
		c=colorbar('peer',gca);
	end
	set(c,'FontSize',getfieldvalue(options,'colorbarfontsize',fontsize),'YColor',getfieldvalue(options,'FontColor','k'));
	if exist(options,'wrapping')
		lim=get(c,'Ylim');
		lim=[lim(1) lim(1)+(lim(2)-lim(1))/getfieldvalue(options,'wrapping')];
		set(c,'Ylim',lim);
	end
	if exist(options,'colorbarpos'),
		set(c,'Position',getfieldvalue(options,'colorbarpos'));
	end
	if exist(options,'log'),
		nlab=length(get(c,'YTick'));
		logvalue=getfieldvalue(options,'log');

		scaleminmax=caxis;
		Min=min(scaleminmax);
		Max=max(scaleminmax);
		set(c,'YLim',[Min Max]); % set colorbar limits
		set(c,'YTick',linspace(Min,Max,nlab));     % set tick mark locations

		labels = cell(1,nlab);
		tick_vals = linspace(Min,Max,nlab);
		tick_vals = exp(log(logvalue)*tick_vals);
		warning off MATLAB:log:logOfZero;
		for i = 1:nlab
			labels{i} = sprintf('%-3.4g',round_ice(tick_vals(i),2));
			%labels{i} = sprintf('%-.4g',round_ice(tick_vals(i),2));
		end
		warning on MATLAB:log:logOfZero;
		set(c,'YTickLabel',labels);
	end 
 	if exist(options,'cbYLim'); 
		set(c,'YLim',getfieldvalue(options,'cbYLim'));
	end
	if exist(options,'colorbartitle'),
		set(get(c,'title'),'FontSize',getfieldvalue(options,'colorbarfontsize',fontsize),'String',getfieldvalue(options,'colorbartitle'),...
			'Color',getfieldvalue(options,'FontColor','k'));
	end
	if exist(options,'colorbarYLabel'),
		set(get(c,'Ylabel'),'FontSize',getfieldvalue(options,'colorbarfontsize',fontsize),'String',getfieldvalue(options,'colorbarYLabel'),...
			'Color',getfieldvalue(options,'FontColor','k'));
	end
	if exist(options,'colorbarwidth'),
		posaxes=get(gca,'Position');
		alpha=getfieldvalue(options,'colorbarwidth',1);
		position=get(c,'Position');
		dx=position(3);
		newdx=dx*alpha;
		position(1)=position(1)+(dx-newdx)/2;
		position(3)=newdx;
		set(c,'Position',position);
		set(gca,'Position',posaxes);
	end
	if exist(options,'colorbarheight'),
		posaxes=get(gca,'Position');
		alpha=getfieldvalue(options,'colorbarheight',1);
		position=get(c,'Position');
		dy=position(4);
		newdy=dy*alpha;
		position(2)=position(2)+(dy-newdy)/2;
		position(4)=newdy;
		set(c,'Position',position);
		set(gca,'Position',posaxes);
	end
	if exist(options,'cbYTickLabel');
		tick_vals=getfieldvalue(options,'cbYTickLabel');
		if ~isnumeric(tick_vals) & strcmp(tick_vals,'on')
			tick_vals=get(c,'YTick')';
			if exist(options,'log')
				logval= getfieldvalue(options,'log');
				for i= 1:numel(tick_vals)
					tick_vals(i)= logval^(tick_vals(i));
				end
			elseif numel(tick_vals) == 3
				tick_vals=[tick_vals(1); mean(tick_vals(1:2)); tick_vals(2); ...
					mean(tick_vals(2:3)); tick_vals(3)];
				set(c,'YTick',tick_vals);
			end
		else
			if exist(options,'log')
				logvalue=getfieldvalue(options,'log');
				set(c,'YTick',log(tick_vals)./log(logvalue));
			else
				set(c,'YTick',tick_vals);
			end
		end
		labels = cell(1,numel(tick_vals));
		for i = 1:numel(tick_vals)
			labels{i} = num2str(tick_vals(i));
		end
		set(c,'YTickLabel',labels);
	end

elseif getfieldvalue(options,'colorbar',1)==0,
	colorbar('off');
else
	%do nothing

end

%area
if exist(options,'area'),
	antzoom(getfieldvalue(options,'area'));
end

%expdisp
if exist(options,'expdisp'),
	filename=(getfieldvalue(options,'expdisp'));
	style=(getfieldvalue(options,'expstyle'));
	linewidth=(getfieldvalue(options,'linewidth',1));
	for i=1:length(getfieldvalue(options,'expdisp')),
		filenamei=filename{i};
		stylei=style{i};
		if length(linewidth)==1,
			linewidthi=linewidth;
		else
			linewidthi=linewidth{i};
		end
		expdisp(filenamei,gcf,stylei,linewidthi,getfieldvalue(options,'unit',1));
	end
end

%text (default value is empty, not NaN...)
if exist(options,'text');
	textstring=getfieldvalue(options,'text');
	textweight=getfieldvalue(options,'textweight','b');
	textsize=getfieldvalue(options,'textsize');
	textcolor=getfieldvalue(options,'textcolor');
	textposition=getfieldvalue(options,'textposition');
	textrotation=getfieldvalue(options,'textrotation');
	for i=1:length(getfieldvalue(options,'text'));
		textstringi=textstring{i};
		textweighti=textweight{i};
		textsizei=textsize{i};
		textcolori=textcolor{i};
		textpositioni=textposition{i};
		textrotationi=textrotation{i};
		h=text(textpositioni(1),textpositioni(2),10,textstringi,'FontSize',textsizei,'FontWeight',textweighti,'Color',textcolori,'Rotation',textrotationi);
		set(h,'Clipping','on'); %prevent text from appearing outside of the box
	end
end

%north arrow
if exist(options,'northarrow'),
	northarrow(getfieldvalue(options,'northarrow'));
end

%curved arrow
if exist(options,'curvedarrow'),
	curvedoptions=getfieldvalue(options,'curvedarrow');
	curvedarrow(curvedoptions{:});
end

%Scale ruler
if exist(options,'scaleruler'),
	scaleruler(options);
end

%streamliness
if exist(options,'streamlines'),
	plot_streamlines(md,options);
end

%contours
if exist(options,'contourlevels'),
	plot_contour(md,data,options);
end

%YTickLabel
if exist(options,'yticklabel'),
	set(gca,'YTickLabel',getfieldvalue(options,'YTickLabel'));
end

%XTickLabel
if exist(options,'xticklabel'),
	set(gca,'XTickLabel',getfieldvalue(options,'XTickLabel'));
end

%xtick
if exist(options,'xtick'),
	set(gca,'xtick',getfieldvalue(options,'xtick'));
end

%ytick
if exist(options,'ytick'),
	set(gca,'ytick',getfieldvalue(options,'ytick'));
end

%Axis positions
if exist(options,'offsetaxispos'),
	offset=getfieldvalue(options,'offsetaxispos');
	P=get(gca,'pos');
	P(1)=P(1)+offset(1);
	P(2)=P(2)+offset(2);
	P(3)=P(3)+offset(3);
	P(3)=P(4)+offset(4);
	set(gca,'pos',P);
end
if exist(options,'axispos'),
	Axis=getfieldvalue(options,'axispos');
	hold on
	set(gca,'pos',Axis);
end

%axes position
if exist(options,'axesPosition')
	set(gca,'Position',getfieldvalue(options,'axesPosition'));
end

%showregion
if strcmpi(getfieldvalue(options,'showregion','off'),'on'),
	%Keep pointer of main axis
	maingca=gca;
	%get inset relative position (x,y,width,height)
	insetpos=getfieldvalue(options,'insetpos',[0.02 0.70 0.18 0.18]);
	%get current plos position
	cplotpos=get(maingca,'pos');
	%compute inset position
	PosInset=[cplotpos(1)+insetpos(1)*cplotpos(3),cplotpos(2)+insetpos(2)*cplotpos(4), insetpos(3)*cplotpos(3), insetpos(4)*cplotpos(4)];
	axes('pos',PosInset);
	axis equal off
	%box off
	if strcmpi(md.mesh.hemisphere,'n') | strcmpi(md.mesh.hemisphere,'north'),
		A=expread('/u/astrid-r1b/ModelData/Exp/GreenlandBoxFront.exp');
		[A.x A.y]=ll2xy(A.x,A.y,+1,45,70);
		A.x = A.x(1:30:end);
		A.y = A.y(1:30:end);
	elseif strcmpi(md.mesh.hemisphere,'s') | strcmpi(md.mesh.hemisphere,'south'),
		A=expread('/u/astrid-r1b/ModelData/Exp/Antarctica.exp');
	else
		error('applyoptions error message: hemisphere not defined');
	end
	offset=3*10^4;
	Ax=[min(A.x)-offset max(A.x)+offset];
	Ay=[min(A.y)-offset max(A.y)+offset];
	%if we are zooming on a basin, don't take the mesh for the boundaries!
	if exist(options,'basin'),
		[mdx mdy]=basinzoom(options);
	else
		mdx=[min(md.mesh.x)-offset max(md.mesh.x)+offset];
		mdy=[min(md.mesh.y)-offset max(md.mesh.y)+offset];
	end
	line(A.x,A.y,ones(size(A.x)),'color','b');
	patch([Ax(1)  Ax(2)  Ax(2)  Ax(1) Ax(1)],[Ay(1)  Ay(1)  Ay(2)  Ay(2) Ay(1)],[1 1 1],'EdgeColor',[0 0 0],'LineWidth',1,'FaceLighting','none')
	patch([mdx(1) mdx(2) mdx(2) mdx(1)],[mdy(1) mdy(1) mdy(2) mdy(2)],ones(4,1),'EdgeColor',[0 0 0],'FaceColor','r','FaceAlpha',0.5)
	colorbar('off');
	%back to main gca
	set(gcf,'CurrentAxes',maingca)
end

%flag edges of a partition
if exist(options,'partitionedges')
	[xsegments ysegments]=flagedges(md.mesh.elements,md.mesh.x,md.mesh.y,md.qmu.partition);
	xsegments=xsegments*getfieldvalue(options,'unit',1);
	ysegments=ysegments*getfieldvalue(options,'unit',1);
	color=getfieldvalue(options,'partitionedgescolor','r-');
	linewidth=getfieldvalue(options,'linewidth',1);
	hold on;
	for i=1:length(xsegments),
		plot(xsegments(i,:),ysegments(i,:),color,'LineWidth',linewidth);
	end
end

%Scatter
if exist(options,'scatter')
	data=getfieldvalue(options,'scatter');
	hold on
	plot_scatter(data(:,1),data(:,2),data(:,3),options);
end

%backgroundcolor
set(gca,'color',getfieldvalue(options,'backgroundcolor','none'));

%lighting
if strcmpi(getfieldvalue(options,'light','off'),'on'),
	set(gca,'FaceLighting','gouraud','FaceColor','interp','AmbientStrength',0.5);
	light('Position',[0 0.1 0.1],'Style','infinite');
end

%cloud of points: 
if exist(options,'cloud'),
	field=getfieldvalue(options,'cloud');
	x=field(:,1);
	y=field(:,2);
	%unit multiplier:
	if exist(options,'unit'),
		unit=getfieldvalue(options,'unit');
		x=x*unit;
		y=y*unit;
	end
	hold on,p=plot(x,y,'k.');
	markersize=getfieldvalue(options,'markersize',5);
	color=getfieldvalue(options,'cloudcolor','k');
	set(p,'Color',color);
	set(p,'MarkerSize',markersize);
end

%========================%
%OK VERY LAST STEP: INSET|
%========================%
if exist(options,'inset'),

	%Keep pointer of main axis
	maingca=gca;
	%get inset relative position (x,y,width,height)
	insetpos=getfieldvalue(options,'insetpos',[0.56 0.55 0.35 0.35]);
	%get current plot position
	cplotpos=get(gca,'pos');

	X1=getfieldvalue(options,'insetx',xlim);
	Y1=getfieldvalue(options,'insety',ylim);

	for i=1:length(getfieldvalue(options,'insetx')),
		if length(insetpos)==4,
			insetposi=insetpos;
		else
			insetposi=insetpos{i};
		end
		PosInseti=[cplotpos(1)+insetposi(1)*cplotpos(3),cplotpos(2)+insetposi(2)*cplotpos(4), insetposi(3)*cplotpos(3), insetposi(4)*cplotpos(4)];
		%show pos
		if iscell(X1),
			X1i=X1{i};
		else
			X1i=X1;
		end
		if iscell(Y1),
			Y1i=Y1{i};
		else
			Y1i=Y1;
		end
		if strcmpi(getfieldvalue(options,'showinset','off'),'on')
			line(X1i([1 2 2 1 1]),Y1i([1 1 2 2 1]),zeros(1,5),'Color','k','LineWidth',2);
		end

		%Get current figure
		ax1=gca;

		%plot inset
		axes('pos',PosInseti);
		copyobj(get(ax1,'children'),gca);
		patch('Faces',[1 2 3 4 1],'Vertices',[X1i([1 2 2 1])' Y1i([1 1 2 2])'],'FaceColor','None','EdgeColor','k','LineWidth',2);

		%apply options
		options=removefield(options,'text',0);
		options=removefield(options,'title',0);
		options=removefield(options,'xlabel',0);
		options=removefield(options,'ylabel',0);
		options=removefield(options,'inset',0);
		options=removefield(options,'offsetaxispos',0);
		options=removefield(options,'showregion',0);
		options=changefieldvalue(options,'colorbar',0);
		options=changefieldvalue(options,'latlon','off');
		options=changefieldvalue(options,'axis','equal off');
		options=changefieldvalue(options,'xlim',X1i);
		options=changefieldvalue(options,'ylim',Y1i);
		applyoptions(md,data,options);

		%back to main gca
		set(gcf,'CurrentAxes',maingca)
	end
end
