source: trunk/src/sst_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: 9.0 KB
Line 
1;+
2;
3; =======================
4; sst_correction_ncdf.pro
5; =======================
6;
7; .. function:: sst_correction_ncdf(yyyymmddb,yyyymmdde)
8;
9; DESCRIPTION
10; ===========
11;
12; Correction of sst on OAFLUX grid
13;
14; :file:`${PROJECT_OD}/erai_sst_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc`
15; containing
16; ++
17; has been produced by
18; :func:`interp_erai_sst`.
19;
20; Corrected sst on OAFLUX grid
21; is written in
22; :file:`${PROJECT_OD}/TropFlux_sst_{yyyymmdd}_{yyyymmdd}.nc`
23; if this file not already exists.
24;
25; This output file
26; :file:`${PROJECT_OD}/TropFlux_sst_{yyyymmdd}_{yyyymmdd}.nc`
27; will be used by
28; :func:`cronin_gustiness_ncdf`
29; and
30; :func:`tropflux`.
31;
32; This output file
33; :file:`${PROJECT_OD}/TropFlux_sst_{yyyymmdd}_{yyyymmdd}.nc`
34; is one of the deliverable of TropFlux.
35;
36; .. only:: man
37;
38;    Figure is visible on PDF and HTML documents only.
39;
40; .. only:: html or latex
41;
42;     .. graphviz::
43;
44;        digraph sst_correction_ncdf {
45;
46;           file_in [shape=ellipse,
47;           fontname=Courier,
48;           label="${PROJECT_OD}/erai_sst_{yyyymmdd}_{yyyymmdd}_oafluxgrid.nc"];
49;           file_out [shape=ellipse,
50;           fontname=Courier,
51;           label="${PROJECT_OD}/TropFlux_sst_{yyyymmdd}_{yyyymmdd}.nc"];
52;
53;           sst_correction_ncdf [shape=box,
54;           fontname=Courier,
55;           color=blue,
56;           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/sst_correction_ncdf.pro",
57;           label="${PROJECT}/src/sst_correction_ncdf.pro"];
58;
59;           {file_in} -> {sst_correction_ncdf} -> {file_out}
60;
61;        }
62;
63; SEE ALSO
64; ========
65;
66; :ref:`mooring_corrections`
67;
68; Used by :ref:`tropflux.sh`
69;
70; :ref:`project_profile.sh`
71;
72; :func:`interp_erai_sst`
73;
74; :func:`initncdf <saxo:initncdf>`
75; :func:`read_ncdf <saxo:read_ncdf>`
76; :func:`grossemoyenne <saxo:grossemoyenne>`
77; :func:`julday <saxo:julday>`
78; :func:`ncdf_quickwrite <saxo:ncdf_quickwrite>`
79;
80; EXAMPLES
81; ========
82;
83; .. code-block:: idl
84;
85;    yyyymmddb = 20000101L
86;    yyyymmdde = 20001231L
87;    result = sst_correction_ncdf(yyyymmddb, yyyymmdde)
88;    print, result
89;
90; TODO
91; ====
92;
93; make it work on loholt1 idl8:
94; reach end without error but with bad values (9.96921e+36 on time and sst,
95; nb timestep 5716
96;
97; see commands:
98;
99; .. code-block:: bash
100;
101;    ncks -v sst  -d time,0,1 -d latitude,0,1 -d longitude,0,1 /homedata/pinsard/tropflux_d/TropFlux_sst_19890101_20091231.nc
102;
103; very strange value of time on loholt1 idl6 but not always and not always at
104; the same indices !!:
105;
106; .. parsed-literal::
107;
108;    print, time[7080:7090]
109;       2454607.5       2454608.5       2454609.5   9.9692100e+36   9.9692100e+36
110;   9.9692100e+36   9.9692100e+36   9.9692100e+36   9.9692100e+36       2454616.5
111;       2454617.5
112;
113; while :
114;
115; .. code-block:: bash
116;
117;    ncks -H -C -v time -d time,7080,7090 ${PROJECT_ID}/20c3m_erai_sstk_TROP_1989_2009.nc
118;
119; .. parsed-literal::
120;
121;    time[7080]=450432
122;    time[7081]=450456
123;    time[7082]=450480
124;    time[7083]=450504
125;    time[7084]=450528
126;    time[7085]=450552
127;    time[7086]=450576
128;    time[7087]=450600
129;    time[7088]=450624
130;    time[7089]=450648
131;    time[7090]=450672
132;
133; and
134;
135; .. code-block:: bash
136;
137;    ncks -H -C -v time -d time,7080,7090 ${PROJECT_OD}/erai_sst_19890101_20091231_oafluxgrid.nc
138;
139; .. parsed-literal::
140;
141;    time[7080]=21325
142;    time[7081]=21326
143;    time[7082]=21327
144;    time[7083]=9.96920996839e+36
145;    time[7084]=9.96920996839e+36
146;    time[7085]=9.96920996839e+36
147;    time[7086]=9.96920996839e+36
148;    time[7087]=9.96920996839e+36
149;    time[7088]=9.96920996839e+36
150;    time[7089]=21334
151;    time[7090]=21335
152;
153;  and :
154;
155; .. code-block:: bash
156;
157;    fplod@cratos$ ncks -H -C -v time -d time,7080,7090 ${PROJECT_OD}/erai_sst_19890101_20091231_oafluxgrid.nc
158;
159; .. parsed-literal::
160;
161;    time[7080]=21325
162;    time[7081]=21326
163;    time[7082]=21327
164;    time[7083]=21328
165;    time[7084]=21329
166;    time[7085]=21330
167;    time[7086]=21331
168;    time[7087]=21332
169;    time[7088]=21333
170;    time[7089]=21334
171;    time[7090]=21335
172;
173; coding rules
174;
175; temperature SI units = K : why + and - 273...
176;
177; hard coded coefficient correction.
178;
179; KNOWN ISSUES
180; ============
181;
182; test of existence of fullfilename_in not very efficient because
183; MUST_EXIST keyword of :func:`isafile <saxo:isafile>` not yet implemented
184;
185; EVOLUTIONS
186; ==========
187;
188; $Id: sst_correction_ncdf.pro 88 2011-08-19 15:40:14Z pinsard $
189;
190; $URL$
191;
192; - fplod 20120322
193;
194;   * taking project_overwrite into account
195;   * try to add compile_opt seems to be incompatible with ncdf_quickwrite
196;   * pro -> function
197;   * hard coded da1 and da2 replaced by yyyymmddb and yyyymmdde parameters
198;   * get rid of timegen
199;
200; - fplod 20110808T124236Z cratos (Linux)
201;
202;   * usage of ${PROJECT_OD}
203;   * complete description
204;   * remove v20 in output filename
205;
206; - fplod 20101215T114224Z aedon.locean-ipsl.upmc.fr (Darwin)
207;
208;   * add graph in header
209;
210; - fplod 20101214T093615Z aedon.locean-ipsl.upmc.fr (Darwin)
211;
212;   * minimal header
213;
214; - pbk 2008
215;
216;   * creation
217;
218;-
219function sst_correction_ncdf $
220         , yyyymmddb $
221         , yyyymmdde
222;
223;++compile_opt idl2, strictarrsubs, logical_predicate
224;
225@cm_4cal
226@cm_4data
227@cm_4mesh
228@cm_4data
229@cm_project
230;
231; Return to caller if errors
232ON_ERROR, 2
233;
234result = -1
235;
236usage = 'result = sst_correction_ncdf(yyyymmddb, yyyymmdde)'
237nparam = N_PARAMS()
238IF (nparam NE 2) THEN BEGIN
239    ras = report(['Incorrect number of arguments.' $
240          + '!C' $
241          + 'Usage : ' + usage])
242    return, result
243ENDIF
244;
245; test if ${PROJECT_OD} defined
246CASE project_od_env OF
247    '' : BEGIN
248        msg = 'eee : ${PROJECT_OD} is not defined'
249        ras = report(msg)
250        return, result
251    END
252    ELSE : BEGIN
253        msg = 'iii : ${PROJECT_OD} is ' + project_od_env
254        ras = report(msg)
255    END
256ENDCASE
257;
258; check if output data will be possible
259iodirout = isadirectory(project_od_env)
260;
261; existence and protection for reading
262IF (FILE_TEST(iodirout, /DIRECTORY, /EXECUTABLE, /READ) EQ 0) THEN BEGIN
263    msg = 'eee : the directory' + iodirout  + ' is not accessible.'
264    ras = report(msg)
265    return, result
266ENDIF
267;
268; existence and protection for writing
269IF (FILE_TEST(iodirout, /DIRECTORY, /WRITE) EQ 0) THEN BEGIN
270    msg = 'eee : the directory' + iodirout  + ' was not found.'
271    ras = report(msg)
272    return, result
273ENDIF
274;
275; build data filename
276filename='erai_sst_' +  string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '_oafluxgrid.nc'
277;
278; check if this file exists
279msg='iii : looking for ' + filename
280ras = report(msg)
281fullfilename = isafile(iodirout + filename, NEW=0, /MUST_EXIST)
282IF fullfilename[0] EQ '' THEN BEGIN
283    msg = 'eee : the file ' + fullfilename + ' was not found.'
284    ras = report(msg)
285    return, result
286ENDIF
287;
288; build output filename
289filename_out = 'TropFlux_sst_' +  string(yyyymmddb,format='(I8.8)') + '_' + string(yyyymmdde,format='(I8.8)') + '.nc'
290fullfilename_out = iodirout + filename_out
291; in order to avoid unexpected overwritten
292IF ((FILE_TEST(fullfilename_out) EQ 1)  AND (project_overwrite EQ 0)) THEN BEGIN
293    msg = 'eee : the file ' + fullfilename_out  + ' already exists.'
294    ras = report(msg)
295    return, result
296ENDIF
297;
298initncdf, fullfilename
299sst=read_ncdf('sst',yyyymmddb-.5d,yyyymmdde,file=fullfilename,/nostr)
300timein=24.d*(time-julday(1,1,1957,0,0,0))
301jpt=n_elements(timein)
302da=jul2date(time[0])
303cda0=string(da,format='(i8.8)')
304da=jul2date(time[jpt-1])
305cda1=string(da,format='(i8.8)')
306print, 'sst in sst_correction_ncdf first date ', cda0
307print, 'sst in sst_correction_ncdf last date ' , cda1
308;
309sst=sst-273.15
310help, sst
311;
312sst_mean=grossemoyenne(sst,'t',/nan)
313help, sst_mean
314;
315jpt=n_elements(time)
316caldat, time,mon,day,yea
317;
318;++ debug to understand some time value 9.9692100e+36 on idl 6 loholt1
319; print, time[7080:7090] when working on [19890101,19891231]
320;
321sst_m=sst*0.
322;
323for jt=0,jpt-1 do begin
324    jtt=(time[jt]-julday(1,1,yea[jt])) < 364
325    t=reform(sst_mean[*,*])
326    sst_m[*,*,jt]=t
327endfor
328help, sst_m
329;
330sst_ano=sst-sst_m
331;
332; correction for mean based on scatter
333;sst_m=sst_m+0.0521111   ; (2000-2008)
334sst_m=sst_m+0.0533692    ; (2000-2009)
335;
336help, sst_ano
337;
338; applying the correction for variability based on the scatter
339;sst_ano=sst_ano*(1/0.989889)  ; (2000-2008)
340sst_ano=sst_ano*(1/0.986196)   ; (2000-2009)
341;
342sst_new=sst_m+sst_ano+273.15
343help, sst_new
344;
345;writing field
346lat=reform(gphit[0,0:jpj-1])
347lon=reform(glamt[0:jpi-1,0])
348;
349ncfile='!' + fullfilename_out
350lon_attr={units:'degrees_east',long_name:'Longitude'}
351lat_attr={units:'degrees_north',long_name:'Latitude'}
352time_attr={units:'days since 1950-01-01 00:00:00',long_name:'Time axis',time_origin:'1950-JAN-01 00:00:00'}
353globattr={source:'Basic data obtained from ERAI.  Bias and variability correction are applied',timerange:cda0+' - '+cda1}
354sst_attr={units:'degK',missing_value:1.e20,long_name:'Sea Surface Temperature',short_name:'sst',axis:'TYX'}
355;
356ncfields = 'sst[longitude,latitude,*time]=sst_new:sst_attr; ' $
357                      + 'longitude[]=lon:lon_attr; ' $
358                      + 'latitude[]=lat:lat_attr; ' $
359                      + 'time[]=timein:time_attr ' $
360                      + ' @ globattr'
361;
362@ncdf_quickwrite
363;
364result = 0
365return, result
366;
367end
Note: See TracBrowser for help on using the repository browser.