Guest User

Untitled

a guest
Jun 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. # This example demonstrates how to regrid between a cubed sphere Grid and a Mesh.
  2. # The data files can be retrieved from the ESMF data repository by uncommenting the
  3. # following block of code:
  4. #
  5. # import os
  6. # DD = os.path.join(os.getcwd(), "examples/data")
  7. # if not os.path.isdir(DD):
  8. # os.makedirs(DD)
  9. # from ESMF.util.cache_data import cache_data_file
  10. # cache_data_file(os.path.join(DD, "mpas_uniform_10242_dual_counterclockwise.nc"))
  11.  
  12. import ESMF
  13. import numpy
  14.  
  15. import ESMF.util.helpers as helpers
  16. import ESMF.api.constants as constants
  17.  
  18. # This call enables debug logging
  19. # esmpy = ESMF.Manager(debug=True)
  20.  
  21. if ESMF.pet_count() != 6:
  22. print ("ESMPy cubed sphere Grid Mesh Regridding Example requires 6 processors")
  23. import sys; sys.exit(0)
  24.  
  25. grid1 = "examples/data/ll1deg_grid.nc"
  26.  
  27. # Create a cubed sphere grid with 45 elements per tile
  28. srcgrid = ESMF.Grid(tilesize=20, name="cubed_sphere")
  29.  
  30. # create an regular lat lon grid from file
  31. dstgrid = ESMF.Grid(filename=grid1, filetype=ESMF.FileFormat.SCRIP)
  32.  
  33. # create a field on the center stagger locations of the source grid
  34. srcfield = ESMF.Field(srcgrid, name='srcfield', staggerloc=ESMF.StaggerLoc.CENTER)
  35. srcfracfield = ESMF.Field(srcgrid, name='srcfracfield', staggerloc=ESMF.StaggerLoc.CENTER)
  36.  
  37. # create a field on the nodes of the destination mesh
  38. dstfield = ESMF.Field(dstgrid, name='dstfield', staggerloc=ESMF.StaggerLoc.CENTER)
  39. xctfield = ESMF.Field(dstgrid, name='xctfield', staggerloc=ESMF.StaggerLoc.CENTER)
  40. dstfracfield = ESMF.Field(dstgrid, name='dstfracfield', staggerloc=ESMF.StaggerLoc.CENTER)
  41.  
  42. # initialize the fields
  43. [lon,lat] = [0, 1]
  44.  
  45. deg2rad = 3.14/180.
  46.  
  47. gridLon = srcfield.grid.get_coords(lon, ESMF.StaggerLoc.CENTER)
  48. gridLat = srcfield.grid.get_coords(lat, ESMF.StaggerLoc.CENTER)
  49.  
  50. x = numpy.cos(numpy.radians(gridLon))*numpy.sin(numpy.radians(90-gridLat))
  51. y = numpy.sin(numpy.radians(gridLon))*numpy.sin(numpy.radians(90-gridLat))
  52. z = numpy.cos(numpy.radians(90-gridLat))
  53.  
  54. srcfield.data[...] = 200.0 + x + y + z
  55.  
  56. gridLon = xctfield.grid.get_coords(lon, ESMF.StaggerLoc.CENTER)
  57. gridLat = xctfield.grid.get_coords(lat, ESMF.StaggerLoc.CENTER)
  58.  
  59. x = numpy.cos(numpy.radians(gridLon))*numpy.sin(numpy.radians(90-gridLat))
  60. y = numpy.sin(numpy.radians(gridLon))*numpy.sin(numpy.radians(90-gridLat))
  61. z = numpy.cos(numpy.radians(90-gridLat))
  62.  
  63. xctfield.data[...] = 200.0 + x + y + z
  64.  
  65. dstfield.data[...] = 1e20
  66.  
  67. # create an object to regrid data from the source to the destination field
  68. regrid = ESMF.Regrid(srcfield, dstfield,
  69. regrid_method=ESMF.RegridMethod.BILINEAR,
  70. unmapped_action=ESMF.UnmappedAction.ERROR)
  71.  
  72. # do the regridding from source to destination field
  73. dstfield = regrid(srcfield, dstfield)
  74.  
  75. # compute the mean relative error
  76. from operator import mul
  77. num_nodes = numpy.prod(xctfield.data.shape[:])
  78. relerr = 0
  79. maxrelerr = 0
  80. meanrelerr = 0
  81. if num_nodes is not 0:
  82. # ind = numpy.where((dstfield.data != 1e20) & (xctfield.data != 0))[0]
  83. relerr = numpy.sum(numpy.abs(dstfield.data - xctfield.data) / numpy.abs(xctfield.data))
  84. maxrelerr = numpy.max(numpy.abs(dstfield.data - xctfield.data) / numpy.abs(xctfield.data))
  85.  
  86. # handle the parallel case
  87. if ESMF.pet_count() > 1:
  88. relerr = helpers.reduce_val(relerr, op=constants.Reduce.SUM)
  89. maxrelerr = helpers.reduce_val(maxrelerr, op=constants.Reduce.MAX)
  90. num_nodes = helpers.reduce_val(num_nodes, op=constants.Reduce.SUM)
  91.  
  92. # output the results from one processor only
  93. if ESMF.local_pet() is 0:
  94. meanrelerr = relerr / num_nodes
  95. print ("ESMPy cubed sphere Grid Mesh Regridding Example")
  96. print (" interpolation mean relative error = {0}".format(meanrelerr))
  97. print (" interpolation max relative (pointwise) error = {0}".format(maxrelerr))
  98.  
  99. # assert (meanrelerr < 3e-3)
  100. # assert (maxrelerr < 4e-4)
Add Comment
Please, Sign In to add comment