1 | #!/usr/bin/env python3 |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | import os |
---|
5 | import subprocess |
---|
6 | import re |
---|
7 | from netCDF4 import Dataset |
---|
8 | |
---|
9 | # |
---|
10 | # BEGIN USER MODIFICATIONS |
---|
11 | # |
---|
12 | |
---|
13 | # Directory with domccfg target file |
---|
14 | DOMCFG_DIR="../DOMAINcfg" |
---|
15 | # Suffix of domcfg files |
---|
16 | RAD='domain_cfg.nc' |
---|
17 | # Directory with original forcing on native grid |
---|
18 | FORCING_DIR='/Users/rblod/DATA/NEMO/NEMO_v4/ORCA2_ICE_v4.0/' |
---|
19 | # Forcing file names, interpolation method (default bilin), and weigth file name (optional), lon(optional), lat(optional) |
---|
20 | FILES=[ |
---|
21 | ['chlorophyll.nc' ,'bilin','','',''], |
---|
22 | ['geothermal_heating.nc' ,'bilin','','',''], |
---|
23 | ['eddy_viscosity_3D.nc' ,'bilin','','',''], |
---|
24 | ['resto.nc' ,'bilin','','',''], |
---|
25 | ['sss_data.nc' ,'bilin','','',''], |
---|
26 | ['q_10.15JUNE2009_fill.nc','bilin','','',''], |
---|
27 | ['q_10.15JUNE2009_fill.nc','bicub','','',''] |
---|
28 | ] |
---|
29 | |
---|
30 | # |
---|
31 | # END USER MODIFICATIONS |
---|
32 | # |
---|
33 | mycmd="ls "+DOMCFG_DIR+"/?_"+RAD |
---|
34 | returned_output = subprocess.check_output(mycmd, shell=True) |
---|
35 | listcfg = (returned_output.decode("utf-8")).split() |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | for i in range(len(listcfg)): |
---|
40 | print ('Computing weights for cfg file %s :' % listcfg[i]) |
---|
41 | print() |
---|
42 | |
---|
43 | for myfile in FILES: |
---|
44 | print('Input file is %s with %s interpolation' % (myfile[0], myfile[1])) |
---|
45 | if len(myfile[2]) == 0 : |
---|
46 | wfile=str(i+1)+'_'+myfile[1]+'_'+myfile[0] |
---|
47 | else: |
---|
48 | wfile=str(i+1)+'_'+myfile[2] |
---|
49 | print(' Performing weights computation ...') |
---|
50 | if myfile[1]=='namelist_bicub': |
---|
51 | namelist='namelist_bicub' |
---|
52 | else : |
---|
53 | namelist='namelist_bilin' |
---|
54 | |
---|
55 | myfilename=FORCING_DIR+'/'+myfile[0] |
---|
56 | dataset=Dataset(myfilename) |
---|
57 | |
---|
58 | # if len(myfile[3])==0: |
---|
59 | # for key in dataset.variables: |
---|
60 | # if len(dataset.variables[key].dimensions) >=2: |
---|
61 | # myvar=key |
---|
62 | # break |
---|
63 | # else: |
---|
64 | # myvar=myfile[3] |
---|
65 | # print(' Interpolation based on variable %s ...' % myvar) |
---|
66 | |
---|
67 | if len(myfile[3])==0 or len(myfile[4])==0: |
---|
68 | for key in dataset.variables: |
---|
69 | if re.search('lat',key.lower()): |
---|
70 | mylat=key |
---|
71 | if re.search('lon',key.lower()): |
---|
72 | mylon=key |
---|
73 | else: |
---|
74 | mylon=myfile[3] |
---|
75 | mylat=myfile[4] |
---|
76 | print(' Interpolation based on longitude %s ...' % mylon) |
---|
77 | print(' Interpolation based on latitude %s ...' % mylat) |
---|
78 | |
---|
79 | f2=open("namelist_new","w+") |
---|
80 | with open(namelist,"r") as f: |
---|
81 | for line in f: |
---|
82 | match = re.search('nemo_file',line) |
---|
83 | match2 = re.search('input_file',line) |
---|
84 | match3 = re.search('input_lon',line) |
---|
85 | match4 = re.search('input_lat',line) |
---|
86 | match5 = re.search('output_file',line) |
---|
87 | match6 = re.search('output_name',line) |
---|
88 | if match != None : |
---|
89 | line='nemo_file=''\''+listcfg[i]+'\''"\n" |
---|
90 | if match2 != None : |
---|
91 | line='input_file=''\''+myfilename+'\''"\n" |
---|
92 | if match3 != None : |
---|
93 | line='input_lon=''\''+mylon+'\''"\n" |
---|
94 | if match4 != None : |
---|
95 | line='input_lat=''\''+mylat+'\''"\n" |
---|
96 | if match5 != None : |
---|
97 | line='output_file=''\''+wfile+'\''"\n" |
---|
98 | # if match5 != None : |
---|
99 | ## line='output_name=''\''+myvar+'\''"\n" |
---|
100 | f2.write(line) |
---|
101 | f.close() |
---|
102 | f2.close() |
---|
103 | mycheck=subprocess.check_output('./scripgrid.exe namelist_new',shell=True ) |
---|
104 | mycheck=subprocess.check_output('./scrip.exe namelist_new',shell=True ) |
---|
105 | mycheck=subprocess.check_output('./scripshape.exe namelist_new',shell=True ) |
---|
106 | print(' Success ...') |
---|
107 | print(' => weight file is %s' % wfile) |
---|
108 | |
---|
109 | print() |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|