convert_ps_to_SA.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. # L. Brodeau, 2015
  3. # Practical salinity to absolute salinity (TEOS 10)
  4. import sys
  5. import os
  6. import numpy as nmp
  7. from netCDF4 import Dataset
  8. from string import replace
  9. import gsw
  10. #l_accurate = True
  11. l_accurate = False
  12. SSO = 35.16504
  13. if len(sys.argv) < 3:
  14. print 'Usage: '+sys.argv[0]+' <Salinity_file_to_convert> <salinity_name> (2d)'
  15. sys.exit(0)
  16. cf_sal = sys.argv[1]
  17. cv_sal = sys.argv[2]
  18. l2d = False
  19. if len(sys.argv) == 4:
  20. cv_2d = sys.argv[3]
  21. if cv_2d != '2d': print 'Usage: '+sys.argv[0]+' <Salinity_file_to_convert> <salinity_name> (2d)'
  22. l2d=True
  23. cf_out = replace(cf_sal, cf_sal, 'absolute_salinity_'+cf_sal)
  24. os.system('rm -f '+cf_out)
  25. os.system('cp '+cf_sal+' '+cf_out)
  26. print '\n'
  27. # Opening the Netcdf file:
  28. f_out = Dataset(cf_out, 'r+') # r+ => can read and write in the file... )
  29. print 'File ', cf_out, 'is open...\n'
  30. # Extracting tmask at surface level:
  31. if l2d:
  32. xsal = f_out.variables[cv_sal][:,:,:]
  33. else:
  34. xsal = f_out.variables[cv_sal][:,:,:,:]
  35. if l_accurate and not l2d:
  36. vz = f_out.variables['olevel'][:]
  37. [nt,nk,nj,ni] = nmp.shape(xsal)
  38. xdepth = nmp.zeros((nk,nj,ni))
  39. # building 3d arrays of depth, lon and lat:
  40. for jk in range(nk): xdepth[jk,:,:] = vz[jk]
  41. # pressure should be in dbar and it's the same as the depth in metre actually:
  42. for jt in range(nt):
  43. print ' jt =', jt
  44. f_out.variables[cv_sal][jt,:,:,:] = gsw.SA_from_SP(xsal[jt,:,:,:], xdepth, -140., 0.)
  45. else:
  46. # Fabien says it's enough:
  47. if l2d:
  48. f_out.variables[cv_sal][:,:,:] = xsal[:,:,:]*SSO/35.
  49. else:
  50. f_out.variables[cv_sal][:,:,:,:] = xsal[:,:,:,:]*SSO/35.
  51. f_out.variables[cv_sal].long_name = 'Absolute Salinity (TEOS10) build from practical salinity (*35.16504/35)'
  52. f_out.close()
  53. print cf_out+' sucessfully created!'