Exemplo n.º 1
0
        /// <summary>
        /// Evaluates subrequest for given partNo and origin point to place results at.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="partsCount"></param>
        /// <param name="partsNo"></param>
        /// <returns></returns>
        public static Tuple <IFetchRequest, int[]> EvaluateSubrequestData(IFetchRequest request, int partsCount, int partsNo)
        {
            var domain = request.Domain;

            int[] shape  = domain.GetDataArrayShape();
            int[] origin = new int[shape.Length];
            if (partsCount == 1)
            {
                for (int i = 0; i < origin.Length; ++i)
                {
                    origin[i] = 0;
                }
                return(new Tuple <IFetchRequest, int[]>(request, origin));
            }
            //int partWidth = (shape[0] + partsCount - 1) / partsCount;
            int basePartWidth    = shape[0] / partsCount;
            int partWidthResidue = shape[0] % partsCount;
            int leftBorder       = basePartWidth * partsNo + Math.Min(partsNo, partWidthResidue);
            int rightBorder      = leftBorder + basePartWidth;

            if (partsNo < partWidthResidue)
            {
                ++rightBorder;
            }
            if (domain.SpatialRegionType == SpatialRegionSpecification.CellGrid)
            {
                ++rightBorder;
            }
            //if ((domain.SpatialRegionType == SpatialRegionSpecification.Points || domain.SpatialRegionType == SpatialRegionSpecification.Cells) && domain.TimeRegion.SegmentsCount == 1)
            if (shape.Length == 1)
            {
                //assuming that it's possible to divide points into partsCount parts with at least (partsCount - 1) of them having equal size divisible by pageWidth
                if (shape[0] % partsCount == 0 && (shape[0] / partsCount) % pageWidth == 0)
                {
                    //everything is already computed in a right way
                }
                else
                {
                    int chunks       = shape[0] / pageWidth;
                    int sizeInChunks = chunks / (partsCount - 1);
                    int partWidth    = pageWidth * sizeInChunks;
                    leftBorder = partWidth * partsNo;
                    if (partsNo + 1 < partsCount)
                    {
                        rightBorder = leftBorder + partWidth;
                    }
                    else
                    {
                        rightBorder = shape[0];
                    }
                }
                //if (partWidthResidue > 0) ++basePartWidth;
                //int firstPartsWidth = 512 * ((basePartWidth + 511) / 512);
                //int totalFirstParts = shape[0] / firstPartsWidth;
                //if (totalFirstParts < partsCount - 1) throw new ArgumentException(string.Format(@"Can't divide a single dimensional request containing {0} points into {1} parts aligned by 512 points.", shape[0], partsCount));
                //leftBorder = firstPartsWidth * partsNo;
                //if (partsNo < totalFirstParts)
                //{
                //    rightBorder = leftBorder + firstPartsWidth;
                //}
                //else
                //{
                //    rightBorder = shape[0];
                //}
            }
            int width = rightBorder - leftBorder;

            double[] lats = null, lons = null, lats2 = null, lons2 = null;
            if (domain.Lats != null)
            {
                if (domain.SpatialRegionType == SpatialRegionSpecification.Points || domain.SpatialRegionType == SpatialRegionSpecification.Cells)
                {
                    lats = new double[width];
                    for (int i = 0; i < width; ++i)
                    {
                        lats[i] = domain.Lats[leftBorder + i];
                    }
                }
                else
                {
                    lats = new double[domain.Lats.Length];
                    for (int i = 0; i < domain.Lats.Length; ++i)
                    {
                        lats[i] = domain.Lats[i];
                    }
                }
            }
            if (domain.Lats2 != null)
            {
                if (domain.SpatialRegionType == SpatialRegionSpecification.Points || domain.SpatialRegionType == SpatialRegionSpecification.Cells)
                {
                    lats2 = new double[width];
                    for (int i = 0; i < width; ++i)
                    {
                        lats2[i] = domain.Lats2[leftBorder + i];
                    }
                }
                else
                {
                    lats2 = new double[domain.Lats2.Length];
                    for (int i = 0; i < domain.Lats2.Length; ++i)
                    {
                        lats2[i] = domain.Lats2[i];
                    }
                }
            }
            if (domain.Lons != null)
            {
                lons = new double[width];
                for (int i = 0; i < width; ++i)
                {
                    lons[i] = domain.Lons[leftBorder + i];
                }
            }
            if (domain.Lons2 != null)
            {
                lons2 = new double[width];
                for (int i = 0; i < width; ++i)
                {
                    lons2[i] = domain.Lons2[leftBorder + i];
                }
            }
            IFetchDomain newDomain;

            switch (domain.SpatialRegionType)
            {
            case SpatialRegionSpecification.Cells:
                newDomain = FetchDomain.CreateCells(lats, lons, lats2, lons2, domain.TimeRegion, (bool[, ])domain.Mask);
                break;

            case SpatialRegionSpecification.PointGrid:
                newDomain = FetchDomain.CreatePointGrid(lats, lons, domain.TimeRegion, (bool[, , ])domain.Mask);
                break;

            case SpatialRegionSpecification.CellGrid:
                newDomain = FetchDomain.CreateCellGrid(lats, lons, domain.TimeRegion, (bool[, , ])domain.Mask);
                break;

            case SpatialRegionSpecification.Points:
            default:
                newDomain = FetchDomain.CreatePoints(lats, lons, domain.TimeRegion, (bool[, ])domain.Mask);
                break;
            }
            origin[0] = leftBorder;
            for (int i = 1; i < origin.Length; ++i)
            {
                origin[i] = 0;
            }
            IFetchRequest newRequest = new FetchRequest(request.EnvironmentVariableName, newDomain, request.ReproducibilityTimestamp, request.ParticularDataSource);

            return(new Tuple <IFetchRequest, int[]>(newRequest, origin));
        }
