Project

General

Profile

« Previous | Next » 

Revision 86eb951b

Added by Jim Regetz over 12 years ago

  • ID 86eb951befafb6eb1c077969e2599607e8b8b877

added old Oregon topo AML scripts (Tien Ming Lee, John Gallant)

View differences:

shared/extra/organisms-content-migration.sh
11 11

  
12 12
export ORGANISMS="/home/organisms"
13 13
export LAYERS="/home/layers"
14
export REPO="."
14 15

  
15 16
#=======================================================================
16 17
# set up some directories
......
944 945
# topo
945 946
#
946 947

  
947
# for now just migrate to experimental area
948
mv $ORGANISMS/topo $LAYERS/experimental/
948
# first migrate the whole thing to new commons area
949
mv $ORGANISMS/topo $LAYERS/commons/
950
# consolidate first set of dirs
951
mkdir $LAYERS/commons/topo/v1
952
mv -i $LAYERS/commons/topo/{experimental,scripts,tiles} \
953
      $LAYERS/commons/topo/v1/
954
# consolidate second set of dirs
955
mv -i $LAYERS/commons/topo/experimental_2/topo \
956
      $LAYERS/commons/topo/v2
957
rmdir $LAYERS/commons/topo/experimental_2
958

  
959
# remove ArcToolbox temporary files
960
rm $LAYERS/commons/topo/v2/topo/xx00003024.s
961
rm $LAYERS/commons/topo/v2/experimental/xx00013916.x
962
rm $LAYERS/commons/topo/v2/experimental/xx00003916.s
963
rm $LAYERS/commons/topo/v2/experimental/xx00013916.s
964

  
965
# consolidate first set of scripts into git repo
966
mv -i $LAYERS/commons/topo/v1/experimental/layers.aml \
967
      $REPO/terrain/research/oregon/arcgis/v1/
968
mv -i $LAYERS/commons/topo/v1/experimental/aggregate.aml \
969
      $REPO/terrain/research/oregon/arcgis/v1/
970
mv -i $LAYERS/commons/topo/v1/experimental/multiscalesmooth9a_clean.aml \
971
      $REPO/terrain/research/oregon/arcgis/v1/
972
mv -i $LAYERS/commons/topo/v1/experimental/tilemerge_oregon1.aml \
973
      $REPO/terrain/research/oregon/arcgis/v1/
974
mv -i $LAYERS/commons/topo/v1/scripts/unpacktiles.aml \
975
      $REPO/terrain/research/oregon/arcgis/v1/
976
mv -i $LAYERS/commons/topo/v1/scripts/unziptiles.py \
977
      $REPO/terrain/research/oregon/arcgis/v1/
978
mv -i $LAYERS/commons/topo/v1/scripts/mrvbf/mrvbf6g-a3.aml \
979
      $REPO/terrain/research/oregon/arcgis/v1/
980
mv -i $LAYERS/commons/topo/v1/scripts/mrvbf/pctl.aml \
981
      $REPO/terrain/research/oregon/arcgis/v1/
982
mv -i $LAYERS/commons/topo/v1/scripts/mrvbf/chunkproc.aml \
983
      $REPO/terrain/research/oregon/arcgis/v1/
984
mv -i $LAYERS/commons/topo/v1/scripts/mrvbf/pctl-limits.aml \
985
      $REPO/terrain/research/oregon/arcgis/v1/
986
mv -i $LAYERS/commons/topo/v1/scripts/mrvbf/gauss3 \
987
      $REPO/terrain/research/oregon/arcgis/v1/
988
# for now keep this mysterioud Windows binary around...
989
mv -i $LAYERS/commons/topo/v1/scripts/mrvbf/pctl.exe \
990
      $LAYERS/commons/topo/v1/
991
# remove now-empty scripts directory
992
rmdir $LAYERS/commons/topo/v1/scripts/mrvbf
993
rmdir $LAYERS/commons/topo/v1/scripts
994

  
995
# consolidate second set of scripts into git repo
996
mv -i $LAYERS/commons/topo/v2/experimental/layers.aml \
997
      $REPO/terrain/research/oregon/arcgis/v2/
998
mv -i $LAYERS/commons/topo/v2/experimental/aggregate.aml \
999
      $REPO/terrain/research/oregon/arcgis/v2/
1000
mv -i $LAYERS/commons/topo/v2/experimental/multiscalesmooth9a_clean.aml \
1001
      $REPO/terrain/research/oregon/arcgis/v2/
1002
mv -i $LAYERS/commons/topo/v2/experimental/tilemerge_oregon1.aml \
1003
      $REPO/terrain/research/oregon/arcgis/v2/
1004
mv -i $LAYERS/commons/topo/v2/scripts/unpacktiles.aml \
1005
      $REPO/terrain/research/oregon/arcgis/v2/
1006
mv -i $LAYERS/commons/topo/v2/scripts/unziptiles.py \
1007
      $REPO/terrain/research/oregon/arcgis/v2/
1008
mv -i $LAYERS/commons/topo/v2/topo/mrvbf6g-a3.aml \
1009
      $REPO/terrain/research/oregon/arcgis/v2/
1010
mv -i $LAYERS/commons/topo/v2/topo/pctl.aml \
1011
      $REPO/terrain/research/oregon/arcgis/v2/
1012
mv -i $LAYERS/commons/topo/v2/topo/chunkproc.aml \
1013
      $REPO/terrain/research/oregon/arcgis/v2/
1014
mv -i $LAYERS/commons/topo/v2/topo/pctl-limits.aml \
1015
      $REPO/terrain/research/oregon/arcgis/v2/
1016
mv -i $LAYERS/commons/topo/v2/topo/gauss3 \
1017
      $REPO/terrain/research/oregon/arcgis/v2/
