// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; using System.Data.Common; namespace Spludlow.MetSat { public class Process { public static string _ReadyDirectory; public static string _StoreDirectory; public static Spludlow.Data.IDAL _Database; static Process() { _ReadyDirectory = Spludlow.Config.Get("MetSat.Ready"); _StoreDirectory = Spludlow.Config.Get("MetSat.Store"); _Database = Spludlow.Data.DAL.Create("@MetSat"); } /// /// Process all implimented products /// Insert database records and move files in the store /// public static void ProcessReadyDirectory() { Dictionary productATMs = new Dictionary(); productATMs.Add("EO_EUM_DAT_MSG_LRSEVIRI", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); productATMs.Add("EO_EUM_DAT_MSG_HRSEVIRI", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); productATMs.Add("EO_EUM_DAT_MSG_HRSEVIRI-IODC", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); productATMs.Add("EO_EUM_DAT_MULT_EARS-AVHRR", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.AVHRR", "Run" }); //productATMs.Add("EO_EUM_DAT_MSG_AMV", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); // BUFR !!! //productATMs.Add("EO_EUM_DAT_MSG_AMV-IODC", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); //productATMs.Add("EO_EUM_DAT_MSG_ASR", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); // BUFR !!! //productATMs.Add("EO_EUM_DAT_MSG_ASR-IODC", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); productATMs.Add("EO_EUM_DAT_MSG_CLM", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); productATMs.Add("EO_EUM_DAT_MSG_CLM-IODC", new string[] { "Spludlow.MetSat", "Spludlow.MetSat.XRIT", "Run" }); // GRIB // EO_EUM_DAT_MSG_CLAI small dimensions of 1237 × 1237 segments, with each segment formed by 3 × 3 image pixels 100 values // EO_EUM_DAT_MSG_CRM gave up problem with grib full derived size??? (infrerquewnt) // EO_EUM_DAT_MSG_CTH 3 × 3 pixel areas ?? // EO_EUM_DAT_MSG_DIV crap // EO_EUM_DAT_MSG_FIR bit crap fire // EO_EUM_DAT_MSG_MPE-GRIB // EO_EUM_DAT_MSG_OCA // EO_EUM_DAT_MSG_RSS-FIR-GRIB // EO_EUM_DAT_MSG_RSS-MPE-GRIB List unkownProducts = new List(); foreach (string productDirectory in Directory.GetDirectories(_ReadyDirectory)) { string product = Path.GetFileName(productDirectory); if (productATMs.ContainsKey(product) == true) { string[] atm = productATMs[product]; try { Spludlow.Reflections.Invoke(atm[0], atm[1], atm[2], new object[] { productDirectory }, null); } catch (Exception ee) { throw new ApplicationException("MetSat.Process; Error in product Directory:\t" + productDirectory + ", " + ee.Message, ee); } } else { unkownProducts.Add(product); } } if (unkownProducts.Count > 0) Spludlow.Log.Warning("MetSat.Process; Unknown product directories:\t", new object[] { unkownProducts.ToArray() }); } public static DataTable LoadFilenameInfo(string[] columnLayouts, string[] filenames) { DataTable table = new DataTable(); table.Columns.Add("Filename", typeof(string)); table.PrimaryKey = new DataColumn[] { table.Columns["Filename"] }; table.Columns.Add("LastWriteTime", typeof(DateTime)); table.Columns.Add("Length", typeof(long)); List columnList = new List(); foreach (string columnLayout in columnLayouts) { string[] words = Spludlow.Text.Split(columnLayout, '\t', true, false); if (words.Length != 3) throw new ApplicationException("Bad column Layout Line."); columnList.Add(words); table.Columns.Add(words[0], typeof(string)); } foreach (string filename in filenames) { DataRow row = table.NewRow(); row["Filename"] = filename; row["LastWriteTime"] = File.GetLastWriteTime(filename); row["Length"] = Spludlow.Io.Files.FileLength(filename); string line = Path.GetFileName(filename); foreach (string[] words in columnList) { string name = words[0]; int start = Int32.Parse(words[1]); int length = Int32.Parse(words[2]); if (length == 0) row[name] = line.Substring(start).Trim(); else row[name] = line.Substring(start, length).Trim(); } table.Rows.Add(row); } return table; } /// /// Clear database and stored files and thumbnails /// public static void ClearStore(int days) { Spludlow.Drawing.ThumbNails thumbs = new Drawing.ThumbNails("MetSat.Thumbs"); DateTime beforeDate = DateTime.Now.Date.AddDays(-days); DataTable table = _Database.Select("SELECT DataItemId FROM DataItems WHERE (CaptureTime < @CaptureTime)", beforeDate); if (table.Rows.Count == 0) { Spludlow.Log.Report("MetSat.Process; Clear, Nothing to clear."); return; } List dataItemIds = new List(); foreach (DataRow row in table.Rows) dataItemIds.Add((long)row["DataItemId"]); dataItemIds.Sort(); long minId = dataItemIds[0]; long maxId = dataItemIds[dataItemIds.Count - 1]; DataTable filesTable = _Database.Select("SELECT DataItemFileId, DataItemId, StoreExtention FROM DataItemFiles WHERE (DataItemId >= @Min) AND (DataItemId <= @Max)", minId, maxId); long storeDel = 0; long thumbDel = 0; foreach (DataRow fileRow in filesTable.Rows) { if (dataItemIds.Contains((long)fileRow["DataItemId"]) == false) continue; long fileId = (long)fileRow["DataItemFileId"]; string extention = (string)fileRow["StoreExtention"]; string storeFilename = Spludlow.Io.FileStore.FilePath(fileId, _StoreDirectory, extention, false); string thumbFilename = thumbs.WebAndFilePath(storeFilename, false)[1]; if (File.Exists(storeFilename) == true) { storeDel += Spludlow.Io.Files.FileLength(storeFilename); File.Delete(storeFilename); } if (thumbFilename != null && File.Exists(thumbFilename) == true) { thumbDel += Spludlow.Io.Files.FileLength(thumbFilename); File.Delete(thumbFilename); } } _Database.Begin(); try { foreach (int dataItemId in dataItemIds) { _Database.ExecuteNonQuery("DELETE FROM DataItemFiles WHERE DataItemId = " + dataItemId); _Database.ExecuteNonQuery("DELETE FROM DataItems WHERE DataItemId = " + dataItemId); } _Database.Commit(); } catch { _Database.Rollback(); throw; } Spludlow.Log.Report("MetSat.Process; Clear " + Spludlow.Text.DataSize(storeDel) + ", " + Spludlow.Text.DataSize(thumbDel), new object[] { table }); } } }