source: trunk/procs/macros/make_linfitdom.pro @ 106

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

Macros update: make_grad (needs fullcgrid keyword) and linfit

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