source: trunk/src/interp_erai_msl.pro

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

fix thanks to coding rules; typo

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