public InputCatchData(float[] modelLats, float[] modelLons, float cellSize)
        {
            StreamReader r_ht = new StreamReader("input\\data\\Fisheries\\catchratesyr2000HT.csv");
            StreamReader r = new StreamReader("input\\data\\Fisheries\\catchrateyr2000.csv");

            //Read trait data
            Traits = new FishTraits();
            //Retrieve the Max Mass range from the trait data
            var temp = Traits.MassRange();
            double[] MaxMassRange = temp.Item1;
            string[] MaxMassRangeSp = temp.Item2;

            //Calculate a set of mass bins to be used for removing fisheries catches from binned Madingley biomasses 
            //TO DO: make these bins flexible and user defined
            int MinMassbinMax = Convert.ToInt32(Math.Ceiling(Math.Log10(MaxMassRange[0])));
            int MaxMassbinMax = Convert.ToInt32(Math.Ceiling(Math.Log10(MaxMassRange[1])));

            int NumBins = (MaxMassbinMax - MinMassbinMax) + 1;

            _MassBins = new double[NumBins];
            for (int i = 0; i < NumBins - 1; i++)
            {
                _MassBins[i] = Math.Pow(10, MinMassbinMax + i);
            }
            _UnknownMassBinIndex = NumBins - 1;

            string l;
            char[] comma = ",".ToCharArray();

            string[] f;

            List<int> year_ht = new List<int>();
            List<int> cell_ht = new List<int>();
            List<double> catchRate_ht = new List<Double>();
            List<string> taxa_ht = new List<string>();

            List<int> year = new List<int>();
            List<int> cell = new List<int>();
            List<double> catchRate = new List<Double>();
            List<string> taxa = new List<string>();

            //Read the Higher Taxonomy file
            while (!r_ht.EndOfStream)
            {
                l = r_ht.ReadLine();
                // Split fields by commas
                f = l.Split(comma);

                // Lists of the different fields
                year_ht.Add(Convert.ToInt32(f[0]));
                cell_ht.Add(Convert.ToInt32(f[1]));
                catchRate_ht.Add(Convert.ToDouble(f[2]));
                taxa_ht.Add(f[3]);
            }

            //Read the species catch file
            while (!r.EndOfStream)
            {
                l = r.ReadLine();
                // Split fields by commas
                f = l.Split(comma);

                // Lists of the different fields
                year.Add(Convert.ToInt32(f[0]));
                cell.Add(Convert.ToInt32(f[1]));
                catchRate.Add(Convert.ToDouble(f[2]));
                taxa.Add(f[3]);
            }

            float MinLon = -179.75f;
            float MaxLon = 179.75f;
            float MaxLat = 89.75f;
            float MinLat = -89.75f;

            _CatchNumLats = (int)((MaxLat - MinLat) / 0.5) + 1;
            _CatchNumLons = (int)((MaxLon - MinLon) / 0.5) + 1;

            _CatchTotal = new double[_CatchNumLats, _CatchNumLons];
            _CatchBinned = new double[_CatchNumLats, _CatchNumLons, NumBins];

            UnknownTaxa = new List<string>[_CatchNumLats, _CatchNumLons];
            for (int i = 0; i < UnknownTaxa.GetLength(0); i++)
            {
                for (int j = 0; j < UnknownTaxa.GetLength(1); j++)
                {
                    UnknownTaxa[i, j] = new List<string>();
                }
            }

            _CatchLats = new float[_CatchNumLats];
            _CatchLons = new float[_CatchNumLons];

            int[] Index;

            // Match lon index to lon
            for (int i = 0; i < _CatchNumLons; i++)
            {
                _CatchLons[i] = MinLon + (i * 0.5f);
            }

            // Match lat index to lat
            for (int i = 0; i < _CatchNumLats; i++)
            {
                _CatchLats[i] = MaxLat - (i * 0.5f);
            }

            //Will hold the mass bin index for the catch data
            int mb = 0;
            //Allocate the species level catch to cells and mass bins
            for (int i = 0; i < catchRate.Count; i++)
            {
                Index = IndexLookup(cell[i]);

                //Need to convert to size bins
                mb = AssignCatchToMassBin(taxa[i]);

                _CatchTotal[Index[0], Index[1]] += catchRate[i] * 1E6;
                _CatchBinned[Index[0], Index[1], mb] += catchRate[i] * 1E6;

                //If the taxa does not have trait data then list this taxa
                if (mb == UnknownMassBinIndex) UnknownTaxa[Index[0], Index[1]].Add(taxa[i]);
            }
            //Allocate the higher taxa level catch to cells and mass bins
            for (int i = 0; i < catchRate_ht.Count; i++)
            {
                Index = IndexLookup(cell_ht[i]);

                //Need to convert to size bins
                mb = AssignCatchToMassBin(taxa_ht[i]);

                _CatchTotal[Index[0], Index[1]] += catchRate_ht[i] * 1E6;
                _CatchBinned[Index[0], Index[1], mb] += catchRate_ht[i] * 1E6;

                //If the taxa does not have trait data then list this taxa
                if (mb == UnknownMassBinIndex) UnknownTaxa[Index[0], Index[1]].Add(taxa_ht[i]);
            }

            //foreach (var u in UnknownTaxa)
            //{
            //    if (u.Count > 0.0) Console.WriteLine(u.Count);
            //}

            double CumulativeCatch = 0.0;
            for (int i = 0; i < _CatchTotal.GetLength(0); i++)
            {
                for (int j = 0; j < _CatchTotal.GetLength(1); j++)
                {
                    CumulativeCatch += _CatchTotal[i, j];
                }
            }

            Console.WriteLine(CumulativeCatch);

            AggregateCatchData(modelLats, modelLons, cellSize);
        }
