Ejemplo n.º 1
0
 public void RefreshFolder(PiscesFolder parent)
 {
     if (DatabaseChanged != null)
     {
         OnDatabaseChanged(GetPathArray(parent));
     }
 }
Ejemplo n.º 2
0
 public void ChangeParent(PiscesObject piscesObject, PiscesFolder piscesFolder)
 {
     if (piscesObject.Parent.ID == piscesFolder.ID)
         return; // nothing to do
     TimeSeriesDatabaseDataSet.SeriesCatalogRow r = GetSeriesRow(piscesObject.ID);
     r.ParentID = piscesFolder.ID;
     SaveSeriesRow(r);
     OnDatabaseChanged(GetPathArray(piscesObject.Parent));
     OnDatabaseChanged(GetPathArray(piscesFolder));
 }
Ejemplo n.º 3
0
 public void ChangeParent(PiscesObject piscesObject, PiscesFolder piscesFolder)
 {
     if (piscesObject.Parent.ID == piscesFolder.ID)
     {
         return; // nothing to do
     }
     TimeSeriesDatabaseDataSet.SeriesCatalogRow r = GetSeriesRow(piscesObject.ID);
     r.ParentID = piscesFolder.ID;
     SaveSeriesRow(r);
     OnDatabaseChanged(GetPathArray(piscesObject.Parent));
     OnDatabaseChanged(GetPathArray(piscesFolder));
 }
Ejemplo n.º 4
0
        public static void CreatePiscesTree(string filename, PiscesFolder CurrentFolder, TimeSeries.TimeSeriesDatabase db)
        {
            var sqlite = new SQLiteServer(filename);

            /*insert into seriescatalog (id,parentid,name,isfolder) values (1,1,"database name",1);
             * insert into seriescatalog (id,parentid,name,units,tablename)
             *
             */
            Logger.WriteLine("reading " + filename, "ui");
            var variables = sqlite.Table("variables", "select a.var_name,b.value AS units from variables a "
                                         + "left join attributes b on a.var_name = b.var_name and b.attribute_name = 'units' order by a.var_name DESC");

            Logger.WriteLine("variable count = " + variables.Rows.Count, "ui");
            var sc = db.GetSeriesCatalog();

            string rootFolderName = Path.GetFileNameWithoutExtension(filename);
            var    rootID         = sc.AddFolder(rootFolderName);

            var prev_FolderName = rootFolderName;
            int folderID        = rootID;
            int id = sc.NextID();
            var cs = ConnectionStringUtility.MakeFileNameRelative("FileName=" + filename + ";", db.DataSource);

            foreach (DataRow row in variables.Rows)
            {
                var var_name = row["var_name"].ToString();

                if (var_name.IndexOf("_") > 0)
                {
                    var tokens = var_name.Split('_');
                    if (tokens[0] != prev_FolderName)
                    {
                        folderID = sc.AddFolder(tokens[0], rootID);
                        id++;
                        prev_FolderName = tokens[0];
                    }
                }

                var newRow = sc.NewSeriesCatalogRow();
                newRow.id               = id++;
                newRow.ParentID         = folderID;
                newRow.Provider         = "SQLiteSeries";
                newRow.ConnectionString = cs;
                newRow.IsFolder         = false;
                newRow.TableName        = var_name;
                newRow.Units            = row["units"].ToString();
                newRow.Name             = var_name;
                newRow.Parameter        = row["var_name"].ToString();
                sc.AddSeriesCatalogRow(newRow);
            }

            db.Server.SaveTable(sc);
        }
