Skip to content

Commit

Permalink
Merge pull request #57 from xylar/expand-scrip
Browse files Browse the repository at this point in the history
Add capability to expand corners of SCRIP grid
  • Loading branch information
xylar authored Oct 29, 2023
2 parents 76797c9 + b1e54d5 commit 4a7feca
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ci/recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "pyremap" %}
{% set version = "1.1.0" %}
{% set version = "1.2.0" %}

package:
name: {{ name|lower }}
Expand Down
3 changes: 3 additions & 0 deletions docs/versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Documentation On GitHub
`v1.0.0`_ `1.0.0`_
`v1.0.1`_ `1.0.1`_
`v1.1.0`_ `1.1.0`_
`v1.2.0`_ `1.2.0`_
================ ===============

.. _`stable`: ../stable/index.html
Expand All @@ -36,6 +37,7 @@ Documentation On GitHub
.. _`v1.0.0`: ../1.0.0/index.html
.. _`v1.0.1`: ../1.0.1/index.html
.. _`v1.1.0`: ../1.1.0/index.html
.. _`v1.2.0`: ../1.2.0/index.html
.. _`main`: https://github.com/MPAS-Dev/pyremap/tree/main
.. _`0.0.6`: https://github.com/MPAS-Dev/pyremap/tree/0.0.6
.. _`0.0.7`: https://github.com/MPAS-Dev/pyremap/tree/0.0.7
Expand All @@ -51,3 +53,4 @@ Documentation On GitHub
.. _`1.0.0`: https://github.com/MPAS-Dev/pyremap/tree/1.0.0
.. _`1.0.1`: https://github.com/MPAS-Dev/pyremap/tree/1.0.1
.. _`1.1.0`: https://github.com/MPAS-Dev/pyremap/tree/1.1.0
.. _`1.2.0`: https://github.com/MPAS-Dev/pyremap/tree/1.2.0
2 changes: 1 addition & 1 deletion pyremap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
from pyremap.polar import get_polar_descriptor, get_polar_descriptor_from_file
from pyremap.remapper import Remapper

