source: trunk/procs/macros/make_linfit.pro @ 164

Last change on this file since 164 was 164, checked in by pinsard, 15 years ago

set svn Id keyword on .pro files

  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1;+
2;
3; compute point-wise slope of linear regression of two 3D (x,y,t) fields
4;
5; @version
6; $Id$
7;
8;-
9FUNCTION make_linfit,  file_name,  ncdf_db, BOXZOOM = boxzoom,  TIME_1 = time_1,  TIME_2 =  time_2, ALL_DATA = all_data
10
11@common
12@com_eg
13
14   IF debug_w THEN print, '   ENTER make_linfit... '
15
16   var_name1 =  (strsplit(macro_base_fld, ',', ESCAPE = ' ', /EXTRACT))[0]
17   var_name2 =  (strsplit(macro_base_fld, ',', ESCAPE = ' ', /EXTRACT))[1]
18
19;  Build file_name2 if different
20
21   IF strpos (cmd1_back.grid, '#') NE -1 THEN BEGIN
22      varpos = strpos(file_name,  var_name1)
23      file_name2= strmid(file_name, 0, varpos)+var_name2+'.nc'
24      IF debug_w THEN print, '   file_name2 = ', file_name2
25   ENDIF ELSE file_name2 = file_name
26
27;  Read the variables in the correspondant netcdf file
28   var1 =  nc_read(file_name, var_name1,  ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1,  TIME_2 =  time_2)
29   var2 =  nc_read(file_name2, var_name2,  ncdf_db, BOXZOOM = boxzoom, TIME_1 = time_1,  TIME_2 =  time_2)
30
31   varname =  var1.name+'/'+var2.name+' corr.'
32   varunit =  '[-1/1]'
33
34   nxa = (size(var1.data))[1]
35   nya = (size(var1.data))[2]
36
37   IF debug_w THEN print, '   nxa, nya', nxa, nya
38
39   pt_linfit = fltarr(nxa, nya)
40   pt_err = fltarr(nxa, nya)
41   pt_corr = fltarr(nxa, nya)
42   pt_linfit[*, *] = 0
43   pt_err[*, *] = 1.1
44   pt_corr[*, *] = 0.
45
46; Sampling of data and computation of new numbers of values
47   @mth_decode
48
49   FOR imth = 0, nmth-1 DO BEGIN
50
51      IF debug_w THEN print, '   month idx/value: ', imth, strd(imth)
52
53      data1 =  (var1.data)[*, *, reform(idxm(imth,*), njpt)]       
54      data2 =  (var2.data)[*, *, reform(idxm(imth,*), njpt)]       
55      nval =  njpt
56   
57; Compute linear regression
58
59
60      FOR idx = 0, nxa-1 DO FOR idy = 0, nya-1 DO BEGIN
61
62         IF data1(idx, idy, 0) LT valmask/10. THEN BEGIN
63
64            x1i = reform(data1(idx, idy, *), nval)
65            x2i = reform(data2(idx, idy, *), nval)
66            x1 = x1i-mean(x1i)
67            x2 = x2i-mean(x2i)
68
69            no_compute = 0
70
71            IF linfit_map NE '' THEN BEGIN
72            ; compute regression for value above (p - default) or below (m) linfit_sep
73               CASE linfit_map OF
74                  'm': idt = where (x2 LE linfit_sep)
75                  ELSE: idt = where (x2 GE linfit_sep)
76               ENDCASE
77               IF (size(idt))[1] GE 3  THEN BEGIN
78                  IF idt[0] NE -1 THEN BEGIN
79                     x1 = x1(idt)
80                     x2 = x2(idt)
81                  ENDIF
82               ENDIF ELSE BEGIN
83                  no_compute = 1
84               ENDELSE
85            ENDIF
86           
87;           BEGIN
88;             print, '  idx, idy, linfit_map ', idx, idy, linfit_map
89;             print, '  size(idt)',size(idt)
90;             stop
91;          ENDIF
92         
93            IF no_compute EQ 0 THEN BEGIN
94
95               coeffl = linfit(x2, x1, CHISQ = linerrl, PROB = proberrl, SIGMA = sigmaerrl)
96               correl = c_timecorrelate(x2,x1)
97               pt_linfit(idx, idy) = pt_linfit(idx, idy)+coeffl(1)
98               pt_err(idx, idy) = min(pt_err(idx, idy), proberrl)
99               pt_corr(idx, idy) = pt_corr(idx, idy) + correl
100               
101               IF debug_w THEN BEGIN 
102                  IF idx EQ 30 AND idy EQ 15 THEN BEGIN
103                     print, '  idx, idy, linfit_map ', idx, idy, linfit_map
104                     print, '  size(idt)',size(idt)
105                     print, '  pt_linfit, correl = ',coeffl(1), correl
106                  ENDIF
107               ENDIF
108            ENDIF ELSE BEGIN
109               pt_linfit(idx, idy) = valmask
110               pt_err(idx, idy) = 0.
111               pt_corr(idx, idy) = 0.
112            ENDELSE
113
114         ENDIF ELSE BEGIN
115            pt_linfit(idx, idy) = valmask
116            pt_err(idx, idy) = 0.
117            pt_corr(idx, idy) = 0.
118         ENDELSE
119
120      ENDFOR
121   ENDFOR
122
123  ; make mean of period
124
125   idm = where(pt_linfit GE valmask/10.)
126   pt_linfit = pt_linfit/float(nmth)
127   pt_corr =  pt_corr/float(nmth)
128   IF idm[0] NE -1 THEN pt_linfit(idm) = valmask
129
130  ; only take points where correlation larger than 0.2
131
132   idc = where(abs(pt_corr) LT corr_thres)
133   IF idc[0] NE -1 THEN pt_linfit(idc) = !values.f_nan
134
135   corr_txt = '[r>'+strtrim(string(corr_thres, format = '(f5.2)'), 2)+']'
136
137   varname =  varname+' '+ntxt
138
139   print,  '    Linfit min/max pt_corr ', min(pt_corr), max(pt_corr)
140   
141;  Define the outputs of the function   
142   field = {name: varname, data: pt_linfit, legend: '', units: '', origin: '', dim: 0,  direc:'xy'}   
143   field.origin = var1.origin
144   field.dim = var1.dim
145
146   CASE linfit_map OF
147      'p': BEGIN
148         print,  '    Linear fit computed for anomalies ABOVE ',linfit_sep
149         lintxt = ' > '+ strtrim(string(linfit_sep), 2)
150      END
151      'm': BEGIN
152         print,  '    Linear fit computed for anomalies BELOW ',linfit_sep
153         lintxt = ' < '+ strtrim(string(linfit_sep), 2)
154      END
155      ELSE: lintxt = ''
156   ENDCASE
157
158   field.legend = lintxt+' for '+ntxt+' in ['+cmdm.date1+'-'+cmdm.spec+'] '+corr_txt+' -'
159
160; additional computations (pac_5 nino_3 nino_4 averages) *** requires whole domain ****
161
162   IF nxt EQ jpi AND nyt EQ jpj THEN BEGIN
163     
164      n3_linfit = moyenne(pt_linfit,'xy', boite = [210,  270,  -5, 5], NaN = valmask)
165      n4_linfit = moyenne(pt_linfit,'xy', boite = [160,  210,  -5, 5], NaN = valmask)
166      zl_linfit = moyenne(pt_linfit,'xy', boite = [130,  280,  -5, 5], NaN = valmask)
167     
168      print, '    Nino 3 average of slope of linear fit = ', n3_linfit
169      print, '    Nino 4 average of slope of linear fit = ', n4_linfit
170      print, '    Zonal 5N/5S average of slope of linear fit = ', zl_linfit
171
172   ENDIF
173
174 ;  jpt=nval
175 ;  meants=grossemoyenne(data2, 't')
176 ;  meantsp=reform(meants,nxt*nyt) 
177 ;  meanlf=reform(pt_linfit,nxt*nyt)
178 ;  IF min (meantsp) GE 100 THEN meantsp=meantsp-273.15
179 ;  pltsc, meanlf,meantsp,-70,30,20,34,"SST "+mth[strd-1], window=2 
180 ;  pltsc, meanlf,meantsp,-70,30,20,34,"SST "+mth[strd-1], window=2, /noerase, /ov1d, col1d=2
181
182 ;  stop
183
184   IF debug_w THEN print, '   ... EXIT make_linfit'
185   
186   return, field
187
188END
Note: See TracBrowser for help on using the repository browser.