Exemplo n.º 2
0
        public static IFetchRequest ToFetchRequest(this DataSet dataSet)
        {
            Serializable.TimeRegion serializableRegion = new Serializable.TimeRegion();
            serializableRegion.Years = (int[])dataSet[YearAxisVariableName].GetData();
            serializableRegion.Days  = (int[])dataSet[DayAxisVariableName].GetData();
            serializableRegion.Hours = (int[])dataSet[HourAxisVariableName].GetData();
            serializableRegion.IsIntervalsGridYears = (bool)dataSet[YearAxisVariableName].Metadata["IsIntervals"];
            serializableRegion.IsIntervalsGridDays  = (bool)dataSet[DayAxisVariableName].Metadata["IsIntervals"];
            serializableRegion.IsIntervalsGridHours = (bool)dataSet[HourAxisVariableName].Metadata["IsIntervals"];
            TimeRegion region = serializableRegion.ConvertFromSerializable();

            IFetchDomain domain = null;

            double[] lat, lon, latmax = null, lonmax = null;
            if (dataSet.Variables.Contains("latmin"))
            {
                lat    = (double[])dataSet["latmin"].GetData();
                lon    = (double[])dataSet["lonmin"].GetData();
                latmax = (double[])dataSet["latmax"].GetData();
                lonmax = (double[])dataSet["lonmax"].GetData();
            }
            else
            {
                lat = (double[])dataSet["lat"].GetData();
                lon = (double[])dataSet["lon"].GetData();
            }

            if (dataSet.Variables.Contains(MaskVariableName))
            {
                Array mask = dataSet[MaskVariableName].GetData();
                if (latmax == null)
                {
                    if (dataSet.Dimensions.Contains("ci"))
                    {
                        domain = FetchDomain.CreateCellGrid(lat, lon, region, mask);
                    }
                    else if (dataSet.Dimensions.Contains("j"))
                    {
                        domain = FetchDomain.CreatePointGrid(lat, lon, region, mask);
                    }
                    else
                    {
                        domain = FetchDomain.CreatePoints(lat, lon, region, mask);
                    }
                }
                else
                {
                    domain = FetchDomain.CreateCells(lat, lon, latmax, lonmax, region, (bool[, ])mask);
                }
            }
            else
            {
                if (latmax == null)
                {
                    if (dataSet.Dimensions.Contains("ci"))
                    {
                        domain = FetchDomain.CreateCellGrid(lat, lon, region);
                    }
                    else if (dataSet.Dimensions.Contains("j"))
                    {
                        domain = FetchDomain.CreatePointGrid(lat, lon, region);
                    }
                    else
                    {
                        domain = FetchDomain.CreatePoints(lat, lon, region);
                    }
                }
                else
                {
                    domain = FetchDomain.CreateCells(lat, lon, latmax, lonmax, region);
                }
            }

            string envVar = (string)dataSet.Metadata[EnvironmentVariableNameKey];

            string[] dataSourceNames = dataSet.Metadata.ContainsKey(DataSourceNameKey) ? (string[])dataSet.Metadata[DataSourceNameKey] : null;

            if (dataSet.Metadata.ContainsKey(TimeStampKey))
            {
                return(new FetchRequest(
                           envVar,
                           domain,
                           (DateTime)dataSet.Metadata[TimeStampKey],
                           dataSourceNames));
            }
            else
            {
                return(new FetchRequest(envVar, domain, dataSourceNames));
            }
        }