public OutputMapSiteVar(string FileName, ISiteVar <T> values, Func <T, M> func)
        {
            try
            {
                using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(FileName, PlugIn.ModelCore.Landscape.Dimensions))
                {
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            try
                            {
                                outputRaster.BufferPixel.MapCode.Value = (int)double.Parse(func(values[site]).ToString());// int.Parse(values[site].ToString());
                            }
                            catch (System.Exception e)
                            {
                                System.Console.WriteLine("Cannot write " + FileName + " for site " + site.Location.ToString() + " " + e.Message);
                            }
                        }
                        else
                        {
                            outputRaster.BufferPixel.MapCode.Value = 0;
                        }

                        outputRaster.WriteBufferPixel();
                    }
                }
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("Cannot write " + FileName + " " + e.Message);
            }
        }
예제 #2
0
        //---------------------------------------------------------------------


        private void CreateCommunityMap()
        {
            string path = MapNames.ReplaceTemplateVars(outputMapName, PlugIn.ModelCore.CurrentTime);

            //PlugIn.ModelCore.UI.WriteLine("   Writing community biomass map to {1} ...", path);
            PlugIn.ModelCore.UI.WriteLine("   Writing community biomass map to {0} ...", path);

            using (IOutputRaster <IntPixel> outputRaster = modelCore.CreateRaster <IntPixel>(path, modelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = SiteVars.MapCode[site];
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }

                    outputRaster.WriteBufferPixel();
                }
            }
        }
        //---------------------------------------------------------------------

        public override void Run()
        {
            //if keyword == maxage
            foreach (ISpecies species in selectedSpecies)
            {
                IOutputRaster <AgePixel> map = CreateMap(species.Name);
                using (map) {
                    AgePixel pixel = new AgePixel();
                    foreach (Site site in modelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.Band0 = AgeCohort.Util.GetMaxAge(cohorts[site][species]);
                        }
                        else
                        {
                            pixel.Band0 = 0;
                        }
                        map.WritePixel(pixel);
                    }
                }
            }

            WriteMapWithMaxAgeAmongAll();
        }
예제 #4
0
        //---------------------------------------------------------------------

        public Band(Gdal.RasterBand band,
                    IOutputRaster raster)
            : base(band)
        {
            this.raster = raster;
            toBytes     = new ToBytesMethod <T>(new U().ToBytes);
        }
예제 #5
0
        //---------------------------------------------------------------------

        protected void WriteMapsForYear(string pathTemplate,
                                        int yearInTimestep,
                                        GetSpeciesValueAt getSpeciesValueAt)
        {
            int successionTimestep  = seedingData.time_step;
            int firstYearInTimestep = Model.Core.CurrentTime - successionTimestep + 1;
            int year = firstYearInTimestep + yearInTimestep;

            foreach (ISpecies species in Model.Core.Species)
            {
                string path = MapPaths.ReplaceTemplateVars(pathTemplate, year, species.Name);
                int    s    = species.Index;
                using (IOutputRaster <IntPixel> outputRaster = Model.Core.CreateRaster <IntPixel>(path, Model.Core.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in Model.Core.Landscape.AllSites)
                    {
                        int x = site.Location.Column - 1;
                        int y = site.Location.Row - 1;
                        pixel.Band0.Value = getSpeciesValueAt(x, y, s, yearInTimestep);
                        outputRaster.WriteBufferPixel();
                    }
                }
            }
        }
예제 #6
0
        //---------------------------------------------------------------------

        private void WritePoolMap(string poolName,
                                  ISiteVar <Pool> poolSiteVar)
        {
            string path = PoolMapNames.ReplaceTemplateVars(poolMapNameTemplate,
                                                           poolName,
                                                           PlugIn.ModelCore.CurrentTime);

            if (poolSiteVar != null)
            {
                PlugIn.ModelCore.UI.WriteLine("   Writing {0} biomass map to {1} ...", poolName, path);
                using (IOutputRaster <IntPixel> outputRaster = modelCore.CreateRaster <IntPixel>(path, modelCore.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (int)((float)poolSiteVar[site].Mass);
                        }
                        else
                        {
                            pixel.MapCode.Value = 0;
                        }

                        outputRaster.WriteBufferPixel();
                    }
                }
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs the component for a particular timestep.
        /// </summary>
        /// <param name="currentTime">
        /// The current model timestep.
        /// </param>
        public void Run(int currentTime)
        {
            foreach (ISpecies species in selectedSpecies)
            {
                IOutputRaster <AgePixel> map = CreateMap(species.Name, currentTime);
                using (map) {
                    AgePixel pixel = new AgePixel();
                    foreach (Site site in Model.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.Band0 = AgeCohort.Util.GetMaxAge(cohorts[site][species]);
                        }
                        else
                        {
                            pixel.Band0 = 0;
                        }
                        map.WritePixel(pixel);
                    }
                }
            }

            WriteMapWithMaxAgeAmongAll(currentTime);
            nextTimeToRun += timestep;
        }
예제 #8
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs the component for a particular timestep.
        /// </summary>
        /// <param name="currentTime">
        /// The current model timestep.
        /// </param>
        public void Run(int currentTime)
        {
            foreach (IMapDefinition map in mapDefs)
            {
                IForestType[] forestTypes         = map.ForestTypes;
                IOutputRaster <ClassPixel> newmap = CreateMap(map.Name, currentTime);
                string path = newmap.Path;
                using (newmap) {
                    ClassPixel pixel = new ClassPixel();
                    foreach (Site site in Model.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.Band0 = CalcForestType(cohorts[site],
                                                         forestTypes);
                        }
                        else
                        {
                            pixel.Band0 = 0;
                        }
                        newmap.WritePixel(pixel);
                    }
                }

                //Erdas74TrailerFile.Write(path, map.ForestTypes);
            }

            nextTimeToRun += timestep;
        }
예제 #9
0
        //---------------------------------------------------------------------

        private void WriteSpeciesMaps()
        {
            foreach (ISpecies species in selectedSpecies)
            {
                string path = MakeSpeciesMapName(species.Name);
                PlugIn.ModelCore.UI.WriteLine("   Writing {0} biomass map to {1} ...", species.Name, path);

                using (IOutputRaster <IntPixel> outputRaster = modelCore.CreateRaster <IntPixel>(path, modelCore.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (int)Math.Round((double)ComputeSpeciesBiomass(SiteVars.Cohorts[site][species]));
                        }
                        else
                        {
                            pixel.MapCode.Value = 0;
                        }

                        outputRaster.WriteBufferPixel();
                    }
                }
            }
        }
