;+ ; ; @file_comments ; find the name of a file that could potentially contain a date ; the file name must follow the following pattern: ; prefix + * +dates_description + * + suffix + *.nc ; - prefix suffix are described in the input parameters bellow ; - dates_description is described in the keyword datefmt ; ; @categories ; Reading ; ; @param prefix {in}{required}{type=string} ; a string defining the prefix used to identify the file. It can be ; anything except the pattern used to define the dates (see DATEFMT keyword) ; ; @param suffix {in}{required}{type=string} ; a string defining the suffix used to identify the file. It can be ; anything except the pattern used to define the dates (see DATEFMT keyword) ; note that *.nc is added to suffix if it is not finishing with ".nc' ; ; @param date {in}{required}{type=long integer} ; a date following this format yyyymmdd ; ; @param date1 {out}{optional}{type=long integer} ; yyyymmdd1 in the file name ; ; @param date2 {out}{optional}{type=long integer} ; yyyymmdd2 in the file name ; ; @keyword DATEFMT {default='i8_i8'}{type=scalar string} ; a string defining the date format used in the file name. the ; possible choices are: ; - 'i8_i8': yyyymmdd_yyyymmdd (for ex. 20110401_20110431) ; - 'i6_i6': yyyymm_yyyymm (for ex. 201104_201104). we assume date1(date2) = beginning(end) of the month ; - 'i4_i4': yyyy_yyyy (for ex. 2011_2011). we assume date1(date2) = beginning(end) of the year ; - 'i8': yyyymmdd (for ex. 20110401) we assume date2 = date1 ; - 'i6': yyyymm (for ex. 201104) we assume date1(date2) = beginning(end) of the month ; - 'i4': yyyymm (for ex. 2011) we assume date1(date2) = beginning(end) of the year ; - 'yi4mi2di2_yi4mi2di2': yxxxxmxxdxx_yxxxxmxxdxx (for ex. y2011m04d01_y2011m04d31) ; - 'yi4mi2_yi4mi2': yxxxxmxx_yxxxxmxx (for ex. y2011m04_y2011m04) we assume date1(date2) = beginning(end) of the month ; - 'yi4_yi4': yxxxx_yxxxx (for ex. y2011_y2011) we assume date1(date2) = beginning(end) of the year ; - 'yi4mi2di2': yxxxxmxxdxx (for ex. y2011m04d01) we assume date2 = date1 ; - 'yi4mi2': yxxxxmxx (for ex. y2011m04) we assume date1(date2) = beginning(end) of the month ; - 'yi4': yxxxx (for ex. y2011) we assume date1(date2) = beginning(end) of the year ; ; @returns ; the first filename to match (with date between yyyymmdd1 and yyyymmdd2) ; ; @uses ; @cm_general for the common variable "iodir" that specify the ; directory where the files are located ; ; @restrictions ; see description part ; ; @examples ; IDL> file = rseries_getname('TOTO_1d_','_sst_', 20110412) ; TOTO_1d_20110401_20110430_sst_grid_T.nc ; IDL> file = rseries_getname('TOTO_1d_','_sst_', 20110412, DATEFMT = 'yi4mi2di2_yi4mi2di2') ; TOTO_1d_y2011m04d01_y2011m04d30_sst_grid_T.nc ; ; @history ; 2011-04: Creation. Sebastien Masson (smasson\@locean-ipsl.upmc.fr) ; ; @version ; $Id$ ; ;- FUNCTION rseries_getname, prefix, suffix, date, date1, date2, DATEFMT = datefmt compile_opt idl2, strictarrsubs @cm_general ; ; find the list of file that could contain the date we want ; IF n_elements(datefmt) EQ 0 THEN datefmt = 'i8_i8' CASE datefmt OF '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 '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 'i4_i4':d1_d2 = '*[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]*' ; yyyy_yyyy 'i8':d1_d2 = '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*' ; yyyymmdd 'i6':d1_d2 = '*[0-9][0-9][0-9][0-9][0-9][0-9]*' ; yyyymm 'i4':d1_d2 = '*[0-9][0-9][0-9][0-9]*' ; yyyymm '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 '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 'yi4_yi4':d1_d2 = '*y[0-9][0-9][0-9][0-9]_y[0-9][0-9][0-9][0-9]*' ; yxxxx_yxxxx 'yi4mi2di2':d1_d2 = '*y[0-9][0-9][0-9][0-9]m[0-9][0-9]d[0-9][0-9]*' ; yxxxxmxxdxx 'yi4mi2':d1_d2 = '*y[0-9][0-9][0-9][0-9]m[0-9][0-9]*' ; yxxxxmxx 'yi4':d1_d2 = '*y[0-9][0-9][0-9][0-9]*' ; yxxxx ELSE:BEGIN print, 'this date format is not accepted: ' + datefmt return, '' ENDCASE ENDCASE ; prefix + '*' + '_yyyymmdd_yyyymmdd_' + '*' + suffix + '*' + '.nc' IF strlen(suffix) - strpos(suffix, '.nc', /reverse_search) EQ 3 $ AND strlen(suffix) NE 2 THEN suff = suffix ELSE suff = suffix + '*.nc' nn = prefix + d1_d2 + suff file_list = file_search (iodir+nn, count = nb_file ) ; IF nb_file EQ 0 THEN BEGIN print, 'file ' + nn + ' not found' return, '' ENDIF filename = '' i = 0 while i lt nb_file and filename eq '' do BEGIN CASE datefmt OF 'i8_i8':BEGIN d1_d2 = stregex(file_list[i], '[0-9]{8}_[0-9]{8}', /extract) d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT ) date1 = long(d1d2[0]) date2 = long(d1d2[1]) END 'i6_i6':BEGIN d1_d2 = stregex(file_list[i], '[0-9]{6}_[0-9]{6}', /extract) d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT ) date1 = long(d1d2[0]) date2 = long(d1d2[1]) date1 = date1 * 100L + 1L ; yyyymm01 date2 = jul2date(date2jul(date2 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month) END 'i4_i4':BEGIN d1_d2 = stregex(file_list[i], '[0-9]{4}_[0-9]{4}', /extract) d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT ) date1 = long(d1d2[0]) date2 = long(d1d2[1]) date1 = date1 * 10000L + 101L ; yyyy0101 date2 = date2 * 10000L + 1231L ; yyyy1231 END 'i8':BEGIN date1 = long(stregex(file_list[i], '[0-9]{8}', /extract)) date2 = date1 END 'i6':BEGIN date1 = long(stregex(file_list[i], '[0-9]{6}', /extract)) date1 = date1 * 100L + 1L ; yyyymm01 date2 = jul2date(date2jul(date1 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month) END 'i4':BEGIN date1 = long(stregex(file_list[i], '[0-9]{4}', /extract)) date1 = date1 * 10000L + 101L ; yyyy0101 date2 = date1 * 10000L + 1231L ; yyyy1231 END 'yi4mi2di2_yi4mi2di2':BEGIN 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) d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT ) date1 = long(strjoin(strsplit(d1d2[0], '[ymd]', /extract))) date2 = long(strjoin(strsplit(d1d2[1], '[ymd]', /extract))) END 'yi4mi2_yi4mi2':BEGIN d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}_y[0-9]{4}m[0-9]{2}', /extract) d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT ) date1 = long(strjoin(strsplit(d1d2[0], '[ymd]', /extract))) date2 = long(strjoin(strsplit(d1d2[1], '[ymd]', /extract))) date1 = date1 * 100L + 1L ; yyyymm01 date2 = jul2date(date2jul(date2 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month) END 'yi4_yi4':BEGIN d1_d2 = stregex(file_list[i], 'y[0-9]{4}_y[0-9]{4}', /extract) d1d2 = STRSPLIT(d1_d2, '_', /EXTRACT ) date1 = long(strjoin(strsplit(d1d2[0], '[ymd]', /extract))) date2 = long(strjoin(strsplit(d1d2[1], '[ymd]', /extract))) date1 = date1 * 10000L + 101L ; yyyy0101 date2 = date2 * 10000L + 1231L ; yyyy1231 END 'yi4mi2di2':BEGIN d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}d[0-9]{2}', /extract) date1 = long(strjoin(strsplit(d1_d2, '[ymd]', /extract))) date2 = date1 END 'yi4mi2':BEGIN d1_d2 = stregex(file_list[i], 'y[0-9]{4}m[0-9]{2}_y[0-9]{4}m[0-9]{2}', /extract) date1 = long(strjoin(strsplit(d1_d2, '[ymd]', /extract))) date1 = date1 * 100L + 1L ; yyyymm01 date2 = jul2date(date2jul(date1 * 100L + 100L)) ; yyyymmdd (with dd the last day of the month) END 'yi4':BEGIN d1_d2 = stregex(file_list[i], 'y[0-9]{4}_y[0-9]{4}', /extract) date1 = long(strjoin(strsplit(d1_d2, '[ymd]', /extract))) date1 = date1 * 10000L + 101L ; yyyy0101 date2 = date1 * 10000L + 1231L ; yyyy1231 END ENDCASE if date1 - date le 0 and date - date2 le 0 then filename = file_list[i] i = i + 1 endwhile if filename eq '' then BEGIN print, strtrim(date, 1)+' not found in the following list of files:' FOR i = 0, nb_file-1 DO print, file_list[i] ENDIF return, filename end