private void buttonXls_Click(object sender, EventArgs e)
        {
            try
            {
#if SpreadsheetGear
                string tmpFilename = FileUtility.GetTempFileName(".xls");
                string template    = FileUtility.GetFileReference("RatingTableTemplate.xls");
                File.Copy(template, tmpFilename, true);
                ExportToExcel(tmpFilename, checkBoxMultiColumn.Checked, RatingTable);
#else
                string    tmpFilename = FileUtility.GetTempFileName(".csv");
                DataTable table       = RatingTable;
                if (checkBoxMultiColumn.Checked)
                {
                    table = RatingTableUtility.ConvertToMultiColumn(RatingTable);
                }
                CsvFile.WriteToCSV(table, tmpFilename, false, true);
#endif
                System.Diagnostics.Process.Start(tmpFilename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Beispiel #2
0
            /// <summary>
            /// Compute values using a RatingTableDataTable
            /// for example compute flows based on gage height input series
            /// </summary>
            /// <param name="s"></param>
            /// <param name="fileName">rating table filename</param>
            /// <returns></returns>
            public static Series ComputeSeries(Series s, string fileName,
                                               InterpolationMethod method = InterpolationMethod.None)
            {
                var rval = new Series();

                Logger.WriteLine("RatingTableDataTable.ComputeSeries(" + s.Table.TableName + "," + fileName + ")");

                var fn = RatingTableUtility.GetRatingTableAsLocalFile(fileName);

                if (!File.Exists(fn))
                {
                    string msg = "Error: File not found " + fn;
                    Console.WriteLine(msg);
                    Logger.WriteLine(msg);
                    rval.TimeInterval = s.TimeInterval;
                    return(rval);
                }

                var rt = new RatingTableDataTable();

                rt.ReadFile(fn);

                rval = rt.Lookup(s, method);

                rval.TimeInterval = s.TimeInterval;
                return(rval);
            }
 private void checkBoxMultiColumn_CheckedChanged(object sender, EventArgs e)
 {
     if (checkBoxMultiColumn.Checked)
     {
         this.dataGridView1.DataSource = RatingTableUtility.ConvertToMultiColumn(RatingTable);
     }
     else
     {
         this.dataGridView1.DataSource = RatingTable;
     }
 }
Beispiel #4
0
        public static Series FileLookupInterpolate2D(Series s1, Series s2, string fileName)
        {
            var rval = new Series();

            if (s1.IsEmpty || s2.IsEmpty)
            {
                Logger.WriteLine("FileLookupInterpolate2D - input series empty");
                return(rval);
            }


            var fn = RatingTableUtility.GetRatingTableAsLocalFile(fileName);

            if (!File.Exists(fn))
            {
                Logger.WriteLine("FileLookupInterpolate2D - input fileName, file not found");
                return(rval);
            }

            CsvFile csv = new CsvFile(fn, CsvFile.FieldTypes.AllText);

            foreach (var pt in s1)
            {
                Point point = pt;

                var s2_idx = s2.IndexOf(pt.DateTime);
                if (s2_idx < 0)
                {
                    point.Value = Point.MissingValueFlag;
                }
                else
                {
                    point.Value = Interpoloate2D(csv, pt.Value, s2[s2_idx].Value);
                }
                rval.Add(point);
            }

            return(rval);
        }
        private static void ExportToExcel(string tmpXlsName, bool multiColumn,
                                          Reclamation.TimeSeries.TimeSeriesDatabaseDataSet.RatingTableDataTable rt)
        {
#if SpreadsheetGear
            var xls = new SpreadsheetGearExcel(tmpXlsName);

            string     sheetName = multiColumn ? "Standard" : "simple";
            IWorksheet sheet     = (IWorksheet)xls.Workbook.Sheets[sheetName];

            xls.Workbook.Sheets[!multiColumn ? "Standard" : "simple"].Delete();

            IRange rng = sheet.Range[0, 0];

            DataTable table = rt;
            rng[0, 0].Value = rt.Name + " " + rt.EditDate;

            if (multiColumn)
            {
                table = RatingTableUtility.ConvertToMultiColumn(table);
                rng["A3"].CopyFromDataTable(table, SpreadsheetGear.Data.SetDataFlags.AllText);
                for (int c = 1; c <= 10; c++)
                {
                    rng[1, c].Value = rt.YUnits;
                }
            }
            else
            {
                rng["A3"].CopyFromDataTable(table, SpreadsheetGear.Data.SetDataFlags.None);
                rng[2, 0].Value = rt.XUnits;
                rng[2, 1].Value = rt.YUnits;
            }

            xls.Save();
#else
#endif
        }
Beispiel #6
0
        private static void UpdatesingleRatingTable(DataRow dRow,
                                                    bool generateNewTables,
                                                    List <string> stationUpdateList, List <string> attachments, out string attachmentRecipients)
        {
            string urlDownload = "";
            // Define parameters to be used for this checking iteration

            string cbtt = dRow["cbtt"].ToString().ToLower();

            string htmlTitle = "Station: " + cbtt + "<br>\n"
                               + dRow["title"].ToString() + "<br>\n"
                               + DateTime.Now.ToShortDateString();

            attachmentRecipients = "";
            string stationID = dRow["site_id"].ToString();
            var    email     = dRow["email"].ToString();
            string agency    = dRow["agency"].ToString();

            Console.Write(cbtt.PadRight(8, '.') + " " + agency.ToLower().PadLeft(5));
            // Check if the RDB is currently in the system
            string rdbFileName   = Path.Combine(hydrometRTFs, cbtt + ".rdb");
            string shiftFileName = Path.Combine(Path.Combine(hydrometRTFs, "_hj_tables"), cbtt + "_hj.csv");
            string qFileName     = Path.Combine(Path.Combine(hydrometRTFs, "_q_tables"), cbtt + "_q.csv");

            // Get full RatingTableFile
            DataTable fullRatingTable;
            TextFile  webRdbTable;
            TextFile  fileRdbTable;

            if (agency == "USGS")
            {
                usgsRatingTable = new Reclamation.TimeSeries.Usgs.UsgsRatingTable(stationID);
                usgsRatingTable.CreateShiftAndFlowTablesFromWeb();
                usgsRatingTable.CreateFullRatingTableFromWeb();
                fullRatingTable = usgsRatingTable.fullRatingTable;
                webRdbTable     = usgsRatingTable.webRdbTable;
                urlDownload     = usgsRatingTable.downloadURL;
            }
            else if (agency == "OWRD")
            {
                var ratingTable = new Reclamation.TimeSeries.Owrd.OwrdRatingTables(stationID);
                fullRatingTable = ratingTable.fullRatingTable;
                webRdbTable     = ratingTable.rawTable;
                urlDownload     = ratingTable.downloadURL;
            }
            else if (agency == "IDPWR")
            {
                var ratingTable = new Reclamation.TimeSeries.IdahoPower.IdahoPowerRatingTables(cbtt);
                ratingTable.CreateFullRatingTableFromWeb();
                fullRatingTable = ratingTable.fullRatingTable;
                webRdbTable     = ratingTable.webRdbTable;
                urlDownload     = ratingTable.downloadURL;
            }
            else
            {
                throw new Exception(cbtt.ToUpper() + "'s rating table from " + agency + " is not supported.");
            }

            // Create new RDB, files if the file does not currently exist
            if (!File.Exists(rdbFileName))
            {
                stationUpdateList.Add(@"<a href=""" + urlDownload + @""">" + cbtt + " (" + agency + " " + stationID + ")</a>  updated existing table");
                if (agency == "USGS")
                {
                    WriteHjAndQTables(shiftFileName, qFileName, usgsRatingTable);
                }
                WriteCsvFiles(fullRatingTable, cbtt);
                Console.WriteLine("                     new table");
                webRdbTable.SaveAs(rdbFileName);
            }
            // Check the existing file for updates
            else
            {
                // Get old RTF currently on file and copy it into temp. This is done to enable overwriting if the web file has been updated.
                fileRdbTable = GetRDBTableFromFile(cbtt, hydrometRTFs);
                // Compare
                var diff = (TextFile.Compare(fileRdbTable, webRdbTable).Length != 0);
                if (agency == "USGS")
                {
                    diff = UsgsRatingTable.Diff(fileRdbTable, webRdbTable);
                }

                // Save new RTF file to repository and generate new HJ and Q tables if the file was updated
                if (diff || generateNewTables)
                {
                    stationUpdateList.Add(@"<a href=""" + urlDownload + @""">" + cbtt + " (" + agency + " " + stationID + ")</a>  updated existing table");
                    // Copy old RDB to _Attic and save new RDB to repository
                    if (!generateNewTables)
                    {
                        fileRdbTable.SaveAs(Path.Combine(Path.Combine(hydrometRTFs, "_attic"), cbtt + DateTime.Now.ToString("_yyyy-MM-dd") + ".rdb"));//[JR] relies on the existence of an '_Attic' folder in the repository
                        webRdbTable.SaveAs(rdbFileName);
                    }
                    if (agency == "USGS")
                    {
                        WriteHjAndQTables(shiftFileName, qFileName, usgsRatingTable);
                    }
                    WriteCsvFiles(fullRatingTable, cbtt);
                    var htmlFile = Path.Combine(hydrometRTFs, cbtt + ".html");

                    if (fullRatingTable.Columns.Contains("Shift"))
                    {
                        fullRatingTable.Columns.Remove("Shift");
                    }
                    RatingTableUtility.WriteHtmlFile(fullRatingTable, htmlTitle, htmlFile);

                    // Define which attachments to add to the mail message if the 'email' field in the input file is not blank
                    if (email != "")
                    {
                        attachmentRecipients = email;
                        if (agency == "USGS")
                        {
                            attachments.Add(shiftFileName);
                            attachments.Add(qFileName);
                        }

                        attachments.Add(htmlFile);
                    }
                    Console.WriteLine("             UPDATED");
                }
                else
                {
                    Console.WriteLine("   current");
                }
            }
            //return urlDownload;
        }
Beispiel #7
0
        public static Series FileLookupInterpolate2DIslandPark(Series forebay, Series gate1, Series gate2, string file1, string file2)
        {
            var rval = new Series();

            if (forebay.IsEmpty || gate1.IsEmpty || gate2.IsEmpty)
            {
                Logger.WriteLine("FileLookupInterpolate2DIslandPark - input series empty");
                return(rval);
            }

            var fn1 = RatingTableUtility.GetRatingTableAsLocalFile(file1);

            if (!File.Exists(fn1))
            {
                Logger.WriteLine("FileLookupInterpolate2DIslandPark - input fileName, file not found " + file1);
                return(rval);
            }
            var fn2 = RatingTableUtility.GetRatingTableAsLocalFile(file2);

            if (!File.Exists(fn2))
            {
                Logger.WriteLine("FileLookupInterpolate2DIslandPark - input fileName, file not found " + file2);
                return(rval);
            }

            CsvFile csv1 = new CsvFile(fn1, CsvFile.FieldTypes.AllText);
            CsvFile csv2 = new CsvFile(fn2, CsvFile.FieldTypes.AllText);

            forebay.RemoveMissing(true); // remove flagged data
            gate1.RemoveMissing(true);
            gate2.RemoveMissing(true);

            foreach (var pt in forebay)
            {
                Point point = pt;

                var g1_idx = gate1.IndexOf(pt.DateTime);
                var g2_idx = gate2.IndexOf(pt.DateTime);
                if (g1_idx >= 0 && g2_idx >= 0)
                {
                    var g1 = gate1[g1_idx].Value;
                    var g2 = gate2[g2_idx].Value;

                    if (g2 <= 0.01) // gate 1 only
                    {
                        point.Value = Interpoloate2D(csv1, pt.Value, g1);
                    }
                    else if (g1 <= 0.01) // gate 2 only
                    {
                        point.Value = Interpoloate2D(csv1, pt.Value, g2);
                    }
                    else  // both gates operating use second lookup table.
                    {
                        point.Value = Interpoloate2D(csv2, pt.Value, (g1 + g2) * 0.5);
                    }
                }
                else
                {
                    point.Value = Point.MissingValueFlag;
                }

                rval.Add(point);
            }



            return(rval);
        }