source: trunk/SRC/ToBeReviewed/LECTURE/GRIB/read_grib.pro @ 411

Last change on this file since 411 was 327, checked in by pinsard, 17 years ago

modification of headers : mainly blanks around = sign for keywords in declaration of function and pro

  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1;+
2; @file_comments
3;
4; @categories
5;
6; @param VARCODE
7;
8; @param DATE1 {in}{optional}
9; Date of the beginning (yyyymmdd if TIMESTEP is not activate)
10;
11; @param DATE2 {in}{optional}
12; Last date. Optional, if not specified date2=date1
13;
14; @keyword FILE{type=array or string}
15; A single filename or an array of filenames to be retrieved.
16;
17; @returns
18;
19; @restrictions
20;
21; @examples
22;
23; @history
24;
25; @version
26; $Id$
27;
28;-
29FUNCTION read_grib, varcode, date1, date2, FILE=file
30;
31  compile_opt idl2, strictarrsubs
32;
33@common
34; http://www.wmo.ch/web/www/WDM/Guides/Guide-binary-2.html
35;
36;  gribfile =
37;  '/d1fes2-raid6/SINTEX-common/ES10.d.00/atm/5d/ES10.d.00_5d_00911201_00911230.grib'
38  IF keyword_set(file) THEN gribfile = isafile(file = file,  iodir = iodir) ELSE gribfile = '/d1fes2-raid6/SINTEX-common/ES10/atm/5d/ZOOM_IND/ES10_5d_00210101_00301230.grib'
39;
40  openr, num, gribfile, /GET_LUN, ERROR = err, /SWAP_IF_LITTLE_ENDIAN
41  if err ne 0 then begin
42    ras = report(!err_string)
43    return, -1
44  ENDIF
45;
46  recstart = scan_grib_recstart(num)
47;
48;    messize = scan_grib_messize(num, recstart)
49;    addoff = lonarr(n_elements(recstart))
50;    FOR i = 1L, n_elements(recstart)-1 DO $
51;      addoff[i] = recstart[i]-recstart[i-1]-messize[i-1]
52;
53;
54;    nbits = scan_grib_nbits(num, recstart)
55;    print,nbits[uniq(nbits,sort(nbits))]
56;
57  codes = scan_grib_code(num, recstart)
58  nbcodes =  uniq(codes, sort(codes))
59;
60  dates = scan_grib_date(num, recstart)
61  nbdates = uniq(dates, sort(dates))
62;
63  goodvar = where(codes EQ varcode)
64  IF goodvar[0] EQ -1 THEN BEGIN
65    ras = report( 'no var code '+strtrim(varcode, 2)+' in the file')
66    return, -1
67  ENDIF
68;
69  recstart = recstart[goodvar]
70  dates = dates[goodvar]
71;
72  gooddate = where(dates GE date1 AND dates LE date2)
73  IF gooddate[0] EQ -1 THEN BEGIN
74    ras = report( 'no dates between '+strtrim(date1, 2)+' and '+strtrim(date2, 2)+' in the file')
75    return, -1
76  ENDIF
77  recstart = recstart[gooddate]
78  dates = dates[gooddate]
79  key_caltype = '360d'
80  time = date2jul(dates)
81  jpt = n_elements(time)
82  IF jpt EQ 1 THEN vardate = strtrim(dates[0], 2) ELSE vardate = strtrim(dates[0], 2)+' - '+strtrim(dates[jpt-1], 2)
83;  varname
84  vargrid = 'T'
85;  varexp
86;  varunit
87;
88  grib_pds = read_grib_pds(num, recstart[0])
89; grid parameters
90  IF grib_pds.gdsnotomitted THEN BEGIN
91    grib_gds = read_grib_gds(num, recstart[0])
92; min, max of the latitude with a precision of 10^-2
93    lat1 = fix(100*grib_gds.la1)/100.
94    lat2 = fix(100*grib_gds.la2)/100.
95;
96    CASE grib_gds.gridtype OF
97; Latitude/Longitude Grid
98      0:BEGIN
99        computegrid, grib_gds.lo1, grib_gds.la1, grib_gds.di, -grib_gds.dj $
100          , grib_gds.ni, grib_gds.nj
101       END
102; Gaussian Latitude/Longitude Grid
103      4:BEGIN
104; find the latitude axis
105        CASE 1 OF
106; n48
107          grib_gds.n EQ 48 AND lat1 EQ 88.57 AND lat2 EQ -88.57:$
108            gphit = n48gaussian()
109; n80
110           grib_gds.n EQ 80 AND lat1 EQ 89.14 AND lat2 EQ -89.14:$
111             gphit = n80gaussian()
112; n128
113           grib_gds.n EQ 128 AND lat1 EQ 89.46 AND lat2 EQ -89.46:$
114             gphit = n128gaussian()
115; n160
116           grib_gds.n EQ 160 AND lat1 EQ 89.57 AND lat2 EQ -89.57:$
117             gphit = n160gaussian()
118; n256
119           grib_gds.n EQ 256 AND lat1 EQ 89.73 AND lat2 EQ -89.73:$
120             gphit = n256gaussian()
121; part of one of the gaussian grids defined above
122           ELSE:BEGIN
123             cnt = 0
124             REPEAT BEGIN
125               CASE cnt OF
126                 0:gphit = n48gaussian()
127                 1:gphit = n80gaussian()
128                 2:gphit = n128gaussian()
129                 3:gphit = n160gaussian()
130                 4:gphit = n256gaussian()
131                 5:BEGIN
132                   gphit = n80gaussian()
133                   lat1 = 29.71
134                   lat2 = -19.62
135                 END
136                 ELSE:stop
137               ENDCASE
138               nfix = fix(gphit*100)/100.
139               nlat1 = (where(nfix EQ lat1))[0]
140               nlat2 = (where(nfix EQ lat2))[0]
141               IF nlat1 NE -1 AND  nlat2 NE -1 $
142                 AND nlat2-nlat1+1 EQ grib_gds.nj $
143                 THEN gphit = gphit[nlat1:nlat2] ELSE gphit = -1
144               cnt = cnt+1
145             ENDREP UNTIL gphit[0] NE -1
146           END
147         ENDCASE
148         computegrid, grib_gds.lo1, -1, grib_gds.di, -1, grib_gds.ni, -1, YAXIS = gphit
149       END
150; Mercator Projection Grid
151       gridtype EQ 1:
152; Gnomonic Projection Grid
153       gridtype EQ 2:
154; Lambert Conformal, secant or tangent, conical or bipolar (normal or
155; oblique) Projection Grid
156       gridtype EQ 3:
157; Polar Stereographic Projection Grid
158       gridtype EQ 5:
159; Oblique Lambert conformal, secant or tangent, conical or bipolar,
160; projection
161       gridtype EQ 13:
162; Spherical Harmonic Coefficients
163       gridtype EQ 50:
164; Space view perspective or orthographic grid
165       gridtype EQ 90:
166; reserved - see Manual on Codes
167       ELSE:
168     ENDCASE
169   ENDIF ELSE stop
170;
171   res = fltarr(grib_gds.ni, grib_gds.nj, n_elements(recstart))
172   FOR i = 0, n_elements(recstart)-1 DO BEGIN
173     res[*, *, i] = read_grib_bds(num, recstart[i], grib_gds.ni, grib_gds.nj)
174   ENDFOR
175;
176   free_lun, num
177;
178   IF keyword_set(key_yreverse) THEN res = reverse(res, 2)
179
180   RETURN, res
181 END
Note: See TracBrowser for help on using the repository browser.