source: trunk/src/q2m_correction_ncdf.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 URL
File size: 7.0 KB
Line 
1;+
2;
3; =======================
4; q2m_correction_ncdf.pro
5; =======================
6;
7; .. function:: q2m_correction_ncdf(yyyymmddb,yyyymmdde)
8;
9; DESCRIPTION
10; ===========
11;
12; Correction of q2m on OAFLUX grid
13;
14; :file:`${PROJECT_OD}/erai_q2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
15; containing
16; ++
17; has been produced by
18; :func:`d2m_to_q2m_erai`.
19;
20; Corrected q2m on OAFLUX grid
21; is written in
22; :file:`${PROJECT_OD}/TropFlux_q2m_{yyyymmdd}_{yyyymmdd}.nc`
23; if this file not already exists.
24;
25; This file will be used by
26; :func:`tropflux`.
27;
28; .. only:: man
29;
30;    Figure is visible on PDF and HTML documents only.
31;
32; .. only:: html or latex
33;
34;     .. graphviz::
35;
36;        digraph q2m_correction_ncdf {
37;
38;           file_in [shape=ellipse,fontname=Courier,label="${PROJECT_OD}/erai_q2m_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc"];
39;           file_out [shape=ellipse,fontname=Courier,label="${PROJECT_OD}/TropFlux_q2m_{yyyymmdd}_{yyyymmdd}.nc"];
40;
41;           q2m_correction_ncdf [shape=box,
42;           fontname=Courier,
43;           color=blue,
44;           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/q2m_correction_ncdf.pro",
45;           label="${PROJECT}/src/q2m_correction_ncdf.pro"];
46;
47;           {file_in} -> {q2m_correction_ncdf} -> {file_out}
48;
49;        }
50;
51; SEE ALSO
52; ========
53;
54; :ref:`mooring_corrections`
55;
56; Used by :ref:`tropflux.sh`
57;
58; :ref:`project_profile.sh`
59;
60; :func:`d2m_to_q2m_erai`
61;
62; :func:`initncdf <saxo:initncdf>`
63; :func:`read_ncdf <saxo:read_ncdf>`
64; :func:`grossemoyenne <saxo:grossemoyenne>`
65; :func:`julday <saxo:julday>`
66; :func:`ncdf_quickwrite <saxo:ncdf_quickwrite>`
67;
68; EXAMPLES
69; ========
70;
71; .. code-block:: idl
72;
73;    yyyymmddb = 20000101L
74;    yyyymmdde = 20001231L
75;    result = q2m_correction_ncdf(yyyymmddb, yyyymmdde)
76;    print, result
77;
78; TODO
79; ====
80;
81; make it work :
82;
83; .. parsed-literal:
84;
85;    Writing fields:
86;    q2m[longitude,latitude,time*]=q2m_new:q2m_attr; longitude[]=lon:lon_attr; latitude[]=lat:lat_attr; time[]=timein:time_attr  @ globattr
87;    % NCDF_DIMDEF: Unable to create dimension. (NC_ERROR=-59)
88;    % NCDF_VARDEF: Unable to define variable, bad dimension id .
89;    % NCDF_VARPUT: Variable Inquiry failed, -1 is not a valid variable id.
90;    % Stop encountered: Q2M_CORRECTION_NCDF    1
91;              /.autofs/lodyc/incas/fplod/tropflux/tropflux_ws/src/q2m_correction_ncdf.pro
92;
93; No way ... NaNf also in this output !!
94;
95; coding rules
96;
97; EVOLUTIONS
98; ==========
99;
100; $Id: q2m_correction_ncdf.pro 88 2011-08-19 15:40:14Z pinsard $
101;
102; $URL$
103;
104; - fplod 20120322
105;
106;   * taking project_overwrite into account
107;   * try to add compile_opt seems to be incompatible with ncdf_quickwrite
108;   * pro -> function
109;   * hard coded da1 and da2 replaced by yyyymmddb and yyyymmdde parameters
110;   * get rid of timegen
111;
112; - fplod 20110808T143129Z aedon.locean-ipsl.upmc.fr (Darwin)
113;
114;   * usage of ${PROJECT_OD}
115;   * remove v20 in output filename
116;
117; - fplod 20101215T113945Z aedon.locean-ipsl.upmc.fr (Darwin)
118;
119;   * add graph in header
120;
121; - fplod 20101214T093615Z aedon.locean-ipsl.upmc.fr (Darwin)
122;
123;   * minimal header
124;
125; - pbk 2008
126;
127;   * creation
128;
129;-
130function q2m_correction_ncdf $
131         , yyyymmddb $
132         , yyyymmdde
133;
134;++compile_opt idl2, strictarrsubs, logical_predicate
135;
136@cm_4cal
137@cm_4data
138@cm_4mesh
139@cm_4data
140@cm_project
141;
142; Return to caller if errors
143ON_ERROR, 2
144;
145result = -1
146;
147usage = 'result = q2m_correction_ncdf(yyyymmddb, yyyymmdde)'
148nparam = N_PARAMS()
149IF (nparam NE 2) THEN BEGIN
150    ras = report(['Incorrect number of arguments.' $
151          + '!C' $
152          + 'Usage : ' + usage])
153    return, result
154ENDIF
155
156; test if ${PROJECT_OD} defined
157CASE project_od_env OF
158    '' : BEGIN
159        msg = 'eee : ${PROJECT_OD} is not defined'
160        ras = report(msg)
161        return, result
162     END
163     ELSE : BEGIN
164         msg = 'iii : ${PROJECT_OD} is ' + project_od_env
165         ras = report(msg)
166     END
167ENDCASE
168;
169; check if output data will be possible
170iodirout = isadirectory(project_od_env)
171;
172; existence and protection for reading
173IF (FILE_TEST(iodirout, /DIRECTORY, /EXECUTABLE, /READ) EQ 0) THEN BEGIN
174    msg = 'eee : the directory' + iodirout  + ' is not accessible.'
175    ras = report(msg)
176    return, result
177ENDIF
178;
179; existence and protection for writing
180IF (FILE_TEST(iodirout, /DIRECTORY, /WRITE) EQ 0) THEN BEGIN
181    msg = 'eee : the directory' + iodirout  + ' was not found.'
182    ras = report(msg)
183    return, result
184ENDIF
185;
186; build data filename
187filename='erai_q2m_' +  string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '_oafluxgrid.nc'
188;
189; check if this file exists
190msg='iii : looking for ' + filename
191ras = report(msg)
192fullfilename = isafile(iodirout + filename, NEW=0, /MUST_EXIST)
193IF fullfilename[0] EQ '' THEN BEGIN
194    msg = 'eee : the file ' + fullfilename + ' was not found.'
195    ras = report(msg)
196    return, result
197ENDIF
198;
199; build output filename
200filename_out = 'TropFlux_q2m_' +  string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '.nc'
201fullfilename_out = iodirout + filename_out
202; in order to avoid unexpected overwritten
203IF ((FILE_TEST(fullfilename_out) EQ 1)  AND (project_overwrite EQ 0)) THEN BEGIN
204    msg = 'eee : the file ' + fullfilename_out  + ' already exists.'
205    ras = report(msg)
206    return, result
207ENDIF
208;
209initncdf, fullfilename
210q2m=read_ncdf('q2m',yyyymmddb-.5d,yyyymmdde,file=fullfilename,/nostr)
211timein=24.d*(time-julday(1,1,1957,0,0,0))
212jpt=n_elements(timein)
213da=jul2date(time[0])
214cda0=string(da,format='(i8.8)')
215da=jul2date(time[jpt-1])
216cda1=string(da,format='(i8.8)')
217print, 'q2m in q2m_correction_ncdf first date ', cda0
218print, 'q2m in q2m_correction_ncdf last date ' , cda1
219;
220help, q2m
221;
222q2m_mean=grossemoyenne(q2m,'t',/nan)
223help, q2m_mean
224;
225jpt=n_elements(time)
226caldat, time,mon,day,yea
227q2m_m=q2m*0.
228;
229for jt=0,jpt-1 do begin
230    jtt=(time[jt]-julday(1,1,yea[jt])) < 364
231    q=reform(q2m_mean[*,*])
232    q2m_m[*,*,jt]=q
233endfor
234help, q2m_m
235;
236q2m_ano=q2m-q2m_m
237;
238; correction for mean based on scatter
239;q2m_m=q2m_m+0.815445  ;  (2000-2008)
240q2m_m=q2m_m+0.792717   ;  (2000-2009)
241help, q2m_ano
242;
243; applying the correction for variability based on the scatter
244;q2m_ano=q2m_ano*(1/0.919333)  ;  (2000-2008)
245q2m_ano=q2m_ano*(1/0.924674)   ;  (2000-2009)
246;
247q2m_new=q2m_m+q2m_ano
248help, q2m_new
249;
250;writing field
251lat=reform(gphit[0,0:jpj-1])
252lon=reform(glamt[0:jpi-1,0])
253;
254ncfile='!' + fullfilename_out
255lon_attr={units:'degrees_east',long_name:'Longitude'}
256lat_attr={units:'degrees_north',long_name:'Latitude'}
257time_attr={units:'days since 1950-01-01 00:00:00',long_name:'Time axis',time_origin:'1950-JAN-01 00:00:00'}
258globattr={source:'Basic data obtained from ERAI.  Mean correction for bias and correction for variability are applied',timerange:cda0+' - '+cda1}
259q2m_attr={units:'g/kg',missing_value:1.e20,long_name:'Specific humidity at 2m height',short_name:'q2m',axis:'TYX'}
260;
261ncfields = 'q2m[longitude,latitude,time*]=q2m_new:q2m_attr; ' $
262                      + 'longitude[]=lon:lon_attr; ' $
263                      + 'latitude[]=lat:lat_attr; ' $
264                      + 'time[]=timein:time_attr ' $
265                      + ' @ globattr'
266;
267@ncdf_quickwrite
268;
269result = 0
270return, result
271;
272end
Note: See TracBrowser for help on using the repository browser.