image_to_netcdf.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  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 os
  9. from netCDF4 import Dataset
  10. import datetime
  11. #l_fake_coor = True
  12. l_fake_coor = False
  13. l_nemo_like = True
  14. narg = len(sys.argv)
  15. if not narg in [2, 3]:
  16. print('Usage: '+sys.argv[0]+' <image> (<field divider for field>)'); sys.exit(0)
  17. cf_im = sys.argv[1]
  18. idiv = 1
  19. if narg == 3: idiv = int(sys.argv[2])
  20. print(idiv)
  21. cfname, cfext = os.path.splitext(cf_im)
  22. #(nj,ni) = nmp.shape(nav_lon)
  23. cf_nc = str.replace(os.path.basename(cf_im), cfext, '.nc')
  24. # Opening Images:
  25. print(' *** Opening image '+cf_im)
  26. pic = Image.open(cf_im)
  27. lcolor = False ; # if false it is a
  28. vshape_pic = nmp.shape(pic)
  29. if len(vshape_pic) == 3:
  30. (ny,nx,nrgb) = vshape_pic
  31. if nrgb != 3: print(' Problem #1 with your image, not what we expected!') ; sys.exit(0)
  32. lcolor = True ; # RGB color picture => 3 2D array
  33. print("\n It's a RGB color picture!\n")
  34. elif len(vshape_pic) == 2:
  35. lcolor = False ; # grey-scale picture (true black and white) => 1 2D array
  36. (ny,nx) = vshape_pic
  37. nrgb = 1
  38. print("\n It's a grey-scale B&W picture!\n")
  39. else:
  40. print(' Problem #2 with your image, not what we expected!') ; sys.exit(0)
  41. print(" *** shape of pic: ", (ny,nx))
  42. xpic = nmp.array(pic)
  43. if l_fake_coor:
  44. # Prepare coordinates if needed:
  45. vlon = nmp.zeros(nx) ; dx = 360./float(nx)
  46. for ji in range(nx): vlon[ji] = (float(ji) + 0.5)*dx
  47. vlat = nmp.zeros(ny) ; dy = 180./float(ny)
  48. for jj in range(ny): vlat[jj] = -90 + (float(jj) + 0.5)*dy
  49. #print(vlat[:])
  50. #sys.exit(0)
  51. f_out = Dataset(cf_nc, 'w', format='NETCDF4')
  52. # Dimensions:
  53. cdim_x = 'longitude'
  54. cdim_y = 'latitude'
  55. if l_nemo_like:
  56. cdim_x = 'x'
  57. cdim_y = 'y'
  58. #if l_fake_coor:
  59. # cdim_x = 'lon'
  60. # cdim_y = 'lat'
  61. f_out.createDimension(cdim_x, nx)
  62. f_out.createDimension(cdim_y, ny)
  63. #if l_nemo_like: f_out.createDimension('t', None)
  64. if l_fake_coor:
  65. id_lon = f_out.createVariable('lon0','f4',(cdim_x,))
  66. id_lat = f_out.createVariable('lat0','f4',(cdim_y,))
  67. id_lon[:] = vlon[:]
  68. id_lat[:] = vlat[:]
  69. if lcolor:
  70. id_red = f_out.createVariable('red','f4',(cdim_y,cdim_x,))
  71. id_red.long_name = 'Red (of RGB)'
  72. id_green = f_out.createVariable('green','f4',(cdim_y,cdim_x,))
  73. id_green.long_name = 'Green (of RGB)'
  74. id_blue = f_out.createVariable('blue','f4',(cdim_y,cdim_x,))
  75. id_blue.long_name = 'Blue (of RGB)'
  76. id_red[:,:] = nmp.flipud(xpic[:,:,0])
  77. id_green[:,:] = nmp.flipud(xpic[:,:,1])
  78. id_blue[:,:] = nmp.flipud(xpic[:,:,2])
  79. else:
  80. #if l_nemo_like:
  81. # id_bw = f_out.createVariable('bw','i1',('t',cdim_y,cdim_x,))
  82. # id_bw.long_name = 'Grey scale'
  83. # #id_bw[0,:,:] = nmp.flipud(xpic[:,:]) / idiv
  84. # id_bw[0,:,:] = 1 - (nmp.flipud(xpic[:,:]) + 1)/idiv
  85. #else:
  86. id_bw = f_out.createVariable('bw','i1',(cdim_y,cdim_x,))
  87. id_bw.long_name = 'Grey scale'
  88. id_bw[:,:] = 1 - (nmp.flipud(xpic[:,:]) + 1)/idiv
  89. f_out.About = 'Image '+cf_im+' converted to netcdf.'
  90. f_out.Author = 'Generated with image_to_netcdf.py of BARAKUDA (https://github.com/brodeau/barakuda)'
  91. f_out.close()
  92. print(cf_nc+' created!!!')