private void WriteStockGroupRasters(int iteration, int timestep)
        {
            Debug.Assert(this.m_IsSpatial);

            foreach (StockGroup g in this.m_StockGroups)
            {
                if (!g.OutputFilter.HasFlag(Constants.OutputFilter.Spatial))
                {
                    continue;
                }

                StochasticTimeRaster rastOutput = this.STSimTransformer.InputRasters.CreateOutputRaster(RasterDataType.DTDouble);

                foreach (StockTypeLinkage l in g.StockTypeLinkages)
                {
                    StochasticTimeRaster rastStockType = this.STSimTransformer.InputRasters.CreateOutputRaster(RasterDataType.DTDouble);

                    GetStockValues(l.StockType.Id, rastStockType);
                    rastStockType.ScaleDblCells(l.Value);
                    rastOutput.AddDblCells(rastStockType);
                }

                Spatial.WriteRasterData(
                    rastOutput,
                    this.ResultScenario.GetDataSheet(Constants.DATASHEET_OUTPUT_SPATIAL_STOCK_GROUP),
                    iteration,
                    timestep,
                    g.Id,
                    Constants.SPATIAL_MAP_STOCK_GROUP_VARIABLE_PREFIX,
                    Constants.DATASHEET_OUTPUT_SPATIAL_FILENAME_COLUMN);
            }
        }
