source: LMDZ5/trunk/tools/compare_real.py @ 2404

Last change on this file since 2404 was 2082, checked in by lguez, 10 years ago

Script to compare def files. Documentation at:
http://lmdz.lmd.jussieu.fr/utilisateurs/outils/utilisation-de-lmdz#section-5

  • Property svn:executable set to *
File size: 1.5 KB
Line 
1#!/usr/bin/env python
2
3# Author: Lionel GUEZ
4
5# This script should be called by "diffdef.sh". It operates on
6# different lines with common variables. It requires version 2.7 or
7# higher of Python (it also works with Python 3).
8
9import sys, os.path
10
11with open(os.path.join(sys.argv[1], "common_var")) as common_var1, \
12        open(os.path.join(sys.argv[2], "common_var")) as common_var2, \
13        open("common_lines", "a") as common_lines, \
14        open("uniq_val", "w") as uniq_val:
15    uniq_val.write("Comparison of " + sys.argv[1] + " and " +  sys.argv[2] 
16                   + ":\n") # title line
17    for line1, line2 in zip(common_var1, common_var2):
18        i = line1.index("=")
19        var_name = line1[:i]
20        value1 = line1[i + 1:-1]
21        value2 = line2[i + 1:-1]
22        try:
23            value1_num = float(value1)
24            value2_num = float(value2)
25        except ValueError:
26            uniq_val.write(var_name + "=\t" + value1 + "\t" + value2 + "\n")
27        else:
28            if (value1_num, value2_num) == (0, 0) \
29                    or abs(value2_num - value1_num) \
30                    / max(abs(value1_num), abs(value2_num)) <= 1e-5:
31                # These should not be integer values, write a formatted value:
32                common_lines.write(var_name + "=" + str(value1_num) + "\n")
33            else:
34                # These could be integer values, write the original strings:
35                uniq_val.write(var_name + "=\t" + value1 + "\t" + value2
36                               + "\n")
Note: See TracBrowser for help on using the repository browser.