Home > atmlab > geoplots > gmt > makepolar.m

makepolar

PURPOSE ^

MAKEPOLAR create a .cpt-file based on the 'polar' but with extra options

SYNOPSIS ^

function filecpt = makepolar(tickval,in)

DESCRIPTION ^

 MAKEPOLAR create a .cpt-file based on the 'polar' but with extra options

 PURPOSE: To create a .cpt file based on the 'polar'
           colourtable that garantees white color around a reference value 
           (e.g. in.reference = 0)

         
 IN      tickval     [%f,%f,etc]        The data contour values
         in          struct             options here (see help gmt_plot)

 Out:    filecpt     %s                 fullpath to colortable-file

 What it does:
             Similar to makecpt -Cpolar, but more fancy. Uses a gradient in
             the color depending on the number of contours, where the
             reference is relative to max/min data and the number of boxes
             that should be white. Gradient is from blue to white to red,
             and the grandient is equal on both sides of the reference. 

 Created by Salomon Eliasson
 $Id: makepolar.m 8744 2013-11-27 07:14:23Z seliasson $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

makepolar.m

SOURCE CODE ^

0001 function filecpt = makepolar(tickval,in)
0002 % MAKEPOLAR create a .cpt-file based on the 'polar' but with extra options
0003 %
0004 % PURPOSE: To create a .cpt file based on the 'polar'
0005 %           colourtable that garantees white color around a reference value
0006 %           (e.g. in.reference = 0)
0007 %
0008 %
0009 % IN      tickval     [%f,%f,etc]        The data contour values
0010 %         in          struct             options here (see help gmt_plot)
0011 %
0012 % Out:    filecpt     %s                 fullpath to colortable-file
0013 %
0014 % What it does:
0015 %             Similar to makecpt -Cpolar, but more fancy. Uses a gradient in
0016 %             the color depending on the number of contours, where the
0017 %             reference is relative to max/min data and the number of boxes
0018 %             that should be white. Gradient is from blue to white to red,
0019 %             and the grandient is equal on both sides of the reference.
0020 %
0021 % Created by Salomon Eliasson
0022 % $Id: makepolar.m 8744 2013-11-27 07:14:23Z seliasson $
0023 
0024 % string format to file
0025 diff = tickval(2)-tickval(1);
0026 b=~isequal(floor(diff),diff);
0027 x=diff;
0028 while x < 0
0029     b = b+1;
0030     x = x*10^b;
0031 end
0032 prtstr=sprintf('%%g',b);
0033 strtot = strcat(prtstr,'\t','%3.0f','\t','%3.0f','\t','%3.0f','\t',...
0034     prtstr,'\t','%3.0f','\t','%3.0f','\t','%3.0f','\n');
0035 
0036 % REFERENCE
0037 if ~isfield(in,'reference')
0038     ref = (max(tickval)+min(tickval))/2;
0039 else ref = in.reference;
0040 end
0041 
0042 [BT,TT,nw] = get_white_boxes(tickval,ref,in);
0043 
0044 n_colored_steps = [BT TT];
0045 
0046 N = max(n_colored_steps);
0047 inc = 255/N; % step gradient
0048 vect = 255-[1:N]*inc;
0049 
0050 ctable = zeros(length(tickval)-1,3);
0051 
0052 
0053 % BLUE
0054 tmp = abs([vect(BT:-1:1);vect(BT:-1:1);repmat(255,1,BT)])';
0055 len = size(tmp,1);
0056 ctable(1:len,:) = tmp;
0057 
0058 % WHITE
0059 ctable(len+1:len+nw,:) = repmat([255,255,255],nw,1);
0060 len = len+nw;
0061 
0062 % RED
0063 tmp = abs([repmat(255,1,TT);vect(TT:-1:1);vect(TT:-1:1)])';
0064 ctable(len+1:end,:) = tmp(end:-1:1,:);
0065 
0066 
0067 %% Make a cptfile out of it
0068 
0069 filecpt = 'mypolar.cpt';
0070 fid = fopen(filecpt,'w');
0071 %##################### HEADER ###############################
0072 fprintf(fid,'%s\t%s\n','#cpt file created by:','makepolar.m');
0073 fprintf(fid,'%s\n%s\n','#COLOR_MODEL = RGB','#');
0074 %############################################################
0075 
0076 for ii = 1:length(tickval)-1
0077     fprintf(fid,strtot,tickval(ii),ctable(ii,1),ctable(ii,2),ctable(ii,3),...
0078         tickval(ii+1),ctable(ii,1),ctable(ii,2),ctable(ii,3));
0079 end
0080 
0081 % Set Background and forground colors base on the last color minus 2 times
0082 % the gradient of color change used in ctable
0083 if isfield(in,'color_background')
0084     BG = sscanf(in.color_background,'%f/%f/%f');
0085 else
0086     tmp = ctable(1,1);
0087     if (tmp-2*inc)>0
0088         BG = [tmp-2*inc tmp-2*inc 255];
0089     else
0090         BG = [tmp tmp 255-2*inc];
0091     end
0092 end
0093 if isfield(in,'color_foreground')
0094     FG = sscanf(in.color_foreground,'%f/%f/%f');
0095 else
0096     tmp = ctable(end,end);
0097     if (tmp-2*inc)>0
0098         FG = [255 tmp-2*inc tmp-2*inc];
0099     else
0100         FG = [255-2*inc tmp tmp];
0101     end
0102 end
0103 if isfield(in,'color_nan')
0104     N = sscanf(in.color_nan,'%f/%f/%f');
0105 else
0106     N = [128 128 128]; %Grey
0107 end
0108 % Background, Foreground, and NaN color
0109 fprintf(fid,'%s\t%3.0f\t%3.0f\t%3.0f\n','B',abs(BG)); %added abs to garantee >0
0110 fprintf(fid,'%s\t%3.0f\t%3.0f\t%3.0f\n','F',abs(FG));
0111 fprintf(fid,'%s\t%3.0f\t%3.0f\t%3.0f\n','N',abs(N));
0112 
0113 fclose(fid);
0114 
0115 %%%%%%%%%%%%
0116 % SUBFUNCTIONS
0117 %   ||||||
0118 %   vvvvvv
0119 
0120 function [BT,TT,nw] = get_white_boxes(tickval,ref,in)
0121 %% get_white_boxes
0122 
0123 % WHITEBOXES
0124 if ~isfield(in,'nwhite')
0125     nw = ceil((length(tickval)-1)/10);
0126     input = false;
0127 else
0128     nw = in.nwhite;
0129     input=true;
0130 end
0131 
0132 assert(nw<length(tickval)-1,...
0133     ['gmtlab:' mfilename ':input'],'number of white boxes (%.0f) exceedes nlevels (%.0f)',...
0134     nw,length(tickval)-1)
0135 
0136 
0137 % ADJUST nwhite if ref is on a contour line
0138 if ismember(ref,tickval) && mod(nw,2) 
0139     % if ref is on one of the edges and nwhite is odd, increase nwhite by 1
0140     nw = nw+1;
0141     if input
0142         warning(['gmtlab:' mfilename ':badInput'],...
0143             '%s %s nwhite = %.0f to nwhite = %.0f','Reference value is exactly on a contour level,',...
0144             'i.e in.nwhite must be an even number. Increasing ',in.nwhite,nw);
0145     end
0146 end
0147 
0148 % ADJUST nwhite if ref is inbetween contour lines
0149 if ref < tickval(1) || ref > tickval(end)
0150     error(['gmtlab:' mfilename ':badInput'],'Reference value is outside the range of tick values')
0151 end
0152 
0153 if ~ismember(ref,tickval) && ~mod(nw,2) 
0154     % if ref is in between the edges and nwhite is even, decrease by 1
0155     nw = nw-1;
0156     if input
0157         warning(['gmtlab:' mfilename ':badInput'],...
0158             '%s %s nwhite = %.0f to nwhite = %.0f','Reference value is inbetween contour levels,',...
0159             'i.e in.nwhite must be an odd number. Decreasing ',in.nwhite,nw);
0160     end
0161 end
0162 
0163 whitedomain = (nw + mod(nw,2))/2;   
0164 
0165 % BT bottom ticks
0166 BT = sum(tickval < ref ) -whitedomain;
0167 % TT top ticks
0168 TT = sum(tickval > ref ) -whitedomain;
0169 
0170 % zero-it if it's less than 0
0171 BT = BT*(BT>=0);
0172 TT = TT*(TT>=0);
0173 
0174 % if reference is close to an EDGE
0175 if BT < whitedomain || TT < whitedomain
0176     if BT < whitedomain
0177         nw = nw - (whitedomain-BT);
0178         TT = TT + (whitedomain-BT);
0179     end
0180     if TT < whitedomain
0181         nw = nw - (whitedomain-TT);
0182         BT = BT + (whitedomain-TT);
0183         
0184     end
0185 
0186 end
0187 if TT==0
0188     BT = BT-1;
0189 end
0190 if BT==0
0191     TT = TT-1;
0192 end
0193 %special
0194 assert(BT+TT+nw == length(tickval)-1,['gmtlab:' mfilename ':Bug'],...
0195     'BT+TT+nw ~= length(tickval)-1')

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005