Esempio n. 2
0
        public StochasticTimeRaster CreateOutputRaster(RasterDataType dataType)
        {
            StochasticTimeRaster rast = new StochasticTimeRaster(
                "output",
                dataType,
                1,
                this.m_Width,
                this.m_Height,
                this.m_XllCorner,
                this.m_YllCorner,
                this.m_cellSize,
                this.m_cellSizeUnits,
                this.m_Projection,
                this.m_PrimaryStratumRaster.GeoTransform,
                this.m_NoDataValue,
                false,
                Spatial.UndefinedRasterBand);

            if (dataType == RasterDataType.DTInteger)
            {
                rast.InitIntCells();
            }
            else
            {
                rast.InitDblCells();
            }

            return(rast);
        }
        private double GetFlowLateralMultiplier(
            Cell cell,
            FlowLateralMultiplierMap map,
            int flowGroupId,
            int iteration,
            int timestep)
        {
            Debug.Assert(this.m_IsSpatial);
            Debug.Assert(this.m_FlowLateralMultipliers.Count > 0);

            FlowLateralMultiplier m = map.GetFlowLateralMultiplier(flowGroupId, iteration, timestep);

            if (m == null)
            {
                return(1.0);
            }

            if (!this.m_FlowLateralMultiplierRasters.ContainsKey(m.FileName))
            {
                return(1.0);
            }

            StochasticTimeRaster raster = this.m_FlowLateralMultiplierRasters[m.FileName];
            double v = raster.DblCells[cell.CollectionIndex];

            if ((v < 0.0) || (MathUtils.CompareDoublesEqual(v, raster.NoDataValue, double.Epsilon)))
            {
                return(1.0);
            }
            else
            {
                return(v);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Chooses a new raster file
        /// </summary>
        /// <remarks>
        /// Just store the filename. For now, no path required. In the future may want to support absolute path, differentiated by x:\\
        /// </remarks>
        private void ChooseRasterFile(int rowIndex, int colIndex)
        {
            string FileName = RasterUtilities.ChooseRasterFileName("Initial Conditions Raster File", this);

            if (FileName == null)
            {
                return;
            }

            Application.DoEvents();

            using (HourGlass h = new HourGlass())
            {
                try
                {
                    StochasticTimeRaster rast = new StochasticTimeRaster(FileName, RasterDataType.DTInteger);

                    if (colIndex == PRIMARY_STRATUM_FILE_NAME_COLUMN_INDEX && rast.Projection == null)
                    {
                        const string msg = "There is no projection associated with this raster file. The model will still run but outputs will also have no projection.";
                        FormsUtilities.InformationMessageBox(msg);
                    }

                    SetICSpatialFile(rowIndex, colIndex, FileName);
                }
                catch (Exception e)
                {
                    FormsUtilities.ErrorMessageBox(e.Message);
                    return;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Compare the values of the metadata properties to those of the raster argument
        /// </summary>
        /// <param name="raster">A instance of class Raster</param>
        /// <returns>An Enum containing the comparison Result</returns>
        /// <remarks></remarks>
        public CompareMetadataResult CompareMetadata(StochasticTimeRaster raster, ref string compareMsg)
        {
            CompareMetadataResult retVal = CompareMetadataResult.Same;

            compareMsg = "";

            // Test number of cols.
            if (this.Width != raster.Width)
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in Number of Columns ({1} vs {0})", this.Width, raster.Width);
                return(CompareMetadataResult.RowColumnMismatch);
            }

            // Test number of rows.
            if (this.Height != raster.Height)
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in Number of Rows ({1} vs {0})", this.Height, raster.Height);
                return(CompareMetadataResult.RowColumnMismatch);
            }

            // Test XLL Corner. See if NOT negligable difference - arbitrarily 1/10 of cell size.
            // Can't use equality, because of float error
            if (Math.Abs(this.XllCorner - raster.XllCorner) > (this.CellSize / 10.0))
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in XllCorner ({1} vs {0})", this.XllCorner, raster.XllCorner);
                retVal     = CompareMetadataResult.UnimportantDifferences;
            }

            // Test YLL Corner.  See if NOT negligable difference - arbitrarily 1/10 of cell size.
            // Can't use equality, because of float error
            if (Math.Abs(this.YllCorner - raster.YllCorner) > (this.CellSize / 10.0))
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in YllCorner ({1} vs {0})", this.YllCorner, raster.YllCorner);
                retVal     = CompareMetadataResult.UnimportantDifferences;
            }

            // Test ProjectionString
            if (this.Projection != raster.Projection)
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in Projection String");
                retVal     = CompareMetadataResult.UnimportantDifferences;
            }

            // Test Cell Size. Cant use equality because of precision errors ( eg. 30D vs 30.000000000004D)
            if (Math.Abs(this.CellSize - raster.CellSize) > 0.0001)
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in Cell Size ({1} vs {0})", this.CellSize, raster.CellSize);
                retVal     = CompareMetadataResult.UnimportantDifferences;
            }

            // Test Cell Units
            if (this.CellSizeUnits != raster.CellSizeUnits)
            {
                compareMsg = string.Format(CultureInfo.InvariantCulture, "Mismatch in Cell Size Units ({1} vs {0})", this.CellSizeUnits, raster.CellSizeUnits);
                retVal     = CompareMetadataResult.UnimportantDifferences;
            }

            return(retVal);
        }
