static void Main(string[] args) { string stepMessage = ""; FileName outputFileName; try { stepMessage = "command line arguments loading."; CmdArgs cmdArgs = new CmdArgs(args); if (cmdArgs.HasParameter("cfg")) { stepMessage = "configuration file loading."; Config conf = new Config(cmdArgs.Parameters["cfg"]); conf.Load(); stepMessage = "configuration file parsing."; ConfigNode nodeList = conf.Root.ChildNodes.Find(delegate(ConfigNode node){ return node.Name == "timeseries.to.join"; }); if (nodeList == null) throw new Exception("Block 'timeseries.to.join' is missing."); outputFileName = conf.Root["output.file.name", "output.tsr"].AsFileName(); TimeUnits timeUnits = (TimeUnits) Enum.Parse(typeof(TimeUnits), conf.Root["time.units", "seconds"].AsString(), true); List<FileName> list = new List<FileName>(); foreach (KeyValuePair<string, KeywordData> item in nodeList.NodeData) list.Add(item.Value.AsFileName()); if (list.Count <= 1) throw new Exception("Block 'timeseries.to.join' must contain at least 2 entries"); stepMessage = "loading timeseries."; List<TimeSeries> timeSeries = new List<TimeSeries>(); foreach(FileName ts in list) { TimeSeries newTS = new TimeSeries(); newTS.Load(ts); timeSeries.Add(newTS); } DateTime start = timeSeries[0].StartInstant; for (int i = 1; i < timeSeries.Count; i++) { if (timeSeries[i].StartInstant < start) start = timeSeries[i].StartInstant; } stepMessage = "creating output timeseries."; TimeSeries outTS = new TimeSeries(); outTS.StartInstant = start; outTS.TimeUnits = timeUnits; foreach (Column col in timeSeries[0].Columns) { Column newCol = new Column(col.ColumnType); newCol.Header = col.Header; outTS.AddColumn(newCol); } foreach (TimeSeries toJoin in timeSeries) outTS.AddTimeSeries(toJoin); stepMessage = "saving output timeseries."; outTS.Save(outputFileName); Console.WriteLine("Process complete with success."); } else { Console.WriteLine("Parameter --cfg is missing."); Console.WriteLine("Execution aborted."); return; } } catch (Exception ex) { Console.WriteLine("An exception was raised while {0}", stepMessage); Console.WriteLine("Exception message: {0}", ex.Message); } }
protected void JoinTimeseriesByFolder(FileName output, string filter, List<FilePath> folders_to_search, bool search_sub_folders = true, bool overwrite = true, TimeUnits tu = TimeUnits.SECONDS) { try { if (folders_to_search == null || folders_to_search.Count <= 0) throw new Exception("No folders to search timeseries were defined."); if (string.IsNullOrWhiteSpace(output.FullPath)) throw new Exception("No timeseries output was defined"); if (!System.IO.Directory.Exists(output.Path)) throw new Exception("The path '" + output.Path + "' doesn't exist."); if (System.IO.File.Exists(output.FullPath) && !overwrite) throw new Exception("The file '" + output.FullName + "' exists and overwrite is set to false."); if (string.IsNullOrWhiteSpace(filter)) throw new Exception("Filter was not set."); int found = 0; List<FileName> files = new List<FileName>(); List<TimeSeries> timeSeries = new List<TimeSeries>(); System.IO.SearchOption so; if (search_sub_folders) so = System.IO.SearchOption.AllDirectories; else so = System.IO.SearchOption.TopDirectoryOnly; foreach (FilePath path in folders_to_search) { found = FindFiles(path, files, filter, so); foreach (FileName fi in files) { TimeSeries newTS = new TimeSeries(); newTS.Load(fi); timeSeries.Add(newTS); } } if (timeSeries.Count <= 0) return; //Finds the Start Date for the output timeseries in the list of loaded timeseries DateTime start = timeSeries[0].StartInstant; for (int i = 1; i < timeSeries.Count; i++) { if (timeSeries[i].StartInstant < start) start = timeSeries[i].StartInstant; } TimeSeries outTS = new TimeSeries(); outTS.StartInstant = start; outTS.TimeUnits = tu; foreach (Column col in timeSeries[0].Columns) { Column newCol = new Column(col.ColumnType); newCol.Header = col.Header; outTS.AddColumn(newCol); } foreach (TimeSeries toJoin in timeSeries) outTS.AddTimeSeries(toJoin); outTS.Save(output); } catch (Exception ex) { exception_raised = true; exception = new Exception("ScriptV8.JoinTimeseriesByFolder", ex); throw exception; } }
private void JoinButton_Click(object sender, EventArgs e) { try { if (TimeseriesList.Items.Count <= 0) throw new GeneralException("No timeseries to join were defined.", ExceptionType.WARNING); if (string.IsNullOrWhiteSpace(OutputTimeseriesTextbox.Text)) throw new GeneralException("You must define the output timeseries.", ExceptionType.WARNING); else { try { output.FullPath = OutputTimeseriesTextbox.Text; if (!System.IO.Directory.Exists(output.Path)) throw new GeneralException("The path '" + output.Path + "' doesn't exist.", ExceptionType.WARNING); else if (System.IO.File.Exists(output.FullPath)) { if (MessageBox.Show("The file '" + output.FullName + "' exists. Do you want to OVERWRITE it?", "WARNING", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) return; } } catch (GeneralException) { throw; } catch (Exception ex) { throw new GeneralException(ex.Message, ExceptionType.ERROR); } } FileName file = new FileName(); List<TimeSeries> timeSeries = new List<TimeSeries>(); foreach (object ts in TimeseriesList.Items) { file.FullPath = (string)ts; TimeSeries newTS = new TimeSeries(); newTS.Load(file); timeSeries.Add(newTS); } DateTime start = timeSeries[0].StartInstant; for (int i = 1; i < timeSeries.Count; i++) { if (timeSeries[i].StartInstant < start) start = timeSeries[i].StartInstant; } TimeSeries outTS = new TimeSeries(); outTS.StartInstant = start; outTS.TimeUnits = (TimeUnits)Enum.Parse(typeof(TimeUnits), (string)TimeUnitsCombobox.SelectedItem, true); foreach (Column col in timeSeries[0].Columns) { Column newCol = new Column(col.ColumnType); newCol.Header = col.Header; outTS.AddColumn(newCol); } foreach (TimeSeries toJoin in timeSeries) outTS.AddTimeSeries(toJoin); output.FullPath = OutputTimeseriesTextbox.Text; outTS.Save(output); MessageBox.Show("Proccess Completed."); } catch (GeneralException gex) { switch (gex.Type) { case ExceptionType.ERROR: MessageBox.Show(gex.Message, "ERROR", MessageBoxButtons.OK); break; case ExceptionType.WARNING: MessageBox.Show(gex.Message, "WARNING", MessageBoxButtons.OK); break; default: MessageBox.Show("An unknown error happened. The message returned was: " + gex.Message, "ERROR", MessageBoxButtons.OK); break; } } catch (Exception ex) { MessageBox.Show("An unknown error happened. The message returned was: " + ex.Message, "ERROR", MessageBoxButtons.OK); } }
static void Main(string[] args) { Database db = null; string query; OdbcDataReader dbReader; int interval_seconds_guess; string dateFormat; string server = "http://www.meteoagri.com/"; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; try { db = new Database(); CmdArgs cmdArgs = new CmdArgs(args); List<DataToRead> list = new List<DataToRead>(); int defaultDaysToDownload; string user, pass; bool save_ts = cmdArgs.HasOption("save_ts"); bool save_db = cmdArgs.HasOption("save_db"); if (!cmdArgs.HasParameter("user")) throw new Exception("Must provide the user. Uses '--user user_name'."); if (!cmdArgs.HasParameter("pass")) throw new Exception("Must provide the pass. Uses '--pass password'."); user = cmdArgs.Parameters["user"]; pass = cmdArgs.Parameters["pass"]; if (cmdArgs.HasParameter("cfg")) { Config conf = new Config(cmdArgs.Parameters["cfg"]); conf.Load(); if (save_db) { ConfigNode dbConf = conf.Root.ChildNodes.Find(delegate(ConfigNode node) { return node.Name == "database.config"; }); if (dbConf == null) throw new Exception("No 'database.config' block was found."); if (db.Connect(dbConf["db.conn.string"].AsString()) != Result.TRUE) throw new Exception("Connection to database was not possible. The message returned was: " + db.RaisedException.Message); } server = conf.Root["server", "http://www.meteoagri.com/"].AsString(); defaultDaysToDownload = conf.Root["days.to.download", 14].AsInt(); dateFormat = conf.Root["date.format", "#yyyy-MM-dd HH:mm:ss#"].AsString(); interval_seconds_guess = conf.Root["interval.guess", 900].AsInt(); List<ConfigNode> nodeList = conf.Root.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "data.to.read"; }); if (nodeList == null) throw new Exception("No blocks 'nodes.to.read' were found."); DataToRead item; NodeData nd; int blockNumber = 1; foreach (ConfigNode node in nodeList) { item = new DataToRead(); list.Add(item); item.TimeSeriesFileName = node["time.series.file.name", ""].AsString(); item.TableName = node["table.name"].AsString(); item.DBDateColumn = node["db.date.column"].AsString(); List<ConfigNode> cfList = node.ChildNodes.FindAll(delegate(ConfigNode n) { return n.Name == "node"; }); if (cfList == null) throw new Exception("No blocks 'node' were found on block number " + blockNumber.ToString()); foreach (ConfigNode cn in cfList) { nd = new NodeData(); nd.DefaultValue = cn["default.value", ""].AsString(); nd.DBColumn = cn["column"].AsString(); nd.TSHeader = cn["ts.header", nd.DBColumn].AsString(); nd.NodeID = cn["node.id"].AsString(); item.NodesList.Add(nd); } if (save_db) { query = string.Format("SELECT {0} FROM {1} ORDER BY {0} DESC", item.DBDateColumn, item.TableName); dbReader = db.ExecuteQuerySingleRow(query); item.Start = DateTime.Now.AddDays(-defaultDaysToDownload); if (dbReader.HasRows) { while (dbReader.Read()) item.Start = dbReader.GetDateTime(dbReader.GetOrdinal(item.DBDateColumn)).AddDays(1); if (!dbReader.IsClosed) dbReader.Close(); } } else { item.Start = DateTime.Now.AddDays(-defaultDaysToDownload); } blockNumber++; } } else throw new Exception("No valid configuration file provided. use 'cfg' parameter."); MeteoAgriEngine maEng = new MeteoAgriEngine(server); maEng.TimeOut = 360; Console.Write("Connecting to MeteoAgri..."); if (maEng.Login(user, pass)) { Console.WriteLine("[OK]"); int count = 1; int nItems = 0; Console.WriteLine("DATA READ/WRITE in progress."); foreach (DataToRead dr in list) { TimeSeries ts = new TimeSeries(); ts.StartInstant = dr.Start; ts.TimeUnits = TimeUnits.SECONDS; foreach (NodeData nd in dr.NodesList) { Column col = new Column(typeof(string), 0, nd.TSHeader); col.DefaultValue = "0"; ts.AddColumn(col); } //First, find how many "slots" will be downloaded int slots = (int)(DateTime.Now - dr.Start).TotalSeconds / interval_seconds_guess; if (slots <= 0) { count++; continue; //throw new Exception("Start date for 'data.to.read' number " + count.ToString() + " (" + dr.Start.ToString("yyyy-MM-dd") + ") is same than today's date"); } nItems = 0; int column = -1; foreach (NodeData nd in dr.NodesList) { column++; Console.WriteLine("Reading from ARBVS server the node {0}, id = {1}...", nd.DBColumn, nd.NodeID); maEng.GetData(nd.NodeID, dr.Start, slots, ts, column, dateFormat); //if (nd.Data == null) //{ // Console.WriteLine("[NO DATA]"); // //throw new Exception(maEng.ExceptionMessage); //} //else if (nd.Data.Count < 1) //{ // Console.WriteLine("[NO DATA]"); // //throw new Exception("ARBVS server returned an empty node"); //} //else //{ // if (nItems < 1) // nItems = nd.Data[0].Data.Count; // else if (nItems != nd.Data[0].Data.Count) // { // Console.WriteLine("[ERROR]"); // throw new Exception("ARBVS server returned a different number of items than the previous node"); // } // Console.WriteLine("[OK]"); // //Console.WriteLine(""); // //Console.WriteLine("The data returned was:"); // //foreach (MeteoAgriNode node in nodes) // //{ // // Console.WriteLine("NODE : {0}", node.NodeId); // // foreach (MeteoAgriData data in node.Data) // // Console.WriteLine(string.Format(" type:'{0}' d:'{1}' s:'{2}' t:'{3}' value:'{4}'", // // data.Type, data.D, data.S, data.T, data.Value)); // // Console.WriteLine("------------"); // //} //} } string fields, data; //if (nItems > 0) //{ //DateTime start = DateTime.Now; //bool start_defined = false; if (save_db) { Console.Write("Writing info on database..."); for (int i = 0; i < ts.NumberOfInstants; i++) { fields = "Instant"; data = ts.InstantAsDateTime(i).ToString(dateFormat); //if (dr.NodesList[0].Data[0].Data[i].D == "-1") //{ // start = DateTime.ParseExact(dr.NodesList[0].Data[0].Data[i].T, "yyyyMMddTHH:mm:ss", null); // data = start.ToString(dateFormat); // start_defined = true; //} //else //{ // if (!start_defined) // throw new Exception("The start instant was not found in the imported data."); // start = start.AddSeconds(double.Parse(dr.NodesList[0].Data[0].Data[i].T)); // data = start.ToString(dateFormat); //} int column_index = -1; foreach (NodeData nd in dr.NodesList) { column_index++; fields += ", " + nd.DBColumn; data += ", " + (ts[column_index, i] as string); } query = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", dr.TableName, fields, data); //if (db.ExecuteCommand(query) != 1) db.ExecuteCommand(query); } Console.WriteLine("[OK][{0} items inserted]", ts.NumberOfInstants.ToString()); //} } if (save_ts) { if (ts.NumberOfInstants > 1) ts.Save(new FileName(dr.TimeSeriesFileName)); else Console.WriteLine("No data was available for {0}", dr.TimeSeriesFileName); } count++; } Console.Write("Logging out..."); if (maEng.Logout()) Console.WriteLine("[OK]"); else { Console.WriteLine("[ERROR]"); Console.WriteLine("The message returned was:"); Console.WriteLine(maEng.ExceptionMessage); } } else { Console.WriteLine("[ERROR]"); Console.WriteLine("The message returned was:"); Console.WriteLine(maEng.ExceptionMessage); } db.Disconnect(); } catch (Exception ex) { if (db != null) db.Disconnect(); Console.WriteLine(""); Console.WriteLine("An exception has happened. The message returned was:"); Console.WriteLine(ex.Message); } //Console.WriteLine(""); //Console.WriteLine("Press any key..."); //Console.ReadKey(); }