1 | #!/usr/bin/env python |
---|
2 | # coding: utf-8 |
---|
3 | import argparse |
---|
4 | import sys |
---|
5 | |
---|
6 | if len(sys.argv) == 1 : |
---|
7 | namfile='namelist_cfg' |
---|
8 | topofile='GEBCO_2020.nc' |
---|
9 | elif len(sys.argv) == 2 : |
---|
10 | namfile=sys.argv[1] |
---|
11 | topofile='GEBCO_2020.nc' |
---|
12 | elif len(sys.argv) == 3 : |
---|
13 | namfile=sys.argv[1] |
---|
14 | topofile=sys.argv[2] |
---|
15 | else: |
---|
16 | print ("Usage : make_namelist.py namelist_cfg topofile") |
---|
17 | sys.exit (1) |
---|
18 | |
---|
19 | |
---|
20 | Agrilefile="AGRIF_FixedGrids.in" |
---|
21 | grid=[] |
---|
22 | nbghostcells = 3 |
---|
23 | nbghostcells_x = nbghostcells |
---|
24 | nbghostcells_y = nbghostcells |
---|
25 | nbghostcells_y_n = nbghostcells_y |
---|
26 | nbghostcells_y_s = nbghostcells_y |
---|
27 | |
---|
28 | |
---|
29 | with open(Agrilefile) as fp: |
---|
30 | line = fp.readline() |
---|
31 | cnt = 1 |
---|
32 | while line: |
---|
33 | # print("Line {}: {}".format(cnt, line.strip())) |
---|
34 | # print (line.strip(' ')) |
---|
35 | if len(line.split()) >=4 : |
---|
36 | #print(line.split()) |
---|
37 | grid.append(line.split()[0:6]) |
---|
38 | #for word in line.split() : |
---|
39 | #print(word) |
---|
40 | line = fp.readline() |
---|
41 | cnt += 1 |
---|
42 | |
---|
43 | print("Found", len(grid), "grids", ":") |
---|
44 | |
---|
45 | f1="namelist_ref" |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | cnt = 1 |
---|
50 | for g in range(len(grid)) : |
---|
51 | |
---|
52 | f2 = open(str(cnt)+'_'+f1,'w') |
---|
53 | with open(f1) as fp: |
---|
54 | line = fp.readline() |
---|
55 | cnt1 = 1 |
---|
56 | while line : |
---|
57 | line = fp.readline() |
---|
58 | if line.strip().startswith('jpiglo'): |
---|
59 | jpi_parent=line.strip().split()[2] |
---|
60 | f2.write(line) |
---|
61 | cnt1 += 1 |
---|
62 | f2.close() |
---|
63 | |
---|
64 | if int(grid[cnt-1][2]) == 1: |
---|
65 | nbghostcells_y_s = 0 |
---|
66 | if int(grid[cnt-1][1]) + int(grid[cnt-1][0]) == int(jpi_parent) : |
---|
67 | nbghostcells_x = 0 |
---|
68 | |
---|
69 | jpiglo = (int(grid[cnt-1][1])-int(grid[cnt-1][0]))*int(grid[cnt-1][4]) +2 +2*nbghostcells_x |
---|
70 | jpjglo = (int(grid[cnt-1][3])-int(grid[cnt-1][2]))*int(grid[cnt-1][5]) +2 + nbghostcells_y_n + nbghostcells_y_s |
---|
71 | #print( "Grid "+str(cnt)+" : jpiglo = "+cnt(jpiglo)+ " jpjglo = "+str(jpjglo) ) |
---|
72 | print('Grid {:1d} : jpiglo = {:3d} , jpjglo = {:3d}'.format(cnt, jpiglo, jpjglo)) |
---|
73 | |
---|
74 | f2 = open(str(cnt)+'_'+namfile,'w') |
---|
75 | with open(namfile) as fp: |
---|
76 | line = fp.readline() |
---|
77 | cnt1 = 1 |
---|
78 | while line : |
---|
79 | line = fp.readline() |
---|
80 | if line.strip().startswith('jperio'): |
---|
81 | if int(grid[cnt-1][1]) + int(grid[cnt-1][0]) == int(jpi_parent) : |
---|
82 | line = " jperio = 1\n" |
---|
83 | else: |
---|
84 | line = " jperio = 0\n" |
---|
85 | if line.strip().startswith('nn_bathy'): |
---|
86 | line = " nn_bathy = 2\n" |
---|
87 | if line.strip().startswith('nn_interp'): |
---|
88 | line = " nn_interp = 1\n" |
---|
89 | if line.strip().startswith('cn_topo'): |
---|
90 | line = " cn_topo = '"+topofile+"'\n" |
---|
91 | if line.strip().startswith('cn_bath'): |
---|
92 | line = " cn_bath = 'elevation'\n" |
---|
93 | if line.strip().startswith('cn_lon'): |
---|
94 | line = " cn_lon = 'lon'\n" |
---|
95 | if line.strip().startswith('cn_lat'): |
---|
96 | line = " cn_lat = 'lat'\n" |
---|
97 | if line.strip().startswith('rn_scale'): |
---|
98 | line = " rn_scale = -1\n" |
---|
99 | if line.strip().startswith('jpiglo'): |
---|
100 | line = " jpiglo = "+str(jpiglo)+"\n" |
---|
101 | if line.strip().startswith('jpjglo'): |
---|
102 | line = " jpjglo = "+str(jpjglo)+"\n" |
---|
103 | if line.strip().startswith('jpidta'): |
---|
104 | line = " jpidta = "+str(jpiglo)+"\n" |
---|
105 | if line.strip().startswith('jpjdta'): |
---|
106 | line = " jpjdta = "+str(jpjglo)+"\n" |
---|
107 | if line.strip().startswith('cp_cfg'): |
---|
108 | line = " cp_cfg = 'dumb'\n" |
---|
109 | f2.write(line) |
---|
110 | cnt1 += 1 |
---|
111 | f2.close() |
---|
112 | cnt +=1 |
---|