This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
hrit-img.py
126 lines (97 loc) · 4.92 KB
/
hrit-img.py
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
"""
hrit-img.py
https://github.com/sam210723/COMS-1
Extracts image data from HRIT IMG file.
"""
import argparse
from coms import COMS as comsClass
import glob
from PIL import Image
import numpy as np
import os
from subprocess import call
argparser = argparse.ArgumentParser(description="Extracts Meteorological Imager data from HRIT Image (IMG) files.")
argparser.add_argument("INPUT", action="store", help="Input HRIT file/folder path")
argparser.add_argument('OUTPUT', action="store", help="Output BIN file path")
argparser.add_argument('-i', action="store_true", help="Generate BMP from BIN file")
argparser.add_argument('-o', action="store_true", help="Add info text to generated BMP (assumes -i)")
argparser.add_argument('-m', action="store_true", help="Add map overlay to generated BMP (assumes -i)")
argparser.add_argument('-f', action="store", help="Overlay text fill colour", default="white")
args = argparser.parse_args()
segments = [] # List of IMG files
totalWidth = 0
totalHeight = 0
if os.path.isdir(args.INPUT): # If input is a directory
multipleSegments = True
print("Detecting IMG segments...")
# Loop through files with .hrit extension in input folder
for file in glob.glob(args.INPUT + "/*.hrit"):
COMS = comsClass(file)
COMS.parsePrimaryHeader()
COMS.parseImageStructureHeader()
if COMS.primaryHeader['file_type'] == 0: # Check HRIT file has IMG file type
segments.append(file) # Add to list of valid IMG files
totalHeight += COMS.imageStructureHeader['num_lines']
totalWidth = COMS.imageStructureHeader['num_cols']
if segments.__len__() <= 0:
print("No valid IMG files found")
exit(1)
print("Found {0} segments: ".format(segments.__len__()))
for segment in segments: # List detected segments
print(" - {0}".format(segment))
elif os.path.isfile(args.INPUT): # If input is a single file
multipleSegments = False
COMS = comsClass(args.INPUT)
COMS.parsePrimaryHeader()
COMS.parseImageStructureHeader()
if COMS.primaryHeader['file_type'] == 0: # Check HRIT file has IMG file type
segments.append(args.INPUT) # Add to list of valid IMG files
totalHeight += COMS.imageStructureHeader['num_lines']
totalWidth = COMS.imageStructureHeader['num_cols']
print()
# Delete output BIN file if it exists
if os.path.isfile(args.OUTPUT):
os.remove(args.OUTPUT)
print("Deleted existing BIN file: {0}".format(args.OUTPUT))
# Loop through each segment
for hritFile in segments:
# Create COMS class instance and load HRIT file
COMS = comsClass(hritFile)
# Primary Header (type 0, required)
COMS.parsePrimaryHeader()
# START OPTIONAL HEADERS
printOptHeaders = False
COMS.parseImageStructureHeader(printOptHeaders)
COMS.parseImageNavigationHeader(printOptHeaders)
COMS.parseImageDataFunctionHeader(printOptHeaders)
COMS.parseAnnotationTextHeader(printOptHeaders)
COMS.parseTimestampHeader(printOptHeaders)
COMS.parseKeyHeader(printOptHeaders)
COMS.parseImageSegmentationInformationHeader(printOptHeaders)
COMS.parseImageCompensationInformationHeader(printOptHeaders)
COMS.parseImageObservationTimeHeader(printOptHeaders)
COMS.parseImageQualityInformationHeader(printOptHeaders)
# BEGIN DATA DUMPING
binFile = open(args.OUTPUT, "ab")
binFile.write(COMS.readbytes(0, COMS.primaryHeader['data_field_len'])) # Dump image bytes to binary BIN file
print("{1}Image data dumped to \"{0}\"{2}".format(args.OUTPUT, COMS.colours['OKGREEN'], COMS.colours['ENDC']))
if args.i or args.o or args.m:
# See GitHub Issue 1 for details
bmpName = args.OUTPUT[:args.OUTPUT.index('.')] + ".bmp"
binFile = open(args.OUTPUT, 'rb')
z = np.fromfile(binFile, dtype=np.uint16, count=totalWidth * totalHeight)
z = z / 4
img = Image.frombuffer("L", [totalWidth, totalHeight], z.astype('uint8'), 'raw', 'L', 0, 1)
img.save(bmpName)
print("{0}\nBMP image generated\n{1}".format(COMS.colours['OKGREEN'], COMS.colours['ENDC']))
if args.o: # Overlay flag
overlayName = bmpName[:bmpName.index('.')] + "_overlay.bmp"
channel = COMS.imageDataFunctionHeader['data_definition_block'][9:COMS.imageDataFunctionHeader['data_definition_block'].index("\n")]
leftText = "COMS-1 HRIT {0} - {1}".format(COMS.imageTypes[COMS.imageStructureHeader['image_type']], channel)
rightText = "{0} {1} UTC".format(COMS.timestampHeader['t_field_current_date'], COMS.timestampHeader['t_field_current_time'])
if args.m: # Map flag
call(["python3", "overlay.py", "-s", "60", "-m", "-f", args.f, bmpName, overlayName, leftText, rightText])
print("Map overlay and info text added to BMP")
else:
call(["python3", "overlay.py", "-s", "60", "-f", args.f, bmpName, overlayName, leftText, rightText])
print("{0}Info text added to BMP{1}".format(COMS.colours['OKGREEN'], COMS.colours['ENDC']))