Esempio n. 1
0
 /// <summary>
 /// Go to the last dataset
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void Last()
 {
     if (Basins.Count != 0)
     {
         SelectedBasin = Basins.Last();
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Go to the first dataset
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void First()
 {
     if (Basins.Count != 0)
     {
         SelectedBasin = Basins.First();
     }
 }
Esempio n. 3
0
        public int FindBasinStart(int i, int j)
        {
            BasinPoints.Clear();

            FindBasin(i, j);

            int basinSum = BasinPoints.Count();

            Basins.Add(basinSum);
            return(basinSum);
        }
Esempio n. 4
0
        public long FindTop3BasinValues()
        {
            Basins.Sort();
            long total = 1;
            var  vals  = Basins.Skip(Basins.Count - 3).Take(3);

            foreach (var val in vals)
            {
                total *= val;
            }
            return(total);
        }
Esempio n. 5
0
 public void Absorb(Basin other)
 {
     if (other == null || other == this)
     {
         return;
     }
     for (int x = 0; x < 100; ++x)
     {
         for (int y = 0; y < 100; ++y)
         {
             BasinMap[x, y] |= other.BasinMap[x, y];
         }
     }
     Basins.Remove(other);
 }
Esempio n. 6
0
        /// <summary>
        /// Refreshing the dataset
        /// </summary>
        public override void Refresh()
        {
            try
            {
                if (!DataValidation.CheckPrerequisites(CRUD.Add))
                {
                    return;
                }
            }
            catch
            {
                return;
            }

            tblBasin current = SelectedBasin;
            int      id      = 0;

            try
            {
                if (SelectedBasin != null)
                {
                    id = SelectedBasin.basIdPk;
                }

                LoadData();
                SelectedBasin = Basins.Where(p => p.basIdPk == id).First();
            }
            catch
            {
                try
                {
                    LoadData();
                    SelectedBasin = new tblBasin()
                    {
                        basUserIdFk = (int)((ShellViewModel)IoC.Get <IShell>()).UserId
                    };
                }
                catch
                {
                    ((ShellViewModel)IoC.Get <IShell>()).ShowError("An unexpected error occurred.");
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Deleting the currently viewed rock sample
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Delete()
        {
            try
            {
                if (!DataValidation.CheckPrerequisites(CRUD.Delete, SelectedBasin, (int)SelectedBasin.basUserIdFk))
                {
                    return;
                }
            }
            catch
            {
                return;
            }

            // If existing window is visible, delete the customer and all their orders.
            // In a real application, you should add warnings and allow the user to cancel the operation.

            if (((ShellViewModel)IoC.Get <IShell>()).ShowQuestion("Are you sure to delete the record?") == MessageBoxViewResult.No)
            {
                return;
            }

            using (var db = new ApirsRepository <tblBasin>())
            {
                try
                {
                    db.DeleteModelById(SelectedBasin.basIdPk);

                    Basins.Remove(SelectedBasin);
                    allBasins.Remove(SelectedBasin);
                }
                catch (Exception ex)
                {
                    ((ShellViewModel)IoC.Get <IShell>()).ShowError("An unexpected error occurred.");
                }
                finally
                {
                }
            }
        }
        /// <summary>
        /// Read all basic information of all scenarios in the given folder
        /// </summary>
        void setScenario()
        {
            if (!System.IO.Directory.Exists(scenariosDir)) return;
            if (!System.IO.Directory.Exists(scenariosDir + @"\" + scenarioName)) return;

            basins = new Basins();
            basins.hrus = new Dictionary<int, Hrus>();
            basins.reservoirs = new Dictionary<int, int>();
            basins.fiveDigitHruShift = 0;
            PopulateBasins(ref basins, scenarioName);
        }
        /// <summary>
        /// 1. Get variables for reach, subbasin, hru, pond and reservoir outputs
        /// 2. Get all hrus for each subbasin
        /// 3. Get all ponds for each subbasin
        /// </summary>
        /// <param name="basins"></param>
        /// <param name="dirName">should be the scenario name</param>
        /// <returns></returns>
        /// <remarks>Should be just called once for each output</remarks>
        bool PopulateBasins(ref Basins basins, string dirName)
        {
            string dir = scenariosDir + @"\" + dirName;
            string hruData = dir + @"\TxtInOut\" + hruDataFile;
            string reachData = dir + @"\TxtInOut\" + reachDataFile;
            string figData = dir + @"\TxtInOut\" + figFile;
            string line;
            // we should be using rch.dat, sub.dat and hru.dat for the field widths and
            // variables to be found in output.rch etc, but rch.dat at least is unreliable.
            // So we assume we know the field positions and widths, and read the variables from the
            // output files.
            using (StreamReader sr1 = new StreamReader(reachData))
            {
                reachVars = new List<string>();
                // check if SWAT 2012
                sr1.ReadLine();
                line = sr1.ReadLine();
                if (line.Contains("2012"))
                {
                    // SWAT 2012
                    // HRU column in output.wtr and output.hru has an extra space before and after it
                    waterFirstDataCol = 44;
                    waterNumCol = 5;
                    waterDateCol = 30;
                    waterSubNumCol = 19;
                    hruFirstDataCol = 44;
                    hruNumCol = 5;
                    hruDateCol = 30;
                    hruSubNumCol = 19;
                }
                // skip remaining header lines
                for (int i = 0; i < headerLines - 2; i++) sr1.ReadLine();
                line = sr1.ReadLine();
                int count = (line.Length - reachFirstDataCol) / reachDataWidth;
                for (int i = 0; i < count; i++)
                {
                    reachVars.Add(line.Substring(reachFirstDataCol + i * reachDataWidth, reachDataWidth).Trim());
                }
            }
            string subData = dir + @"\TxtInOut\" + subDataFile;
            using (StreamReader sr2 = new StreamReader(subData))
            {
                subVars = new List<string>();
                // skip header lines
                for (int i = 0; i < headerLines; i++) sr2.ReadLine();
                line = sr2.ReadLine();
                int count = (line.Length - subFirstDataCol) / subDataWidth;
                for (int i = 0; i < count; i++)
                {
                    subVars.Add(line.Substring(subFirstDataCol + i * subDataWidth, subDataWidth).Trim());
                }
            }
            string waterData = dir + @"\TxtInOut\" + waterDataFile;
            if (File.Exists(waterData))
            {
                using (StreamReader sr3 = new StreamReader(waterData))
                {
                    waterVars = new List<string>();
                    // skip header lines
                    for (int i = 0; i < headerLines; i++) sr3.ReadLine();
                    line = sr3.ReadLine();
                    int count = (line.Length - waterFirstDataCol) / waterDataWidth;
                    for (int i = 0; i < count; i++)
                    {
                        waterVars.Add(line.Substring(waterFirstDataCol + i * waterDataWidth, waterDataWidth).Trim());
                    }
                }
            }
            string reservoirData = dir + @"\TxtInOut\" + reservoirDataFile;
            using (StreamReader sr4 = new StreamReader(reservoirData))
            {
                reservoirVars = new List<string>();
                // skip header lines
                for (int i = 0; i < headerLines; i++) sr4.ReadLine();
                line = sr4.ReadLine();
                int count = (line.Length - reservoirFirstDataCol) / reservoirDataWidth;
                for (int i = 0; i < count; i++)
                {
                    reservoirVars.Add(line.Substring(reservoirFirstDataCol + i * reservoirDataWidth, reservoirDataWidth).Trim());
                }
            }
            // use output.hru to determine hru numbers for each basin
            // Note we do this for the current, not Default, run since different runs may have different hru selections
            using (StreamReader sr = new StreamReader(hruData))
            {
                // skip header lines
                for (int i = 0; i < headerLines; i++) sr.ReadLine();
                // next is var names line
                line = sr.ReadLine();
                // store hru variables
                hruVars = new List<string>();
                int count = (line.Length - hruFirstDataCol) / hruDataWidth;
                for (int i = 0; i < count; i++)
                {
                    hruVars.Add(line.Substring(hruFirstDataCol + i * hruDataWidth, hruDataWidth).Trim());
                }
                int currentBasin = 0; // basin numbers start from 1
                List<int> hruNums = new List<int>();
                while ((line = sr.ReadLine()) != null)
                {
                    // first time round, check for change in output.hru in latest
                    // swat2005.exe: hrus allowed 5 digits, so following numbers shifted 1 place to right
                    // detected by seeing if last character of 5-digit hru number is a non-space
                    // (should be a 1 if hrus have 5 digits)
                    if ((currentBasin == 0) && (line.Substring(hruNumCol + hruNumWidth, 1) != " "))
                    {
                        basins.fiveDigitHruShift = 1;
                    }
                    int basin = Int32.Parse(line.Substring(hruSubNumCol + basins.fiveDigitHruShift, hruSubNumWidth));
                    int hru = Int32.Parse(line.Substring(hruNumCol, hruNumWidth + basins.fiveDigitHruShift));
                    if (basin == currentBasin)
                    {
                        if (!hruNums.Contains(hru)) hruNums.Add(hru);
                    }
                    else
                    {
                        // store last basin, unless this is the first loop cycle
                        if (currentBasin != 0)
                        {
                            Hrus hrus = new Hrus();
                            hrus.hruNumbers = new List<int>();
                            hrus.pondNumbers = new List<int>();
                            foreach (int i in hruNums) hrus.hruNumbers.Add(i);
                            basins.hrus.Add(currentBasin, hrus);
                        }
                        if (basin < currentBasin) break; // finished with hrus
                        // start a new basin
                        currentBasin = basin;
                        hruNums.Clear();
                        hruNums.Add(hru);
                    }
                }
                // may have curtailed hru file: check to see if we got the last entry
                if ((currentBasin > 0) && (!basins.hrus.ContainsKey(currentBasin)))
                {
                    Hrus hrus = new Hrus();
                    hrus.hruNumbers = new List<int>();
                    hrus.pondNumbers = new List<int>();
                    foreach (int i in hruNums) hrus.hruNumbers.Add(i);
                    basins.hrus.Add(currentBasin, hrus);
                }
            }
            // hru data can be curtailed to just some hrus, so we have to read
            // the reach data file to make sure we have all subbasins
            using (StreamReader sr7 = new StreamReader(reachData))
            {
                // skip header lines and var line
                for (int i = 0; i <= headerLines; i++) sr7.ReadLine();
                int currentBasin = 0; // basin numbers start from 1
                while ((line = sr7.ReadLine()) != null)
                {
                    int basin = Int32.Parse(line.Substring(reachNumCol, reachNumWidth));
                    if (currentBasin != 0)
                    {
                        if (!basins.hrus.ContainsKey(currentBasin))
                        // new basin that was not in output.hru
                        {
                            Hrus hrus = new Hrus();
                            hrus.hruNumbers = new List<int>();
                            hrus.pondNumbers = new List<int>();
                            basins.hrus.Add(currentBasin, hrus);
                        }
                    }
                    if (basin < currentBasin) break; // finished with reach file
                    currentBasin = basin;
                }
            }

            string waterData2 = dir + @"\TxtInOut\" + waterDataFile;
            if (File.Exists(waterData2))
            {
                using (StreamReader sr5 = new StreamReader(waterData2))
                {
                    // skip header lines and var line
                    for (int i = 0; i <= headerLines; i++) sr5.ReadLine();
                    int currentBasin = 0; // basin numbers start from 1
                    List<int> pondNums = new List<int>();
                    int basin = 0;
                    int hru = 0;
                    while ((line = sr5.ReadLine()) != null)
                    {
                        basin = Int32.Parse(line.Substring(waterSubNumCol + basins.fiveDigitHruShift, waterSubNumWidth));
                        hru = Int32.Parse(line.Substring(waterNumCol, waterNumWidth + basins.fiveDigitHruShift));
                        if (basin == currentBasin)
                        {
                            if (!pondNums.Contains(hru)) pondNums.Add(hru);
                        }
                        else
                        {
                            // store last basin, unless first loop
                            if (currentBasin != 0) // not first loop cycle
                            {
                                foreach (int i in pondNums)
                                {
                                    basins.hrus[currentBasin].pondNumbers.Add(i);
                                }
                            }
                            if (basin < currentBasin) break; // finished with ponds
                            currentBasin = basin;
                            pondNums.Clear();
                            pondNums.Add(hru);
                        }
                    }
                }
            }
            // use fig.fig file to set up reservoir data
            using (StreamReader sr6 = new StreamReader(figData))
            {
                int lastBasin = 0;
                while ((line = sr6.ReadLine()) != null)
                {
                    int command;
                    try
                    {
                        command = Int32.Parse(line.Substring(figCommandCol, figCommandWidth));
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    if (command == 2) // route command
                    {
                        // save basin in case next command is a reservoir routing
                        lastBasin = Int32.Parse(line.Substring(figRouteBasinCol, figRouteBasinWidth));
                    }
                    else if (command == 3) // routres command
                    {
                        int res = Int32.Parse(line.Substring(figResCol, figResWidth));
                        // basin removed from routres in SWAT 2012
                        //int basin = Int32.Parse(line.Substring(figResBasinCol, figResBasinWidth));
                        basins.reservoirs.Add(lastBasin, res);
                        //MessageBox.Show("Run " + dirName + " has reservoir " + res.ToString() +
                        //                " in basin " + lastBasin.ToString());
                    }
                }
            }
            return true;
        }
Esempio n. 10
0
        // Commit changes from the new rock sample form
        // or edits made to the existing rock sample form.
        public void Update()
        {
            try
            {
                if (!DataValidation.CheckPrerequisites(CRUD.Update, SelectedBasin, (int)SelectedBasin.basUserIdFk))
                {
                    return;
                }
            }
            catch (Exception e)
            {
                return;
            }

            using (var db = new ApirsRepository <tblBasin>())
            {
                try
                {
                    if (SelectedBasin.basIdPk == 0)
                    {
                        SelectedBasin.basUserIdFk = (int)((ShellViewModel)IoC.Get <IShell>()).UserId;
                        db.InsertModel(SelectedBasin);

                        Basins.Add(SelectedBasin);
                    }
                    else
                    {
                        db.UpdateModel(SelectedBasin, SelectedBasin.basIdPk);
                        db.Save();

                        using (var db1 = new ApirsRepository <tblBasinLithoUnit>())
                        {
                            foreach (tblBasinLithoUnit litunit in BasLithostrat)
                            {
                                if (litunit.baslitIdPk == 0)
                                {
                                    db1.InsertModel(litunit);
                                }
                                else
                                {
                                    db1.UpdateModel(litunit, litunit.baslitIdPk);
                                }
                            }
                        }
                    }
                }
                catch (SqlException ex)
                {
                    ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("Please provide valid input parameters");
                }
                catch (Exception e)
                {
                    if (e.Message.Contains("EntityValidation"))
                    {
                        ((ShellViewModel)IoC.Get <IShell>()).ShowError("Please provide a name for the instrument.");
                    }
                    else
                    {
                        ((ShellViewModel)IoC.Get <IShell>()).ShowError("An unexpected error occured");
                    }
                }
                finally
                {
                }
            }
        }
Esempio n. 11
0
 public Basin(int x, int y)
 {
     BasinMap[x, y] = true; Basins.Add(this);
 }