source: trunk/PLOTS/LABEL/label_date.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

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