tiff_to_orca_mask.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # B a r a K u d a
  3. #
  4. # L. Brodeau, 2017]
  5. import sys
  6. import numpy as nmp
  7. from PIL import Image
  8. import string
  9. import os
  10. from netCDF4 import Dataset
  11. import datetime
  12. iconv = 1 ; # CDFTOOLS convention
  13. #iconv = 2 ; # NEMO convention
  14. narg = len(sys.argv)
  15. if narg != 2:
  16. print 'Usage: '+sys.argv[0]+' <mesh_mask>'; sys.exit(0)
  17. cf_mm = sys.argv[1]
  18. if iconv == 1:
  19. crout = 'tmask'
  20. vbasins = [ 'pac' , 'atl' , 'ind' , 'soc' , 'arc' , 'wed' , 'lab' , 'med' , 'nna' , 'gin' , 'bar' ]
  21. vbnames = [ 'Pacific' , 'Atlantic' , 'Indian' , 'Southern', 'Arctic' , 'Weddell' , 'Labrador' , 'Med' , 'N.Atl.', 'GIN' , 'Barents' ]
  22. vocesea = [ 'Ocean' , 'Ocean' , 'Ocean' , 'Ocean' , 'Ocean' , 'Sea' , 'Sea' , 'Sea' , 'north' , 'Seas', 'Sea' ]
  23. vmandat = [ True , True , True , False , False , False , False , False , False , False, False ] ; # Mandatory ?
  24. elif iconv == 2:
  25. crout = ''
  26. vbasins = [ 'glomsk', 'pacmsk', 'atlmsk' , 'indmsk', 'indpacmsk' ]
  27. vbnames = ['Global' , 'Pacific' , 'Atlantic' , 'Indian' , 'Indo-Pacific' ]
  28. vocesea = ['Ocean' , 'Ocean' , 'Ocean' , 'Ocean' , 'Ocean' ]
  29. vmandat = [ True , True , True , True , True ] ; # Mandatory ?
  30. else:
  31. print ' Booo!'; sys.exit(0)
  32. # Opening mesh_mask:
  33. f_mm = Dataset(cf_mm)
  34. nav_lon = f_mm.variables['nav_lon'][:,:]
  35. nav_lat = f_mm.variables['nav_lat'][:,:]
  36. mask = f_mm.variables['tmask'][0,0,:,:]
  37. f_mm.close()
  38. (nj,ni) = nmp.shape(nav_lon)
  39. cf_bm = string.replace(os.path.basename(cf_mm), 'mesh_', 'basin_')
  40. cf_bm = string.replace(os.path.basename(cf_bm), '.nc4', '.nc')
  41. nb_bas = len(vbasins)
  42. vtreat = nmp.zeros(nb_bas, dtype=bool)
  43. jb = 0 ; Nbt = 0
  44. for cb in vbasins:
  45. cf_tiff = 'mask_'+cb+'.tiff'
  46. if not os.path.exists(cf_tiff):
  47. if vmandat[jb]:
  48. print 'PROBLEM! image '+cf_tiff+' is not here!!!' ; sys.exit(0)
  49. else:
  50. print ' *** good, '+cf_tiff+' is here...'
  51. vtreat[jb] = True
  52. Nbt = Nbt + 1
  53. jb = jb+1
  54. #print vtreat
  55. XBASINS = nmp.zeros((Nbt,nj,ni))
  56. jb = 0 ; jbt = 0
  57. for cb in vbasins:
  58. if vtreat[jb]:
  59. cf_tiff = 'mask_'+cb+'.tiff'
  60. # Opening Images:
  61. pic = Image.open(cf_tiff)
  62. xtmp = nmp.flipud( nmp.array(pic) )
  63. (njb,nib) = xtmp.shape
  64. if (njb,nib) != (nj,ni):
  65. print 'ERROR: something is wrong with the shapes of 2D arrays with basin '+cb+':'
  66. print nj,ni
  67. print njb,nib
  68. sys.exit(0)
  69. xmsk = nmp.zeros((nj,ni))
  70. idx_sea = nmp.where(xtmp > 0)
  71. xmsk[idx_sea] = 1
  72. XBASINS[jbt,:,:] = xmsk[:,:]
  73. jbt = jbt + 1
  74. jb = jb + 1
  75. now = datetime.datetime.now()
  76. cdate = now.strftime("%Y-%m-%d")
  77. f_out = Dataset(cf_bm, 'w', format='NETCDF3_CLASSIC')
  78. # Dimensions:
  79. f_out.createDimension('x', ni)
  80. f_out.createDimension('y', nj)
  81. id_lon = f_out.createVariable('nav_lon' ,'f4',('y','x',))
  82. id_lat = f_out.createVariable('nav_lat' ,'f4',('y','x',))
  83. id_lon[:,:] = nav_lon[:,:]
  84. id_lat[:,:] = nav_lat[:,:]
  85. jb = 0 ; jbt = 0
  86. for cb in vbasins:
  87. if vtreat[jb]:
  88. #id_bas = f_out.createVariable(crout+cb,'i1',('y','x',))
  89. id_bas = f_out.createVariable(crout+cb,'f4',('y','x',))
  90. id_bas.long_name = vbnames[jb]+' '+vocesea[jb]+' basin'
  91. id_bas[:,:] = XBASINS[jbt,:,:]*mask[:,:]
  92. jbt = jbt + 1
  93. jb = jb + 1
  94. f_out.About = 'ORCA025, masks for main ocean basins, created with orca_mesh_mask_to_bitmap.py, Gimp, and tiff_to_orca_mask.py, '+cdate+'.'
  95. f_out.Author = 'L. Brodeau (https://github.com/brodeau/barakuda)'
  96. f_out.close()
  97. print cf_bm+' created!!!'