1 | /* |
---|
2 | * $Id: GridFlag.java,v 1.7 2001/02/02 20:27:37 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.sgt.contour; |
---|
14 | |
---|
15 | import gov.noaa.pmel.util.Debug; |
---|
16 | import gov.noaa.pmel.sgt.dm.SGTGrid; |
---|
17 | |
---|
18 | /** |
---|
19 | * Used to set and test whether a grid value is less than, equal, or |
---|
20 | * greater than a contour level. |
---|
21 | * |
---|
22 | * @author D. W. Denbo |
---|
23 | * @version $Revision: 1.7 $, $Date: 2001/02/02 20:27:37 $ |
---|
24 | * @since 2.0 |
---|
25 | */ |
---|
26 | class GridFlag { |
---|
27 | public final static int LESS_THAN_ZC = -1; |
---|
28 | public final static int MISSING = 10; |
---|
29 | public final static int GREATER_THAN_ZC = 1; |
---|
30 | |
---|
31 | private double[] array_; |
---|
32 | private double[] mask_ = null; |
---|
33 | private byte[] value_; |
---|
34 | private int nx_; |
---|
35 | private int ny_; |
---|
36 | |
---|
37 | public GridFlag(SGTGrid grid, double zc) { |
---|
38 | this(grid, null, zc); |
---|
39 | } |
---|
40 | |
---|
41 | public GridFlag(SGTGrid grid, SGTGrid mask, double zc) { |
---|
42 | if(grid.isXTime()) { |
---|
43 | nx_ = grid.getTSize(); |
---|
44 | } else { |
---|
45 | nx_ = grid.getXSize(); |
---|
46 | } |
---|
47 | if(grid.isYTime()) { |
---|
48 | ny_ = grid.getTSize(); |
---|
49 | } else { |
---|
50 | ny_ = grid.getYSize(); |
---|
51 | } |
---|
52 | value_ = new byte[nx_*ny_]; |
---|
53 | array_ = grid.getZArray(); |
---|
54 | setLevel(zc); |
---|
55 | if(mask != null) setMask(mask); |
---|
56 | } |
---|
57 | |
---|
58 | public void setMask(SGTGrid mask) { |
---|
59 | mask_ = mask.getZArray(); |
---|
60 | applyMask(); |
---|
61 | } |
---|
62 | |
---|
63 | void applyMask() { |
---|
64 | if(mask_ == null) return; |
---|
65 | for(int i=0; i < nx_*ny_; i++) { |
---|
66 | if(Double.isNaN(mask_[i]) || mask_[i] != 0.0) { |
---|
67 | value_[i] = MISSING; |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | public void setLevel(double zc) { |
---|
73 | for(int i=0; i < nx_*ny_; i++) { |
---|
74 | if(!Double.isNaN(array_[i])) { |
---|
75 | if(array_[i] <= zc) { |
---|
76 | value_[i] = LESS_THAN_ZC; |
---|
77 | } else { |
---|
78 | value_[i] = GREATER_THAN_ZC; |
---|
79 | } |
---|
80 | } else { |
---|
81 | value_[i] = MISSING; |
---|
82 | } |
---|
83 | } |
---|
84 | applyMask(); |
---|
85 | } |
---|
86 | |
---|
87 | int index(int i, int j) { |
---|
88 | return j + ny_*i; |
---|
89 | } |
---|
90 | /** |
---|
91 | * True if grid(i,j) is missing. |
---|
92 | */ |
---|
93 | public boolean isMissing(int i, int j) { |
---|
94 | int ind = index(i,j); |
---|
95 | if(Debug.CONTOUR) { |
---|
96 | if(ind < 0 || ind >= nx_*ny_) { |
---|
97 | System.out.println("GridFlag.isMissing(): (i,j) = (" + i + ", " + j + ")"); |
---|
98 | } |
---|
99 | } |
---|
100 | return (value_[ind] == MISSING); |
---|
101 | } |
---|
102 | |
---|
103 | public boolean isGreater(int i, int j ) { |
---|
104 | int ind = index(i,j); |
---|
105 | if(Debug.CONTOUR) { |
---|
106 | if(ind < 0 || ind >= nx_*ny_) { |
---|
107 | System.out.println("GridFlag.isGreater(): (i,j) = (" + i + ", " + j + ")"); |
---|
108 | } |
---|
109 | } |
---|
110 | return (value_[ind] == GREATER_THAN_ZC); |
---|
111 | } |
---|
112 | |
---|
113 | public int getValue(int i, int j) { |
---|
114 | int ind = index(i,j); |
---|
115 | if(Debug.CONTOUR) { |
---|
116 | if(ind < 0 || ind >= nx_*ny_) { |
---|
117 | System.out.println("GridFlag.getValue(): (i,j) = (" + i + ", " + j + ")"); |
---|
118 | } |
---|
119 | } |
---|
120 | return value_[ind]; |
---|
121 | } |
---|
122 | } |
---|