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

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

replace some print by some report in some .pro #2

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