Ejemplo n.º 5
0
        public static void CreatePiscesTree(string fileName, PiscesFolder root,
            TimeSeriesDatabase db)
        {
            mi = new Model();
            s_db = db;
            sdi = db.NextSDI();
            studyFolderID = sdi;
            int parentID = root.ID;

            seriesCatalog = new TimeSeriesDatabaseDataSet.SeriesCatalogDataTable();
            if (File.Exists(fileName))
            {
                XYFileReader.Read(mi, fileName);
                m_xyFilename = Path.GetFileNameWithoutExtension(fileName);
            }
            else
            {
                throw new FileNotFoundException("Modsim xy file is not found " + fileName);
            }

            string mdbJetName = Path.Combine(Path.GetDirectoryName(fileName), m_xyFilename + "OUTPUT.mdb");
            string mdbAceName = Path.Combine(Path.GetDirectoryName(fileName), m_xyFilename + "OUTPUT.accdb");
            if (File.Exists(mdbAceName))
            {
                m_databaseName = mdbAceName;
            }
            else
                m_databaseName = mdbJetName;

            if (File.Exists(m_databaseName))
            {
                m_db = new AccessDB(m_databaseName);
                dir = Path.GetDirectoryName(Path.GetFullPath(m_databaseName));
                //AddNewRow(sdi,parentID,true, "", mi.name, "");
                AddNewRow(sdi, parentID, true, "", Path.GetFileNameWithoutExtension(fileName), "");

                ReservoirsTree();
                DemandsTree();
                RiverLinksTree();
                TotalsTree();

            }
            else
            {
                throw new FileNotFoundException(" MODSIM output not found " + m_databaseName);
            }

            //DataTableOutput.Write(seriesCatalog, @"C:\temp\a.csv",false);
            db.Server.SaveTable(seriesCatalog);
            db.RefreshFolder(root);
        }
Ejemplo n.º 6
0
        public PiscesObject CreateObject(SeriesCatalogRow sr)
        {
            PiscesObject rval = null;

            if (sr.IsFolder == 1)
            {
                rval = new PiscesFolder(db, sr);
            }
            else
            {
                return(GetSeries(sr)); //11.53125 seconds elapsed.
            }

            //    rval.Icon = AssignIcon(sr.iconname);
            return(rval);
        }
Ejemplo n.º 7
0
        public void SortByName(PiscesFolder parent)
        {
            var sc        = m_db.GetSeriesCatalog("parentid = " + parent.ID + " and isfolder = 1 ", "", " order by name");
            int sortOrder = 10;

            for (int i = 0; i < sc.Rows.Count; i++)
            {
                if (sc[i].siteid == "")
                {
                    sc[i].siteid = sc[i].Name; // fix... siteid might be handy on a folder
                }
                sc[i].SortOrder = sortOrder;
                sortOrder      += 10;
            }
            m_db.Server.SaveTable(sc);
        }
