orca_correct_runoff_depth.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # L. Brodeau, 2017
  3. import sys
  4. import os
  5. import numpy as nmp
  6. from netCDF4 import Dataset
  7. from string import replace
  8. cv_rnf_dept = 'rodepth'
  9. cv_bathy_m = 'Bathymetry'
  10. if len(sys.argv) != 4:
  11. print 'Usage: '+sys.argv[0]+' <runoff_depth.nc> <bathy_meter.nc> <min depth (m)>'
  12. sys.exit(0)
  13. cf_rnf_dept = sys.argv[1]
  14. cf_bathy_m = sys.argv[2]
  15. cmin_dept = sys.argv[3] ; rmin_depth = float(cmin_dept)
  16. cf_new = replace(cf_rnf_dept, '.nc', '_min'+cmin_dept+'.nc')
  17. os.system('rm -f '+cf_new)
  18. os.system('cp '+cf_rnf_dept+' '+cf_new)
  19. print '\n'
  20. f_bathy = Dataset(cf_bathy_m)
  21. xbathy = f_bathy.variables[cv_bathy_m][:,:]
  22. f_bathy.close()
  23. (Nj,Ni) = nmp.shape(xbathy)
  24. xnew = nmp.zeros((Nj,Ni))
  25. print '\n'
  26. # Opening the Netcdf file:
  27. f_new = Dataset(cf_new, 'r+') # r+ => can read and write in the file... )
  28. print 'File ', cf_new, 'is open...\n'
  29. # Extracting tmask at surface level:
  30. xtemp = f_new.variables[cv_rnf_dept][:,:,:]
  31. xnew[:,:] = xtemp[0,:,:]
  32. idx = nmp.where( (xbathy[:,:] >= rmin_depth) & (xnew[:,:] < rmin_depth) )
  33. #print idx
  34. xnew[idx] = rmin_depth
  35. # Updating:
  36. f_new.variables[cv_rnf_dept][0,:,:] = xnew[:,:]
  37. f_new.Author = 'L. Brodeau (orca_correct_runoff_depth.py of Barakuda)'
  38. f_new.close()
  39. print cf_new+' sucessfully created!'