freeform.py 4.2 KB

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