source: trunk/MESOSCALE/PLOT/MINIMAL/colorbar.pro @ 134

Last change on this file since 134 was 85, checked in by aslmd, 14 years ago

LMD_MM_MARS et LMD_LES_MARS: ajout des routines IDL pour tracer les sorties --> voir mesoscale/PLOT

File size: 19.4 KB
Line 
1;+
2; NAME:
3;   COLORBAR
4;
5; PURPOSE:
6;
7;       The purpose of this routine is to add a color bar to the current
8;       graphics window.
9;
10; AUTHOR:
11;
12;   FANNING SOFTWARE CONSULTING
13;   David Fanning, Ph.D.
14;   1645 Sheely Drive
15;   Fort Collins, CO 80526 USA
16;   Phone: 970-221-0438
17;   E-mail: davidf@dfanning.com
18;   Coyote's Guide to IDL Programming: http://www.dfanning.com/
19;
20; CATEGORY:
21;
22;       Graphics, Widgets.
23;
24; CALLING SEQUENCE:
25;
26;       COLORBAR
27;
28; INPUTS:
29;
30;       None.
31;
32; KEYWORD PARAMETERS:
33;
34;       ANNOTATECOLOR: The name of the "annotation color" to use. The names are those for
35;                     FSC_COLOR, and using the keyword implies that FSC_COLOR is also in
36;                     your !PATH. If this keyword is used, the annotation color is loaded
37;                     *after* the color bar is displayed. The color will be represented
38;                     as theColor = FSC_COLOR(ANNOTATECOLOR, COLOR). This keyword is provide
39;                     to maintain backward compatibility, but also to solve the problem of
40;                     and extra line in the color bar when this kind of syntax is used in
41;                     conjunction with the indexed (DEVICE, DECOMPOSED=0) model is used:
42;
43;                          LoadCT, 33
44;                          TVImage, image
45;                          Colorbar, Color=FSC_Color('firebrick')
46;
47;                     The proper syntax for device-independent color is like this:
48;
49;                          LoadCT, 33
50;                          TVImage, image
51;                          Colorbar, AnnotateColor='firebrick', Color=255
52;
53;       BOTTOM:       The lowest color index of the colors to be loaded in
54;                     the bar.
55;
56;       CHARSIZE:     The character size of the color bar annotations. Default is !P.Charsize.
57;
58;       COLOR:        The color index of the bar outline and characters. Default
59;                     is !P.Color..
60;
61;       DIVISIONS:    The number of divisions to divide the bar into. There will
62;                     be (divisions + 1) annotations. The default is 6.
63;
64;       FONT:         Sets the font of the annotation. Hershey: -1, Hardware:0, True-Type: 1.
65;
66;       FORMAT:       The format of the bar annotations. Default is '(I0)'.
67;
68;       INVERTCOLORS: Setting this keyword inverts the colors in the color bar.
69;
70;       MAXRANGE:     The maximum data value for the bar annotation. Default is
71;                     NCOLORS.
72;
73;       MINRANGE:     The minimum data value for the bar annotation. Default is 0.
74;
75;       MINOR:        The number of minor tick divisions. Default is 2.
76;
77;       NCOLORS:      This is the number of colors in the color bar.
78;
79;       NODISPLAY:    COLORBAR uses FSC_COLOR to specify some of it colors. Normally,
80;                     FSC_COLOR loads "system" colors as part of its palette of colors.
81;                     In order to do so, it has to create an IDL widget, which in turn
82;                     has to make a connection to the windowing system. If your program
83;                     is being run without a window connection, then this program will
84;                     fail. If you can live without the system colors (and most people
85;                     don't even know they are there, to tell you the truth), then setting
86;                     this keyword will keep them from being loaded, and you can run
87;                     COLORBAR without a display.
88;
89;       POSITION:     A four-element array of normalized coordinates in the same
90;                     form as the POSITION keyword on a plot. Default is
91;                     [0.88, 0.10, 0.95, 0.90] for a vertical bar and
92;                     [0.10, 0.88, 0.90, 0.95] for a horizontal bar.
93;
94;       RANGE:        A two-element vector of the form [min, max]. Provides an
95;                     alternative way of setting the MINRANGE and MAXRANGE keywords.
96;
97;       REVERSE:      Setting this keyword reverses the colors in the colorbar.
98;
99;       RIGHT:        This puts the labels on the right-hand side of a vertical
100;                     color bar. It applies only to vertical color bars.
101;
102;       TICKNAMES:    A string array of names or values for the tick marks.
103;
104;       TITLE:        This is title for the color bar. The default is to have
105;                     no title.
106;
107;       TOP:          This puts the labels on top of the bar rather than under it.
108;                     The keyword only applies if a horizontal color bar is rendered.
109;
110;       VERTICAL:     Setting this keyword give a vertical color bar. The default
111;                     is a horizontal color bar.
112;
113; COMMON BLOCKS:
114;
115;       None.
116;
117; SIDE EFFECTS:
118;
119;       Color bar is drawn in the current graphics window.
120;
121; RESTRICTIONS:
122;
123;       The number of colors available on the graphics display device (not the
124;       PostScript device) is used unless the NCOLORS keyword is used.
125;
126;       Requires the FSC_COLOR program from the Coyote Library:
127;
128;          http://www.dfanning.com/programs/fsc_color.pro
129;
130; EXAMPLE:
131;
132;       To display a horizontal color bar above a contour plot, type:
133;
134;       LOADCT, 5, NCOLORS=100
135;       CONTOUR, DIST(31,41), POSITION=[0.15, 0.15, 0.95, 0.75], $
136;          C_COLORS=INDGEN(25)*4, NLEVELS=25
137;       COLORBAR, NCOLORS=100, POSITION=[0.15, 0.85, 0.95, 0.90]
138;
139; MODIFICATION HISTORY:
140;
141;       Written by: David W. Fanning, 10 JUNE 96.
142;       10/27/96: Added the ability to send output to PostScript. DWF
143;       11/4/96: Substantially rewritten to go to screen or PostScript
144;           file without having to know much about the PostScript device
145;           or even what the current graphics device is. DWF
146;       1/27/97: Added the RIGHT and TOP keywords. Also modified the
147;            way the TITLE keyword works. DWF
148;       7/15/97: Fixed a problem some machines have with plots that have
149;            no valid data range in them. DWF
150;       12/5/98: Fixed a problem in how the colorbar image is created that
151;            seemed to tickle a bug in some versions of IDL. DWF.
152;       1/12/99: Fixed a problem caused by RSI fixing a bug in IDL 5.2. Sigh... DWF.
153;       3/30/99: Modified a few of the defaults. DWF.
154;       3/30/99: Used NORMAL rather than DEVICE coords for positioning bar. DWF.
155;       3/30/99: Added the RANGE keyword. DWF.
156;       3/30/99: Added FONT keyword. DWF
157;       5/6/99: Many modifications to defaults. DWF.
158;       5/6/99: Removed PSCOLOR keyword. DWF.
159;       5/6/99: Improved error handling on position coordinates. DWF.
160;       5/6/99. Added MINOR keyword. DWF.
161;       5/6/99: Set Device, Decomposed=0 if necessary. DWF.
162;       2/9/99: Fixed a problem caused by setting BOTTOM keyword, but not NCOLORS. DWF.
163;       8/17/99. Fixed a problem with ambiguous MIN and MINOR keywords. DWF
164;       8/25/99. I think I *finally* got the BOTTOM/NCOLORS thing sorted out. :-( DWF.
165;       10/10/99. Modified the program so that current plot and map coordinates are
166;            saved and restored after the colorbar is drawn. DWF.
167;       3/18/00. Moved a block of code to prevent a problem with color decomposition. DWF.
168;       4/28/00. Made !P.Font default value for FONT keyword. DWF.
169;       9/26/00. Made the code more general for scalable pixel devices. DWF.
170;       1/16/01. Added INVERTCOLORS keyword. DWF.
171;       5/11/04. Added TICKNAME keyword. DWF.
172;       9/29/05. Added REVERSE keywords, which does the *exact* same thing as
173;           INVERTCOLORS, but I can never remember the latter keyword name. DWF.
174;       1/2/07. Added ANNOTATECOLOR keyword. DWF.
175;       4/14/07. Changed the default FORMAT to I0. DWF.
176;       5/1/07. Unexpected consequence of default format change is colorbar annotations
177;           no longer match contour plot levels. Changed to explicit formating of
178;           colorbar axis labels before PLOT command. DWF.
179;       5/25/07. Previous change has unanticipated effect on color bars using
180;           logarithmic scaling, which is not really supported, but I have an
181;           article on my web page describing how to do it: http://www.dfanning.com/graphics_tips/logcb.html.
182;           Thus, I've fixed the program to accommodate log scaling, while still not OFFICIALLY
183;           supporting it. DWF.
184;       10/3/07. Method used to calculate TICKNAMES produces incorrect values in certain cases when
185;           the min and max range values are integers. Now force range values to be floats. DWF.
186;       10/17/07. Accidentaly use of INTERP keyword in CONGRID results in wrong bar values for
187;           low NCOLORS numbers when INVERTCOLORS or REVERSE keyword is used. Removed INTERP keyword. DWF.
188;       11/10/07. Finished fixing program to accommodate log scaling in ALL possible permutations. DWF.
189;       8 Feb 2008. Added CRONJOB keyword and decided to use month names when I write the date. DWF.
190;       8 Feb 2008. Renamed CRONJOB to NODISPLAY to better reflect its purpose. DWF.
191;      21 May 2008. Changed the default CHARSIZE to !P.CHARSIZE from 1.0. DWF.
192;-
193;
194;###########################################################################
195;
196; LICENSE
197;
198; This software is OSI Certified Open Source Software.
199; OSI Certified is a certification mark of the Open Source Initiative.
200;
201; Copyright 2000-2008 Fanning Software Consulting.
202;
203; This software is provided "as-is", without any express or
204; implied warranty. In no event will the authors be held liable
205; for any damages arising from the use of this software.
206;
207; Permission is granted to anyone to use this software for any
208; purpose, including commercial applications, and to alter it and
209; redistribute it freely, subject to the following restrictions:
210;
211; 1. The origin of this software must not be misrepresented; you must
212;    not claim you wrote the original software. If you use this software
213;    in a product, an acknowledgment in the product documentation
214;    would be appreciated, but is not required.
215;
216; 2. Altered source versions must be plainly marked as such, and must
217;    not be misrepresented as being the original software.
218;
219; 3. This notice may not be removed or altered from any source distribution.
220;
221; For more information on Open Source Software, visit the Open Source
222; web site: http://www.opensource.org.
223;
224;###########################################################################
225
226
227PRO COLORBAR, BOTTOM=bottom, CHARSIZE=charsize, COLOR=color, DIVISIONS=divisions, $
228   FORMAT=format, POSITION=position, MAXRANGE=maxrange, MINRANGE=minrange, NCOLORS=ncolors, $
229   TITLE=title, VERTICAL=vertical, TOP=top, RIGHT=right, MINOR=minor, $
230   RANGE=range, FONT=font, TICKLEN=ticklen, _EXTRA=extra, INVERTCOLORS=invertcolors, $
231   TICKNAMES=ticknames, REVERSE=reverse, ANNOTATECOLOR=annotatecolor, XLOG=xlog, YLOG=ylog, $
232   NODISPLAY=nodisplay
233
234    compile_opt idl2
235
236    ; Return to caller on error.
237    On_Error, 2
238
239    ; Save the current plot state.
240    bang_p = !P
241    bang_x = !X
242    bang_Y = !Y
243    bang_Z = !Z
244    bang_Map = !Map
245
246    ; Are scalable pixels available on the device?
247    IF (!D.Flags AND 1) NE 0 THEN scalablePixels = 1 ELSE scalablePixels = 0
248
249    ; Which release of IDL is this?
250    thisRelease = Float(!Version.Release)
251
252    ; Check and define keywords.
253    IF N_ELEMENTS(ncolors) EQ 0 THEN BEGIN
254
255       ; Most display devices to not use the 256 colors available to
256       ; the PostScript device. This presents a problem when writing
257       ; general-purpose programs that can be output to the display or
258       ; to the PostScript device. This problem is especially bothersome
259       ; if you don't specify the number of colors you are using in the
260       ; program. One way to work around this problem is to make the
261       ; default number of colors the same for the display device and for
262       ; the PostScript device. Then, the colors you see in PostScript are
263       ; identical to the colors you see on your display. Here is one way to
264       ; do it.
265
266       IF scalablePixels THEN BEGIN
267          oldDevice = !D.NAME
268
269             ; What kind of computer are we using? SET_PLOT to appropriate
270             ; display device.
271
272          thisOS = !VERSION.OS_FAMILY
273          thisOS = STRMID(thisOS, 0, 3)
274          thisOS = STRUPCASE(thisOS)
275          CASE thisOS of
276             'MAC': SET_PLOT, thisOS
277             'WIN': SET_PLOT, thisOS
278             ELSE: SET_PLOT, 'X'
279          ENDCASE
280
281          ; Here is how many colors we should use.
282          ncolors = !D.TABLE_SIZE
283          SET_PLOT, oldDevice
284        ENDIF ELSE ncolors = !D.TABLE_SIZE
285    ENDIF
286    IF N_ELEMENTS(bottom) EQ 0 THEN bottom = 0B
287    IF N_ELEMENTS(charsize) EQ 0 THEN charsize = !P.Charsize
288    IF N_ELEMENTS(format) EQ 0 THEN format = '(I0)'
289    IF N_ELEMENTS(color) EQ 0 THEN color = !P.Color
290    minrange = (N_ELEMENTS(minrange) EQ 0) ? 0. : Float(minrange)
291    maxrange = (N_ELEMENTS(maxrange) EQ 0) ? Float(ncolors) : Float(maxrange)
292    IF N_ELEMENTS(ticklen) EQ 0 THEN ticklen = 0.2
293    IF N_ELEMENTS(minor) EQ 0 THEN minor = 2
294    IF N_ELEMENTS(range) NE 0 THEN BEGIN
295       minrange = Float(range[0])
296       maxrange = Float(range[1])
297    ENDIF
298    IF N_ELEMENTS(divisions) EQ 0 THEN divisions = 6
299    IF N_ELEMENTS(font) EQ 0 THEN font = !P.Font
300    IF N_ELEMENTS(title) EQ 0 THEN title = ''
301    xlog = Keyword_Set(xlog)
302    ylog = Keyword_Set(ylog)
303
304    ; You can't have a format set *and* use ticknames.
305    IF N_ELEMENTS(ticknames) NE 0 THEN format = ""
306
307    ; If the format is NOT null, then format the ticknames yourself.
308    IF (xlog XOR ylog) EQ 0 THEN BEGIN
309        IF format NE "" THEN BEGIN
310           step = (maxrange - minrange) / divisions
311           levels = minrange > (Indgen(divisions+1) * step + minrange) < maxrange
312           IF StrPos(StrLowCase(format), 'i') NE -1 THEN levels = Round(levels)
313           ticknames = String(levels, Format=format)
314           format = "" ; No formats allowed in PLOT call now that we have ticknames.
315        ENDIF
316    ENDIF
317
318    IF KEYWORD_SET(vertical) THEN BEGIN
319       bar = REPLICATE(1B,20) # BINDGEN(ncolors)
320       IF Keyword_Set(invertcolors) THEN bar = Reverse(bar, 2)
321       IF N_ELEMENTS(position) EQ 0 THEN BEGIN
322          position = [0.88, 0.1, 0.95, 0.9]
323       ENDIF ELSE BEGIN
324          IF position[2]-position[0] GT position[3]-position[1] THEN BEGIN
325             position = [position[1], position[0], position[3], position[2]]
326          ENDIF
327          IF position[0] GE position[2] THEN Message, "Position coordinates can't be reconciled."
328          IF position[1] GE position[3] THEN Message, "Position coordinates can't be reconciled."
329       ENDELSE
330    ENDIF ELSE BEGIN
331       bar = BINDGEN(ncolors) # REPLICATE(1B, 20)
332       IF Keyword_Set(invertcolors) THEN bar = Reverse(bar, 1)
333       IF N_ELEMENTS(position) EQ 0 THEN BEGIN
334          position = [0.1, 0.88, 0.9, 0.95]
335       ENDIF ELSE BEGIN
336          IF position[3]-position[1] GT position[2]-position[0] THEN BEGIN
337             position = [position[1], position[0], position[3], position[2]]
338          ENDIF
339          IF position[0] GE position[2] THEN Message, "Position coordinates can't be reconciled."
340          IF position[1] GE position[3] THEN Message, "Position coordinates can't be reconciled."
341       ENDELSE
342    ENDELSE
343
344    ; Scale the color bar.
345     bar = BYTSCL(bar, TOP=(ncolors-1) < (255-bottom)) + bottom
346
347     IF Keyword_Set(reverse) THEN BEGIN
348       IF Keyword_Set(vertical) THEN bar = Reverse(bar,2) ELSE bar = Reverse(bar,1)
349     ENDIF
350
351    ; Get starting locations in NORMAL coordinates.
352    xstart = position[0]
353    ystart = position[1]
354
355    ; Get the size of the bar in NORMAL coordinates.
356    xsize = (position[2] - position[0])
357    ysize = (position[3] - position[1])
358
359    ; Display the color bar in the window. Sizing is
360    ; different for PostScript and regular display.
361    IF scalablePixels THEN BEGIN
362
363       TV, bar, xstart, ystart, XSIZE=xsize, YSIZE=ysize, /Normal
364
365    ENDIF ELSE BEGIN
366
367       bar = CONGRID(bar, CEIL(xsize*!D.X_VSize), CEIL(ysize*!D.Y_VSize))
368
369       ; Decomposed color off if device supports it.
370       CASE  StrUpCase(!D.NAME) OF
371            'X': BEGIN
372                IF thisRelease GE 5.2 THEN Device, Get_Decomposed=thisDecomposed
373                Device, Decomposed=0
374                ENDCASE
375            'WIN': BEGIN
376                IF thisRelease GE 5.2 THEN Device, Get_Decomposed=thisDecomposed
377                Device, Decomposed=0
378                ENDCASE
379            'MAC': BEGIN
380                IF thisRelease GE 5.2 THEN Device, Get_Decomposed=thisDecomposed
381                Device, Decomposed=0
382                ENDCASE
383            ELSE:
384       ENDCASE
385
386       TV, bar, xstart, ystart, /Normal
387
388       ; Restore Decomposed state if necessary.
389       CASE StrUpCase(!D.NAME) OF
390          'X': BEGIN
391             IF thisRelease GE 5.2 THEN Device, Decomposed=thisDecomposed
392             ENDCASE
393          'WIN': BEGIN
394             IF thisRelease GE 5.2 THEN Device, Decomposed=thisDecomposed
395             ENDCASE
396          'MAC': BEGIN
397             IF thisRelease GE 5.2 THEN Device, Decomposed=thisDecomposed
398             ENDCASE
399          ELSE:
400       ENDCASE
401
402    ENDELSE
403
404    ; Annotate the color bar.
405    IF N_Elements(annotateColor) NE 0 THEN $
406      color = FSC_Color(annotateColor, color, NODISPLAY=Keyword_Set(nodisplay))
407
408;SPIGA
409;SPIGA
410loadct, 0, /silent
411color=0
412
413
414    IF KEYWORD_SET(vertical) THEN BEGIN
415
416       IF KEYWORD_SET(right) THEN BEGIN
417
418          PLOT, [minrange,maxrange], [minrange,maxrange], /NODATA, XTICKS=1, $
419             YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
420             POSITION=position, COLOR=color, CHARSIZE=charsize, /NOERASE, $
421             XTICKFORMAT='(A1)', YTICKFORMAT='(A1)', YMINOR=minor, _EXTRA=extra, $
422             YTICKNAME=ticknames, FONT=font, YLOG=ylog
423
424          AXIS, YAXIS=1, YRANGE=[minrange, maxrange], YTICKFORMAT=format, YTICKS=divisions, $
425             YTICKLEN=ticklen, YSTYLE=1, COLOR=color, CHARSIZE=charsize, $
426             FONT=font, YTITLE=title, _EXTRA=extra, YMINOR=minor, YTICKNAME=ticknames, YLOG=ylog
427
428       ENDIF ELSE BEGIN
429
430          PLOT, [minrange,maxrange], [minrange,maxrange], /NODATA, XTICKS=1,  $
431             YTICKS=divisions, YSTYLE=1, XSTYLE=1, TITLE=title, $
432             POSITION=position, COLOR=color, CHARSIZE=charsize, /NOERASE, $
433             XTICKFORMAT='(A1)', YTICKFORMAT=format, YMinor=minor, _EXTRA=extra, $
434             YTICKNAME=ticknames, YLOG=ylog, YTICKLEN=ticklen
435
436       ENDELSE
437
438    ENDIF ELSE BEGIN
439
440       IF KEYWORD_SET(top) THEN BEGIN
441
442          PLOT, [minrange,maxrange], [minrange,maxrange], /NODATA, XTICKS=divisions, $
443             YTICKS=1, XSTYLE=9, YSTYLE=1, $
444             POSITION=position, COLOR=color, CHARSIZE=charsize, /NOERASE, $
445             YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', XTICKLEN=ticklen, $
446             XRANGE=[minrange, maxrange], FONT=font, XMINOR=minor,_EXTRA=extra, $
447             XTICKNAME=ticknames, XLOG=xlog
448
449          AXIS, XTICKS=divisions, XSTYLE=1, COLOR=color, CHARSIZE=charsize, $
450             XTICKFORMAT=format, XTICKLEN=ticklen, XRANGE=[minrange, maxrange], XAXIS=1, $
451             FONT=font, XTITLE=title, _EXTRA=extra, XCHARSIZE=charsize, XMINOR=minor, $
452             XTICKNAME=ticknames, XLOG=xlog
453
454       ENDIF ELSE BEGIN
455
456          PLOT, [minrange,maxrange], [minrange,maxrange], /NODATA, XTICKS=divisions, $
457             YTICKS=1, XSTYLE=1, YSTYLE=1, TITLE=title, $
458             POSITION=position, COLOR=color, CHARSIZE=charsize, /NOERASE, $
459             YTICKFORMAT='(A1)', XTICKFORMAT=format, XTICKLEN=ticklen, $
460             XRANGE=[minrange, maxrange], FONT=font, XMinor=minor, _EXTRA=extra, $
461             XTICKNAME=ticknames, XLOG=xlog
462
463        ENDELSE
464
465    ENDELSE
466
467    ; Restore the previous plot and map system variables.
468    !P = bang_p
469    !X = bang_x
470    !Y = bang_y
471    !Z = bang_z
472    !Map = bang_map
473
474END
Note: See TracBrowser for help on using the repository browser.