Esempio n. 6
0
        private void FillFlowLateralMultipliers()
        {
            Debug.Assert(this.m_IsSpatial);
            Debug.Assert(this.m_FlowLateralMultipliers.Count == 0);
            Debug.Assert(this.m_FlowLateralMultiplierRasters.Count == 0);
            DataSheet ds = this.ResultScenario.GetDataSheet(Constants.DATASHEET_FLOW_LATERAL_MULTIPLIER_NAME);

            foreach (DataRow dr in ds.GetData().Rows)
            {
                int    FlowGroupId          = Convert.ToInt32(dr[Constants.FLOW_GROUP_ID_COLUMN_NAME], CultureInfo.InvariantCulture);
                int?   FlowMultiplierTypeId = null;
                int?   Iteration            = null;
                int?   Timestep             = null;
                string FileName             = Convert.ToString(dr[Constants.MULTIPLIER_FILE_COLUMN_NAME], CultureInfo.InvariantCulture);

                if (dr[Constants.FLOW_MULTIPLIER_TYPE_ID_COLUMN_NAME] != DBNull.Value)
                {
                    FlowMultiplierTypeId = Convert.ToInt32(dr[Constants.FLOW_MULTIPLIER_TYPE_ID_COLUMN_NAME], CultureInfo.InvariantCulture);
                }

                if (dr[Constants.ITERATION_COLUMN_NAME] != DBNull.Value)
                {
                    Iteration = Convert.ToInt32(dr[Constants.ITERATION_COLUMN_NAME], CultureInfo.InvariantCulture);
                }

                if (dr[Constants.TIMESTEP_COLUMN_NAME] != DBNull.Value)
                {
                    Timestep = Convert.ToInt32(dr[Constants.TIMESTEP_COLUMN_NAME], CultureInfo.InvariantCulture);
                }

                FlowLateralMultiplier Multiplier = new FlowLateralMultiplier(FlowGroupId, FlowMultiplierTypeId, Iteration, Timestep, FileName);
                string FullFilename = Spatial.GetSpatialInputFileName(ds, FileName, false);
                StochasticTimeRaster MultiplierRaster;

                try
                {
                    MultiplierRaster = new StochasticTimeRaster(FullFilename, RasterDataType.DTDouble);
                }
                catch (Exception)
                {
                    string msg = string.Format(CultureInfo.InvariantCulture, Constants.SPATIAL_PROCESS_WARNING, FullFilename);
                    throw new ArgumentException(msg);
                }

                this.m_FlowLateralMultipliers.Add(Multiplier);

                //Only load a single instance of a each unique filename to conserve memory

                if (!this.m_FlowLateralMultiplierRasters.ContainsKey(FileName))
                {
                    this.STSimTransformer.CompressRasterForCellCollection(MultiplierRaster);
                    this.m_FlowLateralMultiplierRasters.Add(FileName, MultiplierRaster);
                }
            }
        }
        private void WriteAverageLateralFlowRasters()
        {
            Debug.Assert(this.STSimTransformer.IsSpatial);
            Debug.Assert(this.m_CreateAvgSpatialLateralFlowOutput);

            foreach (int id in this.m_AvgLateralFlowMap.Keys)
            {
                FlowGroup sg = this.m_FlowGroups[id];

                if (!sg.OutputFilter.HasFlag(Constants.OutputFilter.AvgSpatial))
                {
                    continue;
                }

                Dictionary <int, double[]> dict = this.m_AvgLateralFlowMap[id];

                foreach (int timestep in dict.Keys)
                {
                    if (timestep == this.STSimTransformer.TimestepZero)
                    {
                        continue;
                    }

                    double[] values    = dict[timestep];
                    var      distArray = values.Distinct();

                    if (distArray.Count() == 1)
                    {
                        var el0 = distArray.ElementAt(0);

                        if (el0.Equals(Spatial.DefaultNoDataValue))
                        {
                            continue;
                        }
                    }

                    StochasticTimeRaster rast = this.STSimTransformer.InputRasters.CreateOutputRaster(RasterDataType.DTDouble);
                    double[]             arr  = rast.DblCells;

                    foreach (Cell c in this.STSimTransformer.Cells)
                    {
                        arr[c.CellId] = values[c.CollectionIndex] / this.STSimTransformer.AmountPerCell;
                    }

                    Spatial.WriteRasterData(
                        rast,
                        this.ResultScenario.GetDataSheet(Constants.DATASHEET_OUTPUT_AVG_SPATIAL_LATERAL_FLOW_GROUP),
                        0,
                        timestep,
                        id,
                        Constants.SPATIAL_MAP_AVG_LATERAL_FLOW_GROUP_VARIABLE_PREFIX,
                        Constants.DATASHEET_OUTPUT_SPATIAL_FILENAME_COLUMN);
                }
            }
        }
        private double GetTransitionSpatialMultiplier(Cell cell, int transitionTypeId, int iteration, int timestep)
        {
            if (this.m_TransitionSpatialMultipliers.Count == 0)
            {
#if DEBUG
                foreach (TransitionMultiplierType tmt in this.m_TransitionMultiplierTypes)
                {
                    Debug.Assert(tmt.TransitionSpatialMultiplierMap == null);
                }
#endif

                return(1.0);
            }

            Debug.Assert(this.m_TransitionMultiplierTypes.Count > 0);

            double         Product = 1.0;
            TransitionType tt      = this.m_TransitionTypes[transitionTypeId];

            foreach (TransitionMultiplierType tmt in this.m_TransitionMultiplierTypes)
            {
                if (tmt.TransitionSpatialMultiplierMap != null)
                {
                    foreach (TransitionGroup tg in tt.TransitionGroups)
                    {
                        TransitionSpatialMultiplier tsmr = tmt.TransitionSpatialMultiplierMap.GetMultiplier(
                            tg.TransitionGroupId, iteration, timestep);

                        if (tsmr != null)
                        {
                            //Using a single instance of each uniquely named TSM raster

                            Debug.Assert(this.m_TransitionSpatialMultiplierRasters.ContainsKey(tsmr.FileName));

                            if (this.m_TransitionSpatialMultiplierRasters.ContainsKey(tsmr.FileName))
                            {
                                StochasticTimeRaster rastMult = this.m_TransitionSpatialMultiplierRasters[tsmr.FileName];
                                double spatialMult            = rastMult.DblCells[cell.CollectionIndex];

                                //Test for NODATA_VALUE

                                if (spatialMult < 0.0 | MathUtils.CompareDoublesEqual(spatialMult, rastMult.NoDataValue, double.Epsilon))
                                {
                                    spatialMult = 1.0;
                                }

                                Product *= spatialMult;
                            }
                        }
                    }
                }
            }

            return(Product);
        }
