get_orca_corners.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import sys
  2. import numpy as np
  3. from netCDF4 import Dataset
  4. import os
  5. import time
  6. org_grid = '/afast/pelletie/orca025/eORCA025-SO_BMbathy_surf.nc'
  7. dest_grid = '/afast/pelletie/orca025/eORCA025-SO_corners_new.nc'
  8. data_org = Dataset(org_grid, mode='r')
  9. glamt = data_org.variables['glamt'][:]
  10. gphit = data_org.variables['gphit'][:]
  11. glamu = data_org.variables['glamu'][:]
  12. gphiu = data_org.variables['gphiu'][:]
  13. glamv = data_org.variables['glamv'][:]
  14. gphiv = data_org.variables['gphiv'][:]
  15. glamf = data_org.variables['glamf'][:]
  16. gphif = data_org.variables['gphif'][:]
  17. mask = data_org.variables['tmask'][:]
  18. data_org.close()
  19. ny, nx = np.shape(glamt)
  20. out_nc = Dataset(dest_grid, mode='w')
  21. out_nc.createDimension('y', ny)
  22. out_nc.createDimension('x', nx)
  23. tmp = out_nc.createVariable(varname='lon', datatype='f8', dimensions=['y', 'x'])
  24. tmp[:] = glamt[:]
  25. tmp.standard_name = "longitude"
  26. tmp.units = "degrees East"
  27. tmp.bounds = "lon_corn"
  28. tmp = out_nc.createVariable(varname='lat', datatype='f8', dimensions=['y', 'x'])
  29. tmp[:] = gphit[:]
  30. tmp.standard_name = "latitude"
  31. tmp.units = "degrees North"
  32. tmp.bounds = "lat_corn"
  33. tmp = out_nc.createVariable(varname='mask', datatype='b', dimensions=['y', 'x'])
  34. tmp[:] = mask[:]
  35. tmp.coordinates = "lat lon"
  36. out_nc.createDimension('corners', 4)
  37. corn_tmp = np.ndarray(shape=[ny+1,nx+1], dtype=float)
  38. corn_tmp[1:, 1:] = glamf
  39. corn_tmp[0,1:] = 2*glamu[0,:] - glamf[0,:]
  40. corn_tmp[:,0] = corn_tmp[:,-3] #- 360.
  41. lon_c = out_nc.createVariable(varname='lon_corn', datatype='f8', dimensions=['y', 'x', 'corners'])
  42. lon_c[:,:,0] = corn_tmp[:-1,:-1]
  43. lon_c[:,:,1] = corn_tmp[1:,:-1]
  44. lon_c[:,:,2] = corn_tmp[:-1,1:]
  45. lon_c[:,:,3] = corn_tmp[1:,1:]
  46. res = .25
  47. corr_lonc = np.logical_or(np.abs(lon_c[:,:,0] - lon_c[:,:,2]) < res/100.,
  48. np.abs(lon_c[:,:,1] - lon_c[:,:,3]) < res/100.)
  49. lon_c[:,:,0] = np.where(corr_lonc,\
  50. glamt[:] - .5 * res, lon_c[:,:,0])
  51. lon_c[:,:,1] = np.where(corr_lonc,\
  52. glamt[:] - .5 * res, lon_c[:,:,1])
  53. lon_c[:,:,2] = np.where(corr_lonc,\
  54. glamt[:] + .5 * res, lon_c[:,:,2])
  55. lon_c[:,:,3] = np.where(corr_lonc,\
  56. glamt[:] + .5 * res, lon_c[:,:,3])
  57. del lon_c
  58. corn_tmp[1:, 1:] = gphif
  59. corn_tmp[0,1:] = 2*gphiu[0,:] - gphif[0,:]
  60. corn_tmp[:,0] = corn_tmp[:,-1]
  61. lat_c = out_nc.createVariable(varname='lat_corn', datatype='f8', dimensions=['y', 'x', 'corners'])
  62. lat_c[:,:,0] = corn_tmp[:-1,:-1]
  63. lat_c[:,:,1] = corn_tmp[1:,:-1]
  64. lat_c[:,:,2] = corn_tmp[:-1,1:]
  65. lat_c[:,:,3] = corn_tmp[1:,1:]
  66. corr_latc = np.logical_or(np.abs(lat_c[:,:,0] - lat_c[:,:,1]) < res/100.,
  67. np.abs(lat_c[:,:,2] - lat_c[:,:,3]) < res/100.)
  68. lat_c[:,:,0] = np.where(corr_latc,\
  69. gphit[:] - .5 * res, lat_c[:,:,0])
  70. lat_c[:,:,1] = np.where(corr_latc,\
  71. gphit[:] + .5 * res, lat_c[:,:,1])
  72. lat_c[:,:,2] = np.where(corr_latc,\
  73. gphit[:] - .5 * res, lat_c[:,:,2])
  74. lat_c[:,:,3] = np.where(corr_latc,\
  75. gphit[:] + .5 * res, lat_c[:,:,3])
  76. out_nc.close()