-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariScanFileManager.cs
493 lines (456 loc) · 19.9 KB
/
VariScanFileManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// --------------------------------------------------------------------------------
// VariScan module
//
// Description:
//
// Environment: Windows 10 executable, 32 and 64 bit
//
// Usage: TBD
//
// Author: (REM) Rick McAlister, [email protected]
//
// Edit Log: Rev 1.0 Initial Version
//
// Date Who Vers Description
// ----------- --- ----- -------------------------------------------------------
//
// ---------------------------------------------------------------------------------
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using TheSky64Lib;
//Static class for opening fits files inside of TSX
namespace VariScan
{
public static class VariScanFileManager
{
public static bool DirectOpenFitsFilePath(ccdsoftImage tsximg, string filePath)
{
tsximg.Path = filePath;
try { tsximg.Open(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
public static bool DirectOpenFitsFile(ccdsoftImage tsximg, string filePath)
{
tsximg.Path = filePath;
try { tsximg.Open(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
public static bool DialogOpenFitsFile(ccdsoftImage tsximg)
{
using (OpenFileDialog fitsDialog = new OpenFileDialog())
{
fitsDialog.Filter = "*.fit|*.fit";
if (fitsDialog.ShowDialog() == DialogResult.OK)
{
tsximg.Path = fitsDialog.FileName;
try { tsximg.Open(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
else return false;
}
}
public static bool AttachFitsFile(ccdsoftImage tsximg)
{
try { tsximg.AttachToActive(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
public static void CloseFitsFile(ccdsoftImage tsximg)
{
try { tsximg.Close(); }
catch { return; };
return;
}
public static string DialogOpenImageFile(ccdsoftImage tsximg, string varName)
{
using (OpenFileDialog imageFileDialog = new OpenFileDialog())
{
imageFileDialog.Filter = "*.fit|*.fit";
Configuration cfg = new Configuration();
string imageFileDirectory = cfg.ImageBankFolder + "\\" + varName;
if (Directory.Exists(imageFileDirectory))
{
imageFileDialog.InitialDirectory = imageFileDirectory;
if (imageFileDialog.ShowDialog() == DialogResult.OK)
{
tsximg.Path = imageFileDialog.FileName;
try { tsximg.Open(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
return imageFileDialog.FileName;
}
else return null;
}
return null;
}
}
public static bool DialogOrphanImageFile(ccdsoftImage tsximg, string varName)
{
using (OpenFileDialog imageFileDialog = new OpenFileDialog())
{
imageFileDialog.Filter = "*.fit|*.fit";
Configuration cfg = new Configuration();
//string imageFileDirectory = cfg.ImageBankFolder + "\\" + varName;
if (imageFileDialog.ShowDialog() == DialogResult.OK)
{
tsximg.Path = imageFileDialog.FileName;
try { tsximg.Open(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
return false;
}
}
public static void CloseImageFile(ccdsoftImage tsximg)
{
try
{
tsximg.DetachOnClose = 0;
tsximg.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error on fits close: " + ex.Message);
return;
};
return;
}
public static List<string> TargetImageList(string targetName)
{
Configuration cfg = new Configuration();
List<string> imageFiles = null;
string imageDir = cfg.ImageBankFolder + "\\" + targetName;
if (Directory.Exists(imageDir))
imageFiles = Directory.EnumerateFiles(imageDir, "*.fit").ToList();
return imageFiles;
}
public static List<string> GetTargetPathList()
{
//Make a list of paths for all target folders with images
Configuration cfg = new Configuration();
//get the image bank folder name from configuration file
//make a new one if it didn't exist
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
Directory.CreateDirectory(iBank);
//get the list of target directories in the bank and cull it for empty target directories
List<string> imageBankDirs = Directory.GetDirectories(iBank).ToList();
List<string> targetDirs = new List<string>();
foreach (string iDir in imageBankDirs)
if (Directory.GetFiles(iDir).Length > 0)
targetDirs.Add(iDir);
//sort alphabetically
targetDirs.Sort();
return targetDirs;
}
public static List<string> GetTargetNameList()
{
//Make a list of target names for all target folders with images
Configuration cfg = new Configuration();
//get the image bank folder name from configuration file
//make a new one if it didn't exist
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
Directory.CreateDirectory(iBank);
//get the list of target directories in the bank and cull it for empty target directories
List<string> imageBankDirs = Directory.GetDirectories(iBank).ToList();
List<string> targetDirNames = new List<string>();
foreach (string iDir in imageBankDirs)
if (Directory.GetFiles(iDir, "*.fit").Length > 0)
targetDirNames.Add(Path.GetFileNameWithoutExtension(iDir));
//cull duplicates and sort alphabetically
targetDirNames.Distinct().ToList().Sort();
return targetDirNames;
}
public static List<string> GetTargetSessionSetList(string tgtName)
{
//Make a list of target names for all target folders with images
List<string> targetSessionSetList = new List<string>();
List<string> targetNameList = GetTargetNameList();
return targetNameList;
}
public static List<string> GetCollectionFilenameList()
{
//Make a list of all image filenames in image bank
Configuration cfg = new Configuration();
//get the image bank folder name from configuration file
//make a new one if it didn't exist
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
Directory.CreateDirectory(iBank);
//get the list of target directories in the bank and cull it for empty target directories
List<string> targetDirNames = new List<string>();
foreach (string iDir in Directory.GetDirectories(iBank).ToList())
foreach (string iFile in Directory.GetFiles(iDir, "*.fit").ToList())
targetDirNames.Add(Path.GetFileNameWithoutExtension(iFile));
//cull duplicates and sort alphabetically
targetDirNames.Distinct().ToList().Sort();
return targetDirNames;
}
public static List<string> GetTargetPathList(DateTime imageDate)
{
//Return list of image directories that contain images on the imageDate (full path)
List<string> nonEmptyTargetDirectories = new List<string>();
//Get list of imagefile out of each target directory
foreach (string nonEmptyDirectory in GetTargetPathList())
{
foreach (string path in Directory.GetFiles(nonEmptyDirectory, "*.fit").ToList())
{
(string tName, string iD, string iF, string iC, string iS) =
ParseImageFileName(Path.GetFileNameWithoutExtension(path));
if (Convert.ToDateTime(iD) == imageDate)
{
nonEmptyTargetDirectories.Add(nonEmptyDirectory);
break;
}
}
}
nonEmptyTargetDirectories.Distinct().ToList().Sort();
return nonEmptyTargetDirectories;
}
public static List<string> GetAllImageDates()
{
//Returns sorted list of all dates that have images in image bank
List<string> imageDateList = new List<string>();
//Get list of imagefile out of each target directory
foreach (string nonEmptyDirectory in GetTargetPathList())
{
foreach (string path in Directory.GetFiles(nonEmptyDirectory, "*.fit").ToList())
{
(string n, string iD, string iF, string iC, string iS) =
ParseImageFileName(Path.GetFileNameWithoutExtension(path));
imageDateList.Add(iD);
}
}
imageDateList = imageDateList.Distinct().ToList();
imageDateList.Sort(new SpecialDateComparer());
return imageDateList;
}
public static List<string> GetTargetSessionPaths(string targetName, DateTime sessionDate)
{
//REturn list of files for a session date for a target
//A Session Date is a window from +/- 6 hours of 0:00 AM of file creation time
Configuration cfg = new Configuration();
List<string> datedFiles = new List<string>();
//Look for image bank folder, create it if missing then return empty handed
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
{
Directory.CreateDirectory(iBank);
return (datedFiles);
}
//take a look in the iBankName target directory
string iBankDir = iBank + "\\" + targetName;
//parse and convert each dir name to a date, then add to list
foreach (string f in Directory.GetFiles(iBankDir, "*.fit").ToList())
{
(string tName, string iDate, string iFilter, string iSeq, string iSet) = ParseImageFileName(Path.GetFileNameWithoutExtension(f));
DateTime fileDate = Convert.ToDateTime(iDate);
//DateTime fileDate = File.GetCreationTime(f);
if (Utility.NightTest(fileDate, sessionDate))
datedFiles.Add(f);
}
return datedFiles.ToList();
}
public static List<string> GetTargetSessionPaths(string targetName, DateTime sessionDate, int sessionSet)
{
//REturn list of files for a session date for a target
//A Session Date is a window from +/- 6 hours of 0:00 AM of file creation time
Configuration cfg = new Configuration();
List<string> datedFiles = new List<string>();
//Look for image bank folder, create it if missing then return empty handed
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
{
Directory.CreateDirectory(iBank);
return (datedFiles);
}
//take a look in the iBankName target directory
string iBankDir = iBank + "\\" + targetName;
//parse and convert each dir name to a date, then add to list
foreach (string f in Directory.GetFiles(iBankDir, "*.fit").ToList())
{
(string tName, string iDate, string iFilter, string iSeq, string iSet) = ParseImageFileName(Path.GetFileNameWithoutExtension(f));
DateTime fileDate = Convert.ToDateTime(iDate);
//DateTime fileDate = File.GetCreationTime(f);
if (Utility.NightTest(fileDate, sessionDate) && Convert.ToInt32(iSet) == sessionSet)
datedFiles.Add(f);
}
return datedFiles.ToList();
}
public static List<string> GetTargetSessionPaths(string targetName, DateTime sessionDate, string filter)
{
//REturn list of files for a session date for a target and filter
Configuration cfg = new Configuration();
List<string> datedFilterFiles = new List<string>();
//Look for image bank folder, create it if missing then return empty handed
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
{
Directory.CreateDirectory(iBank);
return (datedFilterFiles);
}
//take a look in the iBankName target directory
string iBankDir = iBank + "\\" + targetName;
//parse and convert each dir name to a date, then add to list
foreach (string f in Directory.GetFiles(iBankDir, "*.fit").ToList())
{
(string tName, string iDate, string iFilter, string iSeq, string iSet) = ParseImageFileName(Path.GetFileNameWithoutExtension(f));
//if (Utility.NightTest(Convert.ToDateTime(iDate), sessionDate) && iFilter == filter)
if (Convert.ToDateTime(iDate) == sessionDate && iFilter == filter)
datedFilterFiles.Add(f);
}
return datedFilterFiles.ToList();
}
public static List<DateTime> GetSessionDates(string targetName)
{
//Return list of session dates for this target
Configuration cfg = new Configuration();
List<DateTime> dateList = new List<DateTime>();
//Look for image bank folder, create it if missing then return empty handed
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
{
Directory.CreateDirectory(iBank);
return (dateList);
}
//take a look in the iBankName target directory
string iBankDir = iBank + "\\" + targetName;
List<string> datedFiles = Directory.GetFiles(iBankDir, "*.fit").ToList();
//parse and convert each dir name to a date, then add to list
foreach (string f in datedFiles)
{
FitsFileStandAlone ffso = new FitsFileStandAlone(f);
dateList.Add(Utility.GetImageSessionDate(ffso.FitsLocalDateTime));
}
dateList.Sort();
return dateList.Distinct().ToList();
}
public static List<string> GetAllVaultFilters(string iBankName)
{
Configuration cfg = new Configuration();
List<string> targetFilters = new List<string>();
//Look for image bank folder, create it if missing then return empty handed
string iBank = cfg.ImageBankFolder;
if (!Directory.Exists(iBank))
{
Directory.CreateDirectory(iBank);
return (targetFilters);
}
//take a look in the iBankName target directory
string iBankDir = iBank + "\\" + iBankName;
List<string> filterFiles = Directory.GetFiles(iBankDir, "*.fit").ToList();
//parse and convert each dir name to a date, then add to list
if (filterFiles.Count > 0)
{
foreach (string f in filterFiles)
{
string[] splitName = Path.GetFileName(f).Split(' ');
for (int i = 0; i < splitName.Length; i++)
if (splitName[i] == "F")
{
int filterIndex = Convert.ToInt32(splitName[i + 1]);
targetFilters.Add(Filters.LookUpFilterName(filterIndex));
}
}
return targetFilters.Distinct().ToList();
}
else
return targetFilters.ToList();
}
public static IEnumerable<string> ImageDirectoryList()
{
Configuration cfg = new Configuration();
IEnumerable<string> imageDirs = null;
string imageDir = cfg.ImageBankFolder;
if (Directory.GetDirectories(imageDir).Length > 0)
imageDirs = Directory.EnumerateDirectories(imageDir);
return imageDirs;
}
public static bool HasImages(string targetName)
{
Configuration cfg = new Configuration();
string[] imageFiles = null;
string imageDir = cfg.ImageBankFolder + "\\" + targetName;
if (Directory.Exists(imageDir))
imageFiles = Directory.GetFiles(imageDir, "*.fit");
if (imageFiles == null)
return false;
else return true;
}
public static DateTime GetCreationDate(string targetImageFilePath)
{
//Looks up the creation date for the file
return File.GetCreationTime(targetImageFilePath);
}
public static DateTime GetImagingDate(string targetImageFilePath)
{
//Looks up the imaging date for the file
DateTime imageDate;
FitsFileStandAlone ff = new FitsFileStandAlone(targetImageFilePath);
imageDate = ff.FitsLocalDateTime;
return imageDate;
}
public static DateTime GetSessionDate(string targetImageFilePath)
{
//Looks up the imaging date for the file
DateTime imageDate;
FitsFileStandAlone ff = new FitsFileStandAlone(targetImageFilePath);
imageDate = Utility.GetImageSessionDate(ff.FitsLocalDateTime);
return imageDate;
}
public static string StripPath(string dirPath)
{
//Removes all path information except the directory name
string[] map = dirPath.Split('\\');
return map[map.Length - 1];
}
public static (string, string, string, string, string) ParseImageFileName(string imageFilenameWithoutExtension)
{
//returns separated strings for date, filter and sequence number and time of creation
// from variscan image filenaming conventions
//
char[] sp = { ' ' };
string[] p = imageFilenameWithoutExtension.Split(sp, StringSplitOptions.RemoveEmptyEntries);
string setStr = p[p.Length - 1].Substring(1);
string seqStr = p[p.Length - 2].Substring(1);
char[] fHdr = new char[] { 'F', '_' };
string filterStr = p[p.Length - 3].TrimStart(fHdr);
string dateStr = p[p.Length - 4];
string name = null;
for (int i = 0; i <= p.Length - 4; i++)
name += p[i] + " ";
return (name.TrimEnd(' '), dateStr, filterStr, seqStr, setStr);
}
}
}