Esempio n. 9
0
 public void SetMetadata(StochasticTimeRaster raster)
 {
     this.m_Width         = raster.Width;
     this.m_Height        = raster.Height;
     this.m_cellSize      = raster.CellSize;
     this.m_cellSizeUnits = raster.CellSizeUnits;
     this.m_XllCorner     = raster.XllCorner;
     this.m_YllCorner     = raster.YllCorner;
     this.m_NoDataValue   = Spatial.DefaultNoDataValue;
     this.m_Projection    = raster.Projection;
 }
        private void WriteLateralFlowRasters(int iteration, int timestep)
        {
            Debug.Assert(this.m_IsSpatial);

            foreach (FlowGroup g in this.m_FlowGroups)
            {
                if (!g.OutputFilter.HasFlag(Constants.OutputFilter.Spatial))
                {
                    continue;
                }

                bool AtLeastOne = false;
                StochasticTimeRaster rastOutput = this.STSimTransformer.InputRasters.CreateOutputRaster(RasterDataType.DTDouble);

                foreach (FlowTypeLinkage l in g.FlowTypeLinkages)
                {
                    if (GetLateralOutputFlowDictionary().ContainsKey(l.FlowType.Id))
                    {
                        SpatialOutputFlowRecord rec = GetLateralOutputFlowDictionary()[l.FlowType.Id];

                        if (rec.HasOutputData)
                        {
                            StochasticTimeRaster rastFlowType =
                                this.STSimTransformer.InputRasters.CreateOutputRaster(RasterDataType.DTDouble);

                            double[] arr = rastFlowType.DblCells;

                            foreach (Cell c in this.m_STSimTransformer.Cells)
                            {
                                arr[c.CellId] = rec.Data[c.CollectionIndex];
                            }

                            rastFlowType.ScaleDblCells(l.Value);
                            rastOutput.AddDblCells(rastFlowType);

                            AtLeastOne = true;
                        }
                    }
                }

                if (AtLeastOne)
                {
                    Spatial.WriteRasterData(
                        rastOutput,
                        this.ResultScenario.GetDataSheet(Constants.DATASHEET_OUTPUT_LATERAL_FLOW_GROUP),
                        iteration,
                        timestep,
                        g.Id,
                        Constants.SPATIAL_MAP_LATERAL_FLOW_GROUP_VARIABLE_PREFIX,
                        Constants.DATASHEET_OUTPUT_SPATIAL_FILENAME_COLUMN);
                }
            }
        }