Ejemplo n.º 2
0
        public InputCatchData(float[] modelLats, float[] modelLons, float cellSize)
        {
            StreamReader r_ht = new StreamReader("input\\data\\Fisheries\\catchratesyr2000HT.csv");
            StreamReader r    = new StreamReader("input\\data\\Fisheries\\catchrateyr2000.csv");

            //Read trait data
            Traits = new FishTraits();
            //Retrieve the Max Mass range from the trait data
            var temp = Traits.MassRange();

            double[] MaxMassRange   = temp.Item1;
            string[] MaxMassRangeSp = temp.Item2;

            //Calculate a set of mass bins to be used for removing fisheries catches from binned Madingley biomasses
            //TO DO: make these bins flexible and user defined
            int MinMassbinMax = Convert.ToInt32(Math.Ceiling(Math.Log10(MaxMassRange[0])));
            int MaxMassbinMax = Convert.ToInt32(Math.Ceiling(Math.Log10(MaxMassRange[1])));

            int NumBins = (MaxMassbinMax - MinMassbinMax) + 1;

            _MassBins = new double[NumBins];
            for (int i = 0; i < NumBins - 1; i++)
            {
                _MassBins[i] = Math.Pow(10, MinMassbinMax + i);
            }
            _UnknownMassBinIndex = NumBins - 1;

            string l;

            char[] comma = ",".ToCharArray();

            string[] f;

            List <int>    year_ht      = new List <int>();
            List <int>    cell_ht      = new List <int>();
            List <double> catchRate_ht = new List <Double>();
            List <string> taxa_ht      = new List <string>();

            List <int>    year      = new List <int>();
            List <int>    cell      = new List <int>();
            List <double> catchRate = new List <Double>();
            List <string> taxa      = new List <string>();

            //Read the Higher Taxonomy file
            while (!r_ht.EndOfStream)
            {
                l = r_ht.ReadLine();
                // Split fields by commas
                f = l.Split(comma);

                // Lists of the different fields
                year_ht.Add(Convert.ToInt32(f[0]));
                cell_ht.Add(Convert.ToInt32(f[1]));
                catchRate_ht.Add(Convert.ToDouble(f[2]));
                taxa_ht.Add(f[3]);
            }

            //Read the species catch file
            while (!r.EndOfStream)
            {
                l = r.ReadLine();
                // Split fields by commas
                f = l.Split(comma);

                // Lists of the different fields
                year.Add(Convert.ToInt32(f[0]));
                cell.Add(Convert.ToInt32(f[1]));
                catchRate.Add(Convert.ToDouble(f[2]));
                taxa.Add(f[3]);
            }

            float MinLon = -179.75f;
            float MaxLon = 179.75f;
            float MaxLat = 89.75f;
            float MinLat = -89.75f;

            _CatchNumLats = (int)((MaxLat - MinLat) / 0.5) + 1;
            _CatchNumLons = (int)((MaxLon - MinLon) / 0.5) + 1;

            _CatchTotal  = new double[_CatchNumLats, _CatchNumLons];
            _CatchBinned = new double[_CatchNumLats, _CatchNumLons, NumBins];

            UnknownTaxa = new List <string> [_CatchNumLats, _CatchNumLons];
            for (int i = 0; i < UnknownTaxa.GetLength(0); i++)
            {
                for (int j = 0; j < UnknownTaxa.GetLength(1); j++)
                {
                    UnknownTaxa[i, j] = new List <string>();
                }
            }

            _CatchLats = new float[_CatchNumLats];
            _CatchLons = new float[_CatchNumLons];

            int[] Index;

            // Match lon index to lon
            for (int i = 0; i < _CatchNumLons; i++)
            {
                _CatchLons[i] = MinLon + (i * 0.5f);
            }

            // Match lat index to lat
            for (int i = 0; i < _CatchNumLats; i++)
            {
                _CatchLats[i] = MaxLat - (i * 0.5f);
            }

            //Will hold the mass bin index for the catch data
            int mb = 0;

            //Allocate the species level catch to cells and mass bins
            for (int i = 0; i < catchRate.Count; i++)
            {
                Index = IndexLookup(cell[i]);

                //Need to convert to size bins
                mb = AssignCatchToMassBin(taxa[i]);

                _CatchTotal[Index[0], Index[1]]      += catchRate[i] * 1E6;
                _CatchBinned[Index[0], Index[1], mb] += catchRate[i] * 1E6;

                //If the taxa does not have trait data then list this taxa
                if (mb == UnknownMassBinIndex)
                {
                    UnknownTaxa[Index[0], Index[1]].Add(taxa[i]);
                }
            }
            //Allocate the higher taxa level catch to cells and mass bins
            for (int i = 0; i < catchRate_ht.Count; i++)
            {
                Index = IndexLookup(cell_ht[i]);

                //Need to convert to size bins
                mb = AssignCatchToMassBin(taxa_ht[i]);

                _CatchTotal[Index[0], Index[1]]      += catchRate_ht[i] * 1E6;
                _CatchBinned[Index[0], Index[1], mb] += catchRate_ht[i] * 1E6;

                //If the taxa does not have trait data then list this taxa
                if (mb == UnknownMassBinIndex)
                {
                    UnknownTaxa[Index[0], Index[1]].Add(taxa_ht[i]);
                }
            }

            //foreach (var u in UnknownTaxa)
            //{
            //    if (u.Count > 0.0) Console.WriteLine(u.Count);
            //}

            double CumulativeCatch = 0.0;

            for (int i = 0; i < _CatchTotal.GetLength(0); i++)
            {
                for (int j = 0; j < _CatchTotal.GetLength(1); j++)
                {
                    CumulativeCatch += _CatchTotal[i, j];
                }
            }

            Console.WriteLine(CumulativeCatch);

            AggregateCatchData(modelLats, modelLons, cellSize);
        }