//Two recursive algorithms are used:
        //------1st goes through all the latitudes (1st column) - binary search method is utilized
        //------2nd establishes an initial step and them goes through the data, if search direction
        //is flipped then the step is reduced

        public IMultipleValueDataPoint2D FindPointInDataFileRecursive(decimal Latitude, decimal Longitude, int startIndex, int EndIndex)
        {
            int chunkSize = 1 + (EndIndex - startIndex);

            if (chunkSize <= 0)
            {
                return(null);
            }

            int currentIndex = startIndex + (chunkSize / 2);
            IMultipleValueDataPoint2D FirstPointWithGivenLatitude = readPointValue(currentIndex);

            if (FirstPointWithGivenLatitude.Latitude == Latitude)
            {
                IMultipleValueDataPoint2D foundPoint = FindPointInDataFileByLongitudeRecursive(FirstPointWithGivenLatitude, currentIndex, Latitude, Longitude, CurrentIncrementDirection.Unknown, 100);
                return(foundPoint);
            }
            else     //if latitude did not match
            {
                if (FirstPointWithGivenLatitude.Latitude > Latitude)
                {
                    return(FindPointInDataFileRecursive(Latitude, Longitude, currentIndex + 1, EndIndex));
                }
                else
                {
                    return(FindPointInDataFileRecursive(Latitude, Longitude, startIndex, currentIndex - 1));
                }
            }
        }
Esempio n. 2
0
 public MapDataElement
 (
     decimal Latitude, decimal Longitude,
     IMultipleValueDataPoint2D LowerLeftPoint,
     IMultipleValueDataPoint2D LowerRightPoint,
     IMultipleValueDataPoint2D UpperLeftPoint,
     IMultipleValueDataPoint2D UpperRightPoint)
 {
     this.latitude        = Latitude;
     this.longitude       = Longitude;
     this.LowerLeftPoint  = LowerLeftPoint;
     this.LowerRightPoint = LowerRightPoint;
     this.UpperLeftPoint  = UpperLeftPoint;
     this.UpperRightPoint = UpperRightPoint;
 }