Esempio n. 11
0
        public StochasticTimeRaster GetRaster(int transitionGroupId, int iteration)
        {
            if (!this.m_HasItems)
            {
                return(null);
            }

            SortedKeyMap1 <InitialTSTSpatial> m = this.m_Map.GetItem(transitionGroupId);

            if (m == null)
            {
                return(null);
            }

            InitialTSTSpatial v = m.GetItem(iteration);

            if (v == null)
            {
                return(null);
            }

            string FullFileName = Spatial.GetSpatialInputFileName(this.m_DataSheet, v.FileName, false);

            if (!this.m_Rasters.ContainsKey(FullFileName))
            {
                string CmpMsg                   = null;
                StochasticTimeRaster  r         = new StochasticTimeRaster(FullFileName, RasterDataType.DTInteger);
                CompareMetadataResult cmpResult = this.m_InputRasters.CompareMetadata(r, ref CmpMsg);

                if (cmpResult == CompareMetadataResult.RowColumnMismatch)
                {
                    string Message = string.Format(CultureInfo.InvariantCulture,
                                                   MessageStrings.ERROR_SPATIAL_FILE_MISMATCHED_METADATA,
                                                   v.FileName, CmpMsg);

                    throw new STSimException(Message);
                }
                else if (cmpResult == CompareMetadataResult.UnimportantDifferences)
                {
                    string Message = string.Format(CultureInfo.InvariantCulture,
                                                   MessageStrings.STATUS_SPATIAL_FILE_MISMATCHED_METADATA_INFO,
                                                   v.FileName, CmpMsg);

                    this.Scenario.RecordStatus(StatusType.Information, Message);
                }

                this.m_Rasters.Add(FullFileName, r);
            }

            return(this.m_Rasters[FullFileName]);
        }
Esempio n. 12
0
        public override void Validate(DataRow proposedRow, DataTransferMethod transferMethod)
        {
            base.Validate(proposedRow, transferMethod);

            List <string> ColNames = new List <string>();

            ColNames.Add(Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);
            ColNames.Add(Strings.DATASHEET_SPIC_SECONDARY_STRATUM_FILE_COLUMN_NAME);
            ColNames.Add(Strings.DATASHEET_SPIC_STATE_CLASS_FILE_COLUMN_NAME);
            ColNames.Add(Strings.DATASHEET_SPIC_AGE_FILE_COLUMN_NAME);

            DataTable            ThisData    = this.GetData();
            StochasticTimeRaster FirstRaster = null;

            if (ThisData.DefaultView.Count == 0)
            {
                FirstRaster = this.LoadRaster(proposedRow, Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);
            }
            else
            {
                FirstRaster = this.LoadRaster(ThisData.DefaultView[0].Row, Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);
            }

            try
            {
                ValidatingRasters?.Invoke(this, new EventArgs());

                foreach (string s in ColNames)
                {
                    if (proposedRow[s] != DBNull.Value)
                    {
                        StochasticTimeRaster rast = this.LoadRaster(proposedRow, s);

                        try
                        {
                            this.ValidateRaster(rast, FirstRaster.Height, FirstRaster.Width, s);
                        }
                        catch (Exception)
                        {
                            proposedRow[s] = DBNull.Value;
                            throw;
                        }
                    }
                }
            }
            finally
            {
                RastersValidated?.Invoke(this, new EventArgs());
            }
        }
Esempio n. 13
0
        public void Save(string mergedRasterOutputFilename, GeoTiffCompressionType compressionType)
        {
            if (File.Exists(mergedRasterOutputFilename))
            {
                File.SetAttributes(mergedRasterOutputFilename, FileAttributes.Normal);
                File.Delete(mergedRasterOutputFilename);
            }

            Debug.Assert(this.m_rasterMerge.DataType == RasterDataType.DTDouble);
            StochasticTimeRaster OutRast = new StochasticTimeRaster(mergedRasterOutputFilename, this.m_rasterMerge);

            OutRast.DblCells = this.m_rasterMerge.DblCells;
            OutRast.Save(compressionType);
        }
