convert_ps_to_SA.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. SSO = 35.16504
  11. cdepth = 'deptht'
  12. if len(sys.argv) != 3:
  13. print 'Usage: '+sys.argv[0]+' <Salinity_file_to_convert> <salinity_name>'
  14. sys.exit(0)
  15. cf_sal = sys.argv[1]
  16. cv_sal = sys.argv[2]
  17. cf_out = replace(cf_sal, cv_sal, cv_sal+'_TEOS10')
  18. os.system('rm -f '+cf_out)
  19. os.system('cp '+cf_sal+' '+cf_out)
  20. l_accurate = False
  21. print '\n'
  22. # Opening the Netcdf file:
  23. f_out = Dataset(cf_out, 'r+') # r+ => can read and write in the file... )
  24. print 'File ', cf_out, 'is open...\n'
  25. # Inquire variables in the file to see if a depth is there...
  26. list_var = f_out.variables.keys() ; print ' *** list_var =', list_var
  27. if cdepth in list_var: l_accurate = True
  28. vcdim = f_out.variables[cv_sal].dimensions
  29. cv_t = vcdim[0]; print ' *** record dimension is called "'+cv_t+'"'
  30. Nt = f_out.dimensions[cv_t].size ; print ' *** There are '+str(Nt)+' time records...\n'
  31. # Inquire the shape of arrays:
  32. nb_dim = len(vcdim)
  33. print ' *** '+cf_out+' has '+str(nb_dim)+' dimmensions!'
  34. if not nb_dim in [ 2, 3, 4 ]: print ' ERROR: unsupported number of dimmensions! =>', nb_dim ; sys.exit(0)
  35. for jt in range(Nt):
  36. print '\n --- treating record # '+str(jt)
  37. if nb_dim==4: xsal = f_out.variables[cv_sal][jt,:,:,:]
  38. if nb_dim==3: xsal = f_out.variables[cv_sal][jt,:,:]
  39. if nb_dim==2: xsal = f_out.variables[cv_sal][jt,:]
  40. if jt == 0: shp = nmp.shape(xsal)
  41. xtmp = nmp.zeros(shp)
  42. xtmp = xsal
  43. if l_accurate:
  44. if jt == 0:
  45. print '\n Using accurate method with depth !'
  46. vz = f_out.variables['deptht'][:]
  47. xdepth = nmp.zeros(shp)
  48. nk = f_out.dimensions['z'].size ; print ' *** There are '+str(nk)+' vertical levels...'
  49. # building 3d arrays of depth:
  50. for jk in range(nk):
  51. if nb_dim==4: xdepth[jk,:,:] = vz[jk]
  52. if nb_dim==3: xdepth[jk,:] = vz[jk]
  53. if nb_dim==2: xdepth[jk] = vz[jk]
  54. # pressure should be in dbar and it's the same as the depth in metre actually:
  55. if nb_dim==4: f_out.variables[cv_sal][jt,:,:,:] = gsw.SA_from_SP(xtmp[:,:,:], xdepth, -140., 0.)
  56. if nb_dim==3: f_out.variables[cv_sal][jt,:,:] = gsw.SA_from_SP(xtmp[:,:], xdepth, -140., 0.)
  57. if nb_dim==2: f_out.variables[cv_sal][jt,:] = gsw.SA_from_SP(xtmp[:], xdepth, -140., 0.)
  58. else:
  59. # Fabien says it's enough:
  60. if nb_dim==4: f_out.variables[cv_sal][jt,:,:,:] = xtmp[:,:,:]*SSO/35.
  61. if nb_dim==3: f_out.variables[cv_sal][jt,:,:] = xtmp[:,:]*SSO/35.
  62. if nb_dim==2: f_out.variables[cv_sal][jt,:] = xtmp[:]*SSO/35.
  63. f_out.variables[cv_sal].long_name = 'Absolute Salinity (TEOS10) build from practical salinity'
  64. f_out.close()
  65. print cf_out+' sucessfully created!'