Debian Codegraph @

src/dr/geo/GTOPO30Panel.java

raw symbols SCIP
1 /*
2 * GTOPO30Panel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Alexei Drummond
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 * @param y latitudinal pixel, increasing south from north edge
93 * @param x longitudinal pixel index, increasing east from west edge
94 * @return the height of the pixel at y,x
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 }