Esempio n. 14
0
        private void InitTSTFromRaster(Cell simulationCell, int iteration)
        {
            if (!this.m_InitialTstSpatialMap.HasItems)
            {
                return;
            }

            foreach (Tst tst in simulationCell.TstValues)
            {
                StochasticTimeRaster r = this.m_InitialTstSpatialMap.GetRaster(tst.TransitionGroupId, iteration);

                if (r != null)
                {
                    tst.TstValue = r.IntCells[simulationCell.CellId];
                }
            }
        }
Esempio n. 15
0
        protected override void OnRowsAdded(object sender, DataSheetRowEventArgs e)
        {
            base.OnRowsAdded(sender, e);

            var       ThisData = this.GetData();
            DataSheet dsProp   = this.GetDataSheet(Strings.DATASHEET_SPPIC_NAME);
            DataRow   drProp   = dsProp.GetDataRow();

            if (drProp == null && ThisData.DefaultView.Count > 0)
            {
                dsProp.BeginAddRows();
                drProp = dsProp.GetData().NewRow();

                DataRow FirstRow = ThisData.DefaultView[0].Row;
                StochasticTimeRaster FirstRast = this.LoadRaster(FirstRow, Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);

                if (FirstRast.IntCells == null)
                {
                    FirstRast.LoadData();
                }

                drProp[Strings.DATASHEET_SPPIC_NUM_ROWS_COLUMN_NAME]           = FirstRast.Height;
                drProp[Strings.DATASHEET_SPPIC_NUM_COLUMNS_COLUMN_NAME]        = FirstRast.Width;
                drProp[Strings.DATASHEET_SPPIC_NUM_CELLS_COLUMN_NAME]          = FirstRast.GetNumberValidCells();
                drProp[Strings.DATASHEET_SPPIC_XLLCORNER_COLUMN_NAME]          = FirstRast.XllCorner;
                drProp[Strings.DATASHEET_SPPIC_YLLCORNER_COLUMN_NAME]          = FirstRast.YllCorner;
                drProp[Strings.DATASHEET_SPPIC_CELL_SIZE_COLUMN_NAME]          = FirstRast.CellSize;
                drProp[Strings.DATASHEET_SPPIC_CELL_SIZE_UNITS_COLUMN_NAME]    = FirstRast.CellSizeUnits;
                drProp[Strings.DATASHEET_SPPIC_SRS_COLUMN_NAME]                = FirstRast.Projection;
                drProp[Strings.DATASHEET_SPPIC_CELL_AREA_OVERRIDE_COLUMN_NAME] = false;

                string          amountlabel  = null;
                TerminologyUnit destUnitsVal = 0;
                double          cellArea     = System.Math.Pow((double)FirstRast.CellSize, 2);

                TerminologyUtilities.GetAmountLabelTerminology(this.Project.GetDataSheet(Strings.DATASHEET_TERMINOLOGY_NAME), ref amountlabel, ref destUnitsVal);
                drProp[Strings.DATASHEET_SPPIC_CELL_AREA_COLUMN_NAME] = CalcCellArea(cellArea, FirstRast.CellSizeUnits, destUnitsVal);

                dsProp.Changes.Add(new ChangeRecord(this, "Added raster metadata"));
                dsProp.GetData().Rows.Add(drProp);
                dsProp.EndAddRows();
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Get Stock Values for the specified Stock Type ID, placing then into the DblCells() in the specified raster.
        /// </summary>
        /// <param name="stockTypeId">The Stock Type ID that we want values for</param>
        /// <param name="rastStockType">An object of type ApexRaster, where we will write the Stock Type values. The raster should be initialized with metadata and appropriate array sizing.</param>
        /// <remarks></remarks>
        private void GetStockValues(int stockTypeId, StochasticTimeRaster rastStockType)
        {
            double AmountPerCell = this.m_STSimTransformer.AmountPerCell;

            foreach (Cell c in this.STSimTransformer.Cells)
            {
                Dictionary <int, double> StockAmounts = GetStockAmountDictionary(c);

                if (StockAmounts.Count > 0)
                {
                    rastStockType.DblCells[c.CellId] = (StockAmounts[stockTypeId] / AmountPerCell);
                }
                else
                {
                    //I wouldnt expect to get here because of Stratum/StateClass test above
                    Debug.Assert(false);
                }
            }
        }
Esempio n. 17
0
        public void Merge(string inpRasterFileName, int numIterations)
        {
            if (m_rasterMerge == null)
            {
                this.m_rasterMerge = new StochasticTimeRaster(inpRasterFileName, RasterDataType.DTDouble);
                m_rasterMerge.ScaleDblCells(numIterations);
                return;
            }

            StochasticTimeRaster rastInput = new StochasticTimeRaster(inpRasterFileName, RasterDataType.DTDouble);

            if (rastInput.Width != m_rasterMerge.Width || rastInput.Height != m_rasterMerge.Height)
            {
                string sMsg = string.Format(CultureInfo.InvariantCulture,
                                            "The metadata of the merge raster file '{0}' does not match that used in previous raster files.",
                                            inpRasterFileName);

                throw new ArgumentException(sMsg);
            }

            rastInput.ScaleDblCells(numIterations);
            m_rasterMerge.AddDblCells(rastInput);
        }
Esempio n. 18
0
        protected override void BeforeImportData(DataTable proposedData, string importFileName)
        {
            base.BeforeImportData(proposedData, importFileName);

            if (proposedData.Rows.Count == 0)
            {
                return;
            }

            List <string> ColNames = new List <string>();

            ColNames.Add(Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);
            ColNames.Add(Strings.DATASHEET_SPIC_SECONDARY_STRATUM_FILE_COLUMN_NAME);
            ColNames.Add(Strings.DATASHEET_SPIC_STATE_CLASS_FILE_COLUMN_NAME);
            ColNames.Add(Strings.DATASHEET_SPIC_AGE_FILE_COLUMN_NAME);

            DataTable            ThisData    = this.GetData();
            StochasticTimeRaster FirstRaster = null;

            if (ThisData.DefaultView.Count == 0)
            {
                FirstRaster = this.LoadRaster(proposedData.Rows[0], Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);
            }
            else
            {
                FirstRaster = this.LoadRaster(ThisData.DefaultView[0].Row, Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME);
            }

            try
            {
                ValidatingRasters?.Invoke(this, new EventArgs());

                foreach (DataRow dr in proposedData.Rows)
                {
                    if (dr.RowState != DataRowState.Deleted)
                    {
                        foreach (string s in ColNames)
                        {
                            if (dr[s] != DBNull.Value)
                            {
                                StochasticTimeRaster rast = this.LoadRaster(dr, s);

                                try
                                {
                                    this.ValidateRaster(rast, FirstRaster.Height, FirstRaster.Width, s);
                                }
                                catch (Exception)
                                {
                                    dr[s] = DBNull.Value;
                                    throw;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                RastersValidated?.Invoke(this, new EventArgs());
            }
        }
Esempio n. 19
0
        private void ValidateRaster(StochasticTimeRaster rast, int rows, int columns, string columnName)
        {
            string PrimaryStratumLabel   = null;
            string SecondaryStratumLabel = null;
            string TertiaryStratumLabel  = null;
            string ColumnDisplayName     = this.Columns[columnName].DisplayName;

            DataSheet TerminologySheet =
                this.Project.GetDataSheet(Strings.DATASHEET_TERMINOLOGY_NAME);

            TerminologyUtilities.GetStratumLabelTerminology(
                TerminologySheet, ref PrimaryStratumLabel, ref SecondaryStratumLabel, ref TertiaryStratumLabel);

            if (columnName == Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME)
            {
                ColumnDisplayName = PrimaryStratumLabel;
            }
            else if (columnName == Strings.DATASHEET_SPIC_SECONDARY_STRATUM_FILE_COLUMN_NAME)
            {
                ColumnDisplayName = SecondaryStratumLabel;
            }

            if (rast.Height != rows)
            {
                string msg = null;

                if (columnName == Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME)
                {
                    msg = string.Format(CultureInfo.InvariantCulture,
                                        "The number of rows for the '{0}' raster does not match that of the other '{1}' rasters.",
                                        PrimaryStratumLabel, PrimaryStratumLabel);
                }
                else
                {
                    msg = string.Format(CultureInfo.InvariantCulture,
                                        "The number of rows for the '{0}' raster does not match that of the '{1}' raster.",
                                        ColumnDisplayName, PrimaryStratumLabel);
                }

                throw new DataException(msg);
            }
            else if (rast.Width != columns)
            {
                string msg = null;

                if (columnName == Strings.DATASHEET_SPIC_STRATUM_FILE_COLUMN_NAME)
                {
                    msg = string.Format(CultureInfo.InvariantCulture,
                                        "The number of columns for the '{0}' raster does not match that of the other '{1}' rasters.",
                                        PrimaryStratumLabel, PrimaryStratumLabel);
                }
                else
                {
                    msg = string.Format(CultureInfo.InvariantCulture,
                                        "The number of columns for the '{0}' raster does not match that of the '{1}' raster.",
                                        ColumnDisplayName, PrimaryStratumLabel);
                }

                throw new DataException(msg);
            }
        }
Esempio n. 20
0
        private void FillInitialStocksSpatial()
        {
            Debug.Assert(this.m_IsSpatial);
            Debug.Assert(this.m_InitialStocksSpatial.Count == 0);
            DataSheet ds = this.ResultScenario.GetDataSheet(Constants.DATASHEET_INITIAL_STOCK_SPATIAL);

            foreach (DataRow dr in ds.GetData().Rows)
            {
                string stockFileName = Convert.ToString(dr[Constants.RASTER_FILE_COLUMN_NAME], CultureInfo.InvariantCulture);
                string fullFilename  = Spatial.GetSpatialInputFileName(ds, stockFileName, false);
                int?   Iteration     = null;

                if (dr[Constants.ITERATION_COLUMN_NAME] != DBNull.Value)
                {
                    Iteration = Convert.ToInt32(dr[Constants.ITERATION_COLUMN_NAME], CultureInfo.InvariantCulture);
                }

                //Load Initial Stock raster file
                StochasticTimeRaster raster;

                try
                {
                    raster = new StochasticTimeRaster(fullFilename, RasterDataType.DTDouble);
                }
                catch (Exception)
                {
                    string message = string.Format(CultureInfo.InvariantCulture, Constants.SPATIAL_FILE_STOCK_LOAD_WARNING, stockFileName);
                    throw new ArgumentException(message);
                }

                //Compare the Stock raster metadata to standard to make rasters match
                string cmpMsg    = "";
                var    cmpResult = this.STSimTransformer.InputRasters.CompareMetadata(raster, ref cmpMsg);

                if (cmpResult == STSim.CompareMetadataResult.RowColumnMismatch)
                {
                    string message = string.Format(CultureInfo.InvariantCulture, Constants.SPATIAL_FILE_STOCK_ROW_COLUMN_MISMATCH, stockFileName, cmpMsg);
                    ExceptionUtils.ThrowArgumentException(message);
                }
                else
                {
                    if (cmpResult == STSim.CompareMetadataResult.UnimportantDifferences)
                    {
                        string message = string.Format(CultureInfo.InvariantCulture, Constants.SPATIAL_FILE_STOCK_METADATA_INFO, stockFileName, cmpMsg);
                        this.RecordStatus(StatusType.Information, message);
                    }

                    this.m_InitialStocksSpatial.Add(
                        new InitialStockSpatial(
                            Iteration,
                            Convert.ToInt32(dr[Constants.STOCK_TYPE_ID_COLUMN_NAME], CultureInfo.InvariantCulture),
                            stockFileName));

                    //Only loading single instance of a particular raster, as a way to conserve memory

                    if (!m_InitialStockSpatialRasters.ContainsKey(stockFileName))
                    {
                        this.m_InitialStockSpatialRasters.Add(stockFileName, raster);
                    }
                }
            }
        }