source: trunk/src/interp_erai_t2m.pro @ 156

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

try to add compile_opt but seems to be incompatible with ncdf_quickwrite

  • Property svn:keywords set to Id
File size: 12.6 KB
Line 
1;+
2;
3; ===================
4; interp_erai_t2m.pro
5; ===================
6;
7; .. function interp_erai_t2m(yyyymmddb,yyyymmdde)
8;
9; DESCRIPTION
10; ===========
11;
12; Interpolation of t2 from ERA-I grid to OAFLUX grid
13;
14; :file:`${PROJECT_ID}/20c3m_erai_t2_TROP_1989_2009.nc`
15; containing
16; t2 from ERA-I
17; has been produced by
18; :ref:`compute_erai_daily_region_2d.sh`.
19;
20; :file:`${PROJECT_ID}/mask_oaflux_30N30S.nc`
21; containing OAFLUX grid
22; has been produced by
23; :func:`oaflux_mask_30n30s`.
24;
25; Interpolated t2
26; is written in
27; :file:`${PROJECT_OD}/erai_t2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
28; if this file not already exists.
29;
30; This output file
31; :file:`${PROJECT_OD}/erai_t2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
32; must be processed after by
33; :ref:`t2m_correction_ncdf.pro`.
34;
35; .. only:: man
36;
37;    Figure is visible on PDF and HTML documents only.
38;
39; .. only:: html or latex
40;
41;     .. graphviz::
42;
43;        digraph interp_erai_t2m {
44;
45;           file_in [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/20c3m_erai_t2_TROP_1989_2009.nc"];
46;           mask [shape=ellipse,fontname=Courier,label="${PROJECT_ID}/mask_oaflux_30N30S.nc"];
47;           file_out [shape=ellipse,fontname=Courier,label="${PROJECT_OD}/erai_t2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc"];
48;
49;           interp_erai_t2m [shape=box,
50;           fontname=Courier,
51;           color=blue,
52;           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/interp_erai_t2m.pro",
53;           label="${PROJECT}/src/interp_erai_t2m.pro"];
54;
55;           {file_in mask} -> {interp_erai_t2m} -> {file_out}
56;
57;        }
58;
59; SEE ALSO
60; ========
61;
62; :ref:`interpolate_data`
63;
64; :ref:`project_profile.sh`
65;
66; :ref:`compute_erai_daily_region_2d.sh`
67;
68; :func:`oaflux_mask_30n30s`
69;
70; :func:`report <saxo:report>`
71; :func:`isadirectory <saxo:isadirectory>`
72; :func:`isafile <saxo:isafile>`
73; :func:`initncdf <saxo:initncdf>`
74; :func:`read_ncdf <saxo:read_ncdf>`
75; :func:`ncdf_lec <saxo:ncdf_lec>`
76; :func:`domdef <saxo:domdef>`
77; :func:`call_interp2d <saxo:call_interp2d>`
78; :func:`ncdf_quickwrite <saxo:ncdf_quickwrite>`
79; :func:`ncdf_getatt <saxo:ncdf_getatt>`
80;
81; :ref:`t2m_correction_ncdf.pro`
82;
83; EXAMPLES
84; ========
85;
86; ::
87;
88;  IDL> .compile file_interp
89;  IDL> yyyymmddb = 19890101
90;  IDL> yyyymmdde = 20091231
91;  IDL> result = interp_erai_t2m(yyyymmddb, yyymmdde)
92;  IDL> print, result
93;
94; TODO
95; ====
96;
97; scientific validation (starnge look of data with ncview now)
98;
99; check [yyyymmddb,yyyymmdde] validity
100;
101; check if [yyyymmddb,yyyymmdde] is included in ERA-I file
102;
103; what happen if yyyymmdde > 20091231 : need to read an other ERA-I file
104;
105; add an example with only one month (ie 200912)
106;
107; make it work : pb on loholt1 idl8
108;
109; reach end but bad time and t2m values ::
110;$ ncks -v t2m  -d time,0,1 -d latitude,0,1 -d longitude,0,1 /homedata/pinsard/tropflux_d/erai_t2m_19890101_20091231_oafluxgrid.nc
111;     latitude[0]=-29.5 degrees_north
112;     latitude[1]=-28.5 degrees_north
113;
114;     longitude[0]=30.5 degrees_east
115;     longitude[1]=31.5 degrees_east
116;
117;     time[0]=9.96920996839e+36 latitude[0]=-29.5 longitude[0]=30.5 t2m[0]=9.96921e+36 degK
118;     time[0]=9.96920996839e+36 latitude[0]=-29.5 longitude[1]=31.5 t2m[1]=9.96921e+36 degK
119;     time[0]=9.96920996839e+36 latitude[1]=-28.5 longitude[0]=30.5 t2m[350]=9.96921e+36 degK
120;     time[0]=9.96920996839e+36 latitude[1]=-28.5 longitude[1]=31.5 t2m[351]=9.96921e+36 degK
121;     time[1]=9.96920996839e+36 latitude[0]=-29.5 longitude[0]=30.5 t2m[21000]=9.96921e+36 degK
122;     time[1]=9.96920996839e+36 latitude[0]=-29.5 longitude[1]=31.5 t2m[21001]=9.96921e+36 degK
123;     time[1]=9.96920996839e+36 latitude[1]=-28.5 longitude[0]=30.5 t2m[21350]=9.96921e+36 degK
124;     time[1]=9.96920996839e+36 latitude[1]=-28.5 longitude[1]=31.5 t2m[21351]=9.96921e+36 degK
125;
126;     time[0]=9.96920996839e+36 hours since 1957-01-01 00:00:0.0
127;     time[1]=9.96920996839e+36 hours since 1957-01-01 00:00:0.0
128;
129; loholt1 idl6 : end without error but looking with ncview all brown !
130;
131; strange view (lat and lon shift with ncview) : check grid init
132;
133; remove useless netcdf_read or choose between read_ncdf and ncdf_lec
134;
135; check time values
136;
137; why use :func:`call_interp2d <saxo:call_interp2d>` : this is an hidden function of SAXO
138; see :func:`file_interp <saxo:file_interp>`
139;
140; use as input a file produced by compute_erai_daily_region_2d.sh or
141; get_erai.py + grib to netcdf conversion
142;
143; coding rules
144;
145; hard coded time in module name and in output filename
146;
147; why two "initncdf, fullfilename_msk"
148;
149; hard coded attributes for t2m (missing value, short name, axis) and time (origin) : use ncdf_getatt
150;
151; CF conventions
152;
153; check OUTMASK_IND and SET_OUTMSKVAL added to call_interp2d call
154;
155; KNOWN ISSUES
156; ============
157;
158; test of existence of fullfilename_msk not very efficient because
159; MUST_EXIST keyword of :func:`isafile <saxo:isafile>` not yet implemented
160;
161; EVOLUTIONS
162; ==========
163;
164; $Id$
165;
166; $URL: svn+ssh://pinsard@forge.ipsl.jussieu.fr/ipsl/forge/projets/tropflux/svn/trunk/src/interp_erai_t2m.pro $
167;
168; - fplod 20120306
169;
170;   * try to add compile_opt seems to be incompatible with ncdf_quickwrite
171;   * pro -> function
172;
173; - pinsard 2011-08-23T09:09:16Z loholt1.ipsl.polytechnique.fr (Linux)
174;
175;   * retry on loholt1 with idl8 and idl6
176;
177; - pinsard 2011-08-08T16:11:48Z loholt1.ipsl.polytechnique.fr (Linux)
178;
179;   * remove ncdf_quickwrite exercices
180;
181; - fplod 20110808T094156Z cratos (Linux)
182;
183;   * add OUTMASK_IND and SET_OUTMSKVAL to call_interp2d call
184;
185; - fplod 20110103T153734Z aedon.locean-ipsl.upmc.fr (Darwin)
186;
187;   * computed first and last dates
188;   * start using ncdf_getatt instead of hard coded attributes
189;
190; - fplod 20101223T130406Z aedon.locean-ipsl.upmc.fr (Darwin)
191;
192;   * replace /Volumes/PRAVEEN/TropFlux/input_uncor/ by ${TROPFLUX_OD}
193;   * complete documentation
194;   * get time dimension in data file
195;   * replace read_ncdf by ncdf_lec
196;
197;     usage of timestep keyword in read_ncdf was not efficient because of  memory problem :
198;     can not read the whole time range (ok with 7580, not ok with 7591 while need is 7670
199;     ::
200;
201;       % Unable to allocate memory: to make array.
202;       Cannot allocate memory
203;
204;     When timestep keyword is not used we can see these messages::
205;
206;        /usr/work/incas/fplod/tropflux_d/20c3m_erai_t2_TROP_1989_2009.nc as no time axis variable
207;        given by Praveen
208;
209;        Value of Julian date is out of allowed range
210;
211;        % Array used to subscript array contains out of range subscript: DATAIN
212;
213; - fplod 20101222T163216Z aedon.locean-ipsl.upmc.fr (Darwin)
214;
215;   * replace /Volumes/PRAVEEN/ERAI_global by ${TROPFLUX_ID}
216;
217; - fplod 20101217T140745Z aedon.locean-ipsl.upmc.fr (Darwin)
218;
219;   * remove hard coded directory for mask_oaflux_30N30S.nc
220;
221; - fplod 20101215T112140Z aedon.locean-ipsl.upmc.fr (Darwin)
222;
223;   * add graph in header
224;
225; - fplod 20101214T093615Z aedon.locean-ipsl.upmc.fr (Darwin)
226;
227;   * minimal header
228;
229; - pbk 2008
230;
231;   * creation
232;
233;-
234function interp_erai_t2m $
235         , yyyymmddb $
236         , yyyymmdde
237;
238;++compile_opt idl2, strictarrsubs, logical_predicate
239;
240@cm_4cal
241@cm_4data
242@cm_4mesh
243@cm_4data
244@cm_project
245;
246; Return to caller if errors
247ON_ERROR, 2
248;
249result = -1
250;
251usage = 'result = interp_erai_t2m(yyyymmddb, yyyymmdde)
252nparam = N_PARAMS()
253IF (nparam NE 2) THEN BEGIN
254   ras = report(['Incorrect number of arguments.' $
255         + '!C' $
256         + 'Usage : ' + usage])
257   return, -1
258ENDIF
259
260; check for input directory
261;
262; test if ${PROJECT_ID} defined
263CASE project_id_env OF
264    ''  :  BEGIN
265     msg = 'eee : ${PROJECT_ID} is not defined'
266     ras = report(msg)
267     return, result
268           END
269 ELSE: BEGIN
270     msg = 'iii : ${PROJECT_ID} is ' + project_id_env
271     ras = report(msg)
272       END
273ENDCASE
274;
275iodirin = isadirectory(project_id_env)
276;
277; existence and protection of ${PROJECT_ID}
278IF (FILE_TEST(iodirin, /DIRECTORY, /EXECUTABLE, /READ) EQ 0) THEN BEGIN
279   msg = 'eee : the directory' + iodirin  + ' is not accessible.'
280   ras = report(msg)
281   return, result
282ENDIF
283;
284; build mask filename
285filename_msk='mask_oaflux_30N30S.nc'
286;
287; check if this file exists
288msg='iii : looking for ' + filename_msk
289ras = report(msg)
290fullfilename_msk = isafile(iodirin + filename_msk, NEW=0, /MUST_EXIST)
291IF fullfilename_msk[0] EQ '' THEN BEGIN
292   msg = 'eee : the file ' + fullfilename_msk + ' was not found.'
293   ras = report(msg)
294   return, result
295ENDIF
296;
297; build t2 filename
298filename_t2='20c3m_erai_t2_TROP_1989_2009.nc'
299;
300; check if this file exists
301msg='iii : looking for ' + filename_t2
302ras = report(msg)
303fullfilename_t2 = isafile(iodirin + filename_t2, NEW=0, /MUST_EXIST)
304IF fullfilename_t2[0] EQ '' THEN BEGIN
305   msg = 'eee : the file ' + fullfilename_t2 + ' was not found.'
306   ras = report(msg)
307   return, result
308ENDIF
309;
310; test if ${PROJECT_OD} defined
311CASE project_od_env OF
312  '' : BEGIN
313         msg = 'eee : ${PROJECT_OD} is not defined'
314         ras = report(msg)
315       return, result
316       END
317  ELSE: BEGIN
318          msg = 'iii : ${PROJECT_OD} is ' + project_od_env
319          ras = report(msg)
320        END
321 ENDCASE
322;
323; check if output data will be possible
324iodirout = isadirectory(project_od_env)
325;
326; existence and protection
327IF (FILE_TEST(iodirout, /DIRECTORY, /WRITE) EQ 0) THEN BEGIN
328    msg = 'eee : the directory' + iodirout  + ' was not found.'
329    ras = report(msg)
330    return, result
331ENDIF
332;
333; build output filename
334filename_out = 'erai_t2m_' +  string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '_oafluxgrid.nc'
335fullfilename_out = iodirout + filename_out
336; in order to avoid unexpected overwritten
337IF (FILE_TEST(fullfilename_out) EQ 1) THEN BEGIN
338   msg = 'eee : the file ' + fullfilename_out  + ' already exists.'
339   ras = report(msg)
340   return, result
341ENDIF
342;
343; define grid parameters with t2 file
344initncdf, fullfilename_t2
345domdef
346latin=reform(gphit[0,*])
347lonin=reform(glamt[*,0])
348print, 'lat grid from data',min(latin),max(latin),latin[1]-latin[0]
349print, 'lon grid from data',min(lonin),max(lonin),lonin[1]-lonin[0]
350;
351; get time in t2 file
352timein=ncdf_lec(fullfilename_t2,var='time')
353jptin=n_elements(timein)
354print, 'time steps from data ', jptin
355print, 'The first 10 time values (variable timein) = ', timein[0:9]
356;
357; find first and last dates yyyymmdd
358; they will be written in global attributes of output file
359da=jul2date(julday(01, 01, 1957,timein[0]))
360cda0=string(da,format='(i8.8)')
361da=jul2date(julday(01, 01, 1957,timein[jptin-1]))
362cda1=string(da,format='(i8.8)')
363print, 'first date ', cda0
364print, 'last date ' , cda1
365;
366; read t2 data
367;++ pb memory t2min=read_ncdf("t2",0,jptin-1,/timestep,file=fullfilename_t2,/nostr)
368; the following line is here just to prepare replacement of read_ncdf by ncdf_lec
369t2min_read_ncdf=read_ncdf("t2",0,10,/timestep,file=fullfilename_t2,/nostr)
370help, t2min_read_ncdf
371;++print, 'The first 10 time values (variable time) = ', time[0:9]
372;++print, 'time steps after read_ncdf (variable jpt) ', jpt
373t2min=ncdf_lec(fullfilename_t2,var='t2')
374;
375mskin=glamt*0.+1.
376;
377; define grid parameters with mask file
378initncdf, fullfilename_msk
379domdef
380latout=reform(gphit[0,*])
381lonout=reform(glamt[*,0])
382print, 'lat grid from mask ',min(latout),max(latout),latout[1]-latout[0]
383print, 'lon grid from mask ',min(lonout),max(lonout),lonout[1]-lonout[0]
384mskout=read_ncdf("msk", file=fullfilename_msk,/nostr)
385;
386help, t2min,lonin,latin,mskin,lonout,latout,mskout
387;
388; interpolation and mask
389t2mout=fltarr(jpi,jpj,jptin)
390for jt=0,jptin-1 do begin
391  tab=reform(t2min[*,*,jt])
392  t2mout[*,*,jt]=call_interp2d(tab,lonin,latin,mskin $
393      , lonout,latout,method='bilinear' $
394      , OUTMASK_IND=mskout, SET_OUTMSKVAL=mskout)
395  t2mout[*,*,jt]=t2mout[*,*,jt]*mskout+(1.-mskout)*1.e20
396endfor
397;
398initncdf, fullfilename_msk
399;
400lat=latout
401lon=lonout
402ncfile='!' + fullfilename_out
403ncdf_getatt, fullfilename_msk, 'longitude', units=units
404ncdf_getatt, fullfilename_msk, 'longitude', long_name=long_name
405lon_attr={units:units, long_name:long_name}
406ncdf_getatt, fullfilename_msk, 'latitude', units=units
407ncdf_getatt, fullfilename_msk, 'latitude', long_name=long_name
408lat_attr={units:units, long_name:long_name}
409ncdf_getatt, fullfilename_t2, 'time', units=units
410ncdf_getatt, fullfilename_t2, 'time', long_name=long_name
411time_attr={units:units, long_name:long_name, time_origin:' 1957-JAN-01 00:00:00'}
412globattr={source:'Data are from ECMWF ERA-Interim reanalysis', timerange:cda0+' - '+cda1}
413ncdf_getatt, fullfilename_t2, 't2', units=units
414ncdf_getatt, fullfilename_t2, 't2', long_name=long_name
415t2m_attr={units:units, long_name:long_name, missing_value:1e20, short_name:'t2m',axis:'TYX'}
416;
417help, t2mout
418help, timein
419ncfields = 't2m[longitude,latitude,*time]=t2mout:t2m_attr; ' $
420                      + 'longitude[]=lon:lon_attr; ' $
421                      + 'latitude[]=lat:lat_attr; ' $
422                      + 'time[]=timein:time_attr ' $
423                      + ' @globattr'
424@ncdf_quickwrite
425;
426result = 0
427return, result
428;
429end
Note: See TracBrowser for help on using the repository browser.