source: trunk/src/t2m_correction_ncdf.pro @ 175

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

an other bunch of new functions

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