Index: trunk/SRC/ReadWrite/rseries_getname.pro
===================================================================
--- trunk/SRC/ReadWrite/rseries_getname.pro	(revision 463)
+++ trunk/SRC/ReadWrite/rseries_getname.pro	(revision 463)
@@ -0,0 +1,204 @@
+;+
+;
+; @file_comments
+; find the name of a file that coud potentatialy 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 pattren 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 pattren 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 coices 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
Index: trunk/SRC/ReadWrite/rseries_ncdf.pro
===================================================================
--- trunk/SRC/ReadWrite/rseries_ncdf.pro	(revision 463)
+++ trunk/SRC/ReadWrite/rseries_ncdf.pro	(revision 463)
@@ -0,0 +1,173 @@
+;+
+;
+; @file_comments
+;
+; @categories
+;
+; @param VAR_NAME {in}{required}{type=string}
+; name of the variable to be read.
+;
+; @param date1 {in}{required}{type=scalar, long interger or double precision}
+; date given in the yyyymmdd format. data will be read from date1
+;
+; @param date2 {in}{required}{type=scalar, long interger or double precision}
+; date given in the yyyymmdd format. data will be read until date1
+;
+; @param prefix {in}{required}{type=string}
+; a string defining the prefix used to identify the file.
+;
+; @param suffix {in}{required}{type=string}
+; a string defining the suffix used to identify the file.
+; note that *.nc is added to suffix is it is not finishing with ".nc'
+;
+; @keyword DATEFMT {default='i8_i8'}{type=scalar string}
+; a string defining the date format used in the file name. the
+; possible coices 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
+;
+; @keyword NOSTRUCT {default=0}{type=scalar: 0 or 1}
+; We activate it if we do not want that read_ncdf send back a structure
+; but only the array referring to the field.
+;
+; @keyword _EXTRA
+; Used to pass keywords to <pro>rseries_getname</pro> and <pro>read_ncdf</pro>
+;
+; @returns
+; Structure readable by <pro>litchamp</pro> or an array if NOSTRUCT is 
+; activated.
+;
+; @uses
+;   <pro>cm_4cal</pro>
+;   <pro>cm_4data</pro>
+;   <pro>jul2date</pro>
+;   <pro>date2jul</pro>
+;   <pro>report</pro>
+;   <pro>rseries_getname</pro>
+;   <pro>read_ncdf</pro>
+;
+; @restrictions
+; see <pro>rseries_getname</pro> and <pro>read_ncdf</pro>
+;
+; @examples
+;  IDL> a = rseries_ncdf('zos', 100101,121231,'tr33beta','1Y_grid_')
+;  IDL> a = rseries_ncdf('zos', 100101,121231,'tr33beta','1Y_grid_',direc='xy',/nostruct)
+;
+; @history
+; 2011-04: Creation. Sebastien Masson (smasson\@locean-ipsl.upmc.fr) 
+;
+; @version
+; $Id$
+;
+;-
+FUNCTION rseries_ncdf, var_name, date1, date2, prefix, suffix, NOSTRUCT = nostruct, DATEFMT = datefmt, FILESLIST = fileslist $
+                       , WITHSSH = withssh, SSHPREFIX = sshprefix, SSHSUFFIX = sshsuffix, FIRSTONLY = firstonly, _extra = ex
+
+  compile_opt idl2, strictarrsubs
+
+@cm_4cal
+@cm_4data
+  
+  IF n_params() NE 5 THEN return, report('rseries_ncdf needs 5 parameters')
+  IF n_elements(sshprefix) EQ 0 THEN sshprefix = prefix
+  IF n_elements(sshsuffix) EQ 0 THEN sshsuffix = suffix
+
+; take care ofd the precision of the calendar parameters... 
+  CASE size(date1, /type)  OF
+    1:date1 = long(date1)
+    2:date1 = long(date1)
+    3:
+    4:date1 = double(date1)
+    5:
+  ENDCASE
+  CASE size(date2, /type)  OF
+    1:date2 = long(date2)
+    2:date2 = long(date2)
+    3:
+    4:date2 = double(date2)
+    5:
+  ENDCASE
+  date2p1 = jul2date(date2jul(date2)+1)
+  res = -1
+  tseries = -1
+  ndims = -1
+  flist = ''
+
+  date = date1
+  WHILE date LE date2 DO BEGIN
+
+    filename = rseries_getname(prefix, suffix, date, dd1, dd2, datefmt = datefmt)
+    IF filename EQ '' THEN return, -1
+    time = ncdf_gettime(filename)
+    IF jpt EQ 1 AND time[0] LT 0 THEN return, -1
+    jdate = date2jul(date) ; do it after ncdf_gettime to be sure to use the proper calendar...
+    dummy = where(time GE jdate, cnt)
+    IF cnt NE 0 THEN BEGIN
+print, file_basename(filename)
+      IF keyword_set(fileslist) THEN BEGIN 
+        vlist = ncdf_listvars(filename)
+        IF (where(vlist EQ var_name))[0] NE -1 THEN flist = [flist, filename] ELSE BEGIN 
+          print, var_name + 'not found in the file ' + filename
+          return, ''
+        ENDELSE 
+      ENDIF ELSE BEGIN 
+        IF keyword_set(withssh) THEN BEGIN
+          sshfilename = rseries_getname(sshprefix, sshsuffix, date, datefmt = datefmt)
+          IF sshfilename EQ '' THEN return, -1
+print, file_basename(sshfilename)
+          IF keyword_set(firstonly) THEN ssh = read_ncdf(withssh, filename = sshfilename, /nostruct) $
+          ELSE ssh = read_ncdf(withssh, date, date2, filename = sshfilename, /nostruct)
+        ENDIF
+        IF keyword_set(firstonly) THEN tmp = read_ncdf(var_name, filename = filename, /nostruct, ssh = ssh, _extra = ex) $
+        ELSE tmp = read_ncdf(var_name, date, date2, filename = filename, /nostruct, ssh = ssh, _extra = ex)
+        IF n_elements(tmp) EQ 1 AND tmp[0] EQ -1 THEN return, -1
+; store the shape of the data    
+        sz = size(tmp)
+        IF sz[0] GT ndims THEN BEGIN
+          ndims = sz[0]
+          IF ndims EQ 0 THEN shape = [-1] ELSE BEGIN 
+            IF jpt EQ 1 THEN shape = [sz[1:ndims], 1] ELSE shape = sz[1:ndims]
+          ENDELSE 
+        ENDIF
+; concatenate the arrays (as vectors)  
+        res = [temporary(res), (temporary(tmp))[*]]
+        tseries = [temporary(tseries), time]
+      ENDELSE 
+    ENDIF
+      
+    date = jul2date(date2jul(dd2)+1)
+
+  ENDWHILE
+;
+  IF keyword_set(fileslist) THEN BEGIN 
+    IF n_elements(flist) EQ 1 THEN return, '' ELSE return, flist[1:*]
+  ENDIF
+  IF n_elements(res) EQ 1 THEN return, -1
+;
+  res = (temporary(res))[1:*]
+  time = tseries[1:*]
+  jpt = n_elements(time)
+  IF jpt EQ 1 THEN BEGIN 
+    IF n_elements(shape) NE 1 THEN shape = shape[0:n_elements(shape)-2]
+  ENDIF ELSE shape[n_elements(shape)-1] = jpt
+  IF shape[0] EQ -1 THEN res = res[0] ELSE res = reform(res, shape, /overwrite)
+;
+  IF keyword_set(nostruct) THEN return, res
+  IF keyword_set(key_forgetold) THEN BEGIN
+    return, {arr:temporary(res), grid:vargrid, unit:varunit, experiment:varexp, name:varname}
+  ENDIF ELSE BEGIN
+    return, {tab:temporary(res), grille:vargrid, unite:varunit, experience:varexp, nom:varname}
+  ENDELSE
+
+  RETURN, res
+END
