density_from_T_and_S.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. # L. Brodeau, 2017
  3. # Potential sigma0 density from potential temperature and salinity using
  4. # function sigma0 (TEOS8) of barakuda_physics.py
  5. import sys
  6. #import os
  7. import numpy as nmp
  8. from netCDF4 import Dataset
  9. from string import replace
  10. import barakuda_physics as bp
  11. print '\n'
  12. if len(sys.argv) != 4:
  13. print 'Usage: '+sys.argv[0]+' <NEMO grid_T file> <temperature_name> <salinity_name>'
  14. sys.exit(0)
  15. cf_nemo_T = sys.argv[1]
  16. cv_T = sys.argv[2]
  17. cv_S = sys.argv[3]
  18. cf_out = replace(cf_nemo_T, cf_nemo_T, 'sigma0_'+cf_nemo_T)
  19. f_nemo_T = Dataset(cf_nemo_T) # r+ => can read and write in the file... )
  20. nb_dim = len(f_nemo_T.variables[cv_T].dimensions)
  21. if not nb_dim in [ 3 , 4 ]:
  22. print ' ERROR: we expect T and S to be 2D+T or 3D+T (3 or 4 dimensions!)'
  23. sys.exit(0)
  24. vtime = f_nemo_T.variables['time_counter'][:] ; cu_time = f_nemo_T.variables['time_counter'].units
  25. nav_lon = f_nemo_T.variables['nav_lon'][:,:] ; cu_lon = f_nemo_T.variables['nav_lon'].units
  26. nav_lat = f_nemo_T.variables['nav_lat'][:,:] ; cu_lat = f_nemo_T.variables['nav_lat'].units
  27. if nb_dim == 4:
  28. deptht = f_nemo_T.variables['deptht'][:] ; cu_dpt = f_nemo_T.variables['deptht'].units
  29. Nt = len(vtime)
  30. for jt in range(Nt):
  31. print ' *** time record:', jt+1
  32. if nb_dim == 4:
  33. xtht = f_nemo_T.variables[cv_T][jt,:,:,:]
  34. xsal = f_nemo_T.variables[cv_S][jt,:,:,:]
  35. if nb_dim == 3:
  36. xtht = f_nemo_T.variables[cv_T][jt,:,:]
  37. xsal = f_nemo_T.variables[cv_S][jt,:,:]
  38. if jt == 0:
  39. if nb_dim == 4:
  40. (nk,nj,ni) = nmp.shape(xtht)
  41. xsg0 = nmp.zeros((nk,nj,ni))
  42. if nb_dim == 3:
  43. (nj,ni) = nmp.shape(xtht)
  44. xsg0 = nmp.zeros((nj,ni))
  45. xsg0 = bp.sigma0(xtht, xsal) ; # computing Sigma0 at current time record !
  46. if jt == 0:
  47. f_out = Dataset(cf_out, 'w', format='NETCDF3_CLASSIC')
  48. # Dimensions:
  49. f_out.createDimension('x', ni)
  50. f_out.createDimension('y', nj)
  51. if nb_dim == 4: f_out.createDimension('deptht', nk)
  52. f_out.createDimension('time_counter', None)
  53. id_lon = f_out.createVariable('nav_lon','f4',('y','x',)) ; id_lon.units = cu_lon
  54. id_lat = f_out.createVariable('nav_lat','f4',('y','x',)) ; id_lat.units = cu_lat
  55. if nb_dim == 4: id_dpt = f_out.createVariable('deptht' ,'f4',('deptht',)) ; id_dpt.units = cu_dpt
  56. id_tim = f_out.createVariable('time_counter' ,'f4',('time_counter',)) ; id_tim.units = cu_time
  57. id_lon[:,:] = nav_lon[:,:]
  58. id_lat[:,:] = nav_lat[:,:]
  59. if nb_dim == 4: id_dpt[:] = deptht[:]
  60. if nb_dim == 4: id_sg0 = f_out.createVariable('sigma0','f4',('time_counter','deptht','y','x',))
  61. if nb_dim == 3: id_sg0 = f_out.createVariable('sigma0','f4',('time_counter', 'y','x',))
  62. id_sg0.long_name = 'SIGMA0 density computed from '+cv_T+' and '+cv_S+' with TEOS8 / Jackett and McDougall (1994)'
  63. #f_out.About = 'Bla bla'
  64. f_out.Author = 'barakuda [density_from_T_and_S.py] (https://github.com/brodeau/barakuda)'
  65. id_tim[jt] = vtime[jt]
  66. if nb_dim == 4: id_sg0[jt,:,:,:] = xsg0[:,:,:]
  67. if nb_dim == 3: id_sg0[jt,:,:] = xsg0[:,:]
  68. f_out.close()
  69. f_nemo_T.close()
  70. print '\n'+cf_out+' sucessfully created!\n'