001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    /**
007     * @author lakshman
008     * 
009     */
010    public class RadialSetLookup {
011            private int accuracy;
012    
013            private int[] azToRadialNo;
014    
015            //private int numGates;
016    
017            public RadialSetLookup(int acc, RadialSet rs) {
018                    this.accuracy = acc;
019                    this.azToRadialNo = new int[367 * accuracy + 1];
020                    for (int i = 0; i < azToRadialNo.length; ++i) {
021                            azToRadialNo[i] = -1;
022                    }
023                    //this.numGates = rs.getNumGates();
024                    int numRadials = rs.getNumRadials();
025                    for (int i = 0; i < numRadials; ++i) {
026                            int minaz = (int) (rs.getRadial(i).getStartAzimuth() * accuracy + 0.5);
027                            int maxaz = (int) (rs.getRadial(i).getEndAzimuth() * accuracy + 0.5);
028                            if (minaz < 0 || minaz >= azToRadialNo.length || maxaz < 0
029                                            || maxaz >= azToRadialNo.length) {
030                                    continue;
031                            }
032                            if (minaz > maxaz) {
033                                    // swap (reverse rotating radar)
034                                    int temp = minaz;
035                                    minaz = maxaz;
036                                    maxaz = temp;
037                            }
038                            for (int j = minaz; j <= maxaz; ++j) {
039                                    azToRadialNo[j] = i;
040                            }
041                    }
042                    // wrap-around radial
043                    for (int i = 360 * accuracy; i < azToRadialNo.length; ++i) {
044                            if (azToRadialNo[i] >= 0) {
045                                    azToRadialNo[i - 360 * accuracy] = azToRadialNo[i];
046                            }
047                    }
048            }
049    
050            /**
051             * Pass in the mid-azimuth of the radial you are trying to retrieve. Will
052             * return -1 if the given azimuth is not covered by grid.
053             */
054            public int getRadialNum(float az) {
055                    int az_no = (int) (az * accuracy);
056                    if (az_no < 0) {
057                            az_no += 360 * accuracy;
058                    } else if (az_no >= 360 * accuracy) {
059                            az_no -= 360 * accuracy;
060                    }
061                    return azToRadialNo[az_no];
062            }
063    }