[13024] | 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=[] |
---|
[14687] | 22 | nbghostcells = 4 |
---|
[13024] | 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 | |
---|
[14952] | 45 | f1="namelist_cfg" |
---|
[13024] | 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() |
---|
[14626] | 58 | if line.strip().startswith('Ni0glo'): |
---|
| 59 | Ni0glo_parent=line.strip().split()[2] |
---|
[13024] | 60 | f2.write(line) |
---|
[14690] | 61 | if line.strip().startswith('Nj0glo'): |
---|
| 62 | Nj0glo_parent=line.strip().split()[2] |
---|
| 63 | f2.write(line) |
---|
[13024] | 64 | cnt1 += 1 |
---|
| 65 | f2.close() |
---|
| 66 | |
---|
[14952] | 67 | print(int(Ni0glo_parent), int(Nj0glo_parent)) |
---|
| 68 | |
---|
| 69 | if (int(grid[cnt-1][2]) == 2 ): |
---|
| 70 | nbghostcells_y_s = 1 |
---|
| 71 | if int(grid[cnt-1][3]) == int(Nj0glo_parent) : |
---|
| 72 | nbghostcells_y_n = 1 |
---|
| 73 | if int(grid[cnt-1][1]) - int(grid[cnt-1][0]) == int(Ni0glo_parent) : |
---|
[13024] | 74 | nbghostcells_x = 0 |
---|
[14952] | 75 | |
---|
[14674] | 76 | Ni0glo = (int(grid[cnt-1][1])-int(grid[cnt-1][0]))*int(grid[cnt-1][4]) + 2*nbghostcells_x |
---|
| 77 | Nj0glo = (int(grid[cnt-1][3])-int(grid[cnt-1][2]))*int(grid[cnt-1][5]) + nbghostcells_y_n + nbghostcells_y_s |
---|
[13024] | 78 | #print( "Grid "+str(cnt)+" : jpiglo = "+cnt(jpiglo)+ " jpjglo = "+str(jpjglo) ) |
---|
[14952] | 79 | print(int(grid[cnt-1][0]), int(grid[cnt-1][1]), int(grid[cnt-1][2]),int(grid[cnt-1][3])) |
---|
| 80 | print(nbghostcells_x, nbghostcells_y_s, nbghostcells_y_n) |
---|
[14626] | 81 | print('Grid {:1d} : Ni0glo = {:3d} , Nj0glo = {:3d}'.format(cnt, Ni0glo, Nj0glo)) |
---|
[13024] | 82 | |
---|
| 83 | f2 = open(str(cnt)+'_'+namfile,'w') |
---|
| 84 | with open(namfile) as fp: |
---|
| 85 | line = fp.readline() |
---|
| 86 | cnt1 = 1 |
---|
| 87 | while line : |
---|
| 88 | line = fp.readline() |
---|
| 89 | if line.strip().startswith('jperio'): |
---|
[14952] | 90 | if int(grid[cnt-1][1]) - int(grid[cnt-1][0]) == int(Ni0glo_parent): |
---|
[13024] | 91 | line = " jperio = 1\n" |
---|
| 92 | else: |
---|
| 93 | line = " jperio = 0\n" |
---|
| 94 | if line.strip().startswith('nn_bathy'): |
---|
[14690] | 95 | line = " nn_bathy = 2\n" |
---|
[13024] | 96 | if line.strip().startswith('nn_interp'): |
---|
| 97 | line = " nn_interp = 1\n" |
---|
| 98 | if line.strip().startswith('cn_topo'): |
---|
| 99 | line = " cn_topo = '"+topofile+"'\n" |
---|
| 100 | if line.strip().startswith('cn_bath'): |
---|
| 101 | line = " cn_bath = 'elevation'\n" |
---|
| 102 | if line.strip().startswith('cn_lon'): |
---|
| 103 | line = " cn_lon = 'lon'\n" |
---|
| 104 | if line.strip().startswith('cn_lat'): |
---|
| 105 | line = " cn_lat = 'lat'\n" |
---|
| 106 | if line.strip().startswith('rn_scale'): |
---|
| 107 | line = " rn_scale = -1\n" |
---|
[14626] | 108 | if line.strip().startswith('Ni0glo'): |
---|
| 109 | line = " Ni0glo = "+str(Ni0glo)+"\n" |
---|
| 110 | if line.strip().startswith('Nj0glo'): |
---|
| 111 | line = " Nj0glo = "+str(Nj0glo)+"\n" |
---|
[13024] | 112 | if line.strip().startswith('jpidta'): |
---|
[14626] | 113 | line = " jpidta = "+str(Ni0glo)+"\n" |
---|
[13024] | 114 | if line.strip().startswith('jpjdta'): |
---|
[14626] | 115 | line = " jpjdta = "+str(Nj0glo)+"\n" |
---|
[13024] | 116 | if line.strip().startswith('cp_cfg'): |
---|
[14626] | 117 | line = " cp_cfg = 'dumb'\n" |
---|
| 118 | if line.strip().startswith('ln_read_cfg'): |
---|
| 119 | line = " ln_read_cfg = .false.\n" |
---|
[13024] | 120 | f2.write(line) |
---|
| 121 | cnt1 += 1 |
---|
| 122 | f2.close() |
---|
| 123 | cnt +=1 |
---|