field_to_mask.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. import string
  8. import os
  9. from netCDF4 import Dataset
  10. #l_fake_coor = True
  11. #l_fake_coor = False
  12. l_use_fillval = True
  13. narg = len(sys.argv)
  14. if narg not in [3,4]:
  15. print 'Usage: '+sys.argv[0]+' <netcdf_file.nc> <2D or 3D netCDF field> (<value>)'
  16. print ' => if no <value> is specified: the "_FillValue" attribute is used!\n'
  17. sys.exit(0)
  18. cf_nc = sys.argv[1]
  19. cv_nc = sys.argv[2]
  20. if narg == 4:
  21. l_use_fillval = False
  22. rfill_val = float(sys.argv[3])
  23. cfname, cncext = os.path.splitext(cf_nc)
  24. #cf_msk = 'lsm_'+string.replace(os.path.basename(cf_nc), cv_nc, 'mask')
  25. cf_msk = 'mask.nc'
  26. print ' *** Will create mask '+cf_msk
  27. # Reading data array:
  28. f_nc = Dataset(cf_nc)
  29. ndim = len(f_nc.variables[cv_nc].dimensions)
  30. #
  31. if l_use_fillval:
  32. list_att_var = f_nc.variables[cv_nc].ncattrs()
  33. if '_FillValue' in list_att_var:
  34. rfill_val = f_nc.variables[cv_nc]._FillValue
  35. elif 'missing_value' in list_att_var:
  36. rfill_val = f_nc.variables[cv_nc].missing_value
  37. else:
  38. print 'ERROR: found neither "_FillValue" nor "missing_value" attribute for variable '+cv_nc+' !'; sys.exit(0)
  39. #
  40. print '\n *** Field value to use to generate mask: rfill_val =',rfill_val,'\n'
  41. #
  42. # Looking at the dimmensions of the variable:
  43. list_dim_var = f_nc.dimensions.keys()
  44. print 'list_dim_var = ', list_dim_var
  45. # Check if one is unlimited:
  46. inu = 0
  47. for cd in list_dim_var:
  48. if f_nc.dimensions[cd].isunlimited(): inu = inu + 1
  49. if inu > 1:
  50. print 'PROBLEM: there are more than one UNLIMITED dimension in the file!'
  51. sys.exit(0)
  52. NbDim = 3
  53. #
  54. if ndim == 4:
  55. xfield = f_nc.variables[cv_nc][0,:,:,:] # 3D ! BAD!
  56. elif ndim == 3:
  57. if inu==1:
  58. xfield = f_nc.variables[cv_nc][0,:,:] ; # 2D !
  59. NbDim = 2
  60. else:
  61. xfield = f_nc.variables[cv_nc][:,:,:] ; # 3D !
  62. elif ndim == 2:
  63. if inu==0:
  64. xfield = f_nc.variables[cv_nc][:,:]
  65. NbDim = 2
  66. else:
  67. print 'PROBLEM: your field does not seem to be 3D!'
  68. else:
  69. print ' ERROR (mk_zonal_average.py) => weird shape for your mask array!'
  70. sys.exit(0)
  71. #xfield = f_nc.variables[cv_nc][:,:]
  72. f_nc.close()
  73. nz = -1
  74. if NbDim==3:
  75. (nz,ny,nx) = nmp.shape(xfield)
  76. print("nx, ny, nz =",nx,ny,nz)
  77. mask = nmp.zeros((nz,ny,nx))
  78. else:
  79. (ny,nx) = nmp.shape(xfield)
  80. print("nx, ny =",nx,ny)
  81. mask = nmp.zeros((ny,nx))
  82. if l_use_fillval:
  83. if rfill_val > 0:
  84. idd = nmp.where( xfield < rfill_val )
  85. else:
  86. idd = nmp.where( xfield > rfill_val )
  87. #
  88. else:
  89. idd = nmp.where( xfield != rfill_val )
  90. mask[idd]=1
  91. f_out = Dataset(cf_msk, 'w', format='NETCDF4')
  92. # Dimensions:
  93. cdim_x = 'x'
  94. cdim_y = 'y'
  95. cdim_z = 'z'
  96. f_out.createDimension(cdim_x, nx)
  97. f_out.createDimension(cdim_y, ny)
  98. if NbDim==3: f_out.createDimension(cdim_z, nz)
  99. #if l_fake_coor:
  100. # id_lon = f_out.createVariable('lon0','f4',(cdim_x,))
  101. # id_lat = f_out.createVariable('lat0','f4',(cdim_y,))
  102. # id_lon[:] = vlon[:]
  103. # id_lat[:] = vlat[:]
  104. if NbDim==3:
  105. id_msk = f_out.createVariable('mask','i1',(cdim_z,cdim_y,cdim_x,))
  106. id_msk[:,:,:] = mask[:,:,:]
  107. else:
  108. id_msk = f_out.createVariable('mask','i1',(cdim_y,cdim_x,))
  109. id_msk[:,:] = mask[:,:]
  110. id_msk.long_name = 'Land-Sea mask'
  111. f_out.About = 'Variable '+cv_nc+' converted to a mask...'
  112. f_out.Author = 'Generated with image_to_netcdf.py of BARAKUDA (https://github.com/brodeau/barakuda)'
  113. f_out.close()
  114. print cf_msk+' created!!!'