-
Notifications
You must be signed in to change notification settings - Fork 0
/
pds_detach.py
executable file
·139 lines (112 loc) · 5.87 KB
/
pds_detach.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
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# Copyright 2018, Ross A. Beyer ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This program is for dealing with .img files that come out of isis2pds and splitting them
# into detached labels for PDS3.
import os, sys, optparse
import pvl
def man(option, opt, value, parser):
print >>sys.stderr, parser.usage
print >>sys.stderr, '''\
This program works with PDS images and labels.
'''
sys.exit()
class UsageError(Exception):
def __init__(self, msg):
self.msg = msg
def main():
parser = optparse.OptionParser(usage="usage %prog [--help] [-o outname][-i isis2pds commands][-e] <file.img|file.cub>")
parser.add_option("-o","--output", dest="outname", help="output will be written to FILE.lbl and FILE.img", metavar="FILE")
parser.add_option("-i","--isis2pds", dest="isis2pds", help="string of options to pass to pds2isis")
parser.add_option("-e", "--edit", action="store_true", dest="edit", default=False,
help="The program will attempt to edit the labels.")
(options, args) = parser.parse_args()
if not args: parser.error("need an .img or .cub file")
imgfilename = args[0]
(root, ext) = os.path.splitext( imgfilename )
if( ext == '.cub' ):
# Check for weird pixels that PDS doesn't want.
statspvl = root+'_stats.pvl'
statscmd = 'stats fr='+args[0]+' to= '+statspvl
print( statscmd )
os.system(statscmd)
stats = pvl.load( statspvl )
os.remove( statspvl )
if( stats['Results']['LisPixels'] != 0 ): parser.error( args[0]+' has LisPixels!' )
if( stats['Results']['LrsPixels'] != 0 ): parser.error( args[0]+' has LrsPixels!' )
if( stats['Results']['HisPixels'] != 0 ): parser.error( args[0]+' has HisPixels!' )
if( stats['Results']['HrsPixels'] != 0 ): parser.error( args[0]+' has HrsPixels!' )
# Run isis2pds
imgfilename = 'isis2pds.img'
cmd = 'isis2pds fr= '+args[0]+' to= '+imgfilename
if options.isis2pds: cmd += ' '+options.isis2pds
print( cmd )
os.system(cmd)
label = pvl.load( imgfilename )
label_file = root+'-det.lbl'
data_file = root+'-det.img'
if options.outname:
label_file = options.outname+'.lbl'
data_file = options.outname+'.img'
if( os.path.exists(label_file) ):
parser.error(label_file+' exists!')
if( os.path.exists(data_file) ):
parser.error(data_file+' exists!')
with open( imgfilename, 'rb') as infile:
with open( label_file, 'wb') as outfile:
## outfile.write( infile.read( label['LABEL_RECORDS'].value ) )
# It sure would be nice to perform this manipulation with the pvl library,
# but it parses input values instead of preserving the bytes, so we'll have
# to do this the heavy-handed way:
label_lines = infile.read( label['LABEL_RECORDS'].value ).split(b'\r\n')
for line in label_lines:
#print( line )
if( label['RECORD_TYPE'] == 'UNDEFINED' and line.startswith( b'RECORD_TYPE' ) ):
outfile.write( line.replace(b'UNDEFINED',b'FIXED_LENGTH')+b'\r\n' )
i = line.find(b'=')
outfile.write( b'FILE_RECORDS'.ljust(i)+b'= '+str(
label['IMAGE']['LINES'] ).encode('ascii') + b'\r\n' )
outfile.write( b'RECORD_BYTES'.ljust(i)+b'= '+ str(
int( (label['IMAGE']['SAMPLE_BITS'] / 8) * label['IMAGE']['LINE_SAMPLES'] )
).encode('ascii') + b'\r\n' )
elif( line.startswith( b'LABEL_RECORDS' ) ): continue
elif( line.startswith( b'^IMAGE' ) ):
outfile.write( b'^IMAGE'.ljust(i)+b'= "'+os.path.basename(data_file).encode('ascii')+b'"\r\n' )
elif( options.edit ):
if( label['IMAGE']['OFFSET'] != 0.0 and line.lstrip().startswith( b'OFFSET' ) ):
i = line.find(b'=')
outfile.write( b' OFFSET'.ljust(i)+b'= 0.0\r\n' )
elif( label['IMAGE']['SCALING_FACTOR'] != 1.0 and line.lstrip().startswith( b'SCALING_FACTOR' ) ):
i = line.find(b'=')
outfile.write( b' SCALING_FACTOR'.ljust(i)+b'= 1.0\r\n' )
elif( line.lstrip().startswith( b'CORE_NULL' ) ):
outfile.write( line.replace(b'CORE_NULL ',b'MISSING_CONSTANT')+b'\r\n' )
elif( line.lstrip().startswith( b'CORE_LOW_REPR_SATURATION' ) ): continue
elif( line.lstrip().startswith( b'CORE_LOW_INSTR_SATURATION' ) ): continue
elif( line.lstrip().startswith( b'CORE_HIGH_REPR_SATURATION' ) ): continue
elif( line.lstrip().startswith( b'CORE_HIGH_INSTR_SATURATION' ) ): continue
else: outfile.write( line + b'\r\n' )
else: outfile.write( line + b'\r\n' )
with open( data_file, 'wb') as outfile:
outfile.write( infile.read() )
if( ext == '.cub' ): os.remove( imgfilename )
#print( pvl.dumps( label ) )
#print( str( pvl.dumps( label ), 'ascii') )
# # label_file
# outfile = 'label.lbl'
# ascii_label = pvl.dumps(label)
# with open( outfile, 'wb') as f:
# f.write(ascii_label)
if __name__ == "__main__":
sys.exit(main())