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

Last change on this file since 122 was 122, checked in by ericg, 16 years ago

Correct bug in make_linfit.pro when corr_thres = 0

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