Ejemplo n.º 8
0
        public PiscesObject CreateObject(SeriesCatalogRow sr)
        {
            if (sr.IsFolder)
            {
                var v = new PiscesFolder(db, sr);
                v.Icon = AssignIcon(sr.iconname);
                return(v);
            }
            else
            {
                return(GetSeries(sr)); //11.53125 seconds elapsed.
                //return GetSeries(sdi, db); //31.96875
            }

            //return new PiscesObject();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Put all series into a consistent folder structure
        /// </summary>
        public void OrganizeSeriesCatalog(PiscesFolder folder)
        {
            if (folder.ID != folder.ParentID)
            {
                return; // must start from root folder.
            }

            var selectedPath = new List <string>();

            selectedPath.AddRange(m_seriesCatalog.GetPath(folder.ID));
            selectedPath.Add(folder.Name);


            for (int i = 0; i < m_seriesCatalog.Count; i++)
            {
                var row = m_seriesCatalog[i];
                if (row.IsFolder == 1)
                {
                    continue;
                }

                var existingPath = new List <string>();
                existingPath.AddRange(m_seriesCatalog.GetPath(row.id));


                var newPath = GetDefaultPath(folder.Name, row);



                if (!SamePath(newPath, existingPath))
                {
                    Console.WriteLine("Moving " + row.TableName);
                    Console.WriteLine("From :" + String.Join("/", existingPath.ToArray()));
                    Console.WriteLine("To   :" + String.Join("/", newPath.ToArray()));
                    var id = m_seriesCatalog.GetOrCreateFolder(newPath.ToArray());
                    row.ParentID = id;
                }
            }

            m_db.Server.SaveTable(m_seriesCatalog);
        }
Ejemplo n.º 10
0
        public PiscesObject CreateObject(SeriesCatalogRow sr)
        {
            PiscesObject rval = null;

            if (sr.IsFolder)
            {
                rval = new PiscesFolder(db, sr);
            }
            else if (sr.IsMeasurement)
            {
                rval = GetMeasurement(sr);
            }
            else if (sr.IsRatingTable)
            {
                rval = GetRatingTable(sr);
            }
            else
            {
                return(GetSeries(sr)); //11.53125 seconds elapsed.
            }

            rval.Icon = AssignIcon(sr.iconname);
            return(rval);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Insert the contents of another time series database 
        /// at the specified folder.
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="db"></param>
        public void InsertDatabase(PiscesFolder folder, TimeSeriesDatabase db)
        {
            var sc = GetSeriesCatalog();

            var new_sc = db.GetSeriesCatalog();
            for (int i = 0; i < new_sc.Rows.Count; i++)
            {
                var row = new_sc[i];
                var path = new_sc.GetPath(row.id);

                var folderID = sc.GetOrCreateFolder(path);

                if (row.IsFolder == 0)
                {// add series
                    var s = db.Factory.GetSeries(row.id);
                    var sr = sc.AddSeriesCatalogRow(s, sc.NextID(), folderID, s.Table.TableName);

                    // get time series data
                    s.Read();
                    ImportTimeSeriesTable(s.Table,sr, DatabaseSaveOptions.Insert);
                }

            }
            Server.SaveTable(sc);

            // Add Sites.
            var sites = GetSiteCatalog();

            var new_sites = db.GetSiteCatalog();
            for (int i = 0; i < new_sites.Count; i++)
            {
                if( !sites.Exists(new_sites[i].siteid))
                {
                    var newRow =sites.NewsitecatalogRow();
                    newRow.ItemArray = new_sites[i].ItemArray;
                    sites.AddsitecatalogRow(newRow);
                }
            }

            Server.SaveTable(sites);

            // add properties. TO DO.
        }
Ejemplo n.º 12
0
 // Does the conversion from an instant series to an hourly series.
 private static Series ProcessHourlySeries(Series sIn, string sName, TimeSeriesDatabase pDB, PiscesFolder dFldr)
 {
     Console.Write(sName + ", ");
     // Hourly averaging
     Series rval = Reclamation.TimeSeries.Math.Average(sIn, TimeInterval.Hourly);
     rval.Provider = "Series";
     rval.Name = sName;
     pDB.AddSeries(rval, dFldr);
     return rval;
 }
Ejemplo n.º 13
0
 // Populates Pisces DB with instant 15-minute data
 private static Series GetInstantSeries(string CBTT, string PCODE, DateTime t1, DateTime t2, TimeSeriesDatabase pDB,
     PiscesFolder rFldr)
 {
     Console.Write(CBTT + "_" + PCODE + ", ");
     Series rval = new HydrometInstantSeries(CBTT, PCODE);
     rval.Read(t1, t2);
     rval.Name = CBTT + "_" + PCODE + "15min";
     pDB.AddSeries(rval, rFldr);
     return rval;
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Lookup Series based on the internal database TableName
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="defaultInterval">default tableName prefix interval</param>
        /// <param name="prefix">table prefix usually 'instant' , 'daily' or monthly </param>
        /// <param name="createMissing"></param>
        /// <returns></returns>
        public Series GetSeriesFromTableName(string tableName, string prefix="",bool createMissing=false)
        {
            Logger.WriteLine("GetSeriesFromTableName(" + tableName + ", '" + prefix+"')");
            if (prefix.ToLower() == "irregular")
                prefix = "instant"; // inconsistency..

            if (Regex.IsMatch(tableName, "^[0-9]")) // table name starting with number is not allowed
            {
                tableName = "_" + tableName; // append with underscore
            }

            TimeSeriesName tn = new TimeSeriesName(tableName);

            if ( tn.Valid && !tn.HasInterval && prefix != "")
            {
                tableName = prefix + "_" + tableName; // add prefix before searching
            }

            string sql = "Select * from seriescatalog where tablename = '" + tableName.ToLower() + "' and isfolder = 0 ";
            var tbl = new TimeSeriesDatabaseDataSet.SeriesCatalogDataTable();
            m_server.FillTable(tbl, sql);
            if (tbl.Rows.Count == 0)
            {

                if (createMissing)
                {
                    Logger.WriteLine("Creating series "+tableName);
                    if (m_missingFolder == null )
                        m_missingFolder =  GetOrCreateFolder("missing");

                    Series ns = new Series();
                    ns.Name = tableName;
                    ns.Table.TableName = tableName;
                    ns.Parameter = tn.pcode;
                    var id = AddSeries(ns,m_missingFolder);
                    return factory.GetSeries(id);
                }
                else
                {
                    Logger.WriteLine("Error: This table name was not found '" + tableName + "'");
                    return null;
                }
            }
            var s =factory.GetSeries(tbl[0]);
            Logger.WriteLine(s.Name + ":  " + s.Table.TableName);
            return s;
        }
Ejemplo n.º 15
0
 public PiscesFolder GetOrCreateFolder( PiscesFolder parent=null,params string[] folderNames)
 {
     PiscesFolder rval = parent;
     for (int i = 0; i < folderNames.Length; i++)
     {
         var fn = folderNames[i];
         string sql = "name ='" + fn + "' and isfolder = 1";
         if (rval != null)
             sql += " and parentid = " + rval.ID;
         var sr = GetSeriesRow(sql);
         if (sr == null)
         {
             if (rval != null)
                 rval = AddFolder(rval, fn);
             else
                 rval = AddFolder(fn);
         }
         else
         {
            rval = this.Factory.GetFolder(sr.id);
            Logger.WriteLine(" found existing folder '"+fn+"'");
         }
     }
     return rval;
 }
Ejemplo n.º 16
0
        public bool DeleteFolderByName(string folderName, PiscesFolder selectedFolder, bool promptUser=false)
        {
            PiscesObject[] obj = GetChildren(selectedFolder);
            bool process = true;
            foreach (PiscesObject o in obj)
            {
                if (string.Equals(o.Name, folderName))
                {
                    var result = DialogResult.OK;

                    if (promptUser)
                    {
                        result = MessageBox.Show("This will delete the current '" + selectedFolder.Name + "/" + folderName + "' folder.",
                                                   "Warning!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    }

                    if (result == DialogResult.OK)
                    {
                        Delete(o);
                    }
                    else if (result == DialogResult.Cancel)
                    {
                        process = false;
                        break;
                    }
                }
            }
            return process;
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Adds new site using template subset of a SeriesCatalog
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="template">copy this sereis catalog changing the siteid </param>
        /// <param name="SiteName"></param>
        /// <param name="SiteID"></param>
        /// <param name="elevation"></param>
        /// <param name="Lat"></param>
        /// <param name="Lon"></param>
        /// <param name="TimeZone"></param>
        /// <param name="Install"></param>
        public void AddSiteWithTemplate(PiscesFolder parent, 
            TimeSeriesDatabaseDataSet.SeriesCatalogDataTable template, string SiteName, string SiteID, 
            string state,double elevation, double Lat, double Lon, string TimeZone, string Install, string program)
        {
            var siteCatalog = GetSiteCatalog();
            var rows = siteCatalog.Select("siteid='"+SiteID+"'");
            if (rows.Length == 0)  // check if site exists before creating.
            {
                siteCatalog.AddsitecatalogRow(SiteID, SiteName, state, Lat, Lon, elevation, TimeZone, Install, "", "", 0, "", "", "", "", "","");
                Server.SaveTable(siteCatalog);
            }

            var siteFolder = GetOrCreateFolder(parent,SiteID);

            var instant = GetOrCreateFolder(parent,SiteID,"instant" );
            var daily = GetOrCreateFolder(parent,SiteID,"daily");
            var quality = GetOrCreateFolder(parent, SiteID, "quality");

            var sc = GetSeriesCatalog();
            var series_properties = GetSeriesProperties();

            foreach (var item in template)
            {
                int id = sc.NextID();
                int parentID = siteFolder.ID;

                if (item.TimeInterval == "Daily")
                    parentID = daily.ID;
                if (item.TimeInterval == "Irregular")
                    parentID = instant.ID;

                if (QualityParameters.Contains(item.Parameter.ToUpper()))
                {
                    parentID = quality.ID;
                }

                 var check = sc.Select("tablename = '"+item.TableName+"'");

                 if (check.Length > 0)
                 {
                     MessageBox.Show("Warning: skipping item that allready exists: "+item.TableName);
                 }
                 else
                 {
                     sc.AddSeriesCatalogRow(id, parentID, 0, id, item.iconname, item.Name, item.siteid, item.Units,
                             item.TimeInterval, item.Parameter, item.TableName, item.Provider, item.ConnectionString, item.Expression, item.Notes, item.enabled);
                     series_properties.AddseriespropertiesRow(series_properties.NextID(), id, "program", program);
                 }

                //series_properties.DuplicateProperties(series item.id, id);
               }
            series_properties.Save();
            Server.SaveTable(sc);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Adds a Series to the database 
        /// </summary>
        /// <returns>assigned database id</returns>    
        public int AddSeries(Series s, PiscesFolder folder)
        {
            s.TimeSeriesDatabase = this;
            s.Parent = folder;
            s.ID = NextSDI();
            s.ConnectionString = ConnectionStringUtility.MakeFileNameRelative(s.ConnectionString, this.DataSource);

            if (s.ExternalDataSource)
                s.SeriesCatalogRow.TableName = "";

            else
            {
                if (s.Table != null && s.Table.TableName.Trim() != "")
                {
                    FixInvalidTableName(s);
                    s.SeriesCatalogRow.TableName = GetUniqueTableName(s.Table.TableName);
                    s.Table.TableName = s.SeriesCatalogRow.TableName;
                    //
                }
                else
                { // nothing to start with
                    s.SeriesCatalogRow.TableName = GetUniqueTableName(s.Name);
                }
            }

            s.Table.TableName = s.SeriesCatalogRow.TableName;
            s.SortOrder = NextSortOrder(s.Parent.ID);
            m_server.SaveTable(s.SeriesCatalogRow.Table); // save catalog

            if (!s.ExternalDataSource)
            {
                ImportTimeSeriesTable(s.Table, s.SeriesCatalogRow, DatabaseSaveOptions.Insert);
            }

            if (!m_supspendTreeUpdates)
            {
                RefreshFolder(folder);
            }

            return s.ID;
        }
Ejemplo n.º 19
0
 public void RefreshFolder(PiscesFolder parent)
 {
     if( DatabaseChanged != null)
       OnDatabaseChanged(GetPathArray(parent));
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Creates a new folder in the database
        /// </summary>
        /// <param name="parentID">parent of new folder to create</param>
        /// <returns>sid for the new folder</returns>
        public PiscesFolder AddFolder(PiscesFolder parent, string name)
        {
            int parentID = parent.ID;
            SeriesCatalogRow si = GetNewSeriesRow();
            si.ParentID = parentID;
            si.IsFolder = 1;
            if (name.Trim() == "")
                si.Name = GetUniqueFolderName("New Folder");
            else
                si.Name = name;
            si.TableName = GetUniqueTableName("folder");
            si.SortOrder = NextSortOrder(parentID);
            SaveSeriesRow(si);

            if (!m_supspendTreeUpdates)
            {
                RefreshFolder(parent);
            }

            return this.factory.GetFolder(si.id);
        }
Ejemplo n.º 21
0
        public PiscesObject CreateObject(SeriesCatalogRow sr)
        {
            PiscesObject rval = null;
            if (sr.IsFolder == 1)
            {
                rval = new PiscesFolder(db, sr);
            }
            else if( sr.IsMeasurement)
            {
                rval = GetMeasurement(sr);
            }
            else if( sr.IsRatingTable)
            {
                rval = GetRatingTable(sr);
            }
            else
            {
                return GetSeries(sr); //11.53125 seconds elapsed.
            }

            rval.Icon = AssignIcon(sr.iconname);
            return rval;
        }