source: trunk/src/interp_erai_msl.pro @ 201

Last change on this file since 201 was 191, checked in by pinsard, 12 years ago

introduce new erai dataset : new terminology and new time unit and origin

  • Property svn:keywords set to URL
File size: 10.0 KB
Line 
1;+
2;
3; ===================
4; interp_erai_msl.pro
5; ===================
6;
7; .. function:: interp_erai_msl(yyyymmddb,yyyymmdde, eraitype)
8;
9; DESCRIPTION
10; ===========
11;
12; Interpolation of msl from ERA-I grid to OAFLUX grid
13;
14; If eraitype is set to 1,
15; :file:`${PROJECT_ID}/20c3m_erai_msl_TROP_1989_2009.nc`
16; containing
17; msl from ERA-I
18; has been produced by
19; :ref:`compute_erai_daily_region_2d.sh`.
20;
21; If eraitype is set to 2,
22; :file:`${PROJECT_ID}/erai_msl_{yyyyb}_{yyyye}.nc`
23; containing
24; msl from ERA-I
25; has been produced by
26; :ref:`concat_eraiy.sh`.
27;
28; :file:`${PROJECT_ID}/mask_oaflux_30N30S.nc`
29; containing
30; OAFLUX grid
31; has been produced by
32; :func:`oaflux_mask_30n30s`.
33;
34; Interpolated msl
35; is written in
36; :file:`${PROJECT_OD}/erai_msl_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
37; if this file not already exists.
38;
39; This output file
40; :file:`${PROJECT_OD}/erai_msl_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
41; must be processed after by
42; :func:`d2m_to_q2m_erai`.
43;
44; .. only:: man
45;
46;    Figure is visible on PDF and HTML documents only.
47;
48; .. only:: html or latex
49;
50;     .. graphviz::
51;
52;        digraph interp_erai_msl {
53;
54;           file_in_erai1 [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/20c3m_erai_msl_TROP_1989_2009.nc"];
55;           file_in_erai2 [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/erai_msl_{yyyyb}_{yyyye}.nc"];
56;           mask [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/mask_oaflux_30N30S.nc"];
57;           file_out [shape=ellipse,fontname=Courier,label="${PROJECT_OD}/erai_msl_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc"];
58;
59;           interp_erai_msl [shape=box,
60;           fontname=Courier,
61;           color=blue,
62;           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/interp_erai_msl.pro",
63;           label="${PROJECT}/src/interp_erai_msl.pro"];
64;
65;           {file_in_erai1 file_in_erai2 mask} -> {interp_erai_msl} -> {file_out}
66;
67;        }
68;
69; SEE ALSO
70; ========
71;
72; :ref:`interpolate_data`
73;
74; :ref:`project_profile.sh`
75;
76; Used by :ref:`tropflux.sh`
77;
78; :ref:`compute_erai_daily_region_2d.sh`
79; :ref:`concat_eraiy.sh`
80;
81; :func:`report <saxo:report>`
82; :func:`isadirectory <saxo:isadirectory>`
83; :func:`isafile <saxo:isafile>`
84; :func:`initncdf <saxo:initncdf>`
85; :func:`read_ncdf <saxo:read_ncdf>`
86; :func:`domdef <saxo:domdef>`
87; :func:`call_interp2d <saxo:call_interp2d>`
88; :func:`ncdf_quickwrite <saxo:ncdf_quickwrite>`
89;
90; :func:`d2m_to_q2m_erai`
91;
92; EXAMPLES
93; ========
94;
95; With ERA-I type 1::
96;
97;  .compile file_interp
98;  yyyymmddb = 20000101L
99;  yyyymmdde = 20001231L
100;  eraitype = 1
101;  result = interp_erai_msl(yyyymmddb, yyyymmdde, eraitype)
102;  print, result
103;
104; With ERA-I type 2::
105;
106;  .compile file_interp
107;  yyyymmddb = 20000101L
108;  yyyymmdde = 20001231L
109;  eraitype = 2
110;  result = interp_erai_msl(yyyymmddb, yyyymmdde, eraitype)
111;  print, result
112;
113; TODO
114; ====
115;
116; use real output of :ref:`compute_erai_daily_region_2d.sh` if eraitype is set to 1
117;
118; get_erai.py + grib to netcdf conversion
119;
120; msl_attr={units:'milibars' .. millibar at least, hPa will be probably
121; more SI
122;
123; why 19880101,20100930 as dates in read_ncdf.
124; now (20120319) should be yyyymmddb and yyymmdde but desastrous side effect
125;
126; first step time 14245.5 (days since 1950-01-01 00:00:00) : correct ?
127;
128; KNOWN ISSUES
129; ============
130;
131; test of existence of fullfilename_msk not very efficient because
132; MUST_EXIST keyword of :func:`isafile <saxo:isafile>` not yet implemented
133;
134; EVOLUTIONS
135; ==========
136;
137; $Id: interp_erai_msl.pro 88 2011-08-19 15:40:14Z pinsard $
138;
139; $URL$
140;
141; - fplod 20120704T113504Z cratos (Linux)
142;
143;   * add eraitype parameter
144;   * conversion mPa to hPa
145;   * deal with time units (days vs hours) and time origin
146;
147; - fplod 20120320
148;
149;   * no more timegen
150;
151; - fplod 20120319
152;
153;   * taking project_overwite into account
154;   * pro -> function
155;
156; - pinsard 2011-08-23T09:09:16Z loholt1.ipsl.polytechnique.fr (Linux)
157;
158;   * retry on loholt1 with idl8 and idl6
159;
160; - fplod 20110811T113646Z aedon.locean-ipsl.upmc.fr (Darwin)
161;
162;   * check for units in original file before conversion
163;
164; - fplod 20110809T145539Z aedon.locean-ipsl.upmc.fr (Darwin)
165;
166;   * remove v1 from pro name and file
167;   * header
168;   * usage of ${PROJECT_ID} and ${PROJECT_OD}
169;   * remove return statement
170;   * add test on IO files
171;   * add OUTMASK_IND and SET_OUTMSKVAL to call_interp2d call
172;
173; - pbk 2008
174;
175;   * creation
176;
177;-
178function interp_erai_msl $
179         , yyyymmddb $
180         , yyyymmdde $
181         , eraitype
182;
183;++compile_opt idl2, strictarrsubs, logical_predicate
184;
185@cm_4cal
186@cm_4data
187@cm_4mesh
188@cm_4data
189@cm_project
190;
191; Return to caller if errors
192ON_ERROR, 2
193;
194result = -1
195;
196usage = 'result = interp_erai_msl(yyyymmddb, yyyymmdde, eraitype)'
197nparam = N_PARAMS()
198IF (nparam NE 3) THEN BEGIN
199   ras = report(['Incorrect number of arguments.' $
200   + '!C' $
201   + 'Usage : ' + usage])
202   return, result
203ENDIF
204;
205; check for input directory
206;
207; test if ${PROJECT_ID} defined
208CASE project_id_env OF
209    ''  :  BEGIN
210     msg = 'eee : ${PROJECT_ID} is not defined'
211     ras = report(msg)
212     return, result
213           END
214 ELSE: BEGIN
215     msg = 'iii : ${PROJECT_ID} is ' + project_id_env
216     ras = report(msg)
217       END
218ENDCASE
219;
220iodirin = isadirectory(project_id_env)
221;
222; existence and protection of ${PROJECT_ID}
223IF (FILE_TEST(iodirin, /DIRECTORY, /EXECUTABLE, /READ) EQ 0) THEN BEGIN
224   msg = 'eee : the directory' + iodirin  + ' is not accessible.'
225   ras = report(msg)
226   return, result
227ENDIF
228;
229; build mask filename
230filename_msk='mask_oaflux_30N30S.nc'
231;
232; check if this file exists
233msg='iii : looking for ' + filename_msk
234ras = report(msg)
235fullfilename_msk = isafile(iodirin + filename_msk, NEW=0, /MUST_EXIST)
236IF fullfilename_msk[0] EQ '' THEN BEGIN
237   msg = 'eee : the file ' + fullfilename_msk + ' was not found.'
238   ras = report(msg)
239   return, result
240ENDIF
241;
242; build data filename
243IF eraitype EQ 1 THEN BEGIN
244    erai_var = 'msl'
245    filename='20c3m_erai_msl_TROP_1989_2009.nc'
246    tutoday = 24.d
247    tmporig = date2jul(19570101L,month=immo,day=iddo,year=iyyyyo)
248endif
249IF (eraitype eq 2) THEN BEGIN
250    erai_var = 'msl'
251    tmpb = date2jul(yyyymmddb,month=immb,day=iddb,year=iyyyyb)
252    tmpe = date2jul(yyyymmdde,month=imme,day=idde,year=iyyyye)
253    filename = 'erai_' + erai_var + '_' + string(iyyyyb,format='(I4.4)') + '_' + string(iyyyye,format='(I4.4)') + '.nc'
254    tutoday = 1.d
255    tmporig = date2jul(19500101L,month=immo,day=iddo,year=iyyyyo)
256ENDIF
257;
258; check if this file exists
259msg='iii : looking for ' + filename
260ras = report(msg)
261fullfilename = isafile(iodirin + filename, NEW=0, /MUST_EXIST)
262IF fullfilename[0] EQ '' THEN BEGIN
263   msg = 'eee : the file ' + fullfilename + ' was not found.'
264   ras = report(msg)
265   return, result
266ENDIF ELSE BEGIN
267   msg = 'iii : the file ' + fullfilename + ' found.'
268   ras = report(msg)
269ENDELSE
270;
271; test if ${PROJECT_OD} defined
272CASE project_od_env OF
273  '' : BEGIN
274         msg = 'eee : ${PROJECT_OD} is not defined'
275         ras = report(msg)
276       return, result
277       END
278  ELSE: BEGIN
279          msg = 'iii : ${PROJECT_OD} is ' + project_od_env
280          ras = report(msg)
281        END
282 ENDCASE
283;
284; check if output data will be possible
285iodirout = isadirectory(project_od_env)
286;
287; existence and protection
288IF (FILE_TEST(iodirout, /DIRECTORY, /WRITE) EQ 0) THEN BEGIN
289    msg = 'eee : the directory' + iodirout  + ' was not found.'
290    ras = report(msg)
291    return, result
292ENDIF
293;
294; build output filename
295filename_out = 'erai_msl_' +  string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '_oafluxgrid.nc'
296fullfilename_out = iodirout + filename_out
297; in order to avoid unexpected overwritten
298IF ((FILE_TEST(fullfilename_out) EQ 1)  AND (project_overwrite EQ 0)) THEN BEGIN
299   msg = 'eee : the file ' + fullfilename_out  + ' already exists.'
300   ras = report(msg)
301   return, result
302ENDIF
303;
304initncdf, fullfilename
305domdef
306latin=reform(gphit[0,*])
307lonin=reform(glamt[*,0])
308print, 'lat grid ',min(latin),max(latin),latin[1]-latin[0]
309print, 'lon grid ',min(lonin),max(lonin),lonin[1]-lonin[0]
310mslin=read_ncdf(erai_var,yyyymmddb,yyyymmdde,file=fullfilename,/nostr)
311;
312help, time
313timein=tutoday*(time-julday(immo,iddo,iyyyyo,0,0,0))
314jptin=n_elements(timein)
315da=jul2date(time[0])
316cda0=string(da,format='(i8.8)')
317da=jul2date(time[jpt-1])
318cda1=string(da,format='(i8.8)')
319print, 'msl first date ', cda0
320print, 'msl last date ' , cda1
321;
322tab=mslin[*,*,0]
323mskin=glamt*0.+1.
324;
325initncdf, fullfilename_msk
326domdef
327latout=reform(gphit[0,*])
328lonout=reform(glamt[*,0])
329print, 'lat grid ',min(latout),max(latout),latout[1]-latout[0]
330print, 'lon grid ',min(lonout),max(lonout),lonout[1]-lonout[0]
331mskout=read_ncdf("msk", file=fullfilename_msk,/nostr)
332;
333; conversion to hPa
334ncdf_getatt, fullfilename, erai_var, units=units
335CASE units OF
336    'Pascal': BEGIN
337        mslin=mslin/100
338    END
339    'mPa': BEGIN
340        mslin=mslin*10
341    END
342    ELSE: BEGIN
343        msg = 'eee : ' + units + ' unknown'
344        ras = report(msg)
345        return, result
346    END
347ENDCASE
348;
349help, mslin,lonin,latin,mskin,lonout,latout,mskout
350;
351mslout=fltarr(jpi,jpj,jptin)
352for jt=0,jptin-1 do begin
353  ; ++ print, 'Interpolation jt=',jt,' / ',jptin-1
354  tab=reform(mslin[*,*,jt])
355  mslout[*,*,jt]=call_interp2d(tab,lonin,latin,mskin $
356      , lonout,latout,method='bilinear' $
357      , OUTMASK_IND=mskout, SET_OUTMSKVAL=mskout)
358  mslout[*,*,jt]=mslout[*,*,jt]*mskout+(1.-mskout)*1.e20
359endfor
360;
361initncdf, fullfilename_msk
362;
363lat=latout
364lon=lonout
365ncfile='!' + fullfilename_out
366lon_attr={units:'degrees_east',long_name:'Longitude'}
367lat_attr={units:'degrees_north',long_name:'Latitude'}
368time_attr={units:'days since 1950-01-01 00:00:00',long_name:'Time axis',time_origin:'1950-JAN-01 00:00:00'}
369globattr={source:'Data are from ECMWF ERA-Interim reanalysis', timerange:cda0+' - '+cda1}
370msl_attr={units:'milibars',missing_value:1e20,long_name:'Mean Sea leval pressure',short_name:'msl',axis:'TYX'}
371;
372ncfields = 'msl[longitude,latitude,*time]=mslout:msl_attr; ' $
373                      + 'longitude[]=lon:lon_attr; ' $
374                      + 'latitude[]=lat:lat_attr; ' $
375                      + 'time[]=timein:time_attr ' $
376                      + ' @ globattr'
377;
378@ncdf_quickwrite
379;
380result = 0
381return, result
382;
383end
Note: See TracBrowser for help on using the repository browser.