예제 #10
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Writes an output map of prescriptions that harvested each active site.
        /// </summary>
        private void WritePrescriptionMap(int timestep)
        {
            string path = MapNames.ReplaceTemplateVars(nameTemplate, timestep);

            ModelCore.UI.WriteLine("   Writing prescription map to {0} ...", path);
            using (IOutputRaster <ShortPixel> outputRaster = modelCore.CreateRaster <ShortPixel>(path, modelCore.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in modelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        Prescription prescription = HarvestMgmtLib.SiteVars.Prescription[site];
                        if (prescription == null)
                        {
                            pixel.MapCode.Value = 1;
                        }
                        else
                        {
                            pixel.MapCode.Value = (short)(prescription.Number + 1);
                        }
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
        }
예제 #11
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs the component for a particular timestep.
        /// </summary>
        /// <param name="currentTime">
        /// The current model timestep.
        /// </param>
        public override void Run()
        {
            foreach (IMapDefinition map in mapDefs)
            {
                IForestType[] forestTypes         = map.ForestTypes;
                IOutputRaster <ClassPixel> newmap = CreateMap(map.Name);
                string path = newmap.Path;
                using (newmap) {
                    ClassPixel pixel = new ClassPixel();
                    foreach (Site site in modelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.Band0 = CalcForestType(site, forestTypes);
                        }
                        else
                        {
                            pixel.Band0 = 0;
                        }
                        newmap.WritePixel(pixel);
                    }
                }

                //Erdas74TrailerFile.Write(path, map.ForestTypes);
            }
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs the component for a particular timestep.
        /// </summary>
        /// <param name="currentTime">
        /// The current model timestep.
        /// </param>
        public override void Run()
        {
            foreach (IMapDefinition map in mapDefs)
            {
                List <IForestType> forestTypes = map.ForestTypes;

                string path = MapFileNames.ReplaceTemplateVars(mapNameTemplate, map.Name, modelCore.CurrentTime);
                modelCore.UI.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();
                    }
                }
            }
        }
예제 #13
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Writes an output map of prescriptions that harvested each active
        /// site.
        /// </summary>
        /// <param name="timestep">
        /// Timestep to use in the map's name.
        /// </param>
        public void WriteMap(int timestep)
        {
            string path = MapNames.ReplaceTemplateVars(nameTemplate, timestep);

            using (IOutputRaster <ShortPixel> outputRaster = Model.Core.CreateRaster <ShortPixel>(path, Model.Core.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;

                foreach (Site site in Model.Core.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        Prescription prescription = SiteVars.Prescription[site];
                        if (prescription == null)
                        {
                            pixel.MapCode.Value = 1;
                        }
                        else
                        {
                            pixel.MapCode.Value = (short)(prescription.Number + 1);
                        }
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
        }
        //---------------------------------------------------------------------

        public override void Run()
        {
            //if keyword == maxage
            foreach (ISpecies species in selectedSpecies)
            {
                string path = MapNameTemplates.ReplaceTemplateVars(mapNameTemplate, species.Name, modelCore.CurrentTime);
                modelCore.UI.WriteLine("   Writing maximum age map for {0} to {1} ...", species.Name, path);
                using (IOutputRaster <ShortPixel> outputRaster = modelCore.CreateRaster <ShortPixel>(path, modelCore.Landscape.Dimensions))
                {
                    ShortPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in modelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = SiteVars.GetMaxAge(species, (ActiveSite)site);
                        }
                        else
                        {
                            pixel.MapCode.Value = 0;
                        }

                        outputRaster.WriteBufferPixel();
                    }
                }
            }

            WriteMapWithMaxAgeAmongAll();
        }
        //---------------------------------------------------------------------


        private void WriteSpeciesMaps()
        {
            foreach (ISpecies species in selectedSpecies)
            {
                foreach (AgeClass ageclass in ageClasses[species.Name])
                {
                    string path = MakeSpeciesMapName(species.Name, ageclass.Name);
                    ModelCore.UI.WriteLine("   Writing {0} and {1} map to {2} ...", species.Name, ageclass.Name, 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)((float)Util.ComputeAgeClassBiomass(site, species, ageclass));
                            }
                            else
                            {
                                pixel.MapCode.Value = 0;
                            }

                            outputRaster.WriteBufferPixel();
                        }
                    }
                }
            }
        }
예제 #16
0
        public void Create()
        {
            IOutputRaster <Erdas74Pixel8> raster =
                lib.Create <Erdas74Pixel8>(Data.MakeOutputPath("temp.gis"),
                                           new Dimensions(20, 10),
                                           null);

            raster.Close();
        }
