Пример #1
0
        public static void InitializeMetadata(int Timestep, string SpeciesMapFileName, IEnumerable <ISuitabilityParameters> suitabilityParams, ICore mCore)
        {
            ScenarioReplicationMetadata scenRep = new ScenarioReplicationMetadata()
            {
                RasterOutCellArea = PlugIn.ModelCore.CellArea,
                TimeMin           = PlugIn.ModelCore.StartTime,
                TimeMax           = PlugIn.ModelCore.EndTime //,
                                                             //ProjectionFilePath = "Projection.?"
            };

            Extension = new ExtensionMetadata(mCore)
            {
                Name         = PlugIn.PlugInName,
                TimeInterval = Timestep,
                ScenarioReplicationMetadata = scenRep
            };


            //---------------------------------------
            //          table outputs:
            //---------------------------------------

            // NONE

            //---------------------------------------
            //          map outputs:
            //---------------------------------------

            foreach (SuitabilityParameters habitatModel in suitabilityParams)
            {
                string sppMapPath = MapFileNames.ReplaceTemplateVars(SpeciesMapFileName, habitatModel.HabitatName);

                OutputMetadata mapOut_LocalHabitat = new OutputMetadata()
                {
                    Type = OutputType.Map,
                    Name = ("Local Habitat Map: " + habitatModel.HabitatName),
                    //sppModel.Name,
                    FilePath     = @sppMapPath,
                    Map_DataType = MapDataType.Continuous,
                    Map_Unit     = "Habitat Class",
                    Visualize    = true,
                };
                Extension.OutputMetadatas.Add(mapOut_LocalHabitat);
            }
            //---------------------------------------
            MetadataProvider mp = new MetadataProvider(Extension);

            mp.WriteMetadataToXMLFile("Metadata", Extension.Name, Extension.Name);
        }
