| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo; |
| 27 |
|
| 28 |
import dr.app.gui.ColorFunction; |
| 29 |
import dr.math.distributions.GammaDistribution; |
| 30 |
|
| 31 |
import javax.swing.*; |
| 32 |
import java.awt.*; |
| 33 |
import java.io.IOException; |
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class GTOPO30Panel extends JPanel implements Lattice { |
| 41 |
|
| 42 |
GTOPO30Tile[][] tiles; |
| 43 |
|
| 44 |
int minLat, maxLat; |
| 45 |
int minLong, maxLong; |
| 46 |
|
| 47 |
double scale = 0.5; |
| 48 |
|
| 49 |
public GTOPO30Panel(String[] filenames, ColorFunction colorFunction) throws IOException { |
| 50 |
|
| 51 |
List<GTOPO30Tile> tileList = new ArrayList<GTOPO30Tile>(); |
| 52 |
|
| 53 |
for (String filename : filenames) { |
| 54 |
GTOPO30Tile tile = new GTOPO30Tile(filename, colorFunction); |
| 55 |
|
| 56 |
if (tile.getMaxLatitude() > maxLat) maxLat = tile.getMaxLatitude(); |
| 57 |
if (tile.getMaxLongitude() > maxLong) maxLong = tile.getMaxLongitude(); |
| 58 |
if (tile.getMinLatitude() < minLat) minLat = tile.getMinLatitude(); |
| 59 |
if (tile.getMinLongitude() < minLong) minLong = tile.getMinLongitude(); |
| 60 |
|
| 61 |
tileList.add(tile); |
| 62 |
} |
| 63 |
|
| 64 |
tiles = new GTOPO30Tile[(maxLat - minLat) / 50][(maxLong - minLong) / 40]; |
| 65 |
|
| 66 |
for (GTOPO30Tile tile : tileList) { |
| 67 |
tiles[(maxLat - tile.getMaxLatitude()) / 50][(tile.getMinLongitude() - minLong) / 40] = tile; |
| 68 |
} |
| 69 |
|
| 70 |
setLayout(new GridLayout(tiles.length, tiles[0].length)); |
| 71 |
|
| 72 |
for (GTOPO30Tile[] tileRow : tiles) { |
| 73 |
for (GTOPO30Tile tile : tileRow) { |
| 74 |
if (tile != null) { |
| 75 |
add(tile); |
| 76 |
} else { |
| 77 |
add(new JPanel()); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public Dimension getPreferredSize() { |
| 84 |
|
| 85 |
return new Dimension( |
| 86 |
(int) Math.round(tiles[0].length * GTOPO30Tile.NCOLS * scale), |
| 87 |
(int) Math.round(tiles.length * GTOPO30Tile.NROWS * scale)); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public short getHeight(int y, int x) { |
| 97 |
int tileY = y / GTOPO30Tile.NROWS; |
| 98 |
int tileX = x / GTOPO30Tile.NCOLS; |
| 99 |
|
| 100 |
int ty = y % GTOPO30Tile.NROWS; |
| 101 |
int tx = x % GTOPO30Tile.NCOLS; |
| 102 |
|
| 103 |
|
| 104 |
try { |
| 105 |
|
| 106 |
short height = tiles[tileY][tileX].getHeight(ty, tx); |
| 107 |
return height; |
| 108 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 109 |
System.err.println("Error at y=" + y + ", x=" + x); |
| 110 |
} |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
public Location getLocation(double latitude, double longitude) { |
| 115 |
double relLat = tiles[0][0].getMaxLatitude() - latitude; |
| 116 |
double relLong = longitude - tiles[0][0].getMinLongitude(); |
| 117 |
|
| 118 |
return new Location( |
| 119 |
(int) Math.round(relLong / tiles[0][0].xdim), |
| 120 |
(int) Math.round(relLat / tiles[0][0].ydim) |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
public void setScale(double scale) { |
| 125 |
this.scale = scale; |
| 126 |
setSize(getPreferredSize()); |
| 127 |
repaint(); |
| 128 |
} |
| 129 |
|
| 130 |
public int latticeWidth() { |
| 131 |
return tiles[0].length * GTOPO30Tile.NCOLS; |
| 132 |
} |
| 133 |
|
| 134 |
public int latticeHeight() { |
| 135 |
return tiles.length * GTOPO30Tile.NROWS; |
| 136 |
} |
| 137 |
|
| 138 |
public int getState(int i, int j) { |
| 139 |
return getHeight(j, i) / 100; |
| 140 |
} |
| 141 |
|
| 142 |
public void paintLattice(Graphics g) { |
| 143 |
paint(g); |
| 144 |
} |
| 145 |
|
| 146 |
public RateMatrix getRates(double mu) { |
| 147 |
final double[] toRate = new double[52]; |
| 148 |
for (int j = 0; j < toRate.length; j++) { |
| 149 |
|
| 150 |
int heightStart = j * 100; |
| 151 |
int heightEnd = heightStart + 100; |
| 152 |
|
| 153 |
double shape = 0.55; |
| 154 |
double scale = 5800.0; |
| 155 |
|
| 156 |
toRate[j] = mu * |
| 157 |
(GammaDistribution.cdf(heightEnd, shape, scale) - |
| 158 |
GammaDistribution.cdf(heightStart, shape, scale)); |
| 159 |
|
| 160 |
System.out.println("toRate[" + j + "] = " + toRate[j]); |
| 161 |
} |
| 162 |
|
| 163 |
return new RateMatrix() { |
| 164 |
|
| 165 |
public double getRate(int i, int j) { |
| 166 |
if (j < 0 || j >= toRate.length) return 0; |
| 167 |
return toRate[j]; |
| 168 |
} |
| 169 |
}; |
| 170 |
} |
| 171 |
} |