source: trunk/src/interp_erai_t2m.pro @ 174

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

bunch of new functions

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