1 | #!/usr/bin/env python2.6 |
---|
2 | |
---|
3 | import os |
---|
4 | import shutil |
---|
5 | |
---|
6 | def freer(oldname, newname, fdir, edits): |
---|
7 | |
---|
8 | fin = file(os.path.join(fdir,oldname)) |
---|
9 | lines = fin.readlines() |
---|
10 | fin.close() |
---|
11 | |
---|
12 | if not os.path.exists('src'): |
---|
13 | print "creating src directory" |
---|
14 | os.mkdir('src') |
---|
15 | fout = file(os.path.join('src',newname), 'w') |
---|
16 | |
---|
17 | lastline = None |
---|
18 | for nextline in lines: |
---|
19 | for edit in edits: |
---|
20 | nextline = nextline.replace(edit[0],edit[1]) |
---|
21 | if nextline.strip().startswith('&'): |
---|
22 | parts = lastline.split('!') |
---|
23 | if parts[0].endswith('\n'): |
---|
24 | parts[0] = parts[0].replace('\n',' &\n') |
---|
25 | else: |
---|
26 | parts[0] = parts[0] + ' & ' |
---|
27 | lastline = '!'.join(parts) |
---|
28 | nextline = nextline.replace('&',' ') |
---|
29 | if lastline is not None: |
---|
30 | fout.write(lastline) |
---|
31 | lastline = nextline |
---|
32 | |
---|
33 | # - write out last line (this assumes it is not a continuation line) |
---|
34 | fout.write(lastline) |
---|
35 | |
---|
36 | def scrip(adir, apairs, edits=[]): |
---|
37 | |
---|
38 | if not os.path.exists('src'): |
---|
39 | print "creating src directory" |
---|
40 | os.mkdir('src') |
---|
41 | |
---|
42 | for (f1,f2) in apairs: |
---|
43 | if not f2.endswith('.f90'): |
---|
44 | shutil.copy(os.path.join(adir,f1), os.path.join('src',f2)) |
---|
45 | else: |
---|
46 | freer(f1,f2,adir,edits) |
---|
47 | |
---|
48 | if __name__ == "__main__": |
---|
49 | |
---|
50 | # - changes to scrip routines made here |
---|
51 | |
---|
52 | pairs = [('constants.f', 'constants.f90'), |
---|
53 | ('copyright', 'copyright'), |
---|
54 | ('grids.f', 'grids.f90'), |
---|
55 | ('iounits.f', 'iounits.f90'), |
---|
56 | ('kinds_mod.f', 'kinds_mod.f90'), |
---|
57 | ('netcdf.f', 'netcdf_mod.f90'), |
---|
58 | ('remap.f', 'remap.f90'), |
---|
59 | ('remap_bicubic.f', 'remap_bicubic.f90'), |
---|
60 | ('remap_bilinear.f', 'remap_bilinear.f90'), |
---|
61 | ('remap_conserv.f', 'remap_conserv.f90'), |
---|
62 | ('remap_distwgt.f', 'remap_distwgt.f90'), |
---|
63 | ('remap_read.f', 'remap_read.f90'), |
---|
64 | ('remap_vars.f', 'remap_vars.f90'), |
---|
65 | ('remap_write.f', 'remap_write.f90'), |
---|
66 | ('timers.f', 'timers.f90')] |
---|
67 | |
---|
68 | # - add some edits |
---|
69 | # - note that this is very crude method since every line is inspected for the first string |
---|
70 | # - in every input file |
---|
71 | # - you have been warned! |
---|
72 | |
---|
73 | ed1 = [" .and."] |
---|
74 | ed2 = ["subroutine netcdf_error_handler(istat, mess)"] |
---|
75 | ed3 = [" & istat ! integer status returned by netCDF function call", |
---|
76 | " character (len=*), intent(in), optional :: mess"] |
---|
77 | ed4 = [" if (present(mess)) then", |
---|
78 | " print *,'Error in netCDF: ',nf_strerror(istat), 'Message: ',mess", |
---|
79 | " else", |
---|
80 | " print *,'Error in netCDF: ',nf_strerror(istat)", |
---|
81 | " endif"] |
---|
82 | |
---|
83 | edits = [(". and.",'\n'.join(ed1)), |
---|
84 | ("subroutine netcdf_error_handler(istat)", '\n'.join(ed2)), |
---|
85 | (" & istat ! integer status returned by netCDF function call", '\n'.join(ed3)), |
---|
86 | (" print *,'Error in netCDF: ',nf_strerror(istat)", '\n'.join(ed4)) |
---|
87 | ] |
---|
88 | scrip('SCRIP1.4/source', pairs, edits) |
---|
89 | |
---|
90 | |
---|
91 | # - on to NOCS routines |
---|
92 | |
---|
93 | pairs = [('scrip.F90', 'scrip.F90'), |
---|
94 | ('scripgrid.F90', 'scripgrid.F90'), |
---|
95 | ('scripgrid_mod.F90', 'scripgrid_mod.F90'), |
---|
96 | ('scripinterp.F90', 'scripinterp.F90'), |
---|
97 | ('scripinterp_mod.F90', 'scripinterp_mod.F90'), |
---|
98 | ('scripshape.F90', 'scripshape.F90')] |
---|
99 | scrip('nocsutil', pairs) |
---|
100 | |
---|
101 | changes = """ |
---|
102 | SCRIP code, version 1.4, from Los Alamos National Laboratory (http://climate.lanl.gov/Software/SCRIP) |
---|
103 | |
---|
104 | Changes made at NOCS for inclusion of weights generation code in NEMO 3.3 and later: |
---|
105 | |
---|
106 | - File extensions changed from '.f' to '.f90' |
---|
107 | - File netcdf.f renamed as netcdf_mod.f90 to avoid clash with netcdf library module filename |
---|
108 | - File netcdf.f modified to add error message to netcdf_error_handler |
---|
109 | - Small bug in remap_conserv when using gfortran compiler: replace ". and." with " .and." |
---|
110 | - continuation lines reformatted with '&' moved from the start of the continuation line to |
---|
111 | the end of the line before |
---|
112 | """ |
---|
113 | fp = file("src/CHANGES_BY_NOCS","w") |
---|
114 | fp.write(changes) |
---|
115 | fp.close() |
---|