Project

General

Profile

Download (4.37 KB) Statistics
| Branch: | Revision:
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%!)
(2-2/11)