source: trunk/SRC/ReadWrite/rseries_ncdf.pro @ 463

Last change on this file since 463 was 463, checked in by smasson, 13 years ago

introduce rseries_ncdf

File size: 6.3 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6;
7; @param VAR_NAME {in}{required}{type=string}
8; name of the variable to be read.
9;
10; @param date1 {in}{required}{type=scalar, long interger or double precision}
11; date given in the yyyymmdd format. data will be read from date1
12;
13; @param date2 {in}{required}{type=scalar, long interger or double precision}
14; date given in the yyyymmdd format. data will be read until date1
15;
16; @param prefix {in}{required}{type=string}
17; a string defining the prefix used to identify the file.
18;
19; @param suffix {in}{required}{type=string}
20; a string defining the suffix used to identify the file.
21; note that *.nc is added to suffix is it is not finishing with ".nc'
22;
23; @keyword DATEFMT {default='i8_i8'}{type=scalar string}
24; a string defining the date format used in the file name. the
25; possible coices are:
26;   - 'i8_i8': yyyymmdd_yyyymmdd (for ex. 20110401_20110431)
27;   - 'i6_i6': yyyymm_yyyymm (for ex. 201104_201104). we assume date1(date2) = beginning(end) of the month
28;   - 'i4_i4': yyyy_yyyy (for ex. 2011_2011). we assume date1(date2) = beginning(end) of the year
29;   - 'i8': yyyymmdd (for ex. 20110401) we assume date2 = date1
30;   - 'i6': yyyymm (for ex. 201104) we assume date1(date2) = beginning(end) of the month
31;   - 'i4': yyyymm (for ex. 2011) we assume date1(date2) = beginning(end) of the year
32;   - 'yi4mi2di2_yi4mi2di2': yxxxxmxxdxx_yxxxxmxxdxx (for ex. y2011m04d01_y2011m04d31)
33;   - 'yi4mi2_yi4mi2': yxxxxmxx_yxxxxmxx (for ex. y2011m04_y2011m04) we assume date1(date2) = beginning(end) of the month
34;   - 'yi4_yi4': yxxxx_yxxxx (for ex. y2011_y2011) we assume date1(date2) = beginning(end) of the year
35;   - 'yi4mi2di2': yxxxxmxxdxx (for ex. y2011m04d01) we assume date2 = date1
36;   - 'yi4mi2': yxxxxmxx (for ex. y2011m04) we assume date1(date2) = beginning(end) of the month
37;   - 'yi4': yxxxx (for ex. y2011) we assume date1(date2) = beginning(end) of the year
38;
39; @keyword NOSTRUCT {default=0}{type=scalar: 0 or 1}
40; We activate it if we do not want that read_ncdf send back a structure
41; but only the array referring to the field.
42;
43; @keyword _EXTRA
44; Used to pass keywords to <pro>rseries_getname</pro> and <pro>read_ncdf</pro>
45;
46; @returns
47; Structure readable by <pro>litchamp</pro> or an array if NOSTRUCT is
48; activated.
49;
50; @uses
51;   <pro>cm_4cal</pro>
52;   <pro>cm_4data</pro>
53;   <pro>jul2date</pro>
54;   <pro>date2jul</pro>
55;   <pro>report</pro>
56;   <pro>rseries_getname</pro>
57;   <pro>read_ncdf</pro>
58;
59; @restrictions
60; see <pro>rseries_getname</pro> and <pro>read_ncdf</pro>
61;
62; @examples
63;  IDL> a = rseries_ncdf('zos', 100101,121231,'tr33beta','1Y_grid_')
64;  IDL> a = rseries_ncdf('zos', 100101,121231,'tr33beta','1Y_grid_',direc='xy',/nostruct)
65;
66; @history
67; 2011-04: Creation. Sebastien Masson (smasson\@locean-ipsl.upmc.fr)
68;
69; @version
70; $Id$
71;
72;-
73FUNCTION rseries_ncdf, var_name, date1, date2, prefix, suffix, NOSTRUCT = nostruct, DATEFMT = datefmt, FILESLIST = fileslist $
74                       , WITHSSH = withssh, SSHPREFIX = sshprefix, SSHSUFFIX = sshsuffix, FIRSTONLY = firstonly, _extra = ex
75
76  compile_opt idl2, strictarrsubs
77
78@cm_4cal
79@cm_4data
80 
81  IF n_params() NE 5 THEN return, report('rseries_ncdf needs 5 parameters')
82  IF n_elements(sshprefix) EQ 0 THEN sshprefix = prefix
83  IF n_elements(sshsuffix) EQ 0 THEN sshsuffix = suffix
84
85; take care ofd the precision of the calendar parameters...
86  CASE size(date1, /type)  OF
87    1:date1 = long(date1)
88    2:date1 = long(date1)
89    3:
90    4:date1 = double(date1)
91    5:
92  ENDCASE
93  CASE size(date2, /type)  OF
94    1:date2 = long(date2)
95    2:date2 = long(date2)
96    3:
97    4:date2 = double(date2)
98    5:
99  ENDCASE
100  date2p1 = jul2date(date2jul(date2)+1)
101  res = -1
102  tseries = -1
103  ndims = -1
104  flist = ''
105
106  date = date1
107  WHILE date LE date2 DO BEGIN
108
109    filename = rseries_getname(prefix, suffix, date, dd1, dd2, datefmt = datefmt)
110    IF filename EQ '' THEN return, -1
111    time = ncdf_gettime(filename)
112    IF jpt EQ 1 AND time[0] LT 0 THEN return, -1
113    jdate = date2jul(date) ; do it after ncdf_gettime to be sure to use the proper calendar...
114    dummy = where(time GE jdate, cnt)
115    IF cnt NE 0 THEN BEGIN
116print, file_basename(filename)
117      IF keyword_set(fileslist) THEN BEGIN
118        vlist = ncdf_listvars(filename)
119        IF (where(vlist EQ var_name))[0] NE -1 THEN flist = [flist, filename] ELSE BEGIN
120          print, var_name + 'not found in the file ' + filename
121          return, ''
122        ENDELSE
123      ENDIF ELSE BEGIN
124        IF keyword_set(withssh) THEN BEGIN
125          sshfilename = rseries_getname(sshprefix, sshsuffix, date, datefmt = datefmt)
126          IF sshfilename EQ '' THEN return, -1
127print, file_basename(sshfilename)
128          IF keyword_set(firstonly) THEN ssh = read_ncdf(withssh, filename = sshfilename, /nostruct) $
129          ELSE ssh = read_ncdf(withssh, date, date2, filename = sshfilename, /nostruct)
130        ENDIF
131        IF keyword_set(firstonly) THEN tmp = read_ncdf(var_name, filename = filename, /nostruct, ssh = ssh, _extra = ex) $
132        ELSE tmp = read_ncdf(var_name, date, date2, filename = filename, /nostruct, ssh = ssh, _extra = ex)
133        IF n_elements(tmp) EQ 1 AND tmp[0] EQ -1 THEN return, -1
134; store the shape of the data   
135        sz = size(tmp)
136        IF sz[0] GT ndims THEN BEGIN
137          ndims = sz[0]
138          IF ndims EQ 0 THEN shape = [-1] ELSE BEGIN
139            IF jpt EQ 1 THEN shape = [sz[1:ndims], 1] ELSE shape = sz[1:ndims]
140          ENDELSE
141        ENDIF
142; concatenate the arrays (as vectors) 
143        res = [temporary(res), (temporary(tmp))[*]]
144        tseries = [temporary(tseries), time]
145      ENDELSE
146    ENDIF
147     
148    date = jul2date(date2jul(dd2)+1)
149
150  ENDWHILE
151;
152  IF keyword_set(fileslist) THEN BEGIN
153    IF n_elements(flist) EQ 1 THEN return, '' ELSE return, flist[1:*]
154  ENDIF
155  IF n_elements(res) EQ 1 THEN return, -1
156;
157  res = (temporary(res))[1:*]
158  time = tseries[1:*]
159  jpt = n_elements(time)
160  IF jpt EQ 1 THEN BEGIN
161    IF n_elements(shape) NE 1 THEN shape = shape[0:n_elements(shape)-2]
162  ENDIF ELSE shape[n_elements(shape)-1] = jpt
163  IF shape[0] EQ -1 THEN res = res[0] ELSE res = reform(res, shape, /overwrite)
164;
165  IF keyword_set(nostruct) THEN return, res
166  IF keyword_set(key_forgetold) THEN BEGIN
167    return, {arr:temporary(res), grid:vargrid, unit:varunit, experiment:varexp, name:varname}
168  ENDIF ELSE BEGIN
169    return, {tab:temporary(res), grille:vargrid, unite:varunit, experience:varexp, nom:varname}
170  ENDELSE
171
172  RETURN, res
173END
Note: See TracBrowser for help on using the repository browser.