source: trunk/src/interp_erai_lwr.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: 11.2 KB
Line 
1;+
2;
3;
4; ===================
5; interp_erai_lwr.pro
6; ===================
7;
8; .. function:: interp_erai_lwr(yyyymmddb,yyyymmdde, eraitype)
9;
10; DESCRIPTION
11; ===========
12;
13; Interpolation of str from ERA-I grid to OAFLUX grid
14;
15; If eraitype is set to 1,
16; :file:`${PROJECT_ID}/20c3m_erai_str_TROP_1989_2009.nc`
17; containing str 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_t2m_{yyyyb}_{yyyye}.nc`
23; containing
24; lwr 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 mask
30; has been produced by
31; :func:`oaflux_mask_30n30s`.
32;
33; Interpolated str
34; is written in
35; :file:`${PROJECT_OD}/erai_lwr_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
36; if this file not already exists.
37;
38; This output file
39; :file:`${PROJECT_OD}/erai_lwr_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
40; must be processed after by
41; :ref:`add_19890101.sh`.
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_lwr {
52;
53;           file_in_erai1 [shape=ellipse,
54;           fontname=Courier,
55;           label="${PROJECT_ID}/20c3m_erai_str_TROP_1989_2009.nc"];
56;           file_in_erai2 [shape=ellipse,
57;           fontname=Courier,
58;           label="${PROJECT_ID}/erai_lwr_{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_lwr_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc"];
65;
66;           interp_erai_lwr [shape=box,
67;           fontname=Courier,
68;           color=blue,
69;           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/interp_erai_lwr.pro",
70;           label="${PROJECT}/src/interp_erai_lwr.pro"];
71;
72;           {file_in_erai1 file_in_erai2 mask} -> {interp_erai_lwr} -> {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; :ref:`add_19890101.sh`
98;
99; EXAMPLES
100; ========
101;
102; With ERA-I type 1:
103;
104; .. code-block:: idl
105;
106;    .compile file_interp
107;    eraitype = 1
108;    yyyymmddb = 20000101L
109;    yyyymmdde = 20001231L
110;    result = interp_erai_lwr(yyyymmddb, yyyymmdde, eraitype)
111;    print, result
112;
113; With ERA-I type 2:
114;
115; .. code-block:: idl
116;
117;    .compile file_interp
118;    eraitype = 2
119;    yyyymmddb = 20000101L
120;    yyyymmdde = 20001231L
121;    result = interp_erai_lwr(yyyymmddb, yyyymmdde, eraitype)
122;    print, result
123;
124; TODO
125; ====
126;
127; lwr first date 19991231 vs yyyymmddb = 20000101L
128;
129; make it work : pb on loholt1 idl8:
130;
131; reach end but bad values (all NaN)
132;
133; make it work : pb on loholt1 idl6:
134;
135; .. parsed-literal::
136;
137;    lwr[longitude,latitude,time]=lwrout:lwr_attr; longitude[]=xlon:lon_attr; latitude[]=ylat:lat_attr; tt[*time]=tt:time_attr  @ globattr
138;    % NCDF_VARPUT: Operation Failed, bad file (4) or variable [0] id ?
139;                  (NC_ERROR=-31)
140;    % Stop encountered: INTERP_ERAI_LWR_1989_2009    1
141;      /home/pinsard/tropflux_ws/src/interp_erai_lwr_1989_2009.pro
142;
143; check OUTMASK_IND and SET_OUTMSKVAL added to call_interp2d call
144;
145; use real output of :ref:`compute_erai_daily_region_2d.sh`.
146;
147; remove hard coded time
148;
149; 19890101 is not provided in ERA-Interim file str : a possible workaround is
150; to copy 19890102 dataset to 19890101 using nco after this processing.
151; see :ref:`compute_erai_daily_region_2d.sh`
152;
153; check if there is the same issue with 19790101 vs 19790102.
154;
155; coding rules
156;
157; KNOWN ISSUES
158; ============
159;
160; test of existence of fullfilename_msk not very efficient because
161; MUST_EXIST keyword of :func:`isafile <saxo:isafile>` not yet implemented
162;
163; EVOLUTIONS
164; ==========
165;
166; $Id$
167;
168; $URL: svn+ssh://pinsard@forge.ipsl.jussieu.fr/ipsl/forge/projets/tropflux/svn/trunk/src/interp_erai_lwr.pro $
169;
170; - fplod 20120704T113504Z cratos (Linux)
171;
172;   * add eraitype parameter
173;   * deal with time units (days vs hours) and time origin
174;
175; - fplod 20120320
176;
177;   * no more timegen
178;
179; - fplod 20120319
180;
181;   * taking project_overwrite into account
182;   * try to add compile_opt seems to be incompatible with ncdf_quickwrite
183;   * pro -> function
184;
185; - fplod 20110830T145010Z cratos (Linux)
186;
187;   * replace xlon by lon and xlat by lat; suppress extra initncdf call
188;
189; - fplod 20110830T135030Z cratos (Linux)
190;
191;   * replace tt by time
192;
193; - pinsard 2011-08-23T09:09:16Z loholt1.ipsl.polytechnique.fr (Linux)
194;
195;   * retry on loholt1 with idl8 and idl6
196;
197; - fplod 20110819T084310Z aedon.locean-ipsl.upmc.fr (Darwin)
198;
199;   * replace next step lwr_correction_ncdf.pro by :ref:`add_19890101.sh`
200;   * fix output file name to erai_lwr_19890102_20091231_oafluxgrid.nc
201;
202; - pinsard 2011-08-08T16:03:48Z loholt1.ipsl.polytechnique.fr (Linux)
203;
204;   * remove return statement
205;
206; - fplod 20110808T094156Z cratos (Linux)
207;
208;   * add OUTMASK_IND and SET_OUTMSKVAL to call_interp2d call
209;   * fix typo
210;     .. note::
211;
212;        IDL do not warn when asking size of an undefined array:
213;
214;        .. code-block:: idl
215;
216;           must_crash=size(ginette)
217;
218; - pinsard 2011-07-05T07:33:36Z loholt1.ipsl.polytechnique.fr (Linux)
219;
220;   * usage of $PROJECT_ID and $PROJECT_OD
221;
222; - fplod 20101217T140745Z aedon.locean-ipsl.upmc.fr (Darwin)
223;
224;   * remove hard coded directory for mask_oaflux_30N30S.nc
225;
226; - fplod 20101215T094137Z aedon.locean-ipsl.upmc.fr (Darwin)
227;
228;   * add graph in header
229;
230; - fplod 20101214T093615Z aedon.locean-ipsl.upmc.fr (Darwin)
231;
232;   * minimal header
233;
234; - pbk 2008
235;
236;   * creation
237;
238;-
239function interp_erai_lwr $
240         , yyyymmddb $
241         , yyyymmdde $
242         , eraitype
243;
244;++compile_opt idl2, strictarrsubs, logical_predicate
245;
246@cm_4cal
247@cm_4data
248@cm_4mesh
249@cm_4data
250@cm_project
251;
252; Return to caller if errors
253ON_ERROR, 2
254;
255result = -1
256;
257usage = 'result = interp_erai_lwr(yyyymmddb, yyyymmdde, eraitype)'
258nparam = N_PARAMS()
259IF (nparam NE 3) THEN BEGIN
260    ras = report(['Incorrect number of arguments.' $
261         + '!C' $
262         + 'Usage : ' + usage])
263    return, result
264ENDIF
265;
266; check for input directory
267;
268; test if ${PROJECT_ID} defined
269CASE project_id_env OF
270    '' :  BEGIN
271        msg = 'eee : ${PROJECT_ID} is not defined'
272        ras = report(msg)
273        return, result
274    END
275    ELSE : BEGIN
276        msg = 'iii : ${PROJECT_ID} is ' + project_id_env
277        ras = report(msg)
278    END
279ENDCASE
280;
281iodirin = isadirectory(project_id_env)
282;
283; existence and protection of ${PROJECT_ID}
284IF (FILE_TEST(iodirin, /DIRECTORY, /EXECUTABLE, /READ) EQ 0) THEN BEGIN
285    msg = 'eee : the directory' + iodirin  + ' is not accessible.'
286    ras = report(msg)
287    return, result
288ENDIF
289;
290; build mask filename
291filename_msk='mask_oaflux_30N30S.nc'
292;
293; check if this file exists
294msg='iii : looking for ' + filename_msk
295ras = report(msg)
296fullfilename_msk = isafile(iodirin + filename_msk, NEW=0, /MUST_EXIST)
297IF fullfilename_msk[0] EQ '' THEN BEGIN
298    msg = 'eee : the file ' + fullfilename_msk + ' was not found.'
299    ras = report(msg)
300    return, result
301ENDIF
302;
303st=19890101
304en=20090801
305;
306; build data filename
307IF eraitype EQ 1 THEN BEGIN
308    erai_var = 'str'
309    filename='20c3m_erai_str_TROP_1989_2009.nc'
310    tutoday = 24.d
311    tmporig = date2jul(19570101L,month=immo,day=iddo,year=iyyyyo)
312endif
313IF (eraitype eq 2) THEN BEGIN
314    erai_var = 'lwr'
315    tmpb = date2jul(yyyymmddb,month=immb,day=iddb,year=iyyyyb)
316    tmpe = date2jul(yyyymmdde,month=imme,day=idde,year=iyyyye)
317    filename = 'erai_' + erai_var + '_' + string(iyyyyb,format='(I4.4)') + '_' + string(iyyyye,format='(I4.4)') + '.nc'
318    tutoday = 1.d
319    tmporig = date2jul(19500101L,month=immo,day=iddo,year=iyyyyo)
320ENDIF
321;
322; check if this file exists
323msg='iii : looking for ' + filename
324ras = report(msg)
325fullfilename = isafile(iodirin + filename, NEW=0, /MUST_EXIST)
326IF fullfilename[0] EQ '' THEN BEGIN
327    msg = 'eee : the file ' + fullfilename + ' was not found.'
328    ras = report(msg)
329    return, result
330ENDIF
331;
332; test if ${PROJECT_OD} defined
333CASE project_od_env OF
334    '' : BEGIN
335        msg = 'eee : ${PROJECT_OD} is not defined'
336        ras = report(msg)
337        return, result
338    END
339    ELSE : BEGIN
340        msg = 'iii : ${PROJECT_OD} is ' + project_od_env
341        ras = report(msg)
342    END
343ENDCASE
344;
345; check if output data will be possible
346iodirout = isadirectory(project_od_env)
347;
348; existence and protection
349IF (FILE_TEST(iodirout, /DIRECTORY, /WRITE) EQ 0) THEN BEGIN
350    msg = 'eee : the directory' + iodirout  + ' was not found.'
351    ras = report(msg)
352    return, result
353ENDIF
354;
355; build output filename
356filename_out = 'erai_lwr_' + string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '_oafluxgrid.nc'
357fullfilename_out = iodirout + filename_out
358; in order to avoid unexpected overwritten
359IF ((FILE_TEST(fullfilename_out) EQ 1)  AND (project_overwrite EQ 0)) THEN BEGIN
360    msg = 'eee : the file ' + fullfilename_out  + ' already exists.'
361    ras = report(msg)
362    return, result
363ENDIF
364;
365initncdf, fullfilename
366lwrin=read_ncdf(erai_var,yyyymmddb-.5d,yyyymmdde,file=fullfilename,/nostr)
367lwrin=-1*lwrin
368timein=tutoday*(time-julday(immo,iddo,iyyyyo,0,0,0))
369jptin=n_elements(timein)
370da=jul2date(time[0])
371cda0=string(da,format='(i8.8)')
372da=jul2date(time[jpt-1])
373cda1=string(da,format='(i8.8)')
374print, 'lwr first date ', cda0
375print, 'lwr last date ' , cda1
376;
377initncdf, fullfilename
378domdef
379latin=reform(gphit[0,*])
380lonin=reform(glamt[*,0])
381print, 'lat grid ',min(latin),max(latin),latin[1]-latin[0]
382print, 'lon grid ',min(lonin),max(lonin),lonin[1]-lonin[0]
383;
384tab=lwrin[*,*,0]
385mskin=glamt*0.+1.
386;
387initncdf, fullfilename_msk
388domdef
389latout=reform(gphit[0,*])
390lonout=reform(glamt[*,0])
391print, 'lat grid ',min(latout),max(latout),latout[1]-latout[0]
392print, 'lon grid ',min(lonout),max(lonout),lonout[1]-lonout[0]
393mskout=read_ncdf("msk", file=fullfilename_msk,/nostr)
394;
395lwrout=fltarr(jpi,jpj,jptin)
396;
397for jt=0,jptin-1 do begin
398    ; ++print, 'Interpolation jt=',jt,' / ',jptin-1
399    ;
400    tab=reform(lwrin[*,*,jt])
401    lwrout[*,*,jt]=call_interp2d(tab,lonin,latin,mskin $
402    , lonout,latout,method='bilinear' $
403    , OUTMASK_IND=mskout, SET_OUTMSKVAL=mskout)
404    lwrout[*,*,jt]=lwrout[*,*,jt]*mskout+(1.-mskout)*1.e20
405    ;
406endfor
407;
408lat=latout
409lon=lonout
410ncfile='!' + fullfilename_out
411lon_attr={units:'degrees_east',long_name:'Longitude'}
412lat_attr={units:'degrees_north',long_name:'Latitude'}
413time_attr={units:'days since 1950-01-01 00:00:00',long_name:'Time axis',time_origin:'1950-JAN-01 00:00:00'}
414globattr={source:'Tropical ocean fluxes obtained from ERA Interim',timerange:cda0+' - '+cda1}
415lwr_attr={units:'W/m2',missing_value:valmask,long_name:'Surface net longwave radiation ',short_name:'lwr',axis:'TYX'}
416;
417ncfields = 'lwr[longitude,latitude,*time]=lwrout:lwr_attr; ' $
418                      + 'longitude[]=lon:lon_attr; ' $
419                      + 'latitude[]=lat:lat_attr; ' $
420                      + 'time[]=timein:time_attr ' $
421                      + ' @ globattr'
422;
423@ncdf_quickwrite
424;
425result = 0
426return, result
427;
428end
Note: See TracBrowser for help on using the repository browser.