OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
compare_hdf4.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 """
4 Created on Thu Sep 29 12:19:06 2016
5 
6 @author: dshea
7 """
8 
9 import argparse
10 import subprocess
11 import sys
12 
13 
14 #
15 # make an item class
16 #
17 class DiffItem:
18 
19  def __init__(self, kind, name):
20  self.kind = kind
21  self.name = name
22  self.lines = []
23 
24  def addLine(self, line):
25  self.lines.append(line)
26 
27  def printItem(self):
28  for line in self.lines:
29  print(line)
30 
31 
32 startItems = {}
33 dataItems = {}
34 globalAttributeItems = {}
35 
36 
37 def addItem(item):
38  if item.lines:
39  if item.kind == "start":
40  startItems[item.name] = item
41  if item.kind == "data":
42  dataItems[item.name] = item
43  elif item.kind == "globalAttribute":
44  globalAttributeItems[item.name] = item
45 
46 #
47 # start of main program
48 #
49 parser = argparse.ArgumentParser(description='Compare two HDF4 files.')
50 
51 parser.add_argument('-G', '--globalExclude', help='Global Attrbutes to ignore')
52 parser.add_argument('-p', '--relativeLimit', help='relative limit, criteria is |(b-a)/a| > relativeLimit')
53 parser.add_argument('file1', help='first HDF4 file to compare')
54 parser.add_argument('file2', help='second HDF4 file to compare')
55 
56 args = parser.parse_args()
57 
58 if args.relativeLimit == None:
59  proc = subprocess.Popen(["hdiff", "-e", "10", args.file1, args.file2], stdout=subprocess.PIPE)
60 else:
61  proc = subprocess.Popen(["hdiff", "-e", "10", "-p", args.relativeLimit, args.file1, args.file2], stdout=subprocess.PIPE)
62 
63 out = proc.communicate()[0]
64 
65 # if hdiff returns 0 we are done
66 if(proc.returncode == 0):
67  sys.exit(0)
68 
69 if(proc.returncode != 1):
70  print(out.decode())
71  print("hdiff error return = ", proc.returncode)
72  sys.exit(1)
73 
74 # init the first item
75 item = DiffItem('start', 'first item')
76 
77 # run through all the lines and create DiffItems
78 lines = out.split(b'\n')
79 for line in lines:
80  line = line.decode() # change from binary string into normal ascii string
81  line = line.strip()
82 
83  if len(line) > 0:
84  if line.startswith("---------------------------"):
85  continue
86  elif line.startswith("position"):
87  addItem(item)
88  item = DiffItem('data', line.split()[1])
89  item.addLine(line)
90  elif line.startswith("Attr Name:"):
91  addItem(item)
92  item = DiffItem('globalAttribute', line.split(None,2)[2])
93  item.addLine(line)
94  else:
95  item.addLine(line)
96 
97 # add the last item
98 addItem(item)
99 
100 # run through all of the exclude items and delete thier corresponding DiffItem
101 if args.globalExclude != None:
102  excludes = args.globalExclude.split(",")
103  for exclude in excludes:
104  exclude = exclude.strip()
105  if exclude in globalAttributeItems:
106  del globalAttributeItems[exclude]
107 
108 # set initial exit code
109 exitCode = 0
110 
111 # print out everything
112 if len(startItems) > 0:
113  for key,value in startItems.items():
114  print('---------------------------')
115  value.printItem()
116 
117 if len(dataItems) > 0:
118  exitCode = 1
119  for key,value in dataItems.items():
120  print('---------------------------')
121  value.printItem()
122 
123 if len(globalAttributeItems) > 0:
124  exitCode = 1
125  for key,value in globalAttributeItems.items():
126  print("---------------------------")
127  value.printItem()
128 
129 
130 sys.exit(exitCode)
131 
def __init__(self, kind, name)
Definition: compare_hdf4.py:19
character(len=1000) if
Definition: names.f90:13
def addLine(self, line)
Definition: compare_hdf4.py:24
def addItem(item)
Definition: compare_hdf4.py:37