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