[12080] | 1 | #!/usr/bin/python |
---|
| 2 | # -*- coding:Utf-8 -*- |
---|
| 3 | """ |
---|
| 4 | This script create template of namelist for Siren. |
---|
| 5 | |
---|
[13369] | 6 | see create_templates.py --help |
---|
[12080] | 7 | """ |
---|
| 8 | import os |
---|
| 9 | import argparse |
---|
| 10 | import re |
---|
| 11 | |
---|
| 12 | def get_default(i,var): |
---|
| 13 | """ |
---|
| 14 | Parameters |
---|
| 15 | ---------- |
---|
| 16 | i: str |
---|
| 17 | input filename |
---|
| 18 | var: str |
---|
| 19 | variable name |
---|
| 20 | """ |
---|
| 21 | with open(i) as f: |
---|
| 22 | for line in f: |
---|
| 23 | line=line.strip() |
---|
| 24 | if re.match('.*::.*'+var+'.*', line): |
---|
| 25 | return line.split("::")[1].split("=")[1] |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | def get_nam(i,o,d): |
---|
| 29 | """ |
---|
| 30 | Copy input file in output file, line by line. |
---|
[13369] | 31 | |
---|
[12080] | 32 | If line contains filterlist parameter and src: |
---|
| 33 | - copy the line without the src option |
---|
| 34 | - copy the file to be include line by line |
---|
| 35 | |
---|
| 36 | Parameters |
---|
| 37 | ---------- |
---|
| 38 | i: str |
---|
| 39 | input filename |
---|
| 40 | o: str |
---|
| 41 | output filename |
---|
| 42 | d: bool |
---|
| 43 | added default value |
---|
| 44 | """ |
---|
| 45 | with open(i) as f: |
---|
| 46 | inRecordingMode = False |
---|
| 47 | for line in f: |
---|
| 48 | line=line.strip() |
---|
| 49 | if not inRecordingMode: |
---|
| 50 | if line.startswith('NAMELIST'): |
---|
| 51 | inRecordingMode = True |
---|
| 52 | nam='&'+line.split('/')[1] |
---|
| 53 | cmt='' |
---|
| 54 | if re.match('^.*!<.*$', line): |
---|
| 55 | cmt='!< '+line.split('!<')[1] |
---|
| 56 | print("{0:10} {1}".format(nam,cmt),file=o) |
---|
| 57 | else: |
---|
| 58 | if line.startswith('NAMELIST') or not line: |
---|
| 59 | inRecordingMode = False |
---|
| 60 | print("/",file=o) |
---|
| 61 | else: |
---|
| 62 | if not line.startswith('!'): |
---|
| 63 | var=line.split('&')[1].split(',')[0].split()[0] |
---|
| 64 | if re.match('^.*!<.*$', line): |
---|
| 65 | cmt='!< '+line.split('!<')[1] |
---|
| 66 | if d: |
---|
| 67 | val=get_default(i,var) |
---|
| 68 | print("\t {0:12} = {1:30} {2}".format(var,val,cmt),file=o) |
---|
| 69 | else: |
---|
| 70 | print("\t {0:12} = {1:30} {2}".format(var,"",cmt),file=o) |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | def main(): |
---|
| 75 | """ |
---|
| 76 | Read Fortran src file and create template of namelist from it. |
---|
| 77 | """ |
---|
| 78 | # define parser |
---|
| 79 | parser = argparse.ArgumentParser( |
---|
| 80 | prog="create_templates.py", description="Create template of namelist from Fortran src." |
---|
| 81 | ) |
---|
| 82 | # positional arguments |
---|
| 83 | parser.add_argument("input" , type=str, help="Fortran filename, to be read in Siren/src directory") |
---|
| 84 | parser.add_argument("output", type=str, help="Output filename, to be written in Siren/templates directory") |
---|
| 85 | # optional arguments |
---|
| 86 | parser.add_argument("-v", "--verbose", action="store_true", help="show more things") |
---|
| 87 | parser.add_argument("-d", "--default", action="store_true", help="add default value for some variables") |
---|
| 88 | |
---|
| 89 | # parse arguments |
---|
| 90 | args = parser.parse_args() |
---|
| 91 | |
---|
| 92 | if re.match('.*/.*', args.output): |
---|
| 93 | raise NameError( |
---|
| 94 | "Output filename must not be a path (should not contains /)." |
---|
| 95 | ) |
---|
| 96 | print(args.output.split("/")[1]) |
---|
| 97 | fin=os.path.abspath(os.path.join("../src",args.input)) |
---|
| 98 | fout=os.path.abspath(os.path.join("../templates",args.output)) |
---|
| 99 | if args.verbose: |
---|
| 100 | print("Input file : {0}".format(fin)) |
---|
| 101 | print("Output file : {0}".format(fout)) |
---|
| 102 | if args.default: |
---|
| 103 | print("\nAdd default value to some variables") |
---|
| 104 | |
---|
| 105 | if not os.path.isfile(fin): |
---|
| 106 | raise NameError( |
---|
| 107 | "Can not find {0} in src directory".format(os.path.basename(fin)) |
---|
| 108 | ) |
---|
| 109 | |
---|
| 110 | with open(args.output,'w') as fout: |
---|
| 111 | get_nam(fin,fout,args.default) |
---|
| 112 | |
---|
| 113 | if __name__ == "__main__": |
---|
| 114 | main() |
---|