PHYEX 0.9.5
PHYsique EXternalisée
Loading...
Searching...
No Matches
namelist.py
Go to the documentation of this file.
1"""
2The namelist module deals with the namelists (.namel files) of the 1D model:
3- search of a namelist (user directory, then package)
4- phyex1d-namel_list and phyex1d-namel_show command line tools
5"""
6
7import argparse
8import glob
9import os
10import sys
11import textwrap
12
13import f90nml
14
15from . import user_dir
16from . import commands_help
17
18# Directory of the namelists provided with the package
19PACKAGE_NAMELIST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'namelists')
20
21
23 """
24 Files defining a namelist
25 :param name: namelist name
26 :return: (user file, package file), each being None if the file does not exist; the user
27 file, if any, is the one used (it overrides the package file)
28 """
29 return tuple(os.path.join(namel_dir, name + '.namel')
30 if os.path.exists(os.path.join(namel_dir, name + '.namel')) else None
31 for namel_dir in (user_dir('namelists'), PACKAGE_NAMELIST_DIR))
32
33
34def find_namelist(name):
35 """
36 File of a namelist
37 :param name: namelist name or file name (with the .namel extension)
38 :return: the file name (None if not found)
39 """
40 if name.endswith('.namel') and os.path.exists(name):
41 return name
42 return next((filename for filename in namelist_files(name) if filename is not None), None)
43
44
46 """
47 List of the available namelists (in the user directory and in the package)
48 :return: list of (name, location, readable) tuples, sorted by name; location is 'internal'
49 for a namelist of the package, the file name for a namelist of the user
50 directory and 'internal overridden by <file name>' for a namelist of the
51 package replaced by a namelist of the user directory; readable is False if the
52 namelist file cannot be read
53 """
54 names = sorted(set(os.path.basename(filename)[:-len('.namel')]
55 for namel_dir in (user_dir('namelists'), PACKAGE_NAMELIST_DIR)
56 for filename in glob.glob(os.path.join(namel_dir, '*.namel'))))
57 result = []
58 for name in names:
59 user_file, package_file = namelist_files(name)
60 if user_file is None:
61 location = 'internal'
62 elif package_file is None:
63 location = user_file
64 else:
65 location = 'internal overridden by ' + user_file
66 try:
67 with open(find_namelist(name), 'r', encoding='UTF-8') as f:
68 f90nml.read(f)
69 readable = True
70 except (ValueError, IndexError, KeyError, TypeError, OSError, StopIteration):
71 readable = False
72 result.append((name, location, readable))
73 return result
74
75
77 """Entry point of the phyex1d-namel_list command"""
78 parser = argparse.ArgumentParser(
79 description=textwrap.fill(
80 'Lists the available namelists (name and location). Namelists are searched in '
81 f'{user_dir("namelists")}, then in the package; a namelist of the package can be '
82 'overridden by a file with the same name in the user directory. The namelist '
83 'name can be given to phyex1d-exp_run (namel key of the PHYEX physics classes).'),
84 epilog=commands_help(), formatter_class=argparse.RawDescriptionHelpFormatter)
85 parser.parse_args()
86 namelists = list_namelists()
87 width = max([len('name')] + [len(name) for name, _, _ in namelists])
88 print(f'{"name":{width}s} location')
89 for name, location, readable in namelists:
90 print(f'{name:{width}s} {location}' + ('' if readable else ' (unreadable)'))
91
92
94 """Entry point of the phyex1d-namel_show command"""
95 parser = argparse.ArgumentParser(
96 description=textwrap.fill(
97 'Prints the content of a namelist (.namel file). The output can be used as a '
98 f'starting point to define a new namelist in {user_dir("namelists")}.'),
99 epilog=commands_help(), formatter_class=argparse.RawDescriptionHelpFormatter)
100 parser.add_argument('NAMELIST', help='namelist name (see phyex1d-namel_list)')
101 args = parser.parse_args()
102 filename = find_namelist(args.NAMELIST)
103 if filename is None:
104 sys.exit(f'Error: namelist {args.NAMELIST} not found (see phyex1d-namel_list)')
105 with open(filename, 'r', encoding='UTF-8') as f:
106 sys.stdout.write(f.read())
user_dir(subdir)
Definition __init__.py:35