Skip to content

Commit cbce5ef

Browse files
committed
Added Archive Function and a try to solve the non erased lrit files
1 parent f74ac6f commit cbce5ef

File tree

7 files changed

+254
-230
lines changed

7 files changed

+254
-230
lines changed

LibraryTest/Program.cs

Lines changed: 23 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ class MainClass {
1919
public static void Main (string[] args) {
2020
Console.WriteLine($"XRIT Version: {LibInfo.Version}");
2121
AppDomain.CurrentDomain.UnhandledException += CrashReport.DefaultExceptionHandler;
22-
//*
23-
//Organizer org = new Organizer("./himawari");
24-
//org.Update();
25-
//var gd = org.GroupData[1490489400];
26-
//var od = gd.Infrared;
22+
#region GeoConverter Tests
2723
/*
2824
Console.WriteLine("Initializing organizer");
2925
Organizer org = new Organizer("/home/lucas/Works/OpenSatelliteProject/split/goesdump/XRITLibraryTest/bin/Debug/channels/Images/Full Disk/");
@@ -57,7 +53,9 @@ public static void Main (string[] args) {
5753
bmp.Save("unitedstates.jpg", ImageFormat.Jpeg);
5854
bmp.Dispose();
5955
// */
60-
///*
56+
#endregion
57+
#region Reprojection / Map Test
58+
/*
6159
string filename = "./OR_ABI-L2-CMIPF-M3C13_G16_s20170861545382_e20170861556160_c20170861556231.lrit";
6260
string visFilename = "./OR_ABI-L2-CMIPF-M3C02_G16_s20170861545382_e20170861556149_c20170861556217.lrit";
6361
XRITHeader header = FileParser.GetHeaderFromFile(filename);
@@ -96,7 +94,7 @@ public static void Main (string[] args) {
9694
9795
//reproj.Save("test.png", ImageFormat.Png);
9896
99-
///*
97+
// /*
10098
var mapDrawer = new MapDrawer("/home/lucas/Works/OpenSatelliteProject/split/borders/ne_50m_admin_1_states_provinces_lakes.shp");
10199
//var mapDrawer = new MapDrawer(shapeFile);
102100
//ImageTools.DrawLatLonLines(ref bmp, gc, Color.Brown);
@@ -128,80 +126,8 @@ public static void Main (string[] args) {
128126
129127
//return;
130128
// */
131-
/*
132-
Bitmap test0 = (Bitmap) Bitmap.FromFile("test0.jpg");
133-
Bitmap test1 = (Bitmap) Bitmap.FromFile("test1.jpg");
134-
Bitmap overlay = (Bitmap) Bitmap.FromFile("goes13-fulldisk.jpg");
135-
136-
test0 = test0.ToFormat(PixelFormat.Format24bppRgb, true);
137-
138-
overlay.Save("hue.jpg", ImageFormat.Jpeg);
139-
140-
ImageTools.ApplyOverlay(ref test0, overlay);
141-
test0.Save("test0-ovl.jpg", ImageFormat.Jpeg);
142-
143-
ImageTools.ApplyOverlay(ref test1, overlay);
144-
test1.Save("test1-ovl.jpg", ImageFormat.Jpeg);
145-
146-
test0.Dispose();
147-
test1.Dispose();
148-
overlay.Dispose();
149-
*/
150-
//*
151-
//string dcsFile = "/home/lucas/Works/OpenSatelliteProject/split/goesdump/XRITLibraryTest/bin/Debug/channels/DCS/pM-17085003239-A.dcs";
152-
//List<DCSHeader> d = DCSParser.parseDCS(dcsFile);
153-
/*
154-
//string debugFrames = "/media/ELTN/tmp/demuxdump-1490627438.bin";
155-
//string debugFrames = "/media/ELTN/tmp/debug5/demuxdump-1492732814.bin";
156-
//string debugFrames = "/home/lucas/Works/OpenSatelliteProject/split/issues/trango/3/debug_frames.bin";
157-
//string debugFrames = "/media/ELTN/tmp/debug3/raw_data.bin";
158-
/*
159-
var mapDrawer = new MapDrawer("/home/lucas/Works/OpenSatelliteProject/split/borders/ne_50m_admin_0_countries.shp");
160-
var fList = mapDrawer.ShapeFile.Features.ToList ();
161-
var bmp = new Bitmap (1280, 720, PixelFormat.Format24bppRgb);
162-
using (var graphics = Graphics.FromImage (bmp)) {
163-
Brush bgBrush = new SolidBrush (Color.Black);
164-
Brush polyBrush = new SolidBrush (Color.White);
165-
graphics.FillRectangle (bgBrush, 0, 0, 1280, 720);
166-
int o = 0;
167-
foreach (var fo in fList) {
168-
Console.WriteLine ($"Writting Geometry {o}");
169-
Pen pen = new Pen(Color.FromArgb((int)((255.0 * o) / fList.Count), 127, 127), 3);
170-
o++;
171-
for (var n = 0; n < fo.NumGeometries; n++) {
172-
//Console.WriteLine ($"Writting Geometry {n}");
173-
var fg = fo.GetBasicGeometryN (n);
174-
var k = fg.Coordinates;
175-
176-
float lastX = float.NaN;
177-
float lastY = float.NaN;
178-
179-
List<PointF> points = new List<PointF> ();
180-
foreach (var z in k) {
181-
float lon = (float)z.X;
182-
float lat = (float)z.Y;
183-
184-
float X = bmp.Width / 2 + bmp.Width * (lon / 360);
185-
float Y = bmp.Height / 2 - bmp.Height * (lat / 180);
186-
187-
if (!float.IsNaN (lastX) && !float.IsNaN (lastY)) {
188-
//graphics.DrawLine (pen, lastX, lastY, X, Y);
189-
}
190-
lastX = X;
191-
lastY = Y;
192-
193-
points.Add (new PointF (X, Y));
194-
}
195-
graphics.FillPolygon(polyBrush, points.ToArray());
196-
}
197-
}
198-
}
199-
Console.WriteLine ("Saving");
200-
bmp.Save ("/home/lucas/test.png", ImageFormat.Png);
201-
bmp.Dispose ();
202-
Console.WriteLine ("Done");
203-
return;
204-
//*/
129+
#endregion
130+
#region Ingestor Test
205131
/*
206132
EventMaster.On ("newFile", d => {
207133
var ed = (NewFileReceivedEventData) d.Data;
@@ -216,7 +142,8 @@ public static void Main (string[] args) {
216142
//string debugFrames = "/media/ELTN/tmp/G16JuneTest/demuxdump-1496790733.bin";
217143
//string debugFrames = "/media/ELTN/tmp/G16JuneTest/demuxdump-1500179126.bin";
218144
//string debugFrames = "/media/ELTN/tmp/debug14/demuxdump-1495166529.bin";
219-
string debugFrames = "/media/ELTN/tmp/debug16/demuxdump-1504736974.bin";
145+
//string debugFrames = "/media/ELTN/tmp/debug16/demuxdump-1504736974.bin";
146+
string debugFrames = "/media/ELTN/tmp/debug17/demuxdump-1505145094.bin";
220147
//var mapDrawer = new MapDrawer("/home/lucas/Works/OpenSatelliteProject/split/borders/ne_10m_admin_1_states_provinces.shp");
221148
222149
var im0 = new ImageManager ("output/Images/Full Disk/");
@@ -235,6 +162,7 @@ public static void Main (string[] args) {
235162
im5.InitMapDrawer ();
236163
im6.InitMapDrawer ();
237164
165+
UIConsole.GlobalEnableDebug = true;
238166
ImageManager.GenerateVisible = true;
239167
ImageManager.GenerateInfrared = true;
240168
ImageManager.GenerateFalseColor = true;
@@ -247,13 +175,13 @@ public static void Main (string[] args) {
247175
ImageManager.GenerateLabels = true;
248176
ImageManager.GenerateLatLonLabel = true;
249177
ImageManager.SaveNonOverlay = false;
250-
//im0.Start ();
251-
//im1.Start ();
252-
//im2.Start ();
253-
//im3.Start ();
254-
//im4.Start ();
178+
im0.Start ();
179+
im1.Start ();
180+
im2.Start ();
181+
im3.Start ();
182+
im4.Start ();
255183
im5.Start ();
256-
//im6.Start ();
184+
im6.Start ();
257185
// */
258186
//*/
259187
// /*
@@ -290,65 +218,14 @@ public static void Main (string[] args) {
290218
Console.WriteLine ("\t{0}: {1}", ((NOAAProductID)pID).ToString (), dm.ProductsReceived [pID]);
291219
}
292220
//*/
293-
//im.Stop();
294-
//*/
295-
//ProcessFile("/home/lucas/Works/OpenSatelliteProject/split/goesdump/goesdump/bin/Debug/channels/Text/NWSTEXTdat043204159214.lrit");
296-
/*
297-
Organizer organizer = new Organizer("/home/lucas/Works/OpenSatelliteProject/split/goesdump/goesdump/bin/Debug/channels/Images/Full Disk");
298-
organizer.Update();
299-
300-
var data = organizer.GroupData;
301-
302-
foreach (var z in data) {
303-
var mData = z.Value;
304-
var bmp = ImageTools.GenerateFalseColor(mData);
305-
306-
if (bmp != null) {
307-
bmp.Save(string.Format("{0}-{1}-{2}-{3}.jpg", mData.SatelliteName, mData.RegionName, "FLSCLR", z.Key), ImageFormat.Jpeg);
308-
bmp.Dispose();
309-
mData.IsProcessed = true;
310-
} else {
311-
if (mData.Visible.IsComplete && mData.Visible.MaxSegments != 0) {
312-
bmp = ImageTools.GenerateFullImage(mData.Visible);
313-
bmp.Save(string.Format("{0}-{1}-{2}-{3}.jpg", mData.SatelliteName, mData.RegionName, "VIS", z.Key), ImageFormat.Jpeg);
314-
bmp.Dispose();
315-
}
316-
if (mData.Infrared.IsComplete && mData.Infrared.MaxSegments != 0) {
317-
bmp = ImageTools.GenerateFullImage(mData.Infrared);
318-
bmp.Save(string.Format("{0}-{1}-{2}-{3}.jpg", mData.SatelliteName, mData.RegionName, "IR", z.Key), ImageFormat.Jpeg);
319-
bmp.Dispose();
320-
}
321-
if (mData.WaterVapour.IsComplete && mData.WaterVapour.MaxSegments != 0) {
322-
bmp = ImageTools.GenerateFullImage(mData.WaterVapour);
323-
bmp.Save(string.Format("{0}-{1}-{2}-{3}.jpg", mData.SatelliteName, mData.RegionName, "WV", z.Key), ImageFormat.Jpeg);
324-
bmp.Dispose();
325-
}
326-
Console.WriteLine("Not all segments available!");
327-
}
328-
}
329-
330-
331-
//*/
332-
221+
#endregion
222+
#region Archive Test
333223
/*
334-
string visFile = "/home/lucas/Works/OpenSatelliteProject/split/samples/FD 26-02-17 2106 G13VI.jpg";
335-
string irFile = "/home/lucas/Works/OpenSatelliteProject/split/samples/FD 26-02-17 2106 G13IR.jpg";
336-
337-
Bitmap vis = new Bitmap(visFile);
338-
ImageTools.ApplyCurve(Presets.VIS_FALSE_CURVE, ref vis);
339-
vis.Save("test.jpg", ImageFormat.Jpeg);
340-
//vis = vis.ToFormat(PixelFormat.Format32bppArgb, true);
341-
342-
Bitmap ir = new Bitmap(irFile);
343-
ir = ir.ToFormat(PixelFormat.Format32bppArgb, true);
344-
ImageTools.ApplyLUT(Presets.THERMAL_FALSE_LUT, ref ir, 3);
345-
ir.Save("test2.jpg", ImageFormat.Jpeg);
346-
347-
ir = ir.ToFormat(PixelFormat.Format32bppArgb);
348-
ImageTools.CombineHStoV(ref ir, vis);
349-
350-
ir.Save("final.jpg", ImageFormat.Jpeg);
351-
//*/
224+
UIConsole.GlobalEnableDebug = true;
225+
FileHandler.ArchiveFolder = "/media/ELTN/tmp/archiveTest/archive";
226+
FileHandler.ArchieveHandler("/media/ELTN/tmp/archiveTest/output", "Images");
227+
*/
228+
#endregion
352229
do {
353230
while (! Console.KeyAvailable) {
354231
Thread.Sleep(1000);

XRIT/GOES/FileHandler.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using OpenSatelliteProject.PacketData.Enums;
6+
using System.Linq;
67

78
namespace OpenSatelliteProject {
89
public delegate void FileHandlerFunction(string filename, XRITHeader fileHeader);
@@ -17,6 +18,8 @@ public static class FileHandler {
1718
public static bool SkipWeatherData { get; set; }
1819
public static string TemporaryFileFolder { get; set; }
1920
public static string FinalFileFolder { get; set; }
21+
public static string ArchiveFolder { get; set; }
22+
public static int DaysToArchive { get; set; }
2023

2124
static FileHandler() {
2225
byProductIdHandler = new Dictionary<int, FileHandlerFunction>();
@@ -29,6 +32,58 @@ static FileHandler() {
2932

3033
TemporaryFileFolder = Path.Combine(baseFolder, "tmp");
3134
FinalFileFolder = Path.Combine(baseFolder, "output");
35+
ArchiveFolder = Path.Combine (baseFolder, "archive");
36+
DaysToArchive = 1;
37+
}
38+
39+
/// <summary>
40+
/// Checks if any files in folder has reached the DaysToArchive days and archive them.
41+
/// </summary>
42+
/// <param name="folder">Folder.</param>
43+
/// <param name="groupName">Group name.</param>
44+
public static void ArchieveHandler(string folder, string groupName) {
45+
List<string> files = Directory.GetFiles (folder).ToList ();
46+
List<string> filesToArchive = new List<string> ();
47+
Dictionary<string, List<string>> dateMap = new Dictionary<string, List<string>> ();
48+
files.ForEach (f => {
49+
DateTime dt = File.GetCreationTime(f);
50+
var delta = DateTime.Now.Date - dt.Date;
51+
if (delta.Days >= DaysToArchive) {
52+
UIConsole.Debug($"{f} is to archive: {delta.Days}");
53+
filesToArchive.Add(f);
54+
string dir = Path.Combine(ArchiveFolder);
55+
dir = Path.Combine(dir, dt.Year.ToString());
56+
dir = Path.Combine(dir, dt.Month.ToString());
57+
dir = Path.Combine(dir, dt.Day.ToString());
58+
dir = Path.Combine(dir, groupName);
59+
if (!dateMap.ContainsKey(dir)) {
60+
dateMap.Add(dir, new List<string>());
61+
}
62+
dateMap[dir].Add(f);
63+
}
64+
});
65+
66+
if (filesToArchive.Count > 0) {
67+
UIConsole.Debug ($"{filesToArchive.Count} files to archive");
68+
dateMap.Keys.ToList ().ForEach ((d) => {
69+
var fs = dateMap[d];
70+
try {
71+
if (!Directory.Exists(d)) {
72+
Directory.CreateDirectory(d);
73+
}
74+
} catch (IOException e) {
75+
UIConsole.Error($"Error creating Archive Directory ({d}): {e.Message}");
76+
return;
77+
}
78+
fs.ForEach((f) => {
79+
try {
80+
File.Move(f, Path.Combine(d, Path.GetFileName(f)));
81+
} catch (IOException e) {
82+
UIConsole.Error($"Error archiving file {f}: {e.Message}");
83+
}
84+
});
85+
});
86+
}
3287
}
3388

3489
public static void AttachByCompressionHandler(int compressionType, FileHandlerFunction handler) {

0 commit comments

Comments
 (0)