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

Last change on this file since 495 was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

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