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