1018

  
949 1019

  
950 1020
#
951 1021
# temp_benoit
......
969 1039
# now migrate code into git repository clone
970 1040
#=======================================================================
971 1041

  
972
export REPO="."
973

  
974 1042
# nunokawa terrain scripts
975 1043
mkdir terrain/research/gtopo30
976 1044
mv -i code/terrain/nunokawa-scripts/toProduceData/clipUSGS.r \
terrain/research/oregon/arcgis/v1/aggregate.aml
1
/* need to ensure that the aggregation boxes are consistent across the whole set of layers
2
/* so might need a setwindow here to enforce the correct location
3

  
4
/* Question about 1km Behrman version - should that be projected from 30" stats, or
5
/* should data be projected to Behrman and aggregated over 1km cell. Far from the equator
6
/* these will give quite different results
7

  
8
/* elevation - derive mean, min, max, std. dev, skew
9
elev_mean = aggregate(srtmv41_smth, 10, mean)
10
elev_min = aggregate(srtmv41_smth, 10, min)
11
elev_max = aggregate(srtmv41_smth, 10, max)
12
elev_range = elev_max - elev_min
13
/* stupid arc doesn't have a standard deviation function for aggregation
14
elev_sd = sqrt(aggregate(sqr(srtmv41_smth - elev_mean), 10, sum) / 100)
15

  
16

  
17
/* slope - just mean and standard deviation
18
slopedeg_mean = aggregate(slopedeg, 10, mean)
19
slopedeg_sd = sqrt(aggregate(sqr(slopdeg - slopedeg_mean), 10, sum) / 100)
20

  
21
/* wetness index - mean and standard deviation
22
/* twi not done yet
terrain/research/oregon/arcgis/v1/chunkproc.aml
1
/* Invoke another AML on a grid to produce a grid
2
/* Other AML takes arguments: gridname outgridname otherargs
3

  
4
&args targetaml gridname outgridname otherargs:REST
5

  
6

  
7
/* targetaml-limits.aml takes same arguments as target.aml (except for
8
/* output file) and sets maximum number of cells and overlap required
9
&run %targetaml%-limits %gridname% [unquote %otherargs%]
10

  
11
&describe %gridname%
12
&sv gridxmin = %grd$xmin%
13
&sv gridxmax = %grd$xmax%
14
&sv gridymin = %grd$ymin%
15
&sv gridymax = %grd$ymax%
16
&sv gridncols = %grd$ncols%
17
&sv gridnrows = %grd$nrows%
18
&sv cellsize = %grd$dx%
19

  
20
/****** TEMPORARY *******/
21
/* setcell %cellsize%
22
/**** END TEMPORARY *****/
23

  
24
&sv ncells = %grd$ncols% * %grd$nrows%
25
&type %ncells% cells in %gridname%
26

  
27
&if %ncells% <= %.maxcells% &then &do
28
  /* no need to do chunks, just invoke target AML
29
  &type Process as single chunk
30
  &run %targetaml% %gridname% %outgridname% [unquote %otherargs%]
31
  &return
32
&end
33

  
34
/* need to do chunks
35
&type Process in multiple chunks
36
/* &type X: %gridxmin% %gridxmax% (%gridncols%) 
37
/* &type Y: %gridymin% %gridymax% (%gridnrows%) 
38
/* &type %ncells% cells, %.maxcells% max, %.border% border
39

  
40
&sv nchunks = [truncate [calc %ncells% / %.maxcells%]] + 1
41
&type Need at least %nchunks% chunks
42
&sv chunkdim = [sqrt %.maxcells%] + 2 * %.border%
43

  
44
&if %gridncols% < %chunkdim% &then
45
  &do
46
    &sv colchunks = 1
47
    &sv rowchunks = %nchunks%
48
    &sv chunkncols = %gridncols%
49
    &sv chunknrows = [truncate [calc %gridnrows% / %rowchunks%]] + 1
50
  &end
51
&else &if %gridnrows% < %chunkdim% &then
52
  &do
53
    &sv rowchunks = 1
54
    &sv colchunks = %nchunks%
55
    &sv chunkncols = [truncate [calc %gridncols% / %colchunks%]] + 1
56
  &end
57
&else 
58
  &do
59
    &sv rowchunks = [truncate [calc %gridnrows% / %chunkdim%]] + 1
60
    &sv colchunks = [truncate [calc %gridncols% / %chunkdim%]] + 1
61
    &sv chunkncols = [truncate [calc %gridncols% / %colchunks%]] + 1
62
    &sv chunknrows = [truncate [calc %gridnrows% / %rowchunks%]] + 1
63
  &end
64

  
65

  
66
&type %colchunks% colchunks, %rowchunks% rowchunks
67
&type Chunk size %chunkncols% cols by %chunknrows% rows
68

  
69
&sv qtrcell = %cellsize% / 4
70
&sv halfcell = %cellsize% / 2
71
&sv colist
72

  
73
&sv chunki = 1
74
&do &while %chunki% <= %colchunks%
75
  &sv chunkj = 1
76
  &sv colist_col
77
  &do &while %chunkj% <= %rowchunks%
78
    &type Chunk %chunki% %chunkj%
79

  
80
    &sv coxmin = %gridxmin% + ( %chunki% - 1 ) * %chunkncols% * %cellsize% - %qtrcell%
81
    &sv coxmax = %coxmin% + %chunkncols% * %cellsize% + %halfcell%
82
    &sv coymin = %gridymin% + ( %chunkj% - 1 ) * %chunknrows% * %cellsize% - %qtrcell%
83
    &sv coymax = %coymin% + %chunknrows% * %cellsize% + %halfcell%
84

  
85
    /* &type Output X: %coxmin% %coxmax%
86
    /* &type Output Y: %coymin% %coymax%
87

  
88
    &if %chunki% = 1 &then &sv chunkxmin = %coxmin%
89
    &else &sv chunkxmin = %coxmin% - %.border% * %cellsize%
90
    &if %chunki% = %colchunks% &then &sv chunkxmax = %coxmax%
91
    &else &sv chunkxmax = %coxmax% + %.border% * %cellsize%
92

  
93
    &if %chunkj% = 1 &then &sv chunkymin = %coymin%
94
    &else &sv chunkymin = %coymin% - %.border% * %cellsize%
95
    &if %chunkj% = %rowchunks% &then &sv chunkymax = %coymax%
96
    &else &sv chunkymax = %coymax% + %.border% * %cellsize%
97

  
98
    /* &type Chunk X: %chunkxmin% %chunkxmax%
99
    /* &type Chunk Y: %chunkymin% %chunkymax%
100

  
101
    /* clip input file to chunk (with borders)
102
    setwindow %chunkxmin% %chunkymin% %chunkxmax% %chunkymax% %gridname%
103
    &if [exists chunk -grid] &then kill chunk
104
    chunk = %gridname%  
105
    &if [exists co_tmp -grid] &then kill co_tmp
106
    &run %targetaml% chunk co_tmp [unquote %otherargs%]
107
    kill chunk
108

  
109
    /* clip output file to chunk (without borders)
110
    setwindow %coxmin% %coymin% %coxmax% %coymax% %gridname%
111
    &if [exists co_%chunki%_%chunkj% -grid] &then kill co_%chunki%_%chunkj%
112
    co_%chunki%_%chunkj% = co_tmp
113
    kill co_tmp
114

  
115
    &if %chunkj% = 1 &then &sv colist_col = co_%chunki%_%chunkj%
116
    &else &sv colist_col = %colist_col%, co_%chunki%_%chunkj%
117

  
118
    &sv chunkj = %chunkj% + 1
119
  &end
120
  setwindow maxof
121
  &if [exists co_%chunki% -grid] &then kill co_%chunki%
122
  co_%chunki% = merge(%colist_col%)
123
  kill (!%colist_col%!)
124
  &if %chunki% = 1 &then &sv colist = co_%chunki%
125
  &else &sv colist = %colist%, co_%chunki%
126
  &sv chunki = %chunki% + 1
127
&end
128

  
129
setwindow maxof
130
&if [exists %outgridname% -grid] &then kill %outgridname%
131
%outgridname% = merge(%colist%)
132
kill (!%colist%!)
terrain/research/oregon/arcgis/v1/gauss3
1
11 11
2
0.017 0.046 0.100 0.174 0.242 0.271 0.242 0.174 0.100 0.046 0.017 
3
0.046 0.124 0.271 0.472 0.659 0.736 0.659 0.472 0.271 0.124 0.046 
4
0.100 0.271 0.590 1.028 1.434 1.603 1.434 1.028 0.590 0.271 0.100 
5
0.174 0.472 1.028 1.791 2.500 2.793 2.500 1.791 1.028 0.472 0.174 
6
0.242 0.659 1.434 2.500 3.488 3.898 3.488 2.500 1.434 0.659 0.242 
7
0.271 0.736 1.603 2.793 3.898 4.356 3.898 2.793 1.603 0.736 0.271 
8
0.242 0.659 1.434 2.500 3.488 3.898 3.488 2.500 1.434 0.659 0.242 
9
0.174 0.472 1.028 1.791 2.500 2.793 2.500 1.791 1.028 0.472 0.174 
10
0.100 0.271 0.590 1.028 1.434 1.603 1.434 1.028 0.590 0.271 0.100 
11
0.046 0.124 0.271 0.472 0.659 0.736 0.659 0.472 0.271 0.124 0.046 
12
0.017 0.046 0.100 0.174 0.242 0.271 0.242 0.174 0.100 0.046 0.017 
terrain/research/oregon/arcgis/v1/layers.aml
1
/* using srtmv41 3x3 degree patch smoothed to srtmv41_smth
2

  
3
&describe srtmv41_smth
4
&sv cellsize %grd$dx% * 111000
5
dzdx = (srtmv41_smth(1,0) - srtmv41_smth(-1,0)) / (2 * %cellsize% * cos($$ymap / deg))
6
dzdy = (srtmv41_smth(0,-1) - srtmv41_smth(0,1)) / (2 * %cellsize%)
7
slope = sqrt(sqr(dzdx) + sqr(dzdy))
8
slopedeg = atan(slope) * deg
9
/* use scaling factor that is average of x and y scaling factors, 0.707 and 1 ~= 0.85
10
/* and 0.85 / 111000 ~ 0.0000107
11
arcslope = slope(srtmv41_smth, 0.0000107, degree)
12
hillshade = hillshade(srtmv41_smth, 300, 30, shade, 0.00001)
13

  
14
aspect1 = atan2(0 - dzdy, 0 - dzdx) * deg
15
aspect = con(aspect1 > 90, 450 - aspect1, 90 - aspect1)
16
kill aspect1
17

  
18
/*use elevation 
19
flowdir = flowdirection(srtmv41_smth)
20

  
21
/*cell area to account for latitudinal area effects, smallest area near pole
22
setwindow srtmv41_smth
23
setcell srtmv41_smth 
24
cellarea = sqr(%cellsize%) * cos($$ymap / deg)
25
flowacc = flowaccumulation(flowdir, cellarea)
26

  
27
/*account for sinks
28
/*change # defaulto z limit
29
/*caution fill is a command not function
30
/*fill srtmv41_smth srtmv41_fill sink # flowdir_fill
31

  
32
flowacc_fill = flowaccumulation(flowdir_fill, cellarea)
33

  
34
/*scatch_fill is specific catchment area in m 
35
scatch_fill = flowacc_fill / sqrt(cellarea) + sqrt(cellarea) / 2
36
/*setting the min slope to 0.1
37
adjslope = tan(max(slopedeg, 0.1) / deg)
38

  
39
twi_fill = ln(scatch_fill / adjslope)
terrain/research/oregon/arcgis/v1/mrvbf6g-a3.aml
1
/*   WRR paper is based on vd5r.aml. Prior history in vd5r.aml
2
/*
3
/*   mrvbf6a - vd5r modified to incorporate min (fuzzy and) instead of 
4
/*	multiplication when combining flatness and lowness. The transformation
5
/*	following combination of flatness and lowness is no longer used, so
6
/*	tvf and rvf layers are not computed, vf and rf are used directly.
7
/*
8
/*   mrvbf6b - mrvbf6a modified to use hard thresholds when recombining
9
/*	resolutions, rather than the smooth approach that resulted in
10
/*	values bleeding into lower resolution classes
11
/*	
12
/*	-2 changed threshold for converting pctl to low from 0.4 to 0.5
13
/*
14
/*   mrvbf6c - tried using standard deviation - not continued
15
/*
16
/*   mrvbf6d - another attempt to remove small hilltops from inclusion in
17
/*	valley bottom classes by including a fraction of the low indicator
18
/*	from the previous scale
19
/*
20
/*	-2 changed weighting of previous low/high from 0.3 to 0.5
21
/*
22
/*   mrvbf6f - (ignoring the 6e experiment)
23
/*	-1 Following results from Linda Gregory's transcription of the
24
/*	   method to Python in ArcGIS, removed the use of nibble to
25
/*	   extend the DEM beyond its original borders. Seems to be 
26
/*	   unnecessary.
27
/*	   Included extension of grids by one pixel at each step
28
/*
29
/*   mrvbf6g - all of 6f, plus std dev
30
/*         Use sd thresholds with _3 applying to level 3, _9 to level 4 etc 
31
/*	-2 Reduce standard deviation threshold by a factor of 2, so first
32
/*	   threshold is 4 instead of 8. This follows analysis of -1 that
33
/*	   shows that ridge tops are still being left in that should be
34
/*	   eliminated.
35
/*	-3 Reduce pctl radius from 6 cells to 3 at all steps. Radius is the
36
/*	   same for first two steps.
37
/*	-7 As for -3, but change carrying forward of low indicator to use 
38
/*	   raw pctl from previous scale, not low weight. This means that the
39
/*	   information only comes from one scale finer, not all finer scales.
40
/*	   This is to attempt to remedy the defects introduced by the -3 version
41
/*	   while retaining its ability to remove some problems.
42
/*	-10 As for -8, but pctl radius at 6 cells from step 2 onwards.
43
/*
44
/*	-a2 Added code to automatically detect geographic coords
45
/*	    Calculates sdthres from resolution, same as slopethres
46
/*	-a3 Incorporate mssd calculations directly in the AML, using 
47
/*	    correct methods developed in SRTM project; changed name of 
48
/*	    sd layers to match slope naming
49

  
50

  
51
&args dem
52
/* dem: name of dem to work from
53

  
54
&sv keep 1
55
/* higher keep values result in more intermediate results being kept
56

  
57
&describe %dem%
58
&sv mindim = [min %grd$ncols% %grd$nrows%]
59
&sv maxresmult = 1
60
&sv nsteps = 2
61
&do &while [calc %mindim% / %maxresmult%] > [calc 5 * 3]
62
  &sv maxresmult = [calc %maxresmult% * 3]
63
  &sv nsteps = [calc %nsteps% + 1]
64
&end
65
&type Minimum DEM dimension = %mindim%, max resolution = %maxresmult%
66
&type %nsteps% steps
67

  
68

  
69

  
70
&if [null %prj$units%] &then
71
  &return &error No horizontal units defined for DEM %dem%
72

  
73

  
74
&select %prj$units%
75
  &when METERS
76
    &do
77
      &type DEM horizontal units is metres, good
78
      &sv geographic = .false.
79
    &end
80

  
81
  &when DD
82
    &do
83
      &type DEM horizontal units is decimal degrees, scale
84
      &sv geographic = .true.
85
    &end
86

  
87
  &otherwise
88
      &return &error Unrecognised horizontal units for DEM %dem%: %prj$units%
89

  
90
&end
91

  
92
/* set up scaling factors for geographic data
93
&if %geographic% &then &do
94
  &type Calculating geographic scaling factors
95
  &sv xyfactor [calc 110000 * [cos [calc ( %grd$ymin% + %grd$ymax% ) / 2 / 57.2958] ]  ]
96
  &sv zfactor [calc 1 / %xyfactor% ]
97
  &end
98
&else &do
99
  &sv xyfactor 1
100
  &sv zfactor 1
101
&end
102

  
103
&sv baseres = %grd$dx%
104
&sv resmetres = [calc %baseres% * %xyfactor%]
105

  
106
&type DEM resolution = %resmetres% metres
107

  
108
&sv minvbf = 1
109
&sv vbfres = 4  /* maximum resolution for first vbf = 1
110
&sv slopethres = 64  /* 64% = 33 degrees, about steepest for any deposit
111
&sv sdthres = 0.375  /* developed with 1.5 at 25 m
112
&sv pctlradius = [calc %vbfres% * 3]
113
&do &while %vbfres% < %resmetres%
114
  &sv minvbf = [calc %minvbf% + 1]
115
  &sv vbfres = [calc %vbfres% * 3]
116
  &sv slopethres = [calc %slopethres% / 2]
117
  &sv sdthres = [calc %sdthres% * 2]
118
  &sv pctlradius = [calc %pctlradius% * 3]
119
&end
120
&type CHOICE setting pctlradius = 3 * dem resolution
121
&sv pctlradius = [calc %resmetres% * 3.02]
122
/* Modified 18/10/2006 to overcome numerical issues in pctl's cell inclusion calcs
123

  
124
&type TEMPORARY setting minvbf = 1
125
&sv minvbf = 1
126

  
127
&type VBF for base resolution = %minvbf%
128
&type Slope threshold = %slopethres%, percentile radius = %pctlradius%
129

  
130
setwindow %dem%
131
setcell minof
132

  
133
/* set weighting for pctl value from previous step
134
&sv prevpctlwt = 0.5
135

  
136

  
137
&sv l = 0
138
&sv v = %minvbf%
139

  
140
&sv l = 0
141
&sv l1 = 0
142
&sv res = %baseres%
143

  
144
&do &while %l% < %nsteps%
145
  &if %l% = 0 &then &sv l = 1
146
    &else &sv l = [calc %l% + 1]
147
  &type === Step %l% ====
148
  &if %l% > 2 &then &do
149
    &sv res = [calc %res% * 3]
150
    &sv resmetres = [calc %res% * %xyfactor%]
151
  &end
152
  &type Resolution = %res% (%resmetres% metres)
153
  &type Slope threshold = %slopethres%, pctl radius = %pctlradius%
154
  &type Std dev threshold = %sdthres%
155

  
156
  &type Checkpoint 1
157

  
158
  &if %l% > 2 &then &do
159
    &type Checkpoint 1.1
160
    /* create a smoothed version of the DEM from the previous resolution
161
    &if [exists dem_%l%_%l1% -grid] &then kill dem_%l%_%l1%
162
    &describe %dem%
163
    setwindow [calc %grd$xmin% - %res%] [calc %grd$ymin% - %res%] [calc %grd$xmax% + %res%] [calc %grd$ymax% + %res%]
164
    &if %l% = 3 &then &do
165
      dem_%l%_%l1% = focalsum(%dem%, weight, gauss3, data) / focalsum(con(%dem% > -999, 1, 0), weight, gauss3, data)
166
    &end
167
    &else &do
168
      dem_%l%_%l1% = focalsum(dem_%l1%_%l1%, weight, gauss3, data) / focalsum(con(dem_%l1%_%l1% > -999, 1, 0), weight, gauss3, data)
169
    &end
170

  
171
    &type Checkpoint 1.2
172
    /* calculate standard deviation
173
    &if [exists sd_%l% -grid] &then kill sd_%l%
174
    &if %l% = 3 &then &do
175
      sd_%l% = resample(blockstd(%dem%, rectangle, 3, 3), %res%, nearest)
176
    &end
177
    &else &do
178
      &if [exists sswg_%l% -grid] &then kill sswg_%l%
179
      &if [exists ssbg_%l% -grid] &then kill ssbg_%l%
180
      sswg_%l% = aggregate(pow(sd_%l1%, 2), 3, sum)
181
      ssbg_%l% = 9 * pow(resample(blockstd(dem_%l1%_%l1%, rectangle, 3, 3), %res%, nearest), 2)
182
      sd_%l% = sqrt((sswg_%l% + ssbg_%l%) / 9)
183
      &if %keep% <= 4 &then &do
184
        kill ssbg_%l%
185
        kill sswg_%l%
186
      &end
187
    &end
188

  
189
    &type Checkpoint 1.3
190
    &if %keep% <= 2 &then &do
191
      /* get rid of DEM from previous step
192
      &if %l1% > 2 &then kill dem_%l1%_%l1%
193
      &if %l1% > 2 &then kill sd_%l1%
194
    &end
195
  &end
196

  
197
  &type Checkpoint 2
198

  
199
  &if [exists s_%l%_1 -grid] &then kill s_%l%_1
200
  &if %l% = 1 &then &do
201
    /* first step, compute slope from base resolution
202
    &type Compute slope from DEM
203
    s_1_1 = slope(%dem%, %zfactor%, percentrise)
204
  &end 
205
  &else &if %l% = 2 &then &do
206
    /* second step, slope is same as from first step
207
    &if %keep% <= 4 &then rename s_1_1 s_2_1
208
    &else copy s_1_1 s_2_1
209
  &end
210
  &else &do
211
    /* compute slope from smoothed DEM
212
    &if [exists s_%l%_%l1% -grid] &then kill s_%l%_%l1%
213
    s_%l%_%l1% = slope(dem_%l%_%l1%, %zfactor%, percentrise)
214
    &if %l% > 2 &then &do
215
      /* resample back to base resolution
216
      s_%l%_1 = resample(s_%l%_%l1%, %baseres%, bilinear)
217
    &end
218
    &if %keep% <= 5 &then kill s_%l%_%l1%
219

  
220
    &if [exists dem_%l%_%l% -grid] &then kill dem_%l%_%l%
221
    dem_%l%_%l% = resample(dem_%l%_%l1%, %res%)
222
    &if %keep% <= 5 &then kill dem_%l%_%l1%
223
  &end
224
  &if [exists f_%l% -grid] &then kill f_%l%
225
  f_%l% = 1 / (1 + pow(s_%l%_1 / %slopethres%, 4))
226
  &if %keep% <= 4 &then &do
227
    /* get rid of s_%l%_1, except when l = 1 and we need it for s_2_1
228
    &if %l% ^= 1 &then kill s_%l%_1
229
  &end
230

  
231
  &type Checkpoint 3
232

  
233
  &if [exists pctl_%l%_1 -grid] &then kill pctl_%l%_1
234
  &if [exists cpctl_%l%_1 -grid] &then kill cpctl_%l%_1
235
  &if [exists cf_%l% -grid] &then kill cf_%l%
236
  &if [exists low_%l% -grid] &then kill low_%l%
237
  &if [exists vf_%l% -grid] &then kill vf_%l%
238
  &if [exists high_%l% -grid] &then kill high_%l%
239
  &if [exists rf_%l% -grid] &then kill rf_%l%
240
  &if %l% <= 2 &then &do
241
    &r chunkproc pctl %dem% pctl_%l%_1 %pctlradius% %geographic%
242
    &if %l% = 1 &then &do
243
      low_%l% = 1 / (1 + pow(pctl_%l%_1 / 0.5, 3))
244
    &end
245
    &else &do
246
      cpctl_%l%_1 = [calc 1 - %prevpctlwt%] * pctl_%l%_1 + %prevpctlwt% * pctl_%l1%_1
247
      low_%l% = 1 / (1 + pow(cpctl_%l%_1 / 0.5, 3))
248
    &end
249
    vf_%l% = min(f_%l%, low_%l%)
250
    &if %l% = 1 &then &do
251
      high_%l% = 1 / (1 + pow((1 - pctl_%l%_1) / 0.5, 3))
252
    &end
253
    &else &do
254
      high_%l% = 1 / (1 + pow((1 - cpctl_%l%_1) / 0.5, 3))
255
    &end
256
    rf_%l% = min(f_%l%, high_%l%)
257
    &if %l% = 2 &then &do
258
      cf_2 = f_2 * f_1
259
      &if %keep% <= 3 &then kill f_1
260
    &end
261
  &end
262
  &else &do
263
    &if [exists pctl_%l%_%l% -grid] &then kill pctl_%l%_%l%
264
    &r chunkproc pctl dem_%l%_%l% pctl_%l%_%l% %pctlradius% %geographic%
265
    pctl_%l%_1 = resample(pctl_%l%_%l%, %baseres%, bilinear)
266
    cpctl_%l%_1 = [calc 1 - %prevpctlwt%] * pctl_%l%_1 + %prevpctlwt% * pctl_%l1%_1
267
    &if %keep% <= 5 &then kill pctl_%l%_%l%
268
    cf_%l% = f_%l% * cf_%l1%
269
    &if %keep% <= 3 &then kill cf_%l1%
270
    low_%l% = 1 / (1 + pow(cpctl_%l%_1 / 0.5, 3))
271
    &if [exists lowrelief_%l% -grid] &then kill lowrelief_%l%
272
    lowrelief_%l% = 1 / (1 + pow(sd_%l% / %sdthres%, 4))
273

  
274
    vf_%l% = min(cf_%l%, low_%l%, lowrelief_%l%)
275
    high_%l% = 1 / (1 + pow((1 - cpctl_%l%_1) / 0.5, 3))
276
    rf_%l% = min(cf_%l%, high_%l%, lowrelief_%l%)
277
    &if %keep% <= 3 &then kill lowrelief_%l%
278
  &end
279

  
280
  &type Checkpoint 4
281

  
282
  &if %l% > 1 &then &do
283
    &if %keep% <= 4 &then kill pctl_%l1%_1
284
    &if %keep% <= 4 &then kill cpctl_%l%_1
285
    &if %keep% <= 3 &then kill f_%l%
286
  &end
287
  &if [exists mrvbf_%l% -grid] &then kill mrvbf_%l%
288
  &if [exists mrrtf_%l% -grid] &then kill mrrtf_%l%
289
  &if %l% = 2 &then &do
290
    mrvbf_%l% = con(vf_2 >= 0.5, vf_2 + %v% - 1, vf_1)
291
    mrrtf_%l% = con(rf_2 >= 0.5, rf_2 + %v% - 1, rf_1)
292
    &if %keep% <= 3 &then kill vf_1
293
    &if %keep% <= 3 &then kill rf_1
294
  &end
295
  &else &if %l% > 2 &then &do
296
      mrvbf_%l% = con(vf_%l% >= 0.5, vf_%l% + %v% - 1, mrvbf_%l1%)
297
      mrrtf_%l% = con(rf_%l% >= 0.5, rf_%l% + %v% - 1, mrrtf_%l1%)
298
     &if %keep% <= 3 &then kill mrvbf_%l1%
299
     &if %keep% <= 3 &then kill mrrtf_%l1%
300
  &end
301
  &if %l% >= 2 &then &do
302
    &if %keep% <= 3 &then kill vf_%l%
303
    &if %keep% <= 3 &then kill rf_%l%
304
  &end
305

  
306
  &type Checkpoint 5
307

  
308
  &sv v = [calc %v% + 1]
309

  
310
  &sv l1 = %l%
311
  &sv slopethres = [calc %slopethres% / 2]
312
  &if %l% > 2 &then &do
313
    &sv sdthres = [calc %sdthres% * 2]
314
  &end
315
  &if %l% = 1 &then &sv pctlradius = [calc %pctlradius% * 2]
316
  &else &sv pctlradius = [calc %pctlradius% * 3]
317

  
318
  &type Checkpoint 6
319
&end
320

  
321
&if %keep% <= 4 &then kill cf_%l%
322
&if %keep% <= 4 &then kill pctl_%l%_1
323
&if %keep% <= 2 &then kill dem_%l%_%l%
324

  
325
&if [exists mrvbf6g-a -grid] &then kill mrvbf6g-a
326
&if [exists mrrtf6g-a -grid] &then kill mrrtf6g-a
327
mrvbf6g-a = con(%dem% > -999, con(mrvbf_%l% > [calc %minvbf% - 0.5], mrvbf_%l%, 0))
328
mrrtf6g-a = con(%dem% > -999, con(mrrtf_%l% > [calc %minvbf% - 0.5], mrrtf_%l%, 0))
329
&if %keep% <= 2 &then kill mrvbf_%l%
330
&if %keep% <= 2 &then kill mrrtf_%l%
331

  
terrain/research/oregon/arcgis/v1/multiscalesmooth9a_clean.aml
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
terrain/research/oregon/arcgis/v1/pctl-limits.aml
1
&args gridname radius geographic
2

  
3
&describe %gridname%
4

  
5
&if %geographic% &then &sv xyfactor [calc 110000 * [cos [calc ( %grd$ymin% + %grd$ymax% ) / 2 / 57.2958] ]  ]
6
&else &sv xyfactor 1
7

  
8

  
9
&sv radiuscells = [calc %radius% / ( %grd$dx% * %xyfactor% )]
10

  
11
&type radius = %radiuscells% cells
12

  
13
&sv .maxcells 50000000
14
/* &sv .maxcells 500000 /* Testing on Kyeamba 
15
&sv .border [calc %radiuscells% + 1]
terrain/research/oregon/arcgis/v1/pctl.aml
1
&args dem pctlname radius geographic
2

  
3
&type Running pctl %dem% %pctlname% %radius% %geographic%
4

  
5
&if [exists demtmp.flt -file] &then [delete demtmp.flt -file]
6
demtmp.flt = gridfloat(%dem%)
7
&if %geographic% &then &sys pctl -ud %radius% demtmp.flt pctl.flt
8
&else &sys pctl %radius% demtmp.flt pctl.flt
9
&if [delete demtmp.flt -file] ^= 0 &then &type ERROR deleting demtmp.flt
10
&if [delete demtmp.hdr -file] ^= 0 &then &type ERROR deleting demtmp.hdr
11
&if [delete demtmp.prj -file] ^= 0 &then &type ERROR deleting demtmp.prj
12
&if [exists %pctlname% -grid] &then kill %pctlname%
13
%pctlname% = floatgrid(pctl.flt)
14
&if [delete pctl.flt -file] ^= 0 &then &type ERROR deleting pctl.flt
15
&if [delete pctl.hdr -file] ^= 0 &then &type ERROR deleting pctl.hdr
terrain/research/oregon/arcgis/v1/tilemerge_oregon1.aml
1
/* merge 9 tiles in Oregon around Portland for testing methods
2

  
3

  
4
&if %:program%_ eq ARC_ &then grid
5
&if %:program%_ ne GRID_ &then
6
&return This program must be run from ARC or GRID.
7

  
8
&sv tiledir \\jupiter\\organisms\topo\tiles
9
&sv outdir \\jupiter\\organisms\topo\experimental
10
&sv name srtmv41
11
&sv vernum 1
12

  
13
/* This can be extended to larger areas (beyond 3x3 used here) but at some stage the
14
/* tilelist variable is going to get too long - AML has a limited line length
15
/* At this point, it would be better to tile by longitude band i.e. do a merge for
16
/* each value of w, then merge the resulting strips as a separate step
17
&sv tilelist
18
&do w = 122 &to 124
19
  &do n = 44 &to 46
20
    &sv tilelist %tilelist%, %tiledir%\w%w%\n%n%\%vernum%\%name%\w%w%n%n%
21
  &end
22
&end
23

  
24
%outdir%\srtmv41 = merge(%tilelist%)
25
:q
terrain/research/oregon/arcgis/v1/unpacktiles.aml
1
/* look for all .asc files in input dir
2
/* load them and split into 1 degree tiles
3

  
4
&if %:program%_ eq ARC_ &then grid
5
&if %:program%_ ne GRID_ &then
6
&return This program must be run from ARC or GRID.
7

  
8

  
9
&sv indir \\jupiter\\organisms\topo\incoming\srtmv41
10
&sv tiledir \\jupiter\\organisms\topo\tiles
11
&sv name srtmv41
12
&sv vernum 1
13

  
14

  
15
&do x = 19 &to 72
16
  &if %x% > 9 &then &sv xn %x%; &else &sv xn 0%x%
17
  &do y = 1 &to 36
18
    &if %y% > 9 &then &sv yn %y%; &else &sv yn 0%y%
19
    &sv ascfile %indir%\srtm_%xn%_%yn%.asc
20
    &if [exists %ascfile% -file] &then
21
    &do
22
      &type Got %ascfile%
23
      
24
      /* import the ASCII file 
25
      &sv tmpgrd [scratchname -dir -full]
26
      /* &sv tmpgrd c:\workspace\xx00000 - was using for testing
27
      /* &type Using %tmpgrd% as temporary grid
28
      %tmpgrd% = asciigrid(%ascfile%)
29
      
30
      /* determine x (long) and y (lat) range
31
      &describe %tmpgrd%
32
      &sv xmin %grd$xmin%
33
      &sv ymin %grd$ymin%
34
      &sv xmax %xmin% + 5
35
      &sv ymax %ymin% + 5
36
      &sv cell %grd$dx%
37
      
38
      &type %xmin% %ymin% %xmax% %ymax%
39
      
40
      &do x = %xmin% &to %xmax%
41
        &if %x% < 0 &then &sv xtile w[round [abs %x%]]; &else &sv xtile e[round %x%]
42
        &sv x1 %x% + 1
43
        &do y = %ymin% &to %ymax%
44
          &if %y% < 0 &then &sv ytile s[round [abs %y%]]; &else &sv ytile n[round %y%]
45
          &sv y1 %y% + 1
46
          setwindow %x% %y% %x1% %y1%
47
          &type Tile %x% %y%
48
          show setwindow
49
          &sv tmptile [scratchname -dir -full]
50
          &type Create tile %xtile%%ytile%
51
          %tmptile% = %tmpgrd%
52
          &describe %tmptile%
53
          &if %grd$stdv% > 0 &then &do
54
            /* grid is not all nodata
55
            &sv dirname %tiledir%\%xtile%
56
            &if ^ [exists %dirname% -directory] &then &sys mkdir %dirname%
57
            &sv dirname %tiledir%\%xtile%\%ytile%
58
            &if ^ [exists %dirname% -directory] &then &sys mkdir %dirname%
59
            &sv dirname %tiledir%\%xtile%\%ytile%\%vernum%
60
            &if ^ [exists %dirname% -directory] &then &sys mkdir %dirname%
61
            &sv dirname %tiledir%\%xtile%\%ytile%\%vernum%\%name%
62
            &if ^ [exists %dirname% -directory] &then &sys mkdir %dirname%
63
            &sv gridname %dirname%\%xtile%%ytile%
64
            &if [exists %gridname% -grid] &then &do
65
              &type WARNING: did not expect to find %gridname%!
66
            &end
67
            &else &do
68
              copy %tmptile% %dirname%\%xtile%%ytile%
69
            &end
70
          &end
71
          kill %tmptile%
72
        &end
73
      &end
74
    
75
      kill %tmpgrd%
76
      
77
    &end
78
  &end 
79
&end
terrain/research/oregon/arcgis/v1/unziptiles.py
1
import os, fnmatch, zipfile, datetime
2

  
3
outpath = '//jupiter/organisms/topo/incoming/srtmv41'
4
inpath = '//jupiter/organisms/SRTM_90m_ASCII_v4.1'
5
logname = outpath + '/unzip.log'
6
logfile = open(logname, 'a')
7
logfile.write('Start unziptiles.py at ' + str(datetime.datetime.today()) + '\n')
8

  
9
nprocessed = 0
10
nerrors = 0
11

  
12
try:
13
    
14
    for filename in os.listdir(inpath):
15
        fullname = inpath + '/' + filename
16
        if fnmatch.fnmatch(fullname, '*.zip'):
17
            print fullname
18
            try:
19
                zip = zipfile.ZipFile(fullname, 'r')
20
                for zipi in zip.infolist():
21
                    name = zipi.filename
22
                    if name != 'readme.txt':
23
                        outfilename = outpath + '/' + name
24
                        print outfilename
25
                        if os.path.exists(outfilename):
26
                            print 'done'
27
                        else:
28
                            print 'Process', outfilename
29
                            outfile = open(outfilename, 'w')
30
                            outfile.write(zip.read(name))
31
                            outfile.close()
32
                            nprocessed = nprocessed + 1
33
                zip.close()
34
            except Exception, e:
35
                print str(e)
36
                logfile.write(str(e) + '\n')
37
                nerrors = nerrors + 1
38
except Exception, e:
39
    print "Exception:", e.str()
40
    logfile.write(str(e) + '\n')
41

  
42
logfile.write('Processed ' + str(nprocessed) + '\n')
43
logfile.write('Errors: ' + str(nerrors) + '\n')
44
logfile.write('End unziptiles.py at ' + str(datetime.datetime.today()) + '\n')
45
logfile.close()
terrain/research/oregon/arcgis/v2/aggregate.aml
1
/* need to ensure that the aggregation boxes are consistent across the whole set of layers
2
/* so might need a setwindow here to enforce the correct location
3

  
4
/* Question about 1km Behrman version - should that be projected from 30" stats, or
5
/* should data be projected to Behrman and aggregated over 1km cell. Far from the equator
6
/* these will give quite different results
7

  
8
/* elevation - derive mean, min, max, std. dev, skew
9
elev_mean = aggregate(srtm_or_smth, 10, mean)
10
elev_min = aggregate(srtm_or_smth, 10, min)
11
elev_max = aggregate(srtm_or_smth, 10, max)
12
elev_range = elev_max - elev_min
13

  
14
/* stupid arc doesn't have a standard deviation function for aggregation
15
elev_sd = sqrt(aggregate(sqr(srtm_or_smth - elev_mean), 10, sum) / 100)
16

  
17

  
18
/* slope - just mean and standard deviation
19
slopedeg_mean = aggregate(slopedeg, 10, mean)
20
slopedeg_sd = sqrt(aggregate(sqr(slopdeg - slopedeg_mean), 10, sum) / 100)
21

  
22
/* wetness index - mean and standard deviation
23
/* twi not done yet
24
twi_fill_mean = aggregate(twi_fill, 10, mean)
25
twi_fill_sd = sqrt(aggregate(sqr(twi_fill - twi_fill_mean), 10, sum) / 100)
terrain/research/oregon/arcgis/v2/chunkproc.aml
1
/* Invoke another AML on a grid to produce a grid
2
/* Other AML takes arguments: gridname outgridname otherargs
3

  
4
&args targetaml gridname outgridname otherargs:REST
5

  
6

  
7
/* targetaml-limits.aml takes same arguments as target.aml (except for
8
/* output file) and sets maximum number of cells and overlap required
9
&run %targetaml%-limits %gridname% [unquote %otherargs%]
10

  
11
&describe %gridname%
12
&sv gridxmin = %grd$xmin%
13
&sv gridxmax = %grd$xmax%
14
&sv gridymin = %grd$ymin%
15
&sv gridymax = %grd$ymax%
16
&sv gridncols = %grd$ncols%
17
&sv gridnrows = %grd$nrows%
18
&sv cellsize = %grd$dx%
19

  
20
/****** TEMPORARY *******/
21
/* setcell %cellsize%
22
/**** END TEMPORARY *****/
23

  
24
&sv ncells = %grd$ncols% * %grd$nrows%
25
&type %ncells% cells in %gridname%
26

  
27
&if %ncells% <= %.maxcells% &then &do
28
  /* no need to do chunks, just invoke target AML
29
  &type Process as single chunk
30
  &run %targetaml% %gridname% %outgridname% [unquote %otherargs%]
31
  &return
32
&end
33

  
34
/* need to do chunks
35
&type Process in multiple chunks
36
/* &type X: %gridxmin% %gridxmax% (%gridncols%) 
37
/* &type Y: %gridymin% %gridymax% (%gridnrows%) 
38
/* &type %ncells% cells, %.maxcells% max, %.border% border
39

  
40
&sv nchunks = [truncate [calc %ncells% / %.maxcells%]] + 1
41
&type Need at least %nchunks% chunks
42
&sv chunkdim = [sqrt %.maxcells%] + 2 * %.border%
43

  
44
&if %gridncols% < %chunkdim% &then
45
  &do
46
    &sv colchunks = 1
47
    &sv rowchunks = %nchunks%
48
    &sv chunkncols = %gridncols%
49
    &sv chunknrows = [truncate [calc %gridnrows% / %rowchunks%]] + 1
50
  &end
51
&else &if %gridnrows% < %chunkdim% &then
52
  &do
53
    &sv rowchunks = 1
54
    &sv colchunks = %nchunks%
55
    &sv chunkncols = [truncate [calc %gridncols% / %colchunks%]] + 1
56
  &end
57
&else 
58
  &do
59
    &sv rowchunks = [truncate [calc %gridnrows% / %chunkdim%]] + 1
60
    &sv colchunks = [truncate [calc %gridncols% / %chunkdim%]] + 1
61
    &sv chunkncols = [truncate [calc %gridncols% / %colchunks%]] + 1
62
    &sv chunknrows = [truncate [calc %gridnrows% / %rowchunks%]] + 1
63
  &end
64

  
65

  
66
&type %colchunks% colchunks, %rowchunks% rowchunks
67
&type Chunk size %chunkncols% cols by %chunknrows% rows
68

  
69
&sv qtrcell = %cellsize% / 4
70
&sv halfcell = %cellsize% / 2
71
&sv colist
72

  
73
&sv chunki = 1
74
&do &while %chunki% <= %colchunks%
75
  &sv chunkj = 1
76
  &sv colist_col
77
  &do &while %chunkj% <= %rowchunks%
78
    &type Chunk %chunki% %chunkj%
79

  
80
    &sv coxmin = %gridxmin% + ( %chunki% - 1 ) * %chunkncols% * %cellsize% - %qtrcell%
81
    &sv coxmax = %coxmin% + %chunkncols% * %cellsize% + %halfcell%
82
    &sv coymin = %gridymin% + ( %chunkj% - 1 ) * %chunknrows% * %cellsize% - %qtrcell%
83
    &sv coymax = %coymin% + %chunknrows% * %cellsize% + %halfcell%
84

  
85
    /* &type Output X: %coxmin% %coxmax%
86
    /* &type Output Y: %coymin% %coymax%
87

  
88
    &if %chunki% = 1 &then &sv chunkxmin = %coxmin%
89
    &else &sv chunkxmin = %coxmin% - %.border% * %cellsize%
90
    &if %chunki% = %colchunks% &then &sv chunkxmax = %coxmax%
91
    &else &sv chunkxmax = %coxmax% + %.border% * %cellsize%
92

  
93
    &if %chunkj% = 1 &then &sv chunkymin = %coymin%
94
    &else &sv chunkymin = %coymin% - %.border% * %cellsize%
95
    &if %chunkj% = %rowchunks% &then &sv chunkymax = %coymax%
96
    &else &sv chunkymax = %coymax% + %.border% * %cellsize%
97

  
98
    /* &type Chunk X: %chunkxmin% %chunkxmax%
99
    /* &type Chunk Y: %chunkymin% %chunkymax%
100

  
101
    /* clip input file to chunk (with borders)
102
    setwindow %chunkxmin% %chunkymin% %chunkxmax% %chunkymax% %gridname%
103
    &if [exists chunk -grid] &then kill chunk
104
    chunk = %gridname%  
105
    &if [exists co_tmp -grid] &then kill co_tmp
106
    &run %targetaml% chunk co_tmp [unquote %otherargs%]
107
    kill chunk
108

  
109
    /* clip output file to chunk (without borders)
110
    setwindow %coxmin% %coymin% %coxmax% %coymax% %gridname%
111
    &if [exists co_%chunki%_%chunkj% -grid] &then kill co_%chunki%_%chunkj%
112
    co_%chunki%_%chunkj% = co_tmp
113
    kill co_tmp
114

  
115
    &if %chunkj% = 1 &then &sv colist_col = co_%chunki%_%chunkj%
116
    &else &sv colist_col = %colist_col%, co_%chunki%_%chunkj%
117

  
118
    &sv chunkj = %chunkj% + 1
119
  &end
120
  setwindow maxof
121
  &if [exists co_%chunki% -grid] &then kill co_%chunki%
122
  co_%chunki% = merge(%colist_col%)
123
  kill (!%colist_col%!)
124
  &if %chunki% = 1 &then &sv colist = co_%chunki%
125
  &else &sv colist = %colist%, co_%chunki%
126
  &sv chunki = %chunki% + 1
127
&end
128

  
129
setwindow maxof
130
&if [exists %outgridname% -grid] &then kill %outgridname%
131
%outgridname% = merge(%colist%)
132
kill (!%colist%!)
terrain/research/oregon/arcgis/v2/gauss3
1
11 11
2
0.017 0.046 0.100 0.174 0.242 0.271 0.242 0.174 0.100 0.046 0.017 
3
0.046 0.124 0.271 0.472 0.659 0.736 0.659 0.472 0.271 0.124 0.046 
4
0.100 0.271 0.590 1.028 1.434 1.603 1.434 1.028 0.590 0.271 0.100 
5
0.174 0.472 1.028 1.791 2.500 2.793 2.500 1.791 1.028 0.472 0.174 
6
0.242 0.659 1.434 2.500 3.488 3.898 3.488 2.500 1.434 0.659 0.242 
7
0.271 0.736 1.603 2.793 3.898 4.356 3.898 2.793 1.603 0.736 0.271 
8
0.242 0.659 1.434 2.500 3.488 3.898 3.488 2.500 1.434 0.659 0.242 
9
0.174 0.472 1.028 1.791 2.500 2.793 2.500 1.791 1.028 0.472 0.174 
10
0.100 0.271 0.590 1.028 1.434 1.603 1.434 1.028 0.590 0.271 0.100 
11
0.046 0.124 0.271 0.472 0.659 0.736 0.659 0.472 0.271 0.124 0.046 
12
0.017 0.046 0.100 0.174 0.242 0.271 0.242 0.174 0.100 0.046 0.017 
terrain/research/oregon/arcgis/v2/layers.aml
1
/* using srtmv41 3x3 degree patch smoothed to srtmv41_smth
2
/* need to run GRID
3

  
4
&describe srtm_or_smth
5
&sv cellsize %grd$dx% * 111000
6
dzdx = (srtm_or_smth(1,0) - srtm_or_smth(-1,0)) / (2 * %cellsize% * cos($$ymap / deg))
7
dzdy = (srtm_or_smth(0,-1) - srtm_or_smth(0,1)) / (2 * %cellsize%)
8
slope = sqrt(sqr(dzdx) + sqr(dzdy))
9
slopedeg = atan(slope) * deg
10
/* use scaling factor that is average of x and y scaling factors, 0.707 and 1 ~= 0.85
11
/* and 0.85 / 111000 ~ 0.0000107
12
arcslope = slope(srtm_or_smth, 0.0000107, degree)
13
hillshade = hillshade(srtm_or_smth, 300, 30, shade, 0.00001)
14

  
15
aspect1 = atan2(0 - dzdy, 0 - dzdx) * deg
16
aspect = con(aspect1 > 90, 450 - aspect1, 90 - aspect1)
17
kill aspect1
18

  
19
/*use elevation 
20
flowdir = flowdirection(srtm_or_smth)
21

  
22
/*cell area to account for latitudinal area effects, smallest area near pole
23
setwindow srtm_or_smth
24
setcell srtm_or_smth 
25
cellarea = sqr(%cellsize%) * cos($$ymap / deg)
26
flowacc = flowaccumulation(flowdir, cellarea)
27

  
28
/*account for sinks
29
/*change # defaulto z limit
30
/*caution fill is a command not function
31
/*fill srtm_or_smth srtm_or_fill sink # flowdir_fill
32

  
33
flowacc_fill = flowaccumulation(flowdir_fill, cellarea)
34

  
35
/*scatch_fill is specific catchment area in m
36
/*wrong filling? flowdir_fill produces artefacts
37
scatch_fill = flowacc_fill / sqrt(cellarea) + sqrt(cellarea) / 2
38
/*setting the min slope to 0.1
39
adjslope = tan(max(slopedeg, 0.1) / deg)
40

  
41
twi_fill = ln(scatch_fill / adjslope)
terrain/research/oregon/arcgis/v2/mrvbf6g-a3.aml
1
/*   WRR paper is based on vd5r.aml. Prior history in vd5r.aml
2
/*
3
/*   mrvbf6a - vd5r modified to incorporate min (fuzzy and) instead of 
4
/*	multiplication when combining flatness and lowness. The transformation
5
/*	following combination of flatness and lowness is no longer used, so
6
/*	tvf and rvf layers are not computed, vf and rf are used directly.
7
/*
8
/*   mrvbf6b - mrvbf6a modified to use hard thresholds when recombining
9
/*	resolutions, rather than the smooth approach that resulted in
10
/*	values bleeding into lower resolution classes
11
/*	
12
/*	-2 changed threshold for converting pctl to low from 0.4 to 0.5
13
/*
14
/*   mrvbf6c - tried using standard deviation - not continued
15
/*
16
/*   mrvbf6d - another attempt to remove small hilltops from inclusion in
17
/*	valley bottom classes by including a fraction of the low indicator
18
/*	from the previous scale
19
/*
20
/*	-2 changed weighting of previous low/high from 0.3 to 0.5
21
/*
22
/*   mrvbf6f - (ignoring the 6e experiment)
23
/*	-1 Following results from Linda Gregory's transcription of the
24
/*	   method to Python in ArcGIS, removed the use of nibble to
25
/*	   extend the DEM beyond its original borders. Seems to be 
26
/*	   unnecessary.
27
/*	   Included extension of grids by one pixel at each step
28
/*
29
/*   mrvbf6g - all of 6f, plus std dev
30
/*         Use sd thresholds with _3 applying to level 3, _9 to level 4 etc 
31
/*	-2 Reduce standard deviation threshold by a factor of 2, so first
32
/*	   threshold is 4 instead of 8. This follows analysis of -1 that
33
/*	   shows that ridge tops are still being left in that should be
34
/*	   eliminated.
35
/*	-3 Reduce pctl radius from 6 cells to 3 at all steps. Radius is the
36
/*	   same for first two steps.
37
/*	-7 As for -3, but change carrying forward of low indicator to use 
38
/*	   raw pctl from previous scale, not low weight. This means that the
39
/*	   information only comes from one scale finer, not all finer scales.
40
/*	   This is to attempt to remedy the defects introduced by the -3 version
41
/*	   while retaining its ability to remove some problems.
42
/*	-10 As for -8, but pctl radius at 6 cells from step 2 onwards.
43
/*
44
/*	-a2 Added code to automatically detect geographic coords
45
/*	    Calculates sdthres from resolution, same as slopethres
46
/*	-a3 Incorporate mssd calculations directly in the AML, using 
47
/*	    correct methods developed in SRTM project; changed name of 
48
/*	    sd layers to match slope naming
49

  
50

  
51
&args dem
52
/* dem: name of dem to work from
53

  
54
&sv keep 1
55
/* higher keep values result in more intermediate results being kept
56

  
57
&describe %dem%
58
&sv mindim = [min %grd$ncols% %grd$nrows%]
59
&sv maxresmult = 1
60
&sv nsteps = 2
61
&do &while [calc %mindim% / %maxresmult%] > [calc 5 * 3]
62
  &sv maxresmult = [calc %maxresmult% * 3]
63
  &sv nsteps = [calc %nsteps% + 1]
64
&end
65
&type Minimum DEM dimension = %mindim%, max resolution = %maxresmult%
66
&type %nsteps% steps
67

  
68

  
69

  
70
&if [null %prj$units%] &then
71
  &return &error No horizontal units defined for DEM %dem%
72

  
73

  
74
&select %prj$units%
75
  &when METERS
76
    &do
77
      &type DEM horizontal units is metres, good
78
      &sv geographic = .false.
79
    &end
80

  
81
  &when DD
82
    &do
83
      &type DEM horizontal units is decimal degrees, scale
84
      &sv geographic = .true.
85
    &end
86

  
87
  &otherwise
88
      &return &error Unrecognised horizontal units for DEM %dem%: %prj$units%
89

  
90
&end
91

  
92
/* set up scaling factors for geographic data
93
&if %geographic% &then &do
94
  &type Calculating geographic scaling factors
95
  &sv xyfactor [calc 110000 * [cos [calc ( %grd$ymin% + %grd$ymax% ) / 2 / 57.2958] ]  ]
96
  &sv zfactor [calc 1 / %xyfactor% ]
97
  &end
98
&else &do
99
  &sv xyfactor 1
100
  &sv zfactor 1
101
&end
102

  
103
&sv baseres = %grd$dx%
104
&sv resmetres = [calc %baseres% * %xyfactor%]
105

  
106
&type DEM resolution = %resmetres% metres
107

  
108
&sv minvbf = 1
109
&sv vbfres = 4  /* maximum resolution for first vbf = 1
110
&sv slopethres = 64  /* 64% = 33 degrees, about steepest for any deposit
111
&sv sdthres = 0.375  /* developed with 1.5 at 25 m
112
&sv pctlradius = [calc %vbfres% * 3]
113
&do &while %vbfres% < %resmetres%
114
  &sv minvbf = [calc %minvbf% + 1]
115
  &sv vbfres = [calc %vbfres% * 3]
116
  &sv slopethres = [calc %slopethres% / 2]
117
  &sv sdthres = [calc %sdthres% * 2]
118
  &sv pctlradius = [calc %pctlradius% * 3]
119
&end
120
&type CHOICE setting pctlradius = 3 * dem resolution
121
&sv pctlradius = [calc %resmetres% * 3.02]
122
/* Modified 18/10/2006 to overcome numerical issues in pctl's cell inclusion calcs
123

  
124
&type TEMPORARY setting minvbf = 1
125
&sv minvbf = 1
126

  
127
&type VBF for base resolution = %minvbf%
128
&type Slope threshold = %slopethres%, percentile radius = %pctlradius%
129

  
130
setwindow %dem%
131
setcell minof
132

  
133
/* set weighting for pctl value from previous step
134
&sv prevpctlwt = 0.5
135

  
136

  
137
&sv l = 0
138
&sv v = %minvbf%
139

  
140
&sv l = 0
141
&sv l1 = 0
142
&sv res = %baseres%
143

  
144
&do &while %l% < %nsteps%
145
  &if %l% = 0 &then &sv l = 1
146
    &else &sv l = [calc %l% + 1]
147
  &type === Step %l% ====
148
  &if %l% > 2 &then &do
149
    &sv res = [calc %res% * 3]
150
    &sv resmetres = [calc %res% * %xyfactor%]
151
  &end
152
  &type Resolution = %res% (%resmetres% metres)
153
  &type Slope threshold = %slopethres%, pctl radius = %pctlradius%
154
  &type Std dev threshold = %sdthres%
155

  
156
  &type Checkpoint 1
157

  
158
  &if %l% > 2 &then &do
159
    &type Checkpoint 1.1
160
    /* create a smoothed version of the DEM from the previous resolution
161
    &if [exists dem_%l%_%l1% -grid] &then kill dem_%l%_%l1%
162
    &describe %dem%
163
    setwindow [calc %grd$xmin% - %res%] [calc %grd$ymin% - %res%] [calc %grd$xmax% + %res%] [calc %grd$ymax% + %res%]
164
    &if %l% = 3 &then &do
165
      dem_%l%_%l1% = focalsum(%dem%, weight, gauss3, data) / focalsum(con(%dem% > -999, 1, 0), weight, gauss3, data)
166
    &end
167
    &else &do
168
      dem_%l%_%l1% = focalsum(dem_%l1%_%l1%, weight, gauss3, data) / focalsum(con(dem_%l1%_%l1% > -999, 1, 0), weight, gauss3, data)
169
    &end
170

  
171
    &type Checkpoint 1.2
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff