orca025_create_basin_mask_from_meshmask.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # Petteri Uotila, 2015
  3. import sys
  4. import numpy as nmp
  5. from netCDF4 import Dataset
  6. from string import replace
  7. if len(sys.argv) != 2:
  8. print 'Usage: '+sys.argv[0]+' <mesh_mask_ORCA025_file.nc>'
  9. sys.exit(0)
  10. cf_mm = sys.argv[1]
  11. cf_out = replace(cf_mm, 'mesh_mask', 'basin_mask')
  12. print '\n'
  13. # Opening the Netcdf file:
  14. f_mm = Dataset(cf_mm)
  15. print 'File ', cf_mm, 'is open...\n'
  16. # Extracting the longitude 2D array:
  17. xlon = f_mm.variables['nav_lon'][:,:]
  18. # Extracting the longitude 2D array:
  19. xlat = f_mm.variables['nav_lat'][:,:]
  20. # Extracting tmask at surface level:
  21. tmask = f_mm.variables['tmask'][0,0,:,:]
  22. f_mm.close()
  23. # Info on the shape of t:
  24. [ nj, ni ] = tmask.shape
  25. print 'Dimension = ', ni, nj, '\n'
  26. mask_atl = nmp.zeros((nj,ni))
  27. mask_pac = nmp.zeros((nj,ni))
  28. mask_ind = nmp.zeros((nj,ni))
  29. mask_soc = nmp.zeros((nj,ni))
  30. f_mo025 = Dataset('new_maskglo_ORCA025.nc')
  31. # ATL for ORCA025
  32. # ~~~~~~~~~~~~~
  33. mask_atl[:,:] = f_mo025.variables['tmaskatl'][:]
  34. mask_atl[835:,:] = tmask[835:,:]
  35. # PAC for ORCA1
  36. # ~~~~~~~~~~~~~
  37. mask_pac[:,:] = f_mo025.variables['tmaskpac'][:]
  38. mask_pac[:357,:] = 0
  39. mask_pac[355:376,225:266] = 0 # mask the Great Australian Bight
  40. # IND for ORCA1
  41. # ~~~~~~~~~~~~~
  42. mask_ind[:,:] = f_mo025.variables['tmaskind'][:]
  43. # Indo-Pacific
  44. # ~~~~~~~~~~~~
  45. idx = nmp.where((mask_pac==1)|(mask_ind==1))
  46. mask_inp[idx] = 1
  47. # Southern Ocean
  48. mask_soc[:,:] = f_mo025.variables['tmaskant'][:]
  49. mask_soc[255:357,:] = tmask[255:357,:]
  50. f_mo025.close()
  51. # Creating output file:
  52. f_out = Dataset(cf_out, 'w',format='NETCDF3_CLASSIC')
  53. # Dimensions:
  54. f_out.createDimension('x', ni)
  55. f_out.createDimension('y', nj)
  56. # Variables
  57. id_lon = f_out.createVariable('nav_lon','f4',('y','x',))
  58. id_lat = f_out.createVariable('nav_lat','f4',('y','x',))
  59. id_atl = f_out.createVariable('tmaskatl' ,'f4',('y','x',)) ; id_atl.long_name = 'Atlantic Basin'
  60. id_pac = f_out.createVariable('tmaskpac' ,'f4',('y','x',)) ; id_pac.long_name = 'Pacific Basin'
  61. id_ind = f_out.createVariable('tmaskind' ,'f4',('y','x',)) ; id_ind.long_name = 'Indian Basin'
  62. id_soc = f_out.createVariable('tmasksoc' ,'f4',('y','x',)) ; id_soc.long_name = 'Southern Basin'
  63. id_inp = f_out.createVariable('tmaskinp' ,'f4',('y','x',)) ; id_inp.long_name = 'Indo-Pacific Basin'
  64. # Filling variables:
  65. id_lat[:,:] = xlat[:,:]
  66. id_lon[:,:] = xlon[:,:]
  67. id_atl[:,:] = mask_atl[:,:]
  68. id_pac[:,:] = mask_pac[:,:]
  69. id_ind[:,:] = mask_ind[:,:]
  70. id_soc[:,:] = mask_soc[:,:]
  71. id_inp[:,:] = mask_inp[:,:]
  72. f_out.About = 'ORCA1 main oceanic basin land-sea mask created from '+cf_mm
  73. f_out.Author = ' Generated with "orca025_create_basin_mask_from_meshmask.py" of BaraKuda (https://github.com/brodeau/barakuda)'
  74. f_out.close()
  75. print cf_out+' sucessfully created!'