source: trunk/src/t2m_correction_ncdf.pro

Last change on this file was 205, checked in by pinsard, 10 years ago

website not any more hosted by LOCEAN

  • Property svn:keywords set to URL
File size: 10.0 KB
Line 
1;+
2;
3; =======================
4; t2m_correction_ncdf.pro
5; =======================
6;
7; .. function:: t2m_correction_ncdf(yyyymmddb,yyyymmdde)
8;
9; DESCRIPTION
10; ===========
11;
12; Mean correction for air temperature bias and correction for variability are
13; applied.
14;
15; :file:`${PROJECT_ID}/erai_t2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
16; containing
17; air temperature at 2 m height from ERA-I interpolated on OAFLUX grid
18; has been produced by
19; :func:`interp_erai_t2m`.
20;
21; Corrected air temperature at 2 m height
22; is written in
23; :file:`${PROJECT_OD}/TropFlux_t2m_{yyyymmdd}_{yyyymmdd}.nc`
24; if this file not already exists.
25;
26; This output file
27; :file:`${PROJECT_OD}/TropFlux_t2m_{yyyymmdd}_{yyyymmdd}.nc`
28; will be used by
29; :func:`tropflux`.
30;
31; .. only:: man
32;
33;    Figure is visible on PDF and HTML documents only.
34;
35; .. only:: html or latex
36;
37;     .. graphviz::
38;
39;        digraph t2m_correction_ncdf {
40;
41;           file_in [shape=ellipse,fontname=Courier,label="${PROJECT_OD}/erai_t2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc"];
42;           file_out [shape=ellipse,fontname=Courier,label="${PROJECT_OD}/TropFlux_t2m_{yyyymmdd}_{yyyymmdd}.nc"];
43;
44;           t2m_correction_ncdf [shape=box,
45;           fontname=Courier,
46;           color=blue,
47;           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/t2m_correction_ncdf.pro",
48;           label="${PROJECT}/src/t2m_correction_ncdf.pro"];
49;
50;           {file_in} -> {t2m_correction_ncdf} -> {file_out}
51;
52;        }
53;
54; SEE ALSO
55; ========
56;
57; :ref:`mooring_corrections`
58;
59; Used by :ref:`tropflux.sh`
60;
61; :ref:`project_profile.sh`
62;
63; :func:`interp_erai_t2m`
64;
65; :func:`initncdf <saxo:initncdf>`
66; :func:`read_ncdf <saxo:read_ncdf>`
67; :func:`grossemoyenne <saxo:grossemoyenne>`
68; :func:`caldat <saxo:caldat>`
69; :func:`julday <saxo:julday>`
70; :func:`jul2date <saxo:jul2date>`
71; :func:`ncdf_quickwrite <saxo:ncdf_quickwrite>`
72; :func:`ncdf_getatt <saxo:ncdf_getatt>`
73;
74; EXAMPLES
75; ========
76;
77; .. code-block:: idl
78;
79;    yyyymmddb = 20000101L
80;    yyyymmdde = 20001231L
81;    result = t2m_correction_ncdf(yyyymmddb, yyyymmdde)
82;    print, result
83;
84; TODO
85; ====
86;
87; note .5 in time value ... is this correct ? do others files
88; present the same feature ?
89;
90; .. code-block:: bash
91;
92;    ncks -v time -d time,0,1 /usr/work/incas/fplod/tropflux_d/TropFlux_t2m_19890101_20091231.nc
93;
94; .. parsed-literal::
95;
96;    time: type NC_DOUBLE, 1 dimension, 3 attributes, chunked? no, compressed? no, packed? no, ID = 3
97;    time RAM size is 7670*sizeof(NC_DOUBLE) = 7670*8 = 61360 bytes
98;    time dimension 0: time, size = 7670 NC_DOUBLE, dim. ID = 2 (CRD)(REC)
99;    time attribute 0: units, size = 30 NC_CHAR, value = days since 1950-01-01 00:00:00
100;    time attribute 1: long_name, size = 9 NC_CHAR, value = Time axis
101;    time attribute 2: time_origin, size = 20 NC_CHAR, value = 1950-JAN-01 00:00:00
102;
103;    time[0]=14244.5 days since 1950-01-01 00:00:00
104;    time[1]=14245.5 days since 1950-01-01 00:00:00
105;
106; coding rules
107;
108; understand usage of jtt
109;
110; check time values
111;
112; hard coded correction values
113;
114; check side effect of replacement of read_ncdf by ncdf_lec :
115; Probleme d'adequation entre les tailles du domaine nx*ny*jpt 350*60*1 et du tableau 350*60*7670
116;
117; hard coded attributes
118;
119; CF conventions
120;
121; KNOWN ISSUES
122; ============
123;
124; test of existence of fullfilename_msk not very efficient because
125; MUST_EXIST keyword of :func:`isafile <saxo:isafile>` not yet implemented
126;
127; EVOLUTIONS
128; ==========
129;
130; $Id: t2m_correction_ncdf.pro 88 2011-08-19 15:40:14Z pinsard $
131;
132; $URL$
133;
134; - fplod 20120322
135;
136;   * taking project_overwrite into account
137;   * try to add compile_opt seems to be incompatible with ncdf_quickwrite
138;   * pro -> function
139;   * hard coded da1 and da2 replaced by yyyymmddb and yyyymmdde parameters
140;   * replace t2m=ncdf_lec(... by a t2m=read_ncdf(
141;
142; - fplod 20110822T112525Z aedon.locean-ipsl.upmc.fr (Darwin)
143;
144;   * correction for time values in output file
145;
146; - fplod 20110808T130628Z aedon.locean-ipsl.upmc.fr (Darwin)
147;
148;   * remove v50 in filename output
149;   * replace _ID by _OD (like other correction modules)
150;
151; - fplod 20110104T093758Z aedon.locean-ipsl.upmc.fr (Darwin)
152;
153;   * complete header
154;   * replace /Volumes/Iomega_HDD/TropFlux/input_uncor/ by ${TROPFLUX_ID}
155;   * replace /Volumes/Iomega_HDD/TropFlux/input_cor/full_cor/ by
156;     ${TROPFLUX_OD}
157;   * use :func:`ncdf_getatt <saxo:ncdf_getatt>` for attributes of lat and long
158;     variables
159;   * same problem of time axis and memory like in interp_erai_t2m_1989_2009
160;     using read_ncdf.
161;     replace read_ncdf by netcdf_lec
162;
163; - fplod 20101215T114503Z aedon.locean-ipsl.upmc.fr (Darwin)
164;
165;   * add graph in header
166;
167; - fplod 20101214T093615Z aedon.locean-ipsl.upmc.fr (Darwin)
168;
169;   * minimal header
170;
171; - pbk 2008
172;
173;   * creation
174;
175;-
176function t2m_correction_ncdf $
177, yyyymmddb $
178, yyyymmdde
179;
180;++compile_opt idl2, strictarrsubs, logical_predicate
181;
182@cm_4cal
183@cm_4data
184@cm_4mesh
185@cm_4data
186@cm_project
187;
188; Return to caller if errors
189ON_ERROR, 2
190;
191result = -1
192;
193usage = 'result = sst_correction_ncdf(yyyymmddb, yyyymmdde)'
194nparam = N_PARAMS()
195IF (nparam NE 2) THEN BEGIN
196    ras = report(['Incorrect number of arguments.' $
197          + '!C' $
198          + 'Usage : ' + usage])
199    return, result
200ENDIF
201
202; test if ${PROJECT_OD} defined
203CASE project_id_env OF
204    '' :  BEGIN
205        msg = 'eee : ${PROJECT_OD} is not defined'
206        ras = report(msg)
207        return, result
208    END
209    ELSE : BEGIN
210        msg = 'iii : ${PROJECT_OD} is ' + project_id_env
211        ras = report(msg)
212    END
213ENDCASE
214;
215; check if output data will be possible
216iodirout = isadirectory(project_od_env)
217;
218; existence and protection for reading
219IF (FILE_TEST(iodirout, /DIRECTORY, /EXECUTABLE, /READ) EQ 0) THEN BEGIN
220    msg = 'eee : the directory' + iodirout  + ' is not accessible.'
221    ras = report(msg)
222    return, result
223ENDIF
224;
225; existence and protection for writing
226IF (FILE_TEST(iodirout, /DIRECTORY, /WRITE) EQ 0) THEN BEGIN
227    msg = 'eee : the directory' + iodirout  + ' was not found.'
228    ras = report(msg)
229    return, result
230ENDIF
231;
232; build uncorrected t2 filename
233filename_t2_uncor = 'erai_t2m_' $
234    + string(yyyymmddb,format='(i8.8)') $
235    + '_' $
236    + string(yyyymmdde,format='(i8.8)') $
237    + '_oafluxgrid.nc'
238;
239; check if this file exists
240msg='iii : looking for ' + filename_t2_uncor
241ras = report(msg)
242fullfilename_t2_uncor = isafile(iodirout + filename_t2_uncor, NEW=0, /MUST_EXIST)
243IF fullfilename_t2_uncor[0] EQ '' THEN BEGIN
244   msg = 'eee : the file ' + fullfilename_t2_uncor + ' was not found.'
245   ras = report(msg)
246   return, result
247ENDIF
248;
249; build output filename
250filename_out = 'TropFlux_t2m_' $
251    + string(yyyymmddb,format='(i8.8)') $
252    + '_' $
253    + string(yyyymmdde,format='(i8.8)') $
254    + '.nc'
255;
256fullfilename_out = iodirout + filename_out
257; in order to avoid unexpected overwritten
258IF ((FILE_TEST(fullfilename_out) EQ 1)  AND (project_overwrite EQ 0)) THEN BEGIN
259    msg = 'eee : the file ' + fullfilename_out  + ' already exists.'
260    ras = report(msg)
261    return, result
262ENDIF
263;
264; define grid parameters with t2 file
265initncdf, fullfilename_t2_uncor
266;
267; get time in t2 file
268timein=ncdf_lec(fullfilename_t2_uncor,var='time')
269jptin=n_elements(timein)
270print, 'time steps from data ', jptin
271print, 'The first 10 time values (variable timein) = ', timein[0:9]
272;
273; find first and last dates yyyymmdd
274da=jul2date(julday(01, 01, 1957,timein[0]))
275cda0=string(da,format='(i8.8)')
276da=jul2date(julday(01, 01, 1957,timein[jptin-1]))
277cda1=string(da,format='(i8.8)')
278print, 'for time checking first date ', cda0
279print, 'for time checking last date ' , cda1
280;
281; read t2 data
282initncdf, fullfilename_t2_uncor
283t2m=read_ncdf('t2m',yyyymmddb-.5d,yyyymmdde,file=fullfilename_t2_uncor,/nostr)
284timein=24.d*(time-julday(1,1,1957,0,0,0))
285jpt=n_elements(timein)
286da=jul2date(time[0])
287cda0=string(da,format='(i8.8)')
288da=jul2date(time[jpt-1])
289cda1=string(da,format='(i8.8)')
290print, 't2m in t2m_correction_ncdf first date ', cda0
291print, 't2m in t2m_correction_ncdf last date ' , cda1
292;
293t2m=t2m-273.15
294help, t2m
295;
296jpt=jptin
297t2m_mean=grossemoyenne(t2m,'t',/nan)
298help, t2m_mean
299;
300t2m_m=t2m*0.
301bias_cor=t2m*0.
302;
303; line fit ->  -0.0755589x + 1.71090   ; (2000-2008)
304; line fit ->  -0.0741607x + 1.67601   ; (2000-2009)
305;
306for jt=0,jptin-1 do begin
307    caldat, julday(01, 01, 1957,timein[jt]),mon,day,yea
308    ;++print,day
309    jtt=(julday(01, 01, 1957,timein[jt])-julday(1,1,yea)) < 364
310    t=reform(t2m_mean[*,*])
311    t2m_m[*,*,jt]=t
312    bias_cor[*,*,jt]=(((t-22.6)*0.4004896/(28-22.6)) > 0.) <0.4     ; (2000-2009)
313endfor
314help, t2m_m,bias_cor
315;
316t2m_ano=t2m-t2m_m
317;
318; correction for mean based on scatter
319;
320help, t2m_ano
321;
322; applying the correction for variability based on the scatter
323;t2m_ano=t2m_ano*(1/0.916484)    ; (2000-2008)
324t2m_ano=t2m_ano*(1/0.918085)     ; (2000-2009)
325;
326; applying the correction for variability based on the scatter
327t2m_m=t2m_m+bias_cor
328;
329t2m_new=t2m_m+t2m_ano+273.15
330help, t2m_new
331;
332;writing field
333lat=reform(gphit[0,0:jpj-1])
334lon=reform(glamt[0:jpi-1,0])
335;
336ncfile='!' + fullfilename_out
337;
338ncdf_getatt, fullfilename_t2_uncor, 'longitude', units=units
339ncdf_getatt, fullfilename_t2_uncor, 'longitude', long_name=long_name
340lon_attr={units:units, long_name:long_name}
341ncdf_getatt, fullfilename_t2_uncor, 'latitude', units=units
342ncdf_getatt, fullfilename_t2_uncor, 'latitude', long_name=long_name
343lat_attr={units:units, long_name:long_name}
344time_attr={units:'days since 1950-01-01 00:00:00',long_name:'Time axis',time_origin:'1950-JAN-01 00:00:00'}
345globattr={source:'Basic data obtained from ERAI.  Mean correction for air temperautre bias and correction for variability are applied',timerange:cda0+' - '+cda1}
346ncdf_getatt, fullfilename_t2_uncor, 't2m', units=units
347ncdf_getatt, fullfilename_t2_uncor, 't2m', long_name=long_name
348t2m_attr={units:units,missing_value:1.e20,long_name:long_name,short_name:'t2m',axis:'TYX'}
349;
350ncfields = 't2m[longitude,latitude,*time]=t2m_new:t2m_attr; ' $
351                      + 'longitude[]=lon:lon_attr; ' $
352                      + 'latitude[]=lat:lat_attr; ' $
353                      + 'time[]=timein:time_attr ' $
354                      + ' @ globattr'
355;
356@ncdf_quickwrite
357;
358result = 0
359return, result
360;
361end
Note: See TracBrowser for help on using the repository browser.