convert_pt_to_CT.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # L. Brodeau, 2015
  3. # Potential temperature to conservative temperature (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. if len(sys.argv) != 5:
  12. print 'Usage: '+sys.argv[0]+' <Temperature_file_to_convert> <temperature_name> <Absolute_salinity_file> <salinity_name>'
  13. sys.exit(0)
  14. cf_temp = sys.argv[1]
  15. cv_temp = sys.argv[2]
  16. cf_sal = sys.argv[3]
  17. cv_sal = sys.argv[4]
  18. cf_out = replace(cf_temp, cf_temp, 'conservative_temperature_'+cf_temp)
  19. os.system('rm -f '+cf_out)
  20. os.system('cp '+cf_temp+' '+cf_out)
  21. print '\n'
  22. f_sal = Dataset(cf_sal) # r+ => can read and write in the file... )
  23. xsal = f_sal.variables[cv_sal][:,:,:,:]
  24. f_sal.close()
  25. print '\n'
  26. # Opening the Netcdf file:
  27. f_out = Dataset(cf_out, 'r+') # r+ => can read and write in the file... )
  28. print 'File ', cf_out, 'is open...\n'
  29. # Extracting tmask at surface level:
  30. xtemp = f_out.variables[cv_temp][:,:,:,:]
  31. #xtemp[:,:,:,:] = xtemp[:,:,:,:]*2.
  32. #gsw.CT_from_pt(SA, pt)
  33. f_out.variables[cv_temp][:,:,:,:] = gsw.CT_from_pt(xsal, xtemp)
  34. f_out.variables[cv_temp].long_name = 'Conservative Temperature (TEOS10) built from potential temperature'
  35. f_out.close()
  36. print cf_out+' sucessfully created!'