Пример #2
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs the component for a particular timestep.
        /// </summary>
        /// <param name="currentTime">
        /// The current model timestep.
        /// </param>
        public override void Run()
        {
            // The index variable increments with each suitability file - an index of multiple wildlife input files
            int index = 0;

            foreach (string suitabilityFile in suitabilityFiles)
            {
                // get suitability parameters
                ISuitabilityParameters mySuitabilityParameters = suitabilityParameters[index];

                foreach (Site site in modelCore.Landscape.ActiveSites)
                {
                    double suitabilityValue = 0;
                    // calculate dominant age as site variable (DomAge), store for retrieval
                    //  Note: DomAge site variable is an array of values giving dominant age for this year and last year
                    UpdateDominantAge(site);

                    // calculate forest type as site variable (ForestType), store for retreival
                    //  Note: ForestType site variable is an array of values giving the index of forest type for this year and last year
                    UpdateForestType(index, mySuitabilityParameters, site);

                    // depending on suitability type, calculate final suitability value

                    if (mySuitabilityParameters.SuitabilityType == "AgeClass_ForestType")
                    {
                        //   get forest type (calculated above with UpdateForestType)
                        int currentForestType = SiteVars.ForestType[site][index][0];

                        if (currentForestType > 0)
                        {
                            IForestType forestType = mySuitabilityParameters.ForestTypes[0].ForestTypes[currentForestType - 1];
                            //   calculate dominant age among species in forest type
                            int domFTAge = CalculateDomAgeForestType(site, forestType);
                            //   look up suitability in suitabilityTable for combination of forest type and age
                            Dictionary <int, double> suitabilityRow = mySuitabilityParameters.Suitabilities[forestType.Name];

                            foreach (KeyValuePair <int, double> item in suitabilityRow)
                            {
                                int ageLimit = item.Key;
                                if (domFTAge <= ageLimit)
                                {
                                    suitabilityValue = item.Value;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            suitabilityValue = 0;
                        }
                        //  write sitevar for suitability value
                        SiteVars.SuitabilityValue[site][index] = suitabilityValue;
                    }
                    // if suitabilityType == AgeClass_TimeSinceDisturbance:
                    else if (mySuitabilityParameters.SuitabilityType == "AgeClass_TimeSinceDisturbance")
                    {
                        int    ageAtDisturbanceYear = 0;
                        int    timeSinceDisturbance = 0;
                        double suitabilityWeight    = 0.0;
                        // if disturbanceType == "Fire" then:
                        if (mySuitabilityParameters.DisturbanceType == "Fire")
                        {
                            // Check if fire severity output exists
                            if (SiteVars.FireSeverity == null)
                            {
                                string mesg = string.Format("The DisturbanceType is Fire, but FireSeverity SiteVariable is not defined.  Please double-check that a fire extension is running.");
                                throw new System.ApplicationException(mesg);
                            }
                            else
                            {
                                //   Check this year fire severity
                                int currentFireSeverity = (int)SiteVars.FireSeverity[site];
                                int timeofLastFire      = SiteVars.TimeOfLastFire[site];
                                //   if > 0 then update sites with new values
                                if (currentFireSeverity > 0 && timeofLastFire == ModelCore.CurrentTime)
                                {
                                    //      translate to suitability weight
                                    suitabilityWeight = mySuitabilityParameters.FireSeverities[currentFireSeverity];

                                    SiteVars.SuitabilityWeight[site][index] = suitabilityWeight;

                                    //      if suitability weight > 0 then
                                    if (suitabilityWeight > 0)
                                    {
                                        //        store sitevar YearOfFire by index
                                        SiteVars.YearOfFire[site][index] = ModelCore.CurrentTime;
                                        //        read previous year dominant age
                                        int prevYearDomAge = SiteVars.DominantAge[site][1];
                                        //        store sitevar AgeAtFireYear by index
                                        SiteVars.AgeAtFireYear[site][index] = prevYearDomAge;
                                    }
                                }
                                //  read sitevar AgeAtDisturbanceYear for age value
                                ageAtDisturbanceYear = SiteVars.AgeAtFireYear[site][index];
                                //  read sitevar YearOfFire
                                int yearOfFire = SiteVars.YearOfFire[site][index];
                                //  calculate timeSinceDisturbance = currentYear - YearOfFire
                                timeSinceDisturbance = ModelCore.CurrentTime - yearOfFire;
                            }
                        }
                        // if disturbanceType == "Harvest" then:
                        else if (mySuitabilityParameters.DisturbanceType == "Harvest")
                        {
                            // Check if harvest prescription output exists
                            if (SiteVars.PrescriptionName == null)
                            {
                                string mesg = string.Format("The DisturbanceType is Harvest, but prescriptionName SiteVariable is not defined.  Please double-check that a Harvest extension is running.");
                                throw new System.ApplicationException(mesg);
                            }
                            else
                            {
                                //  Check this year harvest prescription names
                                string currentprescriptionName = SiteVars.PrescriptionName[site];
                                int    timeofLastHarvest       = SiteVars.TimeOfLastHarvest[site];
                                //   if != null then
                                if (currentprescriptionName != null && timeofLastHarvest == ModelCore.CurrentTime)
                                {
                                    //      translate to suitability weight
                                    suitabilityWeight = mySuitabilityParameters.HarvestPrescriptions[currentprescriptionName];
                                    //      if suitability weight > 0 then
                                    if (suitabilityWeight > 0)
                                    {
                                        //        store sitevar YearOfHarvest by index
                                        SiteVars.YearOfHarvest[site][index] = ModelCore.CurrentTime;
                                        //        read previous year dominant age
                                        int prevYearDomAge = SiteVars.DominantAge[site][1];
                                        //        store sitevar AgeAtHarvestYear by index
                                        SiteVars.AgeAtHarvestYear[site][index] = prevYearDomAge;
                                        //        store sitevar SuitabilityWeight by index
                                        SiteVars.SuitabilityWeight[site][index] = suitabilityWeight;
                                    }
                                }
                                //  read sitevar AgeAtDisturbanceYear for age value
                                ageAtDisturbanceYear = SiteVars.AgeAtHarvestYear[site][index];
                                //  read sitevar YearOfHarvest
                                int yearOfHarvest = SiteVars.YearOfHarvest[site][index];
                                //  calculate timeSinceDisturbance = currentYear - YearOfHarvest
                                timeSinceDisturbance = ModelCore.CurrentTime - yearOfHarvest;
                            }
                        }
                        //  look up suitabilty in suitabilityTable for combination of AgeAtDisturbanceYear and timeSinceDisturbance
                        foreach (KeyValuePair <string, Dictionary <int, double> > suitabilityRow in mySuitabilityParameters.Suitabilities)
                        {
                            int maxTimeSinceDist = int.Parse(suitabilityRow.Key);
                            if (timeSinceDisturbance <= maxTimeSinceDist)
                            {
                                foreach (KeyValuePair <int, double> item in suitabilityRow.Value)
                                {
                                    int ageLimit = item.Key;
                                    if (ageAtDisturbanceYear <= ageLimit)
                                    {
                                        suitabilityValue = (item.Value * SiteVars.SuitabilityWeight[site][index]);
                                        break;
                                    }
                                }
                                break;
                            }
                        }


                        // write sitevar for suitability value
                        SiteVars.SuitabilityValue[site][index] = suitabilityValue;
                    }

                    // if suitabilityType == ForestType_TimeSinceDisturbance:
                    else if (mySuitabilityParameters.SuitabilityType == "ForestType_TimeSinceDisturbance")
                    {
                        int    forestTypeAtDisturbanceYear = 0;
                        int    timeSinceDisturbance        = 0;
                        double suitabilityWeight           = 0.0;
                        // if disturbanceType == "Fire" then:
                        if (mySuitabilityParameters.DisturbanceType == "Fire")
                        {
                            // Check if fire output exists
                            if (SiteVars.FireSeverity == null)
                            {
                                string mesg = string.Format("The DisturbanceType is Fire, but FireSeverity SiteVariable is not defined.  Please double-check that a Fire extension is running.");
                                throw new System.ApplicationException(mesg);
                            }
                            else
                            {
                                //   Check this year fire severity
                                int currentFireSeverity = SiteVars.FireSeverity[site];
                                int timeofLastFire      = SiteVars.TimeOfLastFire[site];
                                //   if > 0 then
                                if (currentFireSeverity > 0 && timeofLastFire == ModelCore.CurrentTime)
                                {
                                    //      translate to suitability weight
                                    suitabilityWeight = mySuitabilityParameters.FireSeverities[currentFireSeverity];

                                    //      if suitability weight > 0 then
                                    if (suitabilityWeight > 0)
                                    {
                                        //        store sitevar YearOfFire by index
                                        SiteVars.YearOfFire[site][index] = ModelCore.CurrentTime;
                                        //        read previous year forest type
                                        int prevYearForType = SiteVars.ForestType[site][index][1];
                                        //        store sitevar forestTypeAtFireYear by index
                                        SiteVars.ForestTypeAtFireYear[site][index] = prevYearForType;
                                        //        store sitevar SuitabilityWeight by index
                                        SiteVars.SuitabilityWeight[site][index] = suitabilityWeight;
                                    }
                                }
                            }
                            //  read sitevar ForestTypeFireYear for age value
                            forestTypeAtDisturbanceYear = SiteVars.ForestTypeAtFireYear[site][index];
                            //  read sitevar YearOfFire
                            int yearOfFire = SiteVars.YearOfFire[site][index];
                            //  calculate timeSinceDisturbance = currentYear - YearOfFire
                            timeSinceDisturbance = ModelCore.CurrentTime - yearOfFire;
                        }
                        // if disturbanceType == "Harvest" then:
                        else if (mySuitabilityParameters.DisturbanceType == "Harvest")
                        {
                            // Check if harvest prescription output exists
                            if (SiteVars.PrescriptionName == null)
                            {
                                string mesg = string.Format("The DisturbanceType is Harvest, but prescriptionName SiteVariable is not defined.  Please double-check that a Harvest extension is running.");
                                throw new System.ApplicationException(mesg);
                            }
                            else
                            {
                                //  Check this year harvest prescription names
                                string currentprescriptionName = SiteVars.PrescriptionName[site];
                                int    timeofLastHarvest       = SiteVars.TimeOfLastHarvest[site];
                                //   if != null then
                                if (currentprescriptionName != null && timeofLastHarvest == ModelCore.CurrentTime)
                                {
                                    //      translate to suitability weight
                                    suitabilityWeight = mySuitabilityParameters.HarvestPrescriptions[currentprescriptionName];
                                    //      if suitability weight > 0 then
                                    if (suitabilityWeight > 0)
                                    {
                                        //        store sitevar YearOfHarvest by index
                                        SiteVars.YearOfHarvest[site][index] = ModelCore.CurrentTime;
                                        //        read previous year forest type
                                        int prevYearForType = SiteVars.ForestType[site][index][1];
                                        //        store sitevar forestTypeAtHarvestYear by index
                                        SiteVars.ForestTypeAtHarvestYear[site][index] = prevYearForType;
                                        //        store sitevar SuitabilityWeight by index
                                        SiteVars.SuitabilityWeight[site][index] = suitabilityWeight;
                                    }
                                }
                                //  read sitevar AgeAtHarvestYear for age value
                                forestTypeAtDisturbanceYear = SiteVars.ForestTypeAtHarvestYear[site][index];
                                //  read sitevar YearOfHarvest
                                int yearOfHarvest = SiteVars.YearOfHarvest[site][index];
                                //  calculate timeSinceDisturbance = currentYear - YearOfHarvest
                                timeSinceDisturbance = ModelCore.CurrentTime - yearOfHarvest;
                            }
                        }
                        if (forestTypeAtDisturbanceYear > 0)
                        {
                            IForestType forestType = mySuitabilityParameters.ForestTypes[0].ForestTypes[forestTypeAtDisturbanceYear - 1];

                            // look up suitability in suitabilityTable for combination of forest type and timeSinceDisturbance
                            Dictionary <int, double> suitabilityRow = mySuitabilityParameters.Suitabilities[forestType.Name];

                            foreach (KeyValuePair <int, double> item in suitabilityRow)
                            {
                                int maxTimeSinceDist = item.Key;
                                if (timeSinceDisturbance <= maxTimeSinceDist)
                                {
                                    suitabilityValue = (item.Value * SiteVars.SuitabilityWeight[site][index]);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            suitabilityValue = 0;
                        }

                        // write sitevar for suitability value
                        SiteVars.SuitabilityValue[site][index] = suitabilityValue;
                    }
                }
                // if output timestep then write all maps
                if ((ModelCore.CurrentTime % parameters.OutputTimestep) == 0)
                {
                    string mapName = mySuitabilityParameters.HabitatName;
                    string path    = MapFileNames.ReplaceTemplateVars(mapNameTemplate, mapName, modelCore.CurrentTime);
                    ModelCore.UI.WriteLine("   Writing Habitat Output map to {0} ...", path);
                    using (IOutputRaster <IntPixel> outputRaster = modelCore.CreateRaster <IntPixel>(path, modelCore.Landscape.Dimensions))
                    {
                        IntPixel pixel = outputRaster.BufferPixel;
                        foreach (Site site in modelCore.Landscape.AllSites)
                        {
                            if (site.IsActive)
                            {
                                pixel.MapCode.Value = (int)(SiteVars.SuitabilityValue[site][index] * 100);
                            }
                            else
                            {
                                pixel.MapCode.Value = 0;
                            }

                            outputRaster.WriteBufferPixel();
                        }
                    }
                }


                /*Copied from biomass-reclass
                 * foreach (IMapDefinition map in mapDefs)
                 * {
                 *  List<IForestType> forestTypes = map.ForestTypes;
                 *
                 *  string path = MapFileNames.ReplaceTemplateVars(mapNameTemplate, map.Name, modelCore.CurrentTime);
                 *  modelCore.Log.WriteLine("   Writing Biomass Reclass map to {0} ...", path);
                 *  using (IOutputRaster<BytePixel> outputRaster = modelCore.CreateRaster<BytePixel>(path, modelCore.Landscape.Dimensions))
                 *  {
                 *      BytePixel pixel = outputRaster.BufferPixel;
                 *      foreach (Site site in modelCore.Landscape.AllSites)
                 *      {
                 *          if (site.IsActive)
                 *              pixel.MapCode.Value = CalcForestType(forestTypes, site);
                 *          else
                 *              pixel.MapCode.Value = 0;
                 *
                 *          outputRaster.WriteBufferPixel();
                 *      }
                 *  }
                 *
                 * }
                 * */
                index++;
            }
        }