Project

General

Profile

Download (6.25 KB) Statistics
| Branch: | Revision:
1
&args ingrid sd prob bbox:rest
2

    
3
/* 9a - limit to 4 steps, see if that has any significant deterioration of smoothing performance. Should fix
4
/* the problem with islands and headlands - turns out also need to remove water except
5
/* for one cell adjacent to land, and give that a higher uncertainty. See cluster_multiscalesmooth9a_clean_216
6

    
7
/* Version 9:
8
/* Focus on implementing identical algorithm to directsmooth2 using multiscale method i.e. aggregating by factor of 3 
9
/* from already aggregated data, rather than from original resolution each time.
10

    
11
/* Version 8:
12
/* WARNING: have not yet checked that the additional weighting of the gaussian smoothing is not messing with
13
/* the calculations of variance etc.
14
/* Replaced simple 3x3 aggregation with gaussian smoothing
15
/* Kernel is chosen to give appropriate level of smoothing and produce a fairly accurate approximation of the smoothed
16
/* surface by interpolation of the coarser scale smoothed values
17
/* Details in gaussian.xls on john's laptop
18

    
19
/* Version 7:
20
/* Further reworking of how the final values are selected - a mean is a candidate if its associated variance
21
/* is less than the mean sample uncertainty, and the mean with the lowest variance among the candidates is the chosen one.
22
/* Implement that in the nested sense by taking the lowest group variance divided by the chi^2 value, and its associated mean variance,
23
/* and if that is lower than the data point variance the 
24

    
25
/* approximate critical value of chi^2/N with N degrees of freedom at 5% level as 1 + 2.45/sqrt(N) + 0.55/N
26
/* for 1% level use 1 + 3.4/sqrt(N) + 2.4/N
27

    
28
/* Version 6:
29
/* Done from scratch after careful working through of theory.
30
/* ingrid is the (potentially sparse) grid of data to be smoothed and
31
/* interpolated, which can be of different extent to the output
32
/* (resolution is assumed to be the same, could adapt this to relax that)
33
/*
34
/* var can be a constant or a grid
35
/*
36
/* bbox can be either a grid name or the 'xmin ymin xmax ymax' parameters
37
/* for setwindow
38

    
39
&type NB - using standard deviation as noise specification now, not variance!
40

    
41

    
42
/* set up chisq parameters
43
&sv chisqa = [calc 2.807 - 0.6422 * [log10 %prob% ] - 3.410 * %prob% ** 0.3411 ]
44
&sv chisqb = [calc -5.871 - 3.675 * [log10 %prob% ] + 4.690 * %prob% ** 0.3377 ]
45
&type chisq parameters %chisqa% %chisqb%
46

    
47

    
48
setcell %ingrid%
49

    
50
/* work out maximum of ingrid and bbox extents
51
setwindow [unquote %bbox%] %ingrid%
52
bboxgrid = 1
53
setwindow maxof
54
workgrid = %ingrid% + bboxgrid
55
setwindow workgrid
56
kill workgrid
57

    
58
/* naming:
59
/*  h - the value being smoothed/interpolated
60
/*  vg - total variance of group of data, or of individual measurement
61
/*  v_bg - variance between groups
62
/*  v_wg - variance within groups
63
/*  wa - weighting for aggregation, based on total variance
64
/*  vm - variance of the calculated mean
65
/*  mv - mean of finer scale variances
66
/*  n - effective number of measurements
67

    
68

    
69
/* NB - only calculating sample variances here, not variances of estimated means.
70
/* Also note that v0_bg is an uncertainty, not a sample variance
71
/* and v1_bg is total variances, but both are labelled as "between-group" to simplify the smoothing
72

    
73
h0 = %ingrid%
74
v0 = con(^ isnull(h0), sqr(%sd%))
75
vg0 = v0
76
w0 = con(isnull(v0), 0, 1.0 / v0)
77
wsq0 = sqr(w0)
78
n0 = con(^ isnull(h0), 1, 0)
79

    
80
&describe v0
81
&sv bigvar %grd$zmax%
82

    
83
setcell minof
84

    
85
/* aggregate to broader scales
86
&sv i 1
87
&sv done .false.
88
&describe h0
89

    
90
&do &until %done%
91
  &sv j [calc %i% - 1]
92

    
93
  &type Aggregate from %j% to %i%
94

    
95
  &describe h%j%
96
  &sv cell3 [calc %grd$dx% * 3]
97
  &describe h0
98
  &sv nx0 [round [calc %grd$xmin% / %cell3% - 0.5]]
99
  &sv ny0 [round [calc %grd$ymin% / %cell3% - 0.5]]
100
  &sv nx1 [round [calc %grd$xmax% / %cell3% + 0.5]]
101
  &sv ny1 [round [calc %grd$ymax% / %cell3% + 0.5]]
102
  &sv x0 [calc ( %nx0% - 0.5 ) * %cell3%]
103
  &sv y0 [calc ( %ny0% - 0.5 ) * %cell3%]
104
  &sv x1 [calc ( %nx1% + 0.5 ) * %cell3%]
105
  &sv y1 [calc ( %ny1% + 0.5 ) * %cell3%]
106
  setwindow %x0% %y0% %x1% %y1%
107

    
108
  w%i% = aggregate(w%j%, 3, sum)
109
  wsq%i% = aggregate(wsq%j%, 3, sum)
110
  n%i% = aggregate(n%j%, 3, sum)
111
  neff%i% = w%i% * w%i% / wsq%i%
112
  h%i% = aggregate(w%j% * h%j%, 3, sum) / w%i%
113
  vbg%i% = aggregate(w%j% * sqr(h%j% - h%i%), 3, sum) / w%i%
114
  &if %i% eq 1 &then vwg%i% = n%i% - n%i% /* zero, but with window and cell size set for us
115
  &else vwg%i% = aggregate(w%j% * vg%j%, 3, sum) / w%i%
116
  vg%i% = vbg%i% + vwg%i%
117
  vm%i% = 1.0 / w%i%
118
  mv%i% = n%i% / w%i%
119

    
120
  chisq%i% = 1 + %chisqa% / sqrt(neff%i% - 1) + %chisqb% / (neff%i% - 1)
121
  v%i% = con(vg%i% / chisq%i% < mv%i%, vm%i%, vg%i%)
122

    
123
  /* remove everything except h%i% and v%i%
124
  kill w%j%
125
  kill wsq%j%
126
  kill n%j%
127
  kill neff%i%
128
  kill vbg%i%
129
  kill vwg%i%
130
  kill vg%j%
131
  kill vm%i%
132
  kill mv%i%
133
  kill chisq%i%
134

    
135
  &sv done %i% eq 4
136

    
137
  &sv i [calc %i% + 1]
138
&end
139

    
140

    
141
&sv maxstep [calc %i% - 1]
142
&sv bigvar [calc %bigvar% * 10]
143

    
144
kill w%maxstep%
145
kill wsq%maxstep%
146
kill n%maxstep%
147
kill vg%maxstep%
148

    
149
/* smooth, refine and combine each layer in turn
150

    
151

    
152
copy h%maxstep% hs%maxstep%
153
copy v%maxstep% vs%maxstep%
154
kill h%maxstep%
155
kill v%maxstep%
156
setcell hs%maxstep%
157
setwindow hs%maxstep%
158

    
159
&do j := %maxstep% &to 1 &by -1
160
  &sv i [calc %j% - 1]
161

    
162
  &type Refine from %j% to %i%
163

    
164
  /* for the first stage where the coarser grid is refined and smoothed, set window to the coarse grid
165
  setcell h%i%
166
  setwindow maxof
167

    
168
  /* create smoothed higher resolution versions of h and v_bg, hopefully with no nulls!
169
  hs%j%_%i% = focalmean(hs%j%, circle, 2)
170
  vs%j%_%i% = focalmean(vs%j%, circle, 2)
171

    
172
  setcell h%i%
173
  &describe h%i%
174
  &sv cellsize %grd$dx%
175
  &describe bboxgrid
176
  setwindow [calc %grd$xmin% - 4 * %cellsize%] [calc %grd$ymin% - 4 * %cellsize%] [calc %grd$xmax% + 4 * %cellsize%] [calc %grd$ymax% + 4 * %cellsize%] h%i%
177

    
178
  /* create no-null version of finer h and v
179
  h%i%_c = con(isnull(h%i%), 0, h%i%)
180
  v%i%_c = con(isnull(v%i%), %bigvar%, v%i%)
181

    
182
  /* combine two values using least variance
183
  hs%i% = (h%i%_c / v%i%_c + hs%j%_%i% / vs%j%_%i% ) / (1.0 / v%i%_c + 1.0 / vs%j%_%i%)
184
  vs%i% = 1 / (1.0 / v%i%_c + 1.0 / vs%j%_%i%)
185

    
186
  kill v%i%_c  
187
  kill h%i%_c
188
  kill v%i%
189
  kill h%i%
190
  kill vs%j%_%i%
191
  kill hs%j%_%i%
192
  kill hs%j%
193
  kill vs%j%
194
&end
195

    
196
/* result is hs0, with variance vs0
197

    
198
kill bboxgrid
(6-6/11)