예제 #17
0
        //---------------------------------------------------------------------

        ///<summary>
        /// Run the plug-in at a particular timestep.
        ///</summary>
        public override void Run()
        {
            ModelCore.UI.WriteLine("Processing landscape for wind events ...");

            SiteVars.Event.SiteValues          = null;
            SiteVars.Severity.ActiveSiteValues = 0;

            int eventCount = 0;

            foreach (ActiveSite site in PlugIn.ModelCore.Landscape)
            {
                Event windEvent = Event.Initiate(site, Timestep);
                if (windEvent != null)
                {
                    LogEvent(PlugIn.ModelCore.CurrentTime, windEvent);
                    eventCount++;
                    summaryEventCount++;
                }
            }


            //ModelCore.UI.WriteLine("  Wind events: {0}", eventCount);

            //  Write wind severity map
            string     path       = MapNames.ReplaceTemplateVars(mapNameTemplate, PlugIn.modelCore.CurrentTime);
            Dimensions dimensions = new Dimensions(modelCore.Landscape.Rows, modelCore.Landscape.Columns);

            using (IOutputRaster <BytePixel> outputRaster = modelCore.CreateRaster <BytePixel>(path, dimensions))
            {
                BytePixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        if (SiteVars.Disturbed[site])
                        {
                            pixel.MapCode.Value = (byte)(SiteVars.Severity[site] + 1);
                        }
                        else
                        {
                            pixel.MapCode.Value = 1;
                        }
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            WriteSummaryLog(PlugIn.modelCore.CurrentTime);
            summaryTotalSites = 0;
            summaryEventCount = 0;
        }
예제 #18
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Writes an output map of biomass removed from each active site.
        /// </summary>
        /// <param name="timestep">
        /// Timestep to use in the map's name.
        /// </param>
        public void WriteMap(int timestep)
        {
            string path = MapNames.ReplaceTemplateVars(nameTemplate, timestep);

            PlugIn.ModelCore.UI.WriteLine("   Writing biomass-removed map to {0} ...", path);
            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(path, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    pixel.MapCode.Value = (int)Math.Round(SiteVars.BiomassRemoved[site]);
                    outputRaster.WriteBufferPixel();
                }
            }
        }
        //---------------------------------------------------------------------

        private void WriteMap(string speciesName,
                              MaxAgeCalculator maxAgeCalculator)
        {
            IOutputRaster <AgePixel> map = CreateMap(speciesName);

            using (map) {
                AgePixel pixel        = new AgePixel();
                Location location_1_1 = new Location(1, 1);
                foreach (Site site in modelCore.Landscape.AllSites)
                {
                    ushort age;
                    if (site.IsActive)
                    {
                        ActiveSite activeSite = (ActiveSite)site;
                        if (activeSite.SharesData)
                        {
                            Location blockLocation = activeSite.BroadScaleLocation;
                            if (activeSite.LocationInBlock == location_1_1)
                            {
                                age = maxAgeCalculator.ComputeMaxAge(activeSite);
                                ageBuffer[blockLocation.Column] = age;
                            }
                            else
                            {
                                // already computed age for the block
                                age = ageBuffer[blockLocation.Column];
                            }
                        }
                        else
                        {
                            age = maxAgeCalculator.ComputeMaxAge(activeSite);
                        }
                    }
                    else
                    {
                        // inactive site
                        age = 0;
                    }
                    pixel.Band0 = age;
                    map.WritePixel(pixel);
                }
            }
        }
예제 #20
0
        //---------------------------------------------------------------------

        public static IOutputRaster <T> Create <T>(string path,
                                                   Dimensions dimensions,
                                                   IMetadata metadata)
            where T : IPixel, new()
        {
            try {
                string dir = System.IO.Path.GetDirectoryName(path);
                if (dir.Length > 0)
                {
                    Directory.EnsureExists(dir);
                }
                IOutputRaster <T> raster = driver.Create <T>(path, dimensions, metadata);
                return(raster);
            }
            catch (System.IO.IOException exc) {
                string mesg = string.Format("Error opening map \"{0}\"", path);
                throw new MultiLineException(mesg, exc);
            }
        }
        //---------------------------------------------------------------------

        private void WriteMapWithMaxAgeAmongAll(int currentTime)
        {
            //	Maximum age map for all species
            IOutputRaster <AgePixel> map = CreateMap("all", currentTime);

            using (map) {
                AgePixel pixel = new AgePixel();
                foreach (Site site in Model.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.Band0 = AgeCohort.Util.GetMaxAge(cohorts[site]);
                    }
                    else
                    {
                        pixel.Band0 = 0;
                    }
                    map.WritePixel(pixel);
                }
            }
        }
        //---------------------------------------------------------------------

        private void WriteMapWithMaxAgeAmongAll()
        {
            //    Maximum age map for all species
            string path = MapNameTemplates.ReplaceTemplateVars(mapNameTemplate, "AllSppMaxAge", modelCore.CurrentTime);

            modelCore.UI.WriteLine("   Writing maximum age map for all species to {0} ...", path);
            using (IOutputRaster <ShortPixel> outputRaster = modelCore.CreateRaster <ShortPixel>(path, modelCore.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in modelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = SiteVars.GetMaxAge((ActiveSite)site);
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }

                    outputRaster.WriteBufferPixel();
                }
            }
        }
        public OutputMapSpecies(ISiteVar <int> values, ISpecies species, string MapNameTemplate)
        {
            FileName = FileNames.ReplaceTemplateVars(MapNameTemplate, species.Name, PlugIn.ModelCore.CurrentTime);

            Console.WriteLine("   Writing {0} map to {1} ...", species.Name, FileName);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(FileName, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)values[site];
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }

                    outputRaster.WriteBufferPixel();
                }
            }
        }
예제 #24
0
        //---------------------------------------------------------------------

        private void WriteMapForAllSpecies()
        {
            // Biomass map for all species
            string path = MakeSpeciesMapName("TotalBiomass");

            PlugIn.ModelCore.UI.WriteLine("   Writing total biomass map to {0} ...", path);
            using (IOutputRaster <IntPixel> outputRaster = modelCore.CreateRaster <IntPixel>(path, modelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)Math.Round((double)ComputeTotalBiomass(SiteVars.Cohorts[site]));
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }

                    outputRaster.WriteBufferPixel();
                }
            }
        }
