1 | import subprocess
|
---|
2 |
|
---|
3 | from WriteData import WriteData
|
---|
4 |
|
---|
5 |
|
---|
6 | def marshall(md):
|
---|
7 | """MARSHALL - outputs a compatible binary file from @model md, for certain solution type.
|
---|
8 |
|
---|
9 | The routine creates a compatible binary file from @model md
|
---|
10 | his binary file will be used for parallel runs in JPL-package
|
---|
11 |
|
---|
12 | Usage:
|
---|
13 | marshall(md)
|
---|
14 | """
|
---|
15 |
|
---|
16 | if md.verbose.solution:
|
---|
17 | print("marshalling file {}.bin".format(md.miscellaneous.name))
|
---|
18 |
|
---|
19 | # Open file for binary writing
|
---|
20 | try:
|
---|
21 | fid = open(md.miscellaneous.name + '.bin', 'wb')
|
---|
22 | except IOError as e:
|
---|
23 | raise IOError("marshall error message: could not open '%s.bin' file for binary writing. Due to: ".format(md.miscellaneous.name), e)
|
---|
24 |
|
---|
25 | fields = md.properties()
|
---|
26 | fields.sort() # sort fields so that comparison of binary files is easier
|
---|
27 | for field in fields:
|
---|
28 | # Some properties do not need to be marshalled
|
---|
29 | if field in ['results', 'radaroverlay', 'toolkits', 'cluster', 'private']:
|
---|
30 | continue
|
---|
31 |
|
---|
32 | # Check that current field is an object
|
---|
33 | if not hasattr(getattr(md, field), 'marshall'):
|
---|
34 | raise TypeError("field '{}' is not an object.".format(field))
|
---|
35 |
|
---|
36 | # Marshall current object
|
---|
37 | #print('marshalling {} ...'.format(field) # Uncomment for debugging
|
---|
38 | exec('md.{}.marshall(\'md.{}\', md, fid)'.format(field, field))
|
---|
39 |
|
---|
40 | #Last, write "md.EOF" to make sure that the binary file is not corrupt
|
---|
41 | WriteData(fid, 'XXX', 'name', 'md.EOF', 'data', True, 'format', 'Boolean')
|
---|
42 |
|
---|
43 | #close file
|
---|
44 | try:
|
---|
45 | fid.close()
|
---|
46 |
|
---|
47 | except IOError as e:
|
---|
48 | print('marshall error message: could not close file \'{}.bin\' due to:'.format(md.miscellaneous.name), e)
|
---|
49 |
|
---|
50 | # Uncomment the following to make a copy of the binary input file for
|
---|
51 | # debugging purposes (can be fed into scripts/BinRead.py).
|
---|
52 | # copy_cmd = 'cp {}.bin {}.py.bin'.format(md.miscellaneous.name, md.miscellaneous.name)
|
---|
53 | # subprocess.call(copy_cmd, shell=True)
|
---|