__version_info__ = (1, 1, 0)
__version_info__ = (1, 2, 0)
__version__ = '.'.join(str(vi) for vi in __version_info__)
13 changes: 12 additions & 1 deletion pyremap/descriptor/lat_lon_2d_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pyremap.descriptor.utility import (
add_history,
create_scrip,
expand_scrip,
interp_extrap_corners_2d,
round_res,
unwrap_corners,
Expand Down Expand Up @@ -122,14 +123,21 @@ def read(cls, fileName=None, ds=None, latVarName='lat',
descriptor.history = add_history(ds=ds)
return descriptor

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Create a SCRIP file based on the grid.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""
outFile = netCDF4.Dataset(scripFileName, 'w', format=self.format)

Expand All @@ -150,6 +158,9 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lon'][:] = \
unwrap_corners(self.lonCorner)

if expandDist is not None or expandFactor is not None:
expand_scrip(outFile, expandDist, expandFactor)

setattr(outFile, 'history', self.history)

outFile.close()
Expand Down
13 changes: 12 additions & 1 deletion pyremap/descriptor/lat_lon_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pyremap.descriptor.utility import (
add_history,
create_scrip,
expand_scrip,
interp_extrap_corner,
round_res,
unwrap_corners,
Expand Down Expand Up @@ -198,14 +199,21 @@ def create(cls, latCorner, lonCorner, units='degrees', meshName=None,
descriptor._set_coords('lat', 'lon', 'lat', 'lon')
return descriptor

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Given a lat-lon grid file, create a SCRIP file based on the grid.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""
outFile = netCDF4.Dataset(scripFileName, 'w', format=self.format)

Expand All @@ -228,6 +236,9 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = unwrap_corners(LatCorner)
outFile.variables['grid_corner_lon'][:] = unwrap_corners(LonCorner)

if expandDist is not None or expandFactor is not None:
expand_scrip(outFile, expandDist, expandFactor)

setattr(outFile, 'history', self.history)

outFile.close()
Expand Down
9 changes: 8 additions & 1 deletion pyremap/descriptor/mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, meshName=None, regional=None):
self.format = 'NETCDF4'
self.engine = None

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Subclasses should overload this method to write a SCRIP file based on
the mesh.
Expand All @@ -70,6 +70,13 @@ def to_scrip(self, scripFileName):
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""
raise NotImplementedError(
'to_scrip is not implemented for this descriptor')
Expand Down
14 changes: 12 additions & 2 deletions pyremap/descriptor/mpas_cell_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import add_history, create_scrip
from pyremap.descriptor.utility import add_history, create_scrip, expand_scrip


class MpasCellMeshDescriptor(MeshDescriptor):
Expand Down Expand Up @@ -98,14 +98,21 @@ def __init__(self, fileName, meshName=None, vertices=False):

self.history = add_history(ds=ds)

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""
if self.vertices:
raise ValueError('A SCRIP file won\'t work for remapping vertices')
Expand Down Expand Up @@ -152,6 +159,9 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = grid_corner_lat[:]
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

if expandDist is not None or expandFactor is not None:
expand_scrip(outFile, expandDist, expandFactor)

setattr(outFile, 'history', self.history)

inFile.close()
Expand Down
14 changes: 12 additions & 2 deletions pyremap/descriptor/mpas_edge_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import xarray as xr

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import add_history, create_scrip
from pyremap.descriptor.utility import add_history, create_scrip, expand_scrip


class MpasEdgeMeshDescriptor(MeshDescriptor):
Expand Down Expand Up @@ -70,14 +70,21 @@ def __init__(self, fileName, meshName=None):

self.history = add_history(ds=ds)

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""

inFile = netCDF4.Dataset(self.fileName, 'r')
Expand Down Expand Up @@ -146,6 +153,9 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = grid_corner_lat[:]
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

if expandDist is not None or expandFactor is not None:
expand_scrip(outFile, expandDist, expandFactor)

setattr(outFile, 'history', self.history)

inFile.close()
Expand Down
14 changes: 12 additions & 2 deletions pyremap/descriptor/mpas_vertex_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import xarray as xr

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import add_history, create_scrip
from pyremap.descriptor.utility import add_history, create_scrip, expand_scrip


class MpasVertexMeshDescriptor(MeshDescriptor):
Expand Down Expand Up @@ -70,14 +70,21 @@ def __init__(self, fileName, meshName=None):

self.history = add_history(ds=ds)

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""

inFile = netCDF4.Dataset(self.fileName, 'r')
Expand Down Expand Up @@ -150,6 +157,9 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = grid_corner_lat[:]
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

if expandDist is not None or expandFactor is not None:
expand_scrip(outFile, expandDist, expandFactor)

setattr(outFile, 'history', self.history)

inFile.close()
Expand Down
9 changes: 8 additions & 1 deletion pyremap/descriptor/point_collection_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,21 @@ def __init__(self, lats, lons, collectionName, units='degrees',
self.dimSize = [len(self.lat)]
self.history = add_history()

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""

outFile = netCDF4.Dataset(scripFileName, 'w', format=self.format)
Expand Down
13 changes: 12 additions & 1 deletion pyremap/descriptor/projection_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pyremap.descriptor.utility import (
add_history,
create_scrip,
expand_scrip,
interp_extrap_corner,
unwrap_corners,
)
Expand Down Expand Up @@ -173,14 +174,21 @@ def create(cls, projection, x, y, meshName):
descriptor.history = add_history()
return descriptor

def to_scrip(self, scripFileName):
def to_scrip(self, scripFileName, expandDist=None, expandFactor=None):
"""
Create a SCRIP file based on the grid and projection.
Parameters
----------
scripFileName : str
The path to which the SCRIP file should be written
expandDist : float, optional
A distance in meters to expand each grid cell outward from the
center
expandFactor : float, optional
A factor by which to expand each grid cell outward from the center
"""
outFile = netCDF4.Dataset(scripFileName, 'w', format=self.format)

Expand All @@ -206,6 +214,9 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = unwrap_corners(LatCorner)
outFile.variables['grid_corner_lon'][:] = unwrap_corners(LonCorner)

if expandDist is not None or expandFactor is not None:
expand_scrip(outFile, expandDist, expandFactor)

setattr(outFile, 'history', self.history)

outFile.close()
Expand Down
Loading

0 comments on commit 4a7feca

Please sign in to comment.