public static void HandleTextData(string filename, XRITHeader header) { if (header.PrimaryHeader.FileType == FileTypeCode.TEXT) { string basedir = FileHandler.FinalFileFolder; basedir = Path.Combine(basedir, TextFolder); try { UIConsole.Log($"New {header.ToNameString()}"); if (!Directory.Exists(basedir)) { Directory.CreateDirectory(basedir); } TextHandler.Handler.HandleFile(filename, basedir); File.Delete(filename); } catch (Exception e) { UIConsole.Warn($"Failed to parse Weather Data Image at {Path.GetFileName(filename)}: {e}"); } } else { FileHandler.DefaultHandler(filename, header); } }
public static void DefaultHandler(string filename, XRITHeader fileHeader) { string dir = Path.GetDirectoryName(filename); string ofilename = fileHeader.Filename ?? Path.GetFileName(filename); // Workarround for multi-segment HRIT if (fileHeader.Product.ID == (int)NOAAProductID.GOES16_ABI) { if (fileHeader.SegmentIdentificationHeader != null && fileHeader.SegmentIdentificationHeader.MaxSegments > 1) { string baseName = Path.GetFileNameWithoutExtension(ofilename); string ext = Path.GetExtension(ofilename); string fileH = fileHeader.SegmentIdentificationHeader.Sequence.ToString("D2"); string imageId = fileHeader.SegmentIdentificationHeader.ImageID.ToString(); ofilename = $"{baseName}-img{imageId}-seg{fileH}{ext}"; } } string f = PacketManager.FixFileFolder(dir, ofilename, fileHeader.Product, fileHeader.SubProduct); if ( (fileHeader.Product.ID == (int)NOAAProductID.DCS && SkipDCS) || (fileHeader.Product.ID == (int)NOAAProductID.EMWIN && SkipEMWIN) || (fileHeader.Product.ID == (int)NOAAProductID.HRIT_EMWIN && SkipEMWIN) || (fileHeader.Product.ID == (int)NOAAProductID.WEATHER_DATA && SkipWeatherData) ) { try { File.Delete(filename); } catch (IOException e) { UIConsole.Error(String.Format("Error deleting file {0}: {1}", Path.GetFileName(filename), e)); } return; } if (File.Exists(f)) { string timestamp = DateTime.Now.ToString("yyyyMMddHHmmssffff"); string ext = Path.GetExtension(f); string append = String.Format("--dup-{0}{1}", timestamp, ext); f = f.Replace(String.Format("{0}", ext), append); } UIConsole.Log($"New {fileHeader.ToNameString()}"); /*if (fileHeader.SubProduct.Name != "Unknown") { * UIConsole.Log($"New {fileHeader.Product.Name} - {fileHeader.SubProduct.Name}"); * } else { * UIConsole.Log($"New {fileHeader.Product.Name}"); * }*/ EventMaster.Post(EventTypes.NewFileEvent, new NewFileReceivedEventData { Name = Path.GetFileName(ofilename), Path = ofilename, Metadata = { { "product", fileHeader.Product.Name }, { "subProduct", fileHeader.SubProduct.Name }, { "productId", fileHeader.Product.ID.ToString() }, { "subProductId", fileHeader.SubProduct.ID.ToString() } } }); try { File.Move(filename, f); } catch (IOException e) { UIConsole.Error(String.Format("Error moving file {0} to {1}: {2}", filename, f, e)); } }
public static string DumpFile(string filename, XRITHeader fileHeader, string newExt, bool forceErase = false) { string dir = Path.GetDirectoryName(filename); string f = FixFileFolder(dir, fileHeader.Filename, fileHeader.Product, fileHeader.SubProduct); f = f.Replace(".lrit", "." + newExt); if ( FileHandler.SkipEMWIN && fileHeader.Product.ID == (int)NOAAProductID.HRIT_EMWIN || FileHandler.SkipWeatherData && f.Contains(WeatherDataFolder) ) { try { File.Delete(filename); } catch (Exception) { // Do nothing, file doesn't exists } return(null); } if (File.Exists(f)) { string timestamp = DateTime.Now.ToString("yyyyMMddHHmmssffff"); string ext = Path.GetExtension(f); string append = String.Format("--dup-{0}{1}", timestamp, ext); f = f.Replace(String.Format("{0}", ext), append); } UIConsole.Log($"New {fileHeader.ToNameString()}"); /* * if (fileHeader.SubProduct.Name != "Unknown") { * UIConsole.Log(String.Format("New {0} - {1} ({2})", fileHeader.Product.Name, fileHeader.SubProduct.Name, fileHeader.Filename)); * } else { * UIConsole.Log(String.Format("New {0} ({1})", fileHeader.Product.Name, fileHeader.Filename)); * }*/ FileStream fs = File.OpenRead(filename); fs.Seek(fileHeader.PrimaryHeader.HeaderLength, SeekOrigin.Begin); FileStream os = File.OpenWrite(f); byte[] buffer = new Byte[1024]; int bytesRead; while ((bytesRead = fs.Read(buffer, 0, 1024)) > 0) { os.Write(buffer, 0, bytesRead); } fs.Close(); os.Close(); if (f.Contains(".zip")) { UIConsole.Log(String.Format("Extracting Zip File {0}", Path.GetFileName(f))); ExtractZipFile(f); } else { EventMaster.Post(EventTypes.NewFileEvent, new NewFileReceivedEventData { Name = fileHeader.Filename, Path = f, Metadata = { { "product", fileHeader.Product.Name }, { "subProduct", fileHeader.SubProduct.Name }, { "productId", fileHeader.Product.ID.ToString() }, { "subProductId", fileHeader.SubProduct.ID.ToString() } } }); } if (!forceErase) { // Keep the original lrit file File.Move(filename, f.Replace("." + newExt, ".lrit")); return(f.Replace("." + newExt, ".lrit")); } else { try { File.Delete(filename); } catch (Exception) { // Do nothing, file doesn't exists } return(null); } }
public static void HandleWeatherData(string filename, XRITHeader header) { if (header.Filename.Contains("KWIN")) { // HRIT EMWIN wrongly has product id of weather data if (FileHandler.SkipEMWIN) { try { File.Delete(filename); } catch (Exception) { // Do nothing, file doesn't exists } return; } string fz = null; switch (header.Compression) { case CompressionType.ZIP: fz = DumpFile(filename, header, "zip"); break; case CompressionType.GIF: fz = DumpFile(filename, header, "gif"); break; case CompressionType.JPEG: fz = DumpFile(filename, header, "jpg"); break; case CompressionType.NO_COMPRESSION: fz = DumpFile(filename, header, "txt"); break; default: fz = DumpFile(filename, header, "bin"); break; } if (fz != null) { try { File.Delete(fz); } catch (Exception) { // Do nothing, file doesn't exists } } } else if (header.PrimaryHeader.FileType == FileTypeCode.IMAGE) { string basedir = FileHandler.FinalFileFolder; if (header.Product.ID == (int)NOAAProductID.OTHER_SATELLITES_1 || header.Product.ID == (int)NOAAProductID.OTHER_SATELLITES_2) { basedir = Path.Combine(basedir, OtherSatellitesFolder); } else { basedir = Path.Combine(basedir, WeatherDataFolder); } try { UIConsole.Log($"New {header.ToNameString()}"); //UIConsole.Log($"New Weather Data - {header.SubProduct.Name}"); if (!Directory.Exists(basedir)) { Directory.CreateDirectory(basedir); } ImageHandler.Handler.HandleFile(filename, basedir); File.Delete(filename); } catch (Exception e) { UIConsole.Warn($"Failed to parse Weather Data Image at {Path.GetFileName(filename)}: {e}"); } } else if (header.PrimaryHeader.FileType == FileTypeCode.TEXT) { string fz = DumpFile(filename, header, "txt"); if (fz != null) { try { File.Delete(fz); } catch (Exception) { // Do nothing, file doesn't exists } } } else { FileHandler.DefaultHandler(filename, header); } }