get_temp_sublayers_lucy.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import numpy as np
  2. from netCDF4 import Dataset
  3. import os
  4. import sys
  5. # path to input 4D file
  6. input_file = "/afast/pelletie/nemo_outputs/so025_noisf/pped/so025_noisf_1m_tempsal_1999.nc"
  7. # path to output file
  8. out_file = "/afast/pelletie/nemo_outputs/so025_noisf/pped/so025_noisf_1m_tempsal_layers_1999.nc"
  9. # Upper and lower bounds of average ocean temperature layers.
  10. upper_bounds = [0., 50., 150., 300.]
  11. lower_bounds = [50., 150., 300., 500.]
  12. # path to 'mesh mask' file
  13. mesh_file = "/afast/pelletie/nemo_outputs/so025_noisf/mesh/mesh_mask.nc"
  14. # end input
  15. tmpdat = Dataset(mesh_file, mode='r')
  16. e3t = tmpdat.variables['e3t_0'][:]
  17. gdepth_3d = tmpdat.variables['gdept_0'][:]
  18. tmask = (tmpdat.variables['tmask'][:]).astype(bool)
  19. tmpdat.close()
  20. nz, ny, nx = np.shape(tmask)
  21. tmask = np.where((np.logical_not(tmask[0,:,:]))[None,:,:] * (np.ones(shape=[nz], dtype=bool))[:,None,None], \
  22. False, tmask)
  23. n_layers = np.size(upper_bounds)
  24. thick_intg = np.nan * np.ndarray(shape=[n_layers,nz, ny, nx], dtype=float)
  25. # thick_intg = np.zeros(shape=[n_layers,nz, ny, nx], dtype=float)
  26. upper_mesh = gdepth_3d - .5 * e3t
  27. lower_mesh = gdepth_3d + .5 * e3t
  28. for m in range(0,n_layers):
  29. fillidx = np.nonzero(np.logical_and( upper_bounds[m] <= upper_mesh, lower_bounds[m] >= lower_mesh))
  30. (thick_intg[m,:,:,:])[fillidx] = e3t[fillidx]
  31. fillidx = np.nonzero(np.logical_or( upper_bounds[m] > lower_mesh, lower_bounds[m] < upper_mesh))
  32. (thick_intg[m,:,:,:])[fillidx] = 0.
  33. fillidx = np.nonzero(np.logical_and( upper_bounds[m] > upper_mesh, upper_bounds[m] <= lower_mesh))
  34. (thick_intg[m,:,:,:])[fillidx] = (lower_mesh - upper_bounds[m])[fillidx]
  35. fillidx = np.nonzero(np.logical_and( lower_bounds[m] < lower_mesh, lower_bounds[m] >= upper_mesh))
  36. (thick_intg[m,:,:,:])[fillidx] = (lower_bounds[m] - upper_mesh)[fillidx]
  37. fillidx = np.nonzero(np.logical_not(tmask))
  38. (thick_intg[m,:,:,:])[fillidx] = 0.
  39. # print('nan count', np.count_nonzero(np.isnan(thick_intg[m,:,:,:])), nz*ny*nx)
  40. # print(m,thick_intg[m,:,300,500], upper_mesh[:,300,500], lower_mesh[:,300,500], \
  41. # tmask[:,300,500])
  42. # sys.exit()
  43. del upper_mesh,lower_mesh,e3t, gdepth_3d
  44. fillval = -1.e20
  45. tmpdat = Dataset(input_file, mode='r')
  46. in_temp = tmpdat.variables['thetao'][:]
  47. in_sal = tmpdat.variables['so'][:]
  48. lat = tmpdat.variables['nav_lat'][:]
  49. lon = tmpdat.variables['nav_lon'][:]
  50. depth_bounds = tmpdat.variables['deptht_bounds'][:]
  51. depth = tmpdat.variables['deptht'][:]
  52. out_nc = Dataset(out_file, mode='w')
  53. out_nc.createDimension('y', ny)
  54. out_nc.createDimension('x', nx)
  55. out_nc.createDimension('depth', n_layers)
  56. out_nc.createDimension('time_counter', 0)
  57. out_nc.createDimension('axis_nbounds', 2)
  58. tmp = out_nc.createVariable(varname='time_counter', datatype='f8', dimensions=['time_counter'])
  59. tmp[:] = tmpdat.variables['time_counter'][:]
  60. nt = np.size(tmp[:])
  61. tmp.setncatts( tmpdat.variables['time_counter'].__dict__)
  62. tmp = out_nc.createVariable(varname='time_counter_bounds', datatype='f8', dimensions=['time_counter', 'axis_nbounds'])
  63. tmp[:] = tmpdat.variables['time_counter_bounds'][:]
  64. tmp.setncatts( tmpdat.variables['time_counter_bounds'].__dict__)
  65. tmp = out_nc.createVariable(varname='nav_lat', datatype='f8', dimensions=['y', 'x'])
  66. tmp[:] = tmpdat.variables['nav_lat'][:]
  67. tmp.setncatts( tmpdat.variables['nav_lat'].__dict__)
  68. tmp = out_nc.createVariable(varname='nav_lon', datatype='f8', dimensions=['y', 'x'])
  69. tmp[:] = tmpdat.variables['nav_lon'][:]
  70. tmp.setncatts( tmpdat.variables['nav_lon'].__dict__)
  71. tmp = out_nc.createVariable(varname='depth', datatype='f8', dimensions=['depth'])
  72. tmp[:] = np.mean(np.vstack((upper_bounds, lower_bounds)), axis=0)
  73. tmp.standard_name = "depth"
  74. tmp.units="m"
  75. tmp.axis="Z"
  76. tmp.positive="down"
  77. tmp.bounds="depth_bounds"
  78. tmp = out_nc.createVariable(varname='depth_bounds', datatype='f8', dimensions=['depth', 'axis_nbounds'])
  79. tmp[:,0] = upper_bounds
  80. tmp[:,1] = lower_bounds
  81. out_temp = out_nc.createVariable(varname='thetao', datatype='f4', dimensions=['time_counter', 'depth', 'y', 'x'], fill_value=fillval)
  82. out_temp.standard_name = "conservative_temperature_mean_on_sublayers"
  83. out_temp.units = "degC"
  84. out_temp.coordinates = "time_counter depth nav_lat nav_lon"
  85. out_sal = out_nc.createVariable(varname='so', datatype='f4', dimensions=['time_counter', 'depth', 'y', 'x'], fill_value=fillval)
  86. out_sal.standard_name = "absolute_salinity_mean_on_sublayers"
  87. out_sal.units = "g/kg"
  88. out_sal.coordinates = "time_counter depth nav_lat nav_lon"
  89. for m in range(0,n_layers):
  90. out_temp[:,m,:,:] = np.where( (np.all(thick_intg[m,:,:,:] == 0., axis=0))[None,:,:] * (np.ones(shape=[nt], dtype=bool))[:,None,None], \
  91. fillval, \
  92. np.sum(in_temp * (thick_intg[m,:,:,:])[None,:,:,:] * (np.ones(shape=[nt], dtype=float))[:,None,None,None], axis=1) / np.sum((thick_intg[m,:,:,:])[None,:,:,:] * (np.ones(shape=[nt], dtype=float))[:,None,None,None], axis=1))
  93. out_sal[:,m,:,:] = np.where( (np.all(thick_intg[m,:,:,:] == 0., axis=0))[None,:,:] * (np.ones(shape=[nt], dtype=bool))[:,None,None], \
  94. fillval, \
  95. np.sum(in_sal * (thick_intg[m,:,:,:])[None,:,:,:] * (np.ones(shape=[nt], dtype=float))[:,None,None,None], axis=1) / np.sum((thick_intg[m,:,:,:])[None,:,:,:] * (np.ones(shape=[nt], dtype=float))[:,None,None,None], axis=1))
  96. # print('dbg thick_intg', m, thick_intg[m,:,301,490])
  97. # print('dbg out temp', m, out_temp[m,:,301,490])
  98. # out_temp[:,m,:,:] = np.sum( (thick_intg[m,:,:,:])[None,:,:,:] * in_temp, axis=1)
  99. # print('isnan out', np.count_nonzero(np.isnan(out_temp[0,m,:,:])))
  100. # out_nc.createDimension('z', nz)
  101. # tmp = out_nc.createVariable(varname='thick_intg', datatype='f8', dimensions=['layer', 'z', 'y', 'x'])
  102. # tmp[:] = thick_intg[:]
  103. tmpdat.close()
  104. out_nc.close()