% grads_colormap -- generates a grads 2 colormap from a matlab colormap % % Copyright (C) 2010-2011 Samuel G Trahan % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program. If not, see . function [txt]=grads_colormap(N,cm,cax) % txt=grads_colormap(N,cm,cax) % % Generates a grads 2.0 script that will reproduce a matlab % colormap and color axis limit. % % N -- optional. The number of colors to place in the grads % colormap. If missing, then min(240,size(cm,1)) will be % used. % % cm -- optional. The matlab colormap as an Nx3 array of real % numbers from 0 to 1. If missing, colormap() will be used. % % cax -- optional. Color axis limit; the first and last data % values to use in contour plotting. If missing, caxis() % will be used. % % NOTE: grads has a limit of 240 colors for contour plots, so this % routine will abort if N>240. Also, N must be at least 3. % if(nargin<3) cax=caxis(); if(nargin<2) cm=colormap(); end end nc=size(cm,1); if(nc<2) error(['This function requires at least 2 colors in the ' ... 'colormap.']) end if(numel(cax)~=2 || ~isreal(cax)) error('cax must be a two-argument real vector'); end if(any(cm<0 | cm>1 | ~isreal(cm))) error(['There are invalid values in your colomap. It must contain ' ... 'only real numbers from 0 to 1.']); end if(nargin<1) N=min(240,nc); else if(N>240 || N<3) error('N must be between 3 and 240, inclusive.') end end if(N~=nc) cm=interp1(1:nc,cm,linspace(1,nc,N)); end cax=sort(cax); c=floor(round(cm*255)); txt=''; for i=1:size(cm,1) txt=[txt,sprintf('''set rgb %d %d %d %d''\n',... i+15,c(i,1),c(i,2),c(i,3))]; end txt=[txt,sprintf('\n')]; txt=[txt,'''set clevs ',num2str(linspace(cax(1),cax(2),N-1)),sprintf('''\n')]; txt=[txt,'''set ccols ',num2str(15+(1:N)),sprintf('''\n')]; txt=[txt,sprintf('''set gxout shaded''\n')]; end