-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFitsFileStandAlone.cs
129 lines (112 loc) · 4.49 KB
/
FitsFileStandAlone.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
// --------------------------------------------------------------------------------
// 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.Globalization;
using System.IO;
using System.Linq;
namespace VariScan
{
public class FitsFileStandAlone
{
byte[] headerRecord = new byte[80];
byte[] dataUnit = new byte[2880];
int bCount;
//private UInt16[,] FITSArray;
private List<string> fitsHdr = new List<string>();
private UInt16[] fitsData = new UInt16[1];
private UInt16[] fitsHist = new UInt16[256];
const int ImageHeaderLength = 56 + (256 * 4);
public string FilePath { get; set; }
public DateTime FitsUTCDateTime { get; set; }
public string Filter { get; set; }
public DateTime FitsLocalDateTime { get; set; }
public FitsFileStandAlone(string filepath)
{
//Opens file set by filepath (assumes it's a FITS formatted file)
//Reads in header in 80 character strings, while ("END" is found
//if (dataswitch is true, { an array "FITSimage" is created and populated with the FITS image data as an class Image
// otherwise just the header info is retained
//
FilePath = filepath;
int keyindex = -1;
FileStream FitsHandle = File.OpenRead(filepath);
do
{
keyindex++;
bCount = FitsHandle.Read(headerRecord, 0, 80);
//Check for empty file (file error on creation), just opt out if (so
if (bCount == 0)
return;
fitsHdr.Add(System.Text.Encoding.ASCII.GetString(headerRecord));
} while (!fitsHdr.Last().StartsWith("END "));
//Continue through any remaining header padding
do
{
keyindex++;
bCount = FitsHandle.Read(headerRecord, 0, 80);
} while (!(keyindex % 36 == 0));
//Get the array dimensions
string fitsUTC = ReadKey("DATE-OBS").Split('.')[0];
string[] dsts = fitsUTC.Split('T');
string[] ds = dsts[0].Split('-');
string[] dt = dsts[1].Split(':');
int year = Convert.ToInt16(ds[0]);
int month = Convert.ToInt16(ds[1]);
int day = Convert.ToInt16(ds[2]);
int hour = Convert.ToInt16(dt[0]);
int minute = Convert.ToInt16(dt[1]) % 60;
int second = Convert.ToInt16(dt[2]);
DateTime utcDT = new DateTime(year, month, day, hour, minute, second);
string FitsUTCDate = utcDT.Date.ToShortDateString();
string FitsUTCTime = utcDT.TimeOfDay.ToString();
FitsUTCDateTime = utcDT;
Filter = ReadKey("FILTER");
string fitsLocal = ReadKey("LOCALTIM");
fitsLocal = fitsLocal.Substring(0, fitsLocal.Length - 4);
FitsLocalDateTime = DateTime.ParseExact(fitsLocal, "M d yyyy hh:mm:ss.FFF tt", CultureInfo.CurrentCulture); //'5/18/2020 01:53:24.469 AM STD'
//Close file
FitsHandle.Close();
return;
}
public string ReadKey(string keyword)
{
//return;s contents of key word entry, scrubbed of extraneous characters
foreach (string keyline in fitsHdr)
{
if (keyline.Contains(keyword))
{
int startindex = keyline.IndexOf("=");
int endindex = keyline.IndexOf(" /");
if (endindex == -1)
{
endindex = keyline.Length - 1;
}
string keylineN = keyline.Substring(startindex + 1, endindex - (startindex + 1));
// keyline = Replace(keyline, "//", " ");
keylineN = keylineN.Replace('/', ' ');
keylineN = keylineN.Replace('\'', ' ');
keylineN = keylineN.Trim(' ');
return (keylineN);
}
}
return (null);
}
}
}