Esempio n. 3
0
        protected MapDataElement ReadData(decimal Latitude, decimal Longitude, ReadPointValueDelegate ReadPointValue, decimal DataCoordinateStep)
        {
            this.readPointValue = ReadPointValue;
            //find the lower-left cornerpoint of a quad
            decimal LatLow  = Latitude.FloorWithSignificance(DataCoordinateStep);
            decimal LongLow = Longitude.FloorWithSignificance(DataCoordinateStep);

            //SeismicGroundMotionDataPoint pointFound = this.FindPointInDataFileRecursive(LatLow, LongLow, DataStartIndex+1, DataEndIndex);
            IMultipleValueDataPoint2D foundPoint = this.FindPointInDataFileRecursive(LatLow, LongLow, DataStartIndex + 1, DataEndIndex);
            MapDataElement            de         = new MapDataElement(Latitude, Longitude);

            //add neigboring points for interpolation

            #region Neigbouring points for interpolation
            #region Special cases
            //Corner points
            if (foundPoint.DataArrayIndex == DataStartIndex + 1 || foundPoint.DataArrayIndex == NumberOfColumns + 1 || foundPoint.DataArrayIndex == DataEndIndex || foundPoint.DataArrayIndex == DataEndIndex - NumberOfColumns + 1)
            {
                de.LowerLeftPoint            = foundPoint;
                de.LowerRightPoint           = foundPoint.Clone();
                de.LowerRightPoint.Longitude = de.LowerRightPoint.Longitude + CoordinateIncrement;
                de.UpperLeftPoint            = foundPoint.Clone();
                de.UpperLeftPoint.Latitude   = de.UpperLeftPoint.Latitude + CoordinateIncrement;
                de.UpperRightPoint           = foundPoint.Clone();
                de.UpperRightPoint.Latitude  = de.UpperRightPoint.Latitude + CoordinateIncrement;
                de.UpperRightPoint.Longitude = de.UpperRightPoint.Longitude + CoordinateIncrement;
                return(de);
            }
            //bottom row- do nothing
            //top row
            if (foundPoint.DataArrayIndex < (NumberOfColumns + 1))
            {
                de.LowerLeftPoint = foundPoint;
                IMultipleValueDataPoint2D pNextLongitude = readPointValue(foundPoint.DataArrayIndex + 1);
                de.LowerRightPoint = pNextLongitude;
                IMultipleValueDataPoint2D NextLatitudePoint = foundPoint.Clone();
                NextLatitudePoint.Latitude = foundPoint.Latitude + CoordinateIncrement;
                de.UpperLeftPoint          = NextLatitudePoint;
                IMultipleValueDataPoint2D NextLatitudeAndLongitudePoint = pNextLongitude.Clone();
                NextLatitudeAndLongitudePoint.Latitude  = foundPoint.Latitude + CoordinateIncrement;
                NextLatitudeAndLongitudePoint.Longitude = foundPoint.Longitude + CoordinateIncrement;
                de.UpperRightPoint = NextLatitudeAndLongitudePoint;
                return(de);
            }
            //first column -do nothing

            //last column
            if ((foundPoint.DataArrayIndex - DataStartIndex) % (NumberOfColumns) == 0 && foundPoint.DataArrayIndex > 2)
            {
                de.LowerLeftPoint = foundPoint;
                IMultipleValueDataPoint2D pNextLongitude = foundPoint.Clone();
                pNextLongitude.Longitude = foundPoint.Longitude + CoordinateIncrement;
                de.LowerRightPoint       = pNextLongitude;
                IMultipleValueDataPoint2D pNextLatitude = readPointValue(foundPoint.DataArrayIndex - NumberOfColumns);
                de.UpperLeftPoint = pNextLatitude;
                IMultipleValueDataPoint2D NextLatitudeAndLongitudePoint = pNextLatitude.Clone();
                NextLatitudeAndLongitudePoint.Latitude  = pNextLatitude.Latitude;
                NextLatitudeAndLongitudePoint.Longitude = pNextLongitude.Longitude;
                de.UpperRightPoint = NextLatitudeAndLongitudePoint;
                return(de);
            }

            #endregion
            #region Typical case
            de.LowerLeftPoint  = foundPoint;
            de.LowerRightPoint = readPointValue(foundPoint.DataArrayIndex + 1);                   //NextLongitudePoint;
            de.UpperLeftPoint  = readPointValue(foundPoint.DataArrayIndex - NumberOfColumns);     //NextLatitudePoint;
            de.UpperRightPoint = readPointValue(foundPoint.DataArrayIndex - NumberOfColumns + 1); //NextLatitudeAndLongitudePoint

            #endregion
            #endregion

            //Interpolation is done by MapDataElement


            //if (pointFound!=null)
            //{

            //    decimal SS = GetInterpolatedValue(pointFound, Latitude, Longitude, GroundMotionParameterType.SS);
            //    decimal S1 = GetInterpolatedValue(pointFound, Latitude, Longitude, GroundMotionParameterType.S1);

            //    return new SeismicGroundMotionDataPoint(Latitude, Longitude, SS, S1);
            //}

            //return new SeismicGroundMotionDataPoint();
            return(de);
        }
        private IMultipleValueDataPoint2D FindPointInDataFileByLongitudeRecursive(IMultipleValueDataPoint2D CurrentPoint, int currentIndex, decimal Latitude, decimal Longitude,
                                                                                  CurrentIncrementDirection incrementDirection, int Step)
        {
            decimal thisLongitude = CurrentPoint.Longitude;

            if (thisLongitude == Longitude)
            {
                return(CurrentPoint);
            }
            else     // if did not hot the target
            {
                #region Current longitude less than target
                if (thisLongitude < Longitude)
                {
                    if (incrementDirection == CurrentIncrementDirection.Increase)
                    {
                        Step = (int)Step / 2;
                    }
                    incrementDirection = CurrentIncrementDirection.Decrease;
                    currentIndex       = (currentIndex + Step) <= DataEndIndex ? currentIndex + Step : DataEndIndex;
                    CurrentPoint       = readPointValue(currentIndex);

                    //case when this is a boundary condition
                    if (currentIndex >= DataEndIndex - NumberOfColumns)     // if this is the last row in the table
                    {
                        int count1 = 0;
                        while (CurrentPoint.Longitude != Longitude)    //increment by one
                        {
                            currentIndex = currentIndex + 1;
                            CurrentPoint = readPointValue(currentIndex);
                            count1++;
                            if (count1 > NumberOfColumns)
                            {
                                throw new Exception("Data point not found in table.");
                            }
                        }
                        return(CurrentPoint);
                    }

                    if (CurrentPoint.Latitude != Latitude)     //this means we overshot
                    {
                        //step one by one to go find value
                        while (CurrentPoint.Latitude != Latitude && CurrentPoint.Longitude != Longitude)
                        {
                            currentIndex = currentIndex - 1;
                            CurrentPoint = readPointValue(currentIndex);
                        }
                        return(CurrentPoint);
                    }
                    else
                    {
                        //continue recursive iteration
                        return(FindPointInDataFileByLongitudeRecursive(CurrentPoint, currentIndex,
                                                                       Latitude, Longitude, incrementDirection, Step));
                    }
                }
                #endregion

                #region Current longitude greater than target
                else     //(thisLongitude>Longitude)
                {
                    if (incrementDirection == CurrentIncrementDirection.Decrease)
                    {
                        Step = (int)Step / 2;
                    }
                    incrementDirection = CurrentIncrementDirection.Increase;
                    currentIndex       = Step <= currentIndex ? currentIndex - Step : currentIndex - 1;
                    CurrentPoint       = readPointValue(currentIndex);
                    //case when this is a boundary condition
                    if (currentIndex < NumberOfColumns)     // if this is the first row in the table
                    {
                        int count2 = 0;
                        while (CurrentPoint.Longitude != Longitude)     //increment by one
                        {
                            currentIndex = currentIndex - 1;
                            CurrentPoint = readPointValue(currentIndex);
                            count2++;
                            if (count2 > NumberOfColumns)
                            {
                                throw new Exception("Data point not found in table.");
                            }
                        }
                        return(CurrentPoint);
                    }
                    if (CurrentPoint.Latitude != Latitude)     //this means we overshot
                    {
                        //step one by one to go find value
                        while (CurrentPoint.Latitude != Latitude && CurrentPoint.Longitude != Longitude)
                        {
                            currentIndex = currentIndex + 1;
                            CurrentPoint = readPointValue(currentIndex);
                        }
                        return(CurrentPoint);
                    }
                    else
                    {
                        //continue recursive iteration
                        return(FindPointInDataFileByLongitudeRecursive(CurrentPoint, currentIndex,
                                                                       Latitude, Longitude, incrementDirection, Step));
                    }
                }
                #endregion
            }
        }