source: trunk/SRC/ReadWrite/rseries_getname.pro @ 495

Last change on this file since 495 was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

File size: 9.0 KB
Line 
1;+
2;
3; @file_comments
4; find the name of a file that could potentially contain a date
5; the file name must follow the following pattern:
6;     prefix + * +dates_description + * + suffix + *.nc
7;   - prefix suffix are described in the input parameters bellow
8;   - dates_description is described in the keyword datefmt
9;
10; @categories
11; Reading
12;
13; @param prefix {in}{required}{type=string}
14; a string defining the prefix used to identify the file. It can be
15; anything except the pattern used to define the dates (see DATEFMT keyword)
16;
17; @param suffix {in}{required}{type=string}
18; a string defining the suffix used to identify the file. It can be
19; anything except the pattern used to define the dates (see DATEFMT keyword)
20; note that *.nc is added to suffix if it is not finishing with ".nc'
21;
22; @param date {in}{required}{type=long integer}
23; a date following this format yyyymmdd
24;
25; @param date1 {out}{optional}{type=long integer}
26; yyyymmdd1 in the file name
27;
28; @param date2 {out}{optional}{type=long integer}
29; yyyymmdd2 in the file name
30;
31; @keyword DATEFMT {default='i8_i8'}{type=scalar string}
32; a string defining the date format used in the file name. the
33; possible choices are:
34;   - 'i8_i8': yyyymmdd_yyyymmdd (for ex. 20110401_20110431)
35;   - 'i6_i6': yyyymm_yyyymm (for ex. 201104_201104). we assume date1(date2) = beginning(end) of the month
36;   - 'i4_i4': yyyy_yyyy (for ex. 2011_2011). we assume date1(date2) = beginning(end) of the year
37;   - 'i8': yyyymmdd (for ex. 20110401) we assume date2 = date1
38;   - 'i6': yyyymm (for ex. 201104) we assume date1(date2) = beginning(end) of the month
39;   - 'i4': yyyymm (for ex. 2011) we assume date1(date2) = beginning(end) of the year
40;   - 'yi4mi2di2_yi4mi2di2': yxxxxmxxdxx_yxxxxmxxdxx (for ex. y2011m04d01_y2011m04d31)
41;   - 'yi4mi2_yi4mi2': yxxxxmxx_yxxxxmxx (for ex. y2011m04_y2011m04) we assume date1(date2) = beginning(end) of the month
42;   - 'yi4_yi4': yxxxx_yxxxx (for ex. y2011_y2011) we assume date1(date2) = beginning(end) of the year
43;   - 'yi4mi2di2': yxxxxmxxdxx (for ex. y2011m04d01) we assume date2 = date1
44;   - 'yi4mi2': yxxxxmxx (for ex. y2011m04) we assume date1(date2) = beginning(end) of the month
45;   - 'yi4': yxxxx (for ex. y2011) we assume date1(date2) = beginning(end) of the year
46;
47; @returns
48; the first filename to match (with date between yyyymmdd1 and yyyymmdd2)
49;
50; @uses
51;      @cm_general for the common variable "iodir" that specify the
52;      directory where the files are located
53;
54; @restrictions
55;  see description part
56;
57; @examples
58;  IDL> file = rseries_getname('TOTO_1d_','_sst_', 20110412)
59;  TOTO_1d_20110401_20110430_sst_grid_T.nc
60;  IDL> file = rseries_getname('TOTO_1d_','_sst_', 20110412, DATEFMT = 'yi4mi2di2_yi4mi2di2')
61;  TOTO_1d_y2011m04d01_y2011m04d30_sst_grid_T.nc
62;
63; @history
64; 2011-04: Creation. Sebastien Masson (smasson\@locean-ipsl.upmc.fr)
65;
66; @version
67; $Id$
68;
69;-
70FUNCTION rseries_getname, prefix, suffix, date, date1, date2, DATEFMT = datefmt
71
72  compile_opt idl2, strictarrsubs
73
74@cm_general
75;
76; find the list of file that could contain the date we want
77;
78  IF n_elements(datefmt) EQ 0 THEN datefmt = 'i8_i8'
79  CASE datefmt OF
80    'i8_i8':d1_d2 = '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*'                     ; yyyymmdd_yyyymmdd
81    'i6_i6':d1_d2 = '*[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]*'                                         ; yyyymm_yyyymm
82    'i4_i4':d1_d2 = '*[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]*'                                                             ; yyyy_yyyy
83    'i8':d1_d2 = '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*'                                                                 ; yyyymmdd
84    'i6':d1_d2 = '*[0-9][0-9][0-9][0-9][0-9][0-9]*'                                                                           ; yyyymm
85    'i4':d1_d2 = '*[0-9][0-9][0-9][0-9]*'                                                                                     ; yyyymm
86    'yi4mi2di2_yi4mi2di2':d1_d2 = '*y[0-9][0-9][0-9][0-9]m[0-9][0-9]d[0-9][0-9]_y[0-9][0-9][0-9][0-9]m[0-9][0-9]d[0-9][0-9]*' ; yxxxxmxxdxx_yxxxxmxxdxx
87    'yi4mi2_yi4mi2':d1_d2 = '*y[0-9][0-9][0-9][0-9]m[0-9][0-9]_y[0-9][0-9][0-9][0-9]m[0-9][0-9]*'                             ; yxxxxmxx_yxxxxmxx
88    'yi4_yi4':d1_d2 = '*y[0-9][0-9][0-9][0-9]_y[0-9][0-9][0-9][0-9]*'                                                         ; yxxxx_yxxxx
89    'yi4mi2di2':d1_d2 = '*y[0-9][0-9][0-9][0-9]m[0-9][0-9]d[0-9][0-9]*'                                                       ; yxxxxmxxdxx
90    'yi4mi2':d1_d2 = '*y[0-9][0-9][0-9][0-9]m[0-9][0-9]*'                                                                     ; yxxxxmxx
91    'yi4':d1_d2 = '*y[0-9][0-9][0-9][0-9]*'                                                                                   ; yxxxx
92    ELSE:BEGIN
93      print, 'this date format is not accepted: ' + datefmt
94      return, ''
95    ENDCASE
96  ENDCASE
97
98; prefix + '*' + '_yyyymmdd_yyyymmdd_' + '*' + suffix + '*' + '.nc'
99  IF strlen(suffix) - strpos(suffix, '.nc', /reverse_search) EQ 3 $
100     AND strlen(suffix) NE 2 THEN suff = suffix ELSE suff = suffix + '*.nc'
101  nn = prefix + d1_d2 + suff
102  file_list = file_search (iodir+nn, count = nb_file )
103;
104  IF nb_file EQ 0 THEN BEGIN
105    print, 'file ' + nn + ' not found'
106    return, ''
107  ENDIF
108
109  filename = ''
110  i = 0
111
112  while i lt nb_file and filename eq '' do BEGIN
113
114    CASE datefmt OF
115      'i8_i8':BEGIN
116        d1_d2 = stregex(file_list[i], '[0-9]{8}_[0-9]{8}', /extract)
117        d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT )
118        date1 = long(d1d2[0])
119        date2 = long(d1d2[1])
120      END
121      'i6_i6':BEGIN
122        d1_d2 = stregex(file_list[i], '[0-9]{6}_[0-9]{6}', /extract)
123        d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT )
124        date1 = long(d1d2[0])
125        date2 = long(d1d2[1])
126        date1 = date1 * 100L + 1L                       ; yyyymm01
127        date2 = jul2date(date2jul(date2 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month)
128      END
129      'i4_i4':BEGIN
130        d1_d2 = stregex(file_list[i], '[0-9]{4}_[0-9]{4}', /extract)
131        d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT )
132        date1 = long(d1d2[0])
133        date2 = long(d1d2[1])
134        date1 = date1 * 10000L +  101L ; yyyy0101
135        date2 = date2 * 10000L + 1231L ; yyyy1231
136      END
137      'i8':BEGIN
138        date1 = long(stregex(file_list[i], '[0-9]{8}', /extract))
139        date2 = date1
140      END
141      'i6':BEGIN
142        date1 = long(stregex(file_list[i], '[0-9]{6}', /extract))
143        date1 = date1 * 100L + 1L                       ; yyyymm01
144        date2 = jul2date(date2jul(date1 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month)
145      END
146      'i4':BEGIN
147        date1 = long(stregex(file_list[i], '[0-9]{4}', /extract))
148        date1 = date1 * 10000L +  101L ; yyyy0101
149        date2 = date1 * 10000L + 1231L ; yyyy1231
150      END
151      'yi4mi2di2_yi4mi2di2':BEGIN
152        d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}d[0-9]{2}_y[0-9]{4}m[0-9]{2}d[0-9]{2}', /extract)
153        d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT )
154        date1 = long(strjoin(strsplit(d1d2[0], '[ymd]', /extract)))
155        date2 = long(strjoin(strsplit(d1d2[1], '[ymd]', /extract)))
156      END
157      'yi4mi2_yi4mi2':BEGIN
158        d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}_y[0-9]{4}m[0-9]{2}', /extract)
159        d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT )
160        date1 = long(strjoin(strsplit(d1d2[0], '[ymd]', /extract)))
161        date2 = long(strjoin(strsplit(d1d2[1], '[ymd]', /extract)))
162        date1 = date1 * 100L + 1L                       ; yyyymm01
163        date2 = jul2date(date2jul(date2 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month)
164      END
165      'yi4_yi4':BEGIN
166        d1_d2 = stregex(file_list[i], 'y[0-9]{4}_y[0-9]{4}', /extract)
167        d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT )
168        date1 = long(strjoin(strsplit(d1d2[0], '[ymd]', /extract)))
169        date2 = long(strjoin(strsplit(d1d2[1], '[ymd]', /extract)))
170        date1 = date1 * 10000L +  101L ; yyyy0101
171        date2 = date2 * 10000L + 1231L ; yyyy1231
172      END
173      'yi4mi2di2':BEGIN
174        d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}d[0-9]{2}', /extract)
175        date1 = long(strjoin(strsplit(d1_d2, '[ymd]', /extract)))
176        date2 = date1
177      END
178      'yi4mi2':BEGIN
179        d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}_y[0-9]{4}m[0-9]{2}', /extract)
180        date1 = long(strjoin(strsplit(d1_d2, '[ymd]', /extract)))
181        date1 = date1 * 100L + 1L                       ; yyyymm01
182        date2 = jul2date(date2jul(date1 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month)
183      END
184      'yi4':BEGIN
185        d1_d2 = stregex(file_list[i], 'y[0-9]{4}_y[0-9]{4}', /extract)
186        date1 = long(strjoin(strsplit(d1_d2, '[ymd]', /extract)))
187        date1 = date1 * 10000L +  101L ; yyyy0101
188        date2 = date1 * 10000L + 1231L ; yyyy1231
189      END
190    ENDCASE
191
192    if date1 - date le 0 and date - date2 le 0 then filename = file_list[i]
193    i = i + 1
194
195  endwhile
196
197  if filename eq '' then BEGIN
198    print, strtrim(date, 1)+' not found in the following list of files:'
199    FOR i = 0, nb_file-1 DO print, file_list[i]
200  ENDIF
201
202  return, filename
203
204end
Note: See TracBrowser for help on using the repository browser.