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