source: trunk/SRC/ToBeReviewed/PLOTS/LABEL/label_date.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1; $Id$
2;
3; Copyright (c) 1993-1998, Research Systems, Inc.  All rights reserved.
4;       Unauthorized reproduction prohibited.
5
6;+
7; NAME:
8;       LABEL_DATE
9;
10; PURPOSE:
11;       This function labels axes with dates and times.
12;
13; CATEGORY:
14;       Plotting.
15;
16; CALLING SEQUENCE:
17;       To set up:
18;               dummy = LABEL_DATE(DATE_FORMAT='string')
19;       To use:
20;               PLOT, x, y, XTICKFORMAT='LABEL_DATE'
21;
22; INPUTS:
23;       No explicit user defined inputs. When called from the plotting
24;       routines, the input parameters are (Axis, Index, Value)
25;
26; KEYWORD PARAMETERS:
27;       DATE_FORMAT: a format string which may contain the following:
28;                      %M for month (3 character abbr)
29;                      %N for month (2 digit abbr)
30;                      %D for day of month,
31;                      %Y for 4 digit year.
32;                      %Z for last two digits of year.
33;            For time:
34;                      %H for Hours, 2 digits.
35;                      %I for mInutes, 2 digits.
36;                      %S for Seconds, 2 digits.
37;                      %% is %.
38;                    Other characters are passed directly thru.
39;                    For example, '%M %D, %Y' prints DEC 11, 1993
40;                      '%M %2Y' yields DEC 93
41;                      '%D-%M' yields 11-DEC
42;                      '%D/%N/%Y' yields 11/12/1993
43;                      '%M!C%Y' yields DEC on the top line, 1993 on
44;                      the bottom (!C is the new line graphic command).
45;
46;       MONTHS:      The names of the months, a twelve element string array.
47;                    If omitted, use Jan, Feb, ..., Dec.
48;
49;       OFFSET:      An optional starting offset of the plot.
50;               Unfortunately, single precision floating point is not accurate
51;               enough to properly represent Julian times.  This offset, which
52;               may be double precision, contains an offset that is added to
53;               all x values, before conversion to Julian date and time.
54
55; OUTPUTS:
56;       The date string to be plotted.
57;
58; COMMON BLOCKS:
59;       LABEL_DATE_COM.
60;
61; RESTRICTIONS:
62;       Only one date axis may be simultaneously active.
63;
64; PROCEDURE:
65;       Straightforward.
66;
67;       For an alternative way to label a plot axis with dates, refer to
68;       the C() format code accepted within format strings (applicable via
69;       the [XYZ]TICKFORMAT keywords).  This new format code was
70;       introduced in IDL 5.2.
71;
72; EXAMPLE:
73;       For example, to plot from Jan 1, 1993, to July 12, 1994:
74;         Start_date = julday(1, 1, 1993)
75;         End_date = julday(7, 12, 1994)
76;         Dummy = LABEL_DATE(DATE_FORMAT='%N/%D')  ;Simple mm/dd
77;         x = findgen(end_date+1 - start_date) + start_date ;Time axis
78;         PLOT, x, sqrt(x), XTICKFORMAT = 'LABEL_DATE', XSTYLE=1
79;         (Plot with X axis style set to exact.)
80;       
81; Example with times:
82;       For example, to plot from 3PM, Jan 1, 1993, to 5AM, Jan 3,
83;       1993:
84;       Start_date = Julday(1,1,1993)   ;Also starting offset
85;       Start_time = (3+12)/24.         ;Starting_time less offset
86;       End_time = (Julday(1,3,1993) - Start_date) + 5./24. ;Ending
87;               ;date/time - offset, note that the order of operations is
88;               ; important to avoid loss of precision.
89;       Dummy = LABEL_DATE(DATE_FORMAT='%D %M!C%H:%I', $
90;               offset=Start_date)       ;MMM NN <new line> HH:MM format
91;       x = findgen(20) * (End_time - Start_time) / 19 + start_time ;Time axis
92;       PLOT, x, sqrt(x), XTICKFORMAT = 'LABEL_DATE', XSTYLE=1
93;
94; MODIFICATION HISTORY:
95;       DMS, RSI.       April, 1993.    Written.
96;       DMS, RSI.       March, 1997.    Added Time format.
97;-
98
99FUNCTION LABEL_DATE, axis, index, x, DATE_FORMAT = format, MONTHS = months, $
100              OFFSET= offs, _EXTRA = ex
101;
102  compile_opt idl2, strictarrsubs
103;
104COMMON label_date_com, fmt, month_chr, offset
105
106if keyword_set(format) then begin ;Save format string?
107    if n_elements(offs) ne 0 then offset = double(offs) else offset = 0.0d0
108    if keyword_set(months) then month_chr = months $
109    else month_chr = ['Jan','Feb','Mar', 'Apr', 'May', 'Jun', 'Jul', $
110                      'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
111    fmt = format
112    return, 0
113endif
114
115if n_elements(month_chr) ne 12 or n_elements(fmt) le 0 $
116  or n_elements(offset) eq 0 then $
117  message,' Not initialized.'
118
119x1 = x + offset
120caldat, long(x1), month, day, year, _EXTRA = ex;Get the calendar date from julian
121frac = x1 - long(x1)            ;time of day, from 0 to 1.
122
123n = strlen(fmt)
124out = ''
125
126for i=0, n-1 do begin           ;Each format character...
127    c = strmid(fmt, i, 1)       ;The character.
128    if c eq '%' then begin
129        i = i + 1
130        c = strmid(fmt, i, 1)   ;The function
131        case c of               ;format character?
132            'M' : out = out + month_chr[month-1]
133            'N' : out = out + string(format='(i2.2)', month)
134            'D' : out = out + string(format='(i2.2)', day)
135            'Y' : out = out + string(format='(i4)', year)
136            'Z' : out = out + string(format='(i2.2)', year  mod 100)
137            'H' : out = out + string(format='(i2.2)', floor(24*frac))
138            'I' : out = out + string(format='(i2.2)', floor(1440 * frac mod 60))
139            'S' : out = out + string(format='(i2.2)', 86400L * frac mod 60)
140            '%' : out = out + '%'
141            else : message, 'Illegal character in date format string: '+fmt
142        endcase
143    endif else out = out + c
144endfor
145return, out
146end
Note: See TracBrowser for help on using the repository browser.