/usr/lib/python2.7/dist-packages/pyfits/scripts/fitsdiff.py is in python-pyfits 1:3.4-4ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import glob
import logging
import optparse
import os
import sys
import textwrap
import pyfits.diff
from pyfits.util import fill
log = logging.getLogger('fitsdiff')
USAGE = """
Compare two FITS image files and report the differences in header keywords and
data.
fitsdiff [options] filename1 filename2
where filename1 filename2 are the two files to be compared. They may also be
wild cards, in such cases, they must be enclosed by double or single quotes, or
they may be directory names. If both are directory names, all files in each of
the directories will be included; if only one is a directory name, then the
directory name will be prefixed to the file name(s) specified by the other
argument. for example::
fitsdiff "*.fits" "/machine/data1"
will compare all FITS files in the current directory to the corresponding files
in the directory /machine/data1.
""".strip()
EPILOG = """
If the two files are identical within the specified conditions, it will report
"No difference is found." If the value(s) of -c and -k takes the form
'@filename', list is in the text file 'filename', and each line in that text
file contains one keyword.
Example
-------
fitsdiff -k filename,filtnam1 -n 5 -d 1.e-6 test1.fits test2
This command will compare files test1.fits and test2.fits, report maximum of 5
different pixels values per extension, only report data values larger than
1.e-6 relative to each other, and will neglect the different values of keywords
FILENAME and FILTNAM1 (or their very existence).
fitsdiff command-line arguments can also be set using the environment variable
FITSDIFF_SETTINGS. If the FITSDIFF_SETTINGS environment variable is present,
each argument present will override the corresponding argument on the
command-line unless the --exact option is specified. The FITSDIFF_SETTINGS
environment variable exists to make it easier to change the
behavior of fitsdiff on a global level, such as in a set of regression tests.
""".strip()
class HelpFormatter(optparse.TitledHelpFormatter):
def format_epilog(self, epilog):
return '\n%s\n' % fill(epilog, self.width)
def handle_options(argv=None):
# This is a callback--less trouble than actually adding a new action type
def store_list(option, opt, value, parser):
setattr(parser.values, option.dest, [])
# Accept either a comma-separated list or a filename (starting with @)
# containing a value on each line
if value and value[0] == '@':
value = value[1:]
if not os.path.exists(value):
log.warning('%s argument %s does not exist' % (opt, value))
return
try:
values = [v.strip() for v in open(value, 'r').readlines()]
setattr(parser.values, option.dest, values)
except IOError as exc:
log.warning('reading %s for %s failed: %s; ignoring this '
'argument' % (value, opt, exc))
del exc
else:
setattr(parser.values, option.dest,
[v.strip() for v in value.split(',')])
parser = optparse.OptionParser(usage=USAGE, epilog=EPILOG,
formatter=HelpFormatter())
parser.add_option(
'-q', '--quiet', action='store_true',
help='Produce no output and just return a status code.')
parser.add_option(
'-n', '--num-diffs', type='int', default=10, dest='numdiffs',
metavar='INTEGER',
help='Max number of data differences (image pixel or table element) '
'to report per extension (default %default).')
parser.add_option(
'-d', '--difference-tolerance', type='float', default=0.,
dest='tolerance', metavar='NUMBER',
help='The relative tolerance for comparison of two numbers, '
'specifically two floating point numbers. This applies to data '
'in both images and tables, and to floating point keyword values '
'in headers (default %default).')
parser.add_option(
'-b', '--no-ignore-blanks', action='store_false',
dest='ignore_blanks', default=True,
help="Don't ignore trailing blanks (whitespace) in string values. "
"Otherwise trailing blanks both in header keywords/values and in "
"table column values) are not treated as significant i.e., "
"without this option 'ABCDEF ' and 'ABCDEF' are considered "
"equivalent. ")
parser.add_option(
'--no-ignore-blank-cards', action='store_false',
dest='ignore_blank_cards', default=True,
help="Don't ignore entirely blank cards in headers. Normally fitsdiff "
"does not consider blank cards when comparing headers, but this "
"will ensure that even blank cards match up. ")
parser.add_option(
'--exact', action='store_true',
dest='exact_comparisons', default=False,
help="Report ALL differences, "
"overriding command-line options and FITSDIFF_SETTINGS. ")
parser.add_option(
'-o', '--output-file', metavar='FILE',
help='Output results to this file; otherwise results are printed to '
'stdout.')
group = optparse.OptionGroup(parser, 'Header Comparison Options')
group.add_option(
'-k', '--ignore-keywords', action='callback', callback=store_list,
nargs=1, type='str', default=[], dest='ignore_keywords',
metavar='KEYWORDS',
help='Comma-separated list of keywords not to be compared. Keywords '
'may contain wildcard patterns. To exclude all keywords, use '
'"*"; make sure to have double or single quotes around the '
'asterisk on the command-line.')
group.add_option(
'-c', '--ignore-comments', action='callback', callback=store_list,
nargs=1, type='str', default=[], dest='ignore_comments',
metavar='KEYWORDS',
help='Comma-separated list of keywords whose comments will not be '
'compared. Wildcards may be used as with --ignore-keywords.')
parser.add_option_group(group)
group = optparse.OptionGroup(parser, 'Table Comparison Options')
group.add_option(
'-f', '--ignore-fields', action='callback', callback=store_list,
nargs=1, type='str', default=[], dest='ignore_fields',
metavar='COLUMNS',
help='Comma-separated list of fields (i.e. columns) not to be '
'compared. All columns may be excluded using "*" as with '
'--ignore-keywords.')
parser.add_option_group(group)
options, args = parser.parse_args(argv)
# Determine which filenames to compare
if len(args) != 2:
parser.error('\n' + textwrap.fill(
'fitsdiff requires two arguments; see `fitsdiff --help` for more '
'details.', parser.formatter.width))
return options, args
def setup_logging(outfile=None):
log.setLevel(logging.INFO)
error_handler = logging.StreamHandler(sys.stderr)
error_handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
error_handler.setLevel(logging.WARNING)
log.addHandler(error_handler)
if outfile is not None:
output_handler = logging.FileHandler(outfile)
else:
output_handler = logging.StreamHandler()
class LevelFilter(logging.Filter):
"""Log only messages matching the specified level."""
def __init__(self, name='', level=logging.NOTSET):
logging.Filter.__init__(self, name)
self.level = level
def filter(self, rec):
return rec.levelno == self.level
# File output logs all messages, but stdout logs only INFO messages
# (since errors are already logged to stderr)
output_handler.addFilter(LevelFilter(level=logging.INFO))
output_handler.setFormatter(logging.Formatter('%(message)s'))
log.addHandler(output_handler)
def match_files(paths):
filelists = []
for path in paths:
if glob.has_magic(path):
files = [os.path.abspath(f) for f in glob.glob(path)]
if not files:
log.error(
'Wildcard pattern %r did not match any files.' % path)
sys.exit(2)
filelists.append(files)
elif os.path.isdir(path):
filelists.append([os.path.abspath(f) for f in os.listdir(path)])
elif os.path.isfile(path):
filelists.append([path])
else:
log.error(
'%r is not an existing file, directory, or wildcard pattern; '
'see `fitsdiff --help` for more usage help.' % path)
sys.exit(2)
filelists[0].sort()
filelists[1].sort()
for a, b in [(0, 1), (1, 0)]:
if len(filelists[a]) > len(filelists[b]):
for extra in filelists[a][len(filelists[b]):]:
log.warning('%r has no match in %r' % (extra, paths[b]))
filelists[a] = filelists[a][:len(filelists[b])]
break
return zip(*filelists)
def main():
if 'FITSDIFF_SETTINGS' in os.environ:
argv = os.environ['FITSDIFF_SETTINGS'].split() + sys.argv[1:]
else:
argv = sys.argv[1:]
opts, args = handle_options(argv)
if opts.exact_comparisons:
# override the options so that each is the most restrictive
opts.ignore_keywords = []
opts.ignore_comments = []
opts.ignore_fields = []
opts.tolerance = 0.0
opts.ignore_blanks = False
opts.ignore_blank_cards = False
if not opts.quiet:
setup_logging(opts.output_file)
files = match_files(args)
close_file = False
if opts.quiet:
out_file = None
elif opts.output_file:
out_file = open(opts.output_file, 'wb')
close_file = True
else:
out_file = sys.stdout
identical = []
try:
for a, b in files:
# TODO: pass in any additional arguments here too
diff = pyfits.diff.FITSDiff(
a, b,
ignore_keywords=opts.ignore_keywords,
ignore_comments=opts.ignore_comments,
ignore_fields=opts.ignore_fields,
numdiffs=opts.numdiffs,
tolerance=opts.tolerance,
ignore_blanks=opts.ignore_blanks,
ignore_blank_cards=opts.ignore_blank_cards)
diff.report(fileobj=out_file)
identical.append(diff.identical)
return int(not all(identical))
finally:
if close_file:
out_file.close()
|