예제 #25
0
        //---------------------------------------------------------------------
        public static void WriteLogFile(int CurrentTime)
        {
            double[] avgLiveB       = new double[PlugIn.ModelCore.Ecoregions.Count];
            double[] avgAG_NPP      = new double[PlugIn.ModelCore.Ecoregions.Count];
            double[] avgDefoliation = new double[PlugIn.ModelCore.Ecoregions.Count];
            double[] avgLitterB     = new double[PlugIn.ModelCore.Ecoregions.Count];


            foreach (IEcoregion ecoregion in PlugIn.ModelCore.Ecoregions)
            {
                avgLiveB[ecoregion.Index]       = 0.0;
                avgAG_NPP[ecoregion.Index]      = 0.0;
                avgDefoliation[ecoregion.Index] = 0.0;
                avgLitterB[ecoregion.Index]     = 0.0;
            }



            foreach (ActiveSite site in PlugIn.ModelCore.Landscape)
            {
                IEcoregion ecoregion = PlugIn.ModelCore.Ecoregion[site];
                int        youngBiomass; //ignored

                avgLiveB[ecoregion.Index]       += Cohorts.ComputeBiomass(SiteVars.Cohorts[site], out youngBiomass);
                avgAG_NPP[ecoregion.Index]      += SiteVars.AGNPP[site];
                avgDefoliation[ecoregion.Index] += SiteVars.Defoliation[site];
                avgLitterB[ecoregion.Index]     += SiteVars.Litter[site].Mass;
            }

            foreach (IEcoregion ecoregion in PlugIn.ModelCore.Ecoregions)
            {
                if (EcoregionData.ActiveSiteCount[ecoregion] > 0)
                {
                    PlugIn.summaryLog.Clear();
                    SummaryLog sl = new SummaryLog();
                    sl.Time           = CurrentTime;
                    sl.EcoName        = ecoregion.Name;
                    sl.ActiveCount    = EcoregionData.ActiveSiteCount[ecoregion];
                    sl.AvgAG_NPP      = (avgAG_NPP[ecoregion.Index] / (double)EcoregionData.ActiveSiteCount[ecoregion]);
                    sl.AvgDefoliation = (avgDefoliation[ecoregion.Index] / (double)EcoregionData.ActiveSiteCount[ecoregion]);
                    sl.AvgLitterB     = (avgLitterB[ecoregion.Index] / (double)EcoregionData.ActiveSiteCount[ecoregion]);
                    sl.AvgLiveB       = (avgLiveB[ecoregion.Index] / (double)EcoregionData.ActiveSiteCount[ecoregion]);

                    PlugIn.summaryLog.AddObject(sl);
                    PlugIn.summaryLog.WriteToFile();
                }
            }

            string path = MapNames.ReplaceTemplateVars(@"biomass-succession\biomass-anpp-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(path, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)SiteVars.AGNPP[site];
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
        }
        public static void WriteMaps()
        {
            string pathH2O = MapNames.ReplaceTemplateVars(@"NECN\Annual-water-budget-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathH2O, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.AnnualPPT_AET[site]));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //AMK: Trying out directly writing maps
            string pathANPP = MapNames.ReplaceTemplateVars(@"NECN\AG_NPP-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathANPP, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.AGNPPcarbon[site]));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            string path = MapNames.ReplaceTemplateVars(@"NECN\SOMTC-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(path, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.SOM1surface[site].Carbon + SiteVars.SOM1soil[site].Carbon + SiteVars.SOM2[site].Carbon + SiteVars.SOM3[site].Carbon));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            string path2 = MapNames.ReplaceTemplateVars(@"NECN\SoilN-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <ShortPixel> outputRaster = PlugIn.ModelCore.CreateRaster <ShortPixel>(path2, PlugIn.ModelCore.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (short)(SiteVars.MineralN[site]);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //}

            string path4 = MapNames.ReplaceTemplateVars(@"NECN\ANEE-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <ShortPixel> outputRaster = PlugIn.ModelCore.CreateRaster <ShortPixel>(path4, PlugIn.ModelCore.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (short)(SiteVars.AnnualNEE[site] + 1000);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            string path5 = MapNames.ReplaceTemplateVars(@"NECN\TotalC-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(path5, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)(Outputs.GetOrganicCarbon(site) +
                                                    SiteVars.CohortLeafC[site] +
                                                    SiteVars.CohortFRootC[site] +
                                                    SiteVars.CohortWoodC[site] +
                                                    SiteVars.CohortCRootC[site] +
                                                    SiteVars.SurfaceDeadWood[site].Carbon +
                                                    SiteVars.SoilDeadWood[site].Carbon);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //}

            string pathLAI = MapNames.ReplaceTemplateVars(@"NECN\LAI-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathLAI, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (short)(SiteVars.LAI[site]);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            string pathavailablewater = MapNames.ReplaceTemplateVars(@"NECN\AvailableWater-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathavailablewater, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.AvailableWater[site]));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            if (PlugIn.Parameters.SmokeModelOutputs)
            {
                string pathNeedles = MapNames.ReplaceTemplateVars(@"NECN\ConiferNeedleBiomass-{timestep}.img", PlugIn.ModelCore.CurrentTime);
                using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathNeedles, PlugIn.ModelCore.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (int)(Main.ComputeNeedleBiomass(SiteVars.Cohorts[site]));
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }
                        outputRaster.WriteBufferPixel();
                    }
                }

                string pathDWD = MapNames.ReplaceTemplateVars(@"NECN\DeadWoodBiomass-{timestep}.img", PlugIn.ModelCore.CurrentTime);
                using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathDWD, PlugIn.ModelCore.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (int)(SiteVars.SurfaceDeadWood[site].Carbon * 2.0);
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }
                        outputRaster.WriteBufferPixel();
                    }
                }

                string pathLitter = MapNames.ReplaceTemplateVars(@"NECN\SurfaceLitterBiomass-{timestep}.img", PlugIn.ModelCore.CurrentTime);
                using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathLitter, PlugIn.ModelCore.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (int)((SiteVars.SurfaceStructural[site].Carbon + SiteVars.SurfaceMetabolic[site].Carbon) * 2.0);
                            ;
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }
                        outputRaster.WriteBufferPixel();
                    }
                }

                string pathDuff = MapNames.ReplaceTemplateVars(@"NECN\SurfaceDuffBiomass-{timestep}.img", PlugIn.ModelCore.CurrentTime);
                using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathDuff, PlugIn.ModelCore.Landscape.Dimensions))
                {
                    IntPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (int)(SiteVars.SOM1surface[site].Carbon * 2.0);
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }
                        outputRaster.WriteBufferPixel();
                    }
                }
            }
        }
예제 #27
0
        internal bool GenerateWindFieldRaster(
            string mapNameTemplate, ICore modelCore, ContinentalGrid continentalGrid)
        {
            this.ContinentalGrid = continentalGrid;
            Dimensions dimensions          = new Dimensions(modelCore.Landscape.Rows, modelCore.Landscape.Columns);
            int        columns             = modelCore.Landscape.Columns;
            int        rows                = modelCore.Landscape.Rows;
            double     lowerLeftWindspeed  = this.GetWindSpeed(0, 0);
            double     lowerRightWindSpeed = this.GetWindSpeed(columns, 0);
            double     upperRightWindSpeed = this.GetWindSpeed(columns, rows);
            double     upperLeftWindSpeed  = this.GetWindSpeed(0, rows);
            double     maxWS               = (new[] { lowerLeftWindspeed, lowerRightWindSpeed, upperRightWindSpeed, upperLeftWindSpeed }).Max();

            //double minWS = (new[] { lowerLeftWindspeed, lowerRightWindSpeed, upperRightWindSpeed, upperLeftWindSpeed }).Min();
            this.studyAreaImpacts = true;
            if (maxWS < HurricaneEvent.MinimumWSforDamage)
            {
                //PlugIn.ModelCore.UI.WriteLine("   Hurricane Not Sufficient to Cause Damage:  MaxWS={0}, HurricaneMinWS={1}", maxWS, MinimumWSforDamage);
                this.studyAreaImpacts = false;
                //return false;
            }


            SiteVars.WindSpeed.ActiveSiteValues = 0.0;

            foreach (ActiveSite site in PlugIn.ModelCore.Landscape.ActiveSites)
            {
                currentSite = site;
                SiteVars.WindSpeed[currentSite] = this.GetWindSpeed(site.Location.Column, site.Location.Row);
                KillSiteCohorts(currentSite);
            }

            double activeAreaMinWS = 999.0;
            double activeAreaMaxWS = 0.0;

            string path = MapNames.ReplaceTemplateVars(mapNameTemplate, modelCore.CurrentTime);
            IOutputRaster <BytePixel> outputRaster = null;

            using (outputRaster = modelCore.CreateRaster <BytePixel>(path, dimensions))
            {
                BytePixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        double windspeed = SiteVars.WindSpeed[site];
                        pixel.MapCode.Value = (byte)windspeed;
                        if (windspeed < activeAreaMinWS)
                        {
                            activeAreaMinWS = windspeed;
                        }
                        if (windspeed > activeAreaMaxWS)
                        {
                            activeAreaMaxWS = windspeed;
                        }
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = (byte)0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            this.StudyAreaMinWindspeed = activeAreaMinWS;
            this.StudyAreaMaxWindspeed = activeAreaMaxWS;
            //PlugIn.ModelCore.UI.WriteLine("   Hurricane Caused Damage:  AreaMaxWS={0}, AreaMinWS={1}", activeAreaMaxWS, activeAreaMinWS);
            return(true);
        }
예제 #28
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs the component for a particular timestep.
        /// </summary>
        /// <param name="currentTime">
        /// The current model timestep.
        /// </param>
        public override void Run()
        {
            if (SiteVars.TimeOfLastFire == null)
            {
                SiteVars.ReInitialize();
            }

            SiteVars.FuelType.ActiveSiteValues      = 0;
            SiteVars.DecidFuelType.ActiveSiteValues = 0;

            modelCore.UI.WriteLine("  Calculating the Dynamic Fuel Type for all active cells...");
            foreach (ActiveSite site in modelCore.Landscape)
            {
                CalcFuelType(site, fuelTypes, disturbanceTypes);
                SiteVars.PercentDeadFir[site] = CalcPercentDeadFir(site);
            }

            string path = MapNames.ReplaceTemplateVars(mapNameTemplate, modelCore.CurrentTime);

            modelCore.UI.WriteLine("   Writing Fuel 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 = (byte)((int)SiteVars.FuelType[site] + 1);
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            string conpath = MapNames.ReplaceTemplateVars(pctConiferMapNameTemplate, modelCore.CurrentTime);

            modelCore.UI.WriteLine("   Writing % Conifer map to {0} ...", conpath);
            using (IOutputRaster <BytePixel> outputRaster = modelCore.CreateRaster <BytePixel>(conpath, modelCore.Landscape.Dimensions))
            {
                BytePixel pixel = outputRaster.BufferPixel;
                foreach (Site site in modelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (byte)((int)SiteVars.PercentConifer[site]);
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            string firpath = MapNames.ReplaceTemplateVars(pctDeadFirMapNameTemplate, modelCore.CurrentTime);

            modelCore.UI.WriteLine("   Writing % Dead Fir map to {0} ...", firpath);
            using (IOutputRaster <BytePixel> outputRaster = modelCore.CreateRaster <BytePixel>(firpath, modelCore.Landscape.Dimensions))
            {
                BytePixel pixel = outputRaster.BufferPixel;
                foreach (Site site in modelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (byte)((int)SiteVars.PercentDeadFir[site]);
                    }
                    else
                    {
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
        }
        public static void WriteMaps()
        {
            //if (PlugIn.SoilCarbonMapNames != null)// && (PlugIn.ModelCore.CurrentTime % SoilCarbonMapFrequency) == 0)
            //{
            string pathH2O = MapNames.ReplaceTemplateVars(@"NECN_Hydro\Annual-water-budget-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathH2O, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        //This is incorrect right now. Should be ppt-AET, not SOMTC calc
                        //pixel.MapCode.Value = (int)((SiteVars.SOM1surface[site].Carbon + SiteVars.SOM1soil[site].Carbon + SiteVars.SOM2[site].Carbon + SiteVars.SOM3[site].Carbon));
                        pixel.MapCode.Value = (int)((SiteVars.AnnualPPT_AET[site]));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
                //}
            }
            //AMK: Trying out directly writing maps
            string pathANPP = MapNames.ReplaceTemplateVars(@"NECN_Hydro\AG_NPP-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathANPP, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.AGNPPcarbon[site]));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            //if (PlugIn.SoilCarbonMapNames != null)
            //    {
            string path = MapNames.ReplaceTemplateVars(@"NECN_Hydro\SOMTC-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(path, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.SOM1surface[site].Carbon + SiteVars.SOM1soil[site].Carbon + SiteVars.SOM2[site].Carbon + SiteVars.SOM3[site].Carbon));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //}

            //if (PlugIn.SoilNitrogenMapNames != null)
            //{
            string path2 = MapNames.ReplaceTemplateVars(@"NECN_Hydro\SoilN-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <ShortPixel> outputRaster = PlugIn.ModelCore.CreateRaster <ShortPixel>(path2, PlugIn.ModelCore.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (short)(SiteVars.MineralN[site]);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //}

            string path4 = MapNames.ReplaceTemplateVars(@"NECN_Hydro\ANEE-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <ShortPixel> outputRaster = PlugIn.ModelCore.CreateRaster <ShortPixel>(path4, PlugIn.ModelCore.Landscape.Dimensions))
            {
                ShortPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (short)(SiteVars.AnnualNEE[site] + 1000);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //}
            //if (PlugIn.TotalCMapNames != null)
            //{

            string path5 = MapNames.ReplaceTemplateVars(@"NECN_Hydro\TotalC-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(path5, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)(Outputs.GetOrganicCarbon(site) +
                                                    SiteVars.CohortLeafC[site] +
                                                    SiteVars.CohortFRootC[site] +
                                                    SiteVars.CohortWoodC[site] +
                                                    SiteVars.CohortCRootC[site] +
                                                    SiteVars.SurfaceDeadWood[site].Carbon +
                                                    SiteVars.SoilDeadWood[site].Carbon);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }
            //}

            string pathLAI = MapNames.ReplaceTemplateVars(@"NECN_Hydro\LAI-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathLAI, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (short)(SiteVars.LAI[site]);
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }

            string pathavailablewater = MapNames.ReplaceTemplateVars(@"NECN_Hydro\AvailableWater-{timestep}.img", PlugIn.ModelCore.CurrentTime);

            using (IOutputRaster <IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster <IntPixel>(pathavailablewater, PlugIn.ModelCore.Landscape.Dimensions))
            {
                IntPixel pixel = outputRaster.BufferPixel;
                foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                {
                    if (site.IsActive)
                    {
                        pixel.MapCode.Value = (int)((SiteVars.AvailableWater[site]));
                    }
                    else
                    {
                        //  Inactive site
                        pixel.MapCode.Value = 0;
                    }
                    outputRaster.WriteBufferPixel();
                }
            }


            //if (PlugIn.ShadeClassMapNames != null)// && (PlugIn.ModelCore.CurrentTime % LAIMapFrequency) == 0)
            //{

            //    string path5 = MapNames.ReplaceTemplateVars(PlugIn.ShadeClassMapNames, PlugIn.ModelCore.CurrentTime);
            //    using (IOutputRaster<IntPixel> outputRaster = PlugIn.ModelCore.CreateRaster<IntPixel>(path5, PlugIn.ModelCore.Landscape.Dimensions))
            //    {
            //        IntPixel pixel = outputRaster.BufferPixel;
            //        foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
            //        {
            //            if (site.IsActive)
            //            {
            //                pixel.MapCode.Value = (short)(SiteVars.ShadeClass[site]); //Shade Class SiteCar doesn't exist. Just a placeholder
            //            }
            //            else
            //            {
            //                //  Inactive site
            //                pixel.MapCode.Value = 0;
            //            }
            //            outputRaster.WriteBufferPixel();
            //        }
            //    }
            //}
        }
예제 #30
0
        //---------------------------------------------------------------------
        ///<summary>
        /// Run Biomass Insects extension at a particular timestep.
        ///</summary>
        public override void Run()
        {
            running = true;
            PlugIn.ModelCore.UI.WriteLine("   Processing landscape for Biomass Insect events ...");

            SiteVars.SiteDefoliation.ActiveSiteValues = 0;

            foreach (IInsect insect in manyInsect)
            {
                //SiteVars.BiomassRemoved.ActiveSiteValues = 0;
                //SiteVars.InitialOutbreakProb.ActiveSiteValues = 0.0;

                if (insect.MortalityYear == PlugIn.ModelCore.CurrentTime)
                {
                    Outbreak.Mortality(insect);
                }

                if (insect.MortalityYear != PlugIn.ModelCore.CurrentTime)
                {
                    insect.LastBioRemoved = 0;
                }

                // Copy the data from current to last
                foreach (ActiveSite site in PlugIn.ModelCore.Landscape)
                {
                    double thisYearDefol = insect.ThisYearDefoliation[site];
                    insect.LastYearDefoliation[site] = thisYearDefol;
                    SiteVars.SiteDefoliation[site]  += (int)Math.Round(thisYearDefol * 100);
                    if (thisYearDefol > 0)
                    {
                        SiteVars.TimeOfLastEvent[site] = PlugIn.ModelCore.CurrentTime - 1;
                        SiteVars.InsectName[site]      = insect.Name;
                    }
                }


                insect.ThisYearDefoliation.ActiveSiteValues = 0.0;

                insect.ActiveOutbreak     = false;
                insect.SingleOutbreakYear = false;

                //PlugIn.ModelCore.NormalDistribution.Mu = 0.0;
                //PlugIn.ModelCore.NormalDistribution.Sigma = 1.0;
                //double randomNum = PlugIn.ModelCore.NormalDistribution.NextDouble();
                //randomNum = PlugIn.ModelCore.NormalDistribution.NextDouble();
                double randomNum = Distribution.GenerateRandomNum(DistributionType.Normal, 0.0, 1.0);

                DistributionType distDuration = insect.DurationDistribution;

                double randomNumE = Distribution.GenerateRandomNum(distDuration, insect.DurationParameter1, insect.DurationParameter2);

                // First, has enough time passed since the last outbreak? This is calculated each year,
                // but only used in last year of an outbreak to generate next outbreak characteristics.
                double timeBetweenOutbreaks = insect.MeanTimeBetweenOutbreaks + (insect.StdDevTimeBetweenOutbreaks * randomNum);
                double duration             = System.Math.Round(randomNumE);
                if (distDuration == DistributionType.Exponential || distDuration == DistributionType.Normal)
                {
                    duration = System.Math.Max(duration + 1, 1); // Duration cannot be less than 1. Added to allow Normal distribution parameters.
                }
                else
                {
                    duration = System.Math.Max(duration, 1);  // Duration cannot be less than 1.
                }

                // Apply optional maximum - if not used maxDur = Inf
                if (duration > insect.MaxDuration)
                {
                    duration = insect.MaxDuration;
                }

                double timeAfterDuration = timeBetweenOutbreaks - duration;

                // Users can parameterize model to have overlapping outbreaks, but then patches will not initialize correctly.
                // Do below to prevent overlapping outbreaks of same insect. This will affect the realized distribution of time between outbreaks somewhat.
                while (timeAfterDuration <= 1.0)
                {
                    PlugIn.ModelCore.UI.WriteLine("CAUTION: Time Between Outbreaks TOO SMALL (< 1), you may need to adjust timing parameters:  {0}.", timeAfterDuration);
                    timeBetweenOutbreaks = timeBetweenOutbreaks + 1;
                    timeAfterDuration    = timeBetweenOutbreaks - duration;
                }

                //PlugIn.ModelCore.UI.WriteLine("Calculated time between = {0:0.00}.  inputMeanTime={1:0.00}, inputStdTime={2:0.00}., timeAftDur={3:0.00}.", timeBetweenOutbreaks, insect.MeanTimeBetweenOutbreaks, insect.StdDevTimeBetweenOutbreaks, timeAfterDuration);

                // The logic below determines whether an outbreak is active. And sets a new outbreak duration and timeBetweenOutbreaks
                // for the next outbreak if is the last year of an outbreak.

                // The very first outbreak is set first.
                if (PlugIn.ModelCore.CurrentTime == 1)
                {
                    PlugIn.ModelCore.UI.WriteLine("   Year 1:  Setting initial start and stop times.");
                    double randomNum1 = PlugIn.ModelCore.GenerateUniform();
                    //insect.OutbreakStartYear = (int) (timeBetweenOutbreaks / 2.0) + 1;
                    insect.OutbreakStartYear = Math.Max(2, (int)(randomNum1 * timeBetweenOutbreaks + 1)); // New, try making 1st start year more random. 1st outbreak has to occur > year1 to for InitializeDefoliationPatches to work properly.
                    insect.OutbreakStartYear = Math.Max(insect.OutbreakStartYear, insect.StartYear);      // BRM - optional start year (first outbreak can't be before StartYear)
                    insect.OutbreakStopYear  = insect.OutbreakStartYear + (int)duration - 1;
                    // PlugIn.ModelCore.UI.WriteLine("   {0} is not active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, PlugIn.ModelCore.CurrentTime);
                }
                else if (insect.OutbreakStartYear <= PlugIn.ModelCore.CurrentTime &&
                         insect.OutbreakStopYear > PlugIn.ModelCore.CurrentTime) // first year of a multiyear outbreak
                {
                    // PlugIn.ModelCore.UI.WriteLine("   An outbreak starts or continues.  Start and stop time do not change.");
                    insect.ActiveOutbreak = true;
                    PlugIn.ModelCore.UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, PlugIn.ModelCore.CurrentTime);

                    insect.MortalityYear = PlugIn.ModelCore.CurrentTime + 1;
                    insect.LastStartYear = insect.OutbreakStartYear; // Added here for Brian's log file.
                    insect.LastStopYear  = insect.OutbreakStopYear;  // Added here for Brian's log file.
                }
                //Special case for single year outbreak.
                else if (insect.OutbreakStartYear == PlugIn.ModelCore.CurrentTime &&
                         insect.OutbreakStopYear == PlugIn.ModelCore.CurrentTime)
                {
                    insect.ActiveOutbreak = true;
                    PlugIn.ModelCore.UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, PlugIn.ModelCore.CurrentTime);

                    if (insect.OutbreakStartYear == insect.OutbreakStopYear) // shouldn't need this. JRF
                    {
                        insect.SingleOutbreakYear = true;
                    }
                    insect.MortalityYear     = PlugIn.ModelCore.CurrentTime + 1;
                    insect.LastStartYear     = insect.OutbreakStartYear; // Added here for Brian's log file.
                    insect.LastStopYear      = insect.OutbreakStopYear;  // Added here for Brian's log file.
                    insect.OutbreakStartYear = PlugIn.ModelCore.CurrentTime + (int)timeBetweenOutbreaks;
                    insect.OutbreakStopYear  = insect.OutbreakStartYear + (int)duration - 1;
                }

                else if (insect.OutbreakStopYear <= PlugIn.ModelCore.CurrentTime &&
                         timeAfterDuration > PlugIn.ModelCore.CurrentTime - insect.OutbreakStopYear)
                {
                    // PlugIn.ModelCore.UI.WriteLine("   In between outbreaks, reset start and stop times.");
                    insect.ActiveOutbreak = true;
                    PlugIn.ModelCore.UI.WriteLine("   {0} is active.  StartYear={1}, StopYear={2}, CurrentYear={3}.", insect.Name, insect.OutbreakStartYear, insect.OutbreakStopYear, PlugIn.ModelCore.CurrentTime);

                    insect.MortalityYear     = PlugIn.ModelCore.CurrentTime + 1;
                    insect.LastStartYear     = insect.OutbreakStartYear; // Added here for Brian's log file.
                    insect.LastStopYear      = insect.OutbreakStopYear;  // Added here for Brian's log file.
                    insect.OutbreakStartYear = PlugIn.ModelCore.CurrentTime + (int)timeBetweenOutbreaks;
                    insect.OutbreakStopYear  = insect.OutbreakStartYear + (int)duration - 1;
                }
                PlugIn.ModelCore.UI.WriteLine("  Insect Start Time = {0}, Stop Time = {1}.", insect.OutbreakStartYear, insect.OutbreakStopYear);

                // Now that logic determining when an outbreak will be active is done, tell model what to do when outbreak is occurring.
                if (insect.ActiveOutbreak)
                {
                    //  PlugIn.ModelCore.UI.WriteLine("   OutbreakStartYear={0}.", insect.OutbreakStartYear);

                    if (insect.OutbreakStartYear == PlugIn.ModelCore.CurrentTime || insect.SingleOutbreakYear)
                    {
                        // Initialize neighborhoodGrowthReduction with patches
                        Outbreak.InitializeDefoliationPatches(insect);
                    }
                    else
                    {
                        insect.NeighborhoodDefoliation.ActiveSiteValues = 0;
                    }
                }

                // Now report on the previous year's defoliation, that which has been processed
                // through biomass succession. Calculations for logfile.

                double sumDefoliation  = 0.0;
                double meanDefoliation = 0.0;
                int    numSites0_33    = 0;
                int    numSites33_66   = 0;
                int    numSites66_100  = 0;
                int    numInitialSites = 0;
                int    numSitesActive  = 0; // Just get a sum of all active sites to calculate mean defoliation accurately for log file.

                // ONly calculate for log file when outbreak or mortality is active <- Modified, JRF, add to log file each year.
                if (insect.ActiveOutbreak || insect.SingleOutbreakYear || (insect.LastStopYear + 1 >= PlugIn.ModelCore.CurrentTime) || (insect.LastBioRemoved > 0))
                {
                    foreach (ActiveSite site in PlugIn.ModelCore.Landscape)
                    {
                        sumDefoliation += insect.LastYearDefoliation[site];
                        if (insect.LastYearDefoliation[site] > 0.0 && insect.LastYearDefoliation[site] <= 0.33)
                        {
                            numSites0_33++;
                        }
                        if (insect.LastYearDefoliation[site] > 0.33 && insect.LastYearDefoliation[site] <= 0.66)
                        {
                            numSites33_66++;
                        }
                        if (insect.LastYearDefoliation[site] > 0.66 && insect.LastYearDefoliation[site] <= 1.0)
                        {
                            numSites66_100++;
                        }
                        if (insect.Disturbed[site] && SiteVars.InitialOutbreakProb[site] > 0)
                        {
                            numInitialSites++;
                        }
                        numSitesActive++;
                    }
                    if (insect.OutbreakStartYear == PlugIn.ModelCore.CurrentTime)
                    {
                        insect.InitialSites = numInitialSites;
                    }

                    if (numSites0_33 + numSites33_66 + numSites66_100 > 0)
                    {
                        meanDefoliation = sumDefoliation / (double)numSitesActive;
                    }
                    //PlugIn.ModelCore.UI.WriteLine("   sumDefoliation={0}, numSites={1}.", sumDefoliation, numSites0_33 + numSites33_66 + numSites66_100);
                }

                int totalBioRemoved = 0;
                foreach (ActiveSite site in PlugIn.ModelCore.Landscape)
                {
                    totalBioRemoved += SiteVars.BiomassRemoved[site]; // kg across all defoliated sites
                }
                insect.LastBioRemoved = totalBioRemoved;              //Assign variables for the logfile
                // PlugIn.ModelCore.UI.WriteLine("   totalBioRemoved={0}.", totalBioRemoved);


                // ONly add to log & output maps during outbreak <- Modified, JRF, add to log file each year.
                //if ((insect.ActiveOutbreak && insect.OutbreakStartYear < PlugIn.ModelCore.CurrentTime) || (meanDefoliation > 0) || (insect.LastBioRemoved > 0))
                //{

                /*  Old code for log file
                 *  if (meanDefoliation > 0)
                 *  {
                 *
                 *          log.Write("{0},{1},{2},{3},{4:0.0000},{5},{6},{7},{8},{9}",
                 *                  PlugIn.ModelCore.CurrentTime-1,  //0
                 *                  insect.Name,  //1
                 *                  insect.LastStartYear,  //2
                 *                  insect.LastStopYear,  //3
                 *                  meanDefoliation, //4
                 *                  numSites0_33, //5
                 *                  numSites33_66,  //6
                 *                  numSites66_100, //7
                 *                  insect.InitialSites, //8
                 *                  insect.LastBioRemoved //9
                 *                  );
                 *  }
                 *
                 *  else
                 *  {
                 *      log.Write("{0},{1},{2},{3},{4:0.0000},{5},{6},{7},{8},{9}",
                 *              PlugIn.ModelCore.CurrentTime - 1,  //0
                 *              insect.Name,  //1
                 *              0,  //2
                 *              0,  //3
                 *              meanDefoliation, //4
                 *              numSites0_33, //5
                 *              numSites33_66,  //6
                 *              numSites66_100, //7
                 *              0, //8
                 *              insect.LastBioRemoved //9
                 *              );
                 *  }
                 *
                 *
                 *  //foreach (IEcoregion ecoregion in Ecoregions.Dataset)
                 *  //    log.Write(",{0}", 1);
                 *
                 *  log.WriteLine("");
                 */

                eventLog.Clear();
                EventsLog el = new EventsLog();

                el.Time                     = PlugIn.ModelCore.CurrentTime - 1;
                el.InsectName               = insect.Name;
                el.MeanDefoliation          = meanDefoliation;
                el.NumSitesDefoliated0_33   = numSites0_33;
                el.NumSitesDefoliated33_66  = numSites33_66;
                el.NumSitesDefoliated66_100 = numSites66_100;
                el.NumOutbreakInitialSites  = insect.InitialSites;
                el.MortalityBiomass         = insect.LastBioRemoved;

                if (insect.ActiveOutbreak)
                {
                    el.StartYear = insect.OutbreakStartYear;
                    el.StopYear  = insect.OutbreakStopYear;
                }
                else
                {
                    el.StartYear = insect.LastStartYear;
                    el.StopYear  = insect.LastStopYear;
                }

                eventLog.AddObject(el);
                eventLog.WriteToFile();

                //----- Write Insect Defoliation/GrowthReduction maps --------
                string path = MapNames.ReplaceTemplateVars(mapNameTemplate, insect.Name, PlugIn.ModelCore.CurrentTime - 1);
                using (IOutputRaster <ShortPixel> outputRaster = modelCore.CreateRaster <ShortPixel>(path, modelCore.Landscape.Dimensions))
                {
                    ShortPixel pixel = outputRaster.BufferPixel;

                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            pixel.MapCode.Value = (short)(insect.LastYearDefoliation[site] * 100.0);
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }

                        outputRaster.WriteBufferPixel();
                    }
                }

                //----- Write Initial Patch maps --------
                string path2 = MapNames.ReplaceTemplateVars(mapNameTemplate, ("InitialPatchMap" + insect.Name), PlugIn.ModelCore.CurrentTime);
                using (IOutputRaster <ShortPixel> outputRaster = modelCore.CreateRaster <ShortPixel>(path2, modelCore.Landscape.Dimensions))
                {
                    ShortPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            if (insect.Disturbed[site])
                            {
                                pixel.MapCode.Value = (short)(SiteVars.InitialOutbreakProb[site] * 100);
                            }
                            else
                            {
                                pixel.MapCode.Value = 0;
                            }
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }
                        outputRaster.WriteBufferPixel();
                        //Zero out the InitialOutbreakProb after output maps are written.
                        SiteVars.InitialOutbreakProb[site] = 0;
                    }
                }

                //----- Write Biomass Reduction maps --------
                string path3 = MapNames.ReplaceTemplateVars(mapNameTemplate, ("BiomassRemoved" + insect.Name), PlugIn.ModelCore.CurrentTime);
                using (IOutputRaster <ShortPixel> outputRaster = modelCore.CreateRaster <ShortPixel>(path3, modelCore.Landscape.Dimensions))
                {
                    ShortPixel pixel = outputRaster.BufferPixel;
                    foreach (Site site in PlugIn.ModelCore.Landscape.AllSites)
                    {
                        if (site.IsActive)
                        {
                            if (SiteVars.BiomassRemoved[site] > 0)
                            {
                                pixel.MapCode.Value = Math.Max((short)1, (short)(SiteVars.BiomassRemoved[site] / 100));     // convert to Mg/ha
                            }
                            else
                            {
                                pixel.MapCode.Value = 0;
                            }
                        }
                        else
                        {
                            //  Inactive site
                            pixel.MapCode.Value = 0;
                        }
                        outputRaster.WriteBufferPixel();
                        //Zero out the BiomassRemoved after the last insect mortality event in a given year.
                        SiteVars.BiomassRemoved[site] = 0;
                    }
                }
                //}

                //insect.ThisYearDefoliation.ActiveSiteValues = 0.0;  //reset this year to 0 for all sites, this was already done at the top of loop to initialize defoliation patchs, Outbreak.cs
            }
        }