Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed Sep 18, 2023
1 parent c999a70 commit 8b3640c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
16 changes: 8 additions & 8 deletions examples/make_mpas_to_Antarctic_stereo_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

'''
"""
Creates a mapping file that can be used with ncremap (NCO) to remap MPAS files
to a latitude/longitude grid.
Usage: Copy this script into the main MPAS-Analysis directory (up one level).
Modify the grid name, the path to the MPAS grid file and the output grid
resolution.
'''
"""

import xarray

from pyremap import MpasMeshDescriptor, Remapper, get_polar_descriptor
from pyremap import MpasCellMeshDescriptor, Remapper, get_polar_descriptor


# replace with the MPAS mesh name
Expand All @@ -32,32 +32,32 @@
# https://web.lcrc.anl.gov/public/e3sm/inputdata/ocn/mpas-o/oQU240/ocean.QU.240km.151209.nc
inGridFileName = 'ocean.QU.240km.151209.nc'

inDescriptor = MpasMeshDescriptor(inGridFileName, inGridName)
inDescriptor = MpasCellMeshDescriptor(inGridFileName, inGridName)

# modify the size and resolution of the Antarctic grid as desired
outDescriptor = get_polar_descriptor(Lx=6000., Ly=5000., dx=10., dy=10.,
projection='antarctic')
outGridName = outDescriptor.meshName

mappingFileName = 'map_{}_to_{}_bilinear.nc'.format(inGridName, outGridName)
mappingFileName = f'map_{inGridName}_to_{outGridName}_bilinear.nc'

remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)

# conservative remapping with 4 MPI tasks (using mpirun)
remapper.build_mapping_file(method='bilinear', mpiTasks=4)

# select the SST at the initial time as an example data set
srcFileName = 'temp_{}.nc'.format(inGridName)
srcFileName = f'temp_{inGridName}.nc'
ds = xarray.open_dataset(inGridFileName)
dsOut = xarray.Dataset()
dsOut['temperature'] = ds['temperature'].isel(nVertLevels=0, Time=0)
dsOut.to_netcdf(srcFileName)

# do remapping with ncremap
outFileName = 'temp_{}_file.nc'.format(outGridName)
outFileName = f'temp_{outGridName}_file.nc'
remapper.remap_file(srcFileName, outFileName)

# do remapping again, this time with python remapping
outFileName = 'temp_{}_array.nc'.format(outGridName)
outFileName = f'temp_{outGridName}_array.nc'
dsOut = remapper.remap(dsOut)
dsOut.to_netcdf(outFileName)
12 changes: 6 additions & 6 deletions examples/make_mpas_to_lat_lon_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

'''
"""
Creates a mapping file that can be used with ncremap (NCO) to remap MPAS files
to a latitude/longitude grid.
Usage: Copy this script into the main MPAS-Analysis directory (up one level).
Modify the grid name, the path to the MPAS grid file and the output grid
resolution.
'''
"""

import xarray

from pyremap import MpasMeshDescriptor, Remapper, get_lat_lon_descriptor
from pyremap import MpasCellMeshDescriptor, Remapper, get_lat_lon_descriptor

# replace with the MPAS mesh name
inGridName = 'oQU240'
Expand All @@ -31,20 +31,20 @@
# https://web.lcrc.anl.gov/public/e3sm/inputdata/ocn/mpas-o/oQU240/ocean.QU.240km.151209.nc
inGridFileName = 'ocean.QU.240km.151209.nc'

inDescriptor = MpasMeshDescriptor(inGridFileName, inGridName)
inDescriptor = MpasCellMeshDescriptor(inGridFileName, inGridName)

# modify the resolution of the global lat-lon grid as desired
outDescriptor = get_lat_lon_descriptor(dLon=0.5, dLat=0.5)
outGridName = outDescriptor.meshName

mappingFileName = 'map_{}_to_{}_bilinear.nc'.format(inGridName, outGridName)
mappingFileName = f'map_{inGridName}_to_{outGridName}_bilinear.nc'


remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)

remapper.build_mapping_file(method='bilinear', mpiTasks=1)

outFileName = 'temp_{}.nc'.format(outGridName)
outFileName = f'temp_{outGridName}.nc'
ds = xarray.open_dataset(inGridFileName)
dsOut = xarray.Dataset()
dsOut['temperature'] = ds['temperature']
Expand Down
66 changes: 66 additions & 0 deletions examples/make_mpas_vertex_to_Antarctic_stereo_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# This software is open source software available under the BSD-3 license.
#
# Copyright (c) 2019 Triad National Security, LLC. All rights reserved.
# Copyright (c) 2019 Lawrence Livermore National Security, LLC. All rights
# reserved.
# Copyright (c) 2019 UT-Battelle, LLC. All rights reserved.
#
# Additional copyright and license information can be found in the LICENSE file
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

"""
Creates a mapping file that can be used with ncremap (NCO) to remap MPAS files
to a latitude/longitude grid.
Usage: Copy this script into the main MPAS-Analysis directory (up one level).
Modify the grid name, the path to the MPAS grid file and the output grid
resolution.
"""

import numpy as np
import xarray as xr

from pyremap import MpasVertexMeshDescriptor, Remapper, get_polar_descriptor


# replace with the MPAS mesh name
inGridName = 'oQU240'

# replace with the path to the desired mesh or restart file
# As an example, use:
# https://web.lcrc.anl.gov/public/e3sm/inputdata/ocn/mpas-o/oQU240/ocean.QU.240km.151209.nc
inGridFileName = 'ocean.QU.240km.151209.nc'

inDescriptor = MpasVertexMeshDescriptor(inGridFileName, inGridName)

# modify the size and resolution of the Antarctic grid as desired
outDescriptor = get_polar_descriptor(Lx=6000., Ly=5000., dx=10., dy=10.,
projection='antarctic')
outGridName = outDescriptor.meshName

mappingFileName = f'map_{inGridName}_vertex_to_{outGridName}_conserve.nc'

remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)

# conservative remapping with 4 MPI tasks (using mpirun)
remapper.build_mapping_file(method='conserve', mpiTasks=4,
esmf_parallel_exec='mpirun', include_logs=True)

# select the SST at the initial time as an example data set
srcFileName = f'vertex_id_{inGridName}.nc'
ds = xr.open_dataset(inGridFileName)
dsOut = xr.Dataset()
dsOut['indexToVertexID'] = ds['indexToVertexID'].astype(float)
dsOut['random'] = ('nVertices', np.random.random(ds.sizes['nVertices']))
dsOut.to_netcdf(srcFileName)

# do remapping with ncremap
outFileName = f'vertex_id_{outGridName}_file.nc'
remapper.remap_file(srcFileName, outFileName)

# do remapping again, this time with python remapping
outFileName = f'vertex_id_{outGridName}_array.nc'
dsOut = remapper.remap(dsOut)
dsOut.to_netcdf(outFileName)
13 changes: 6 additions & 7 deletions examples/remap_stereographic.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python

'''
"""
Creates a mapping file and remaps the variables from an input file on an
Antarctic grid to another grid with the same extent but a different resolution.
The mapping file can be used with ncremap (NCO) to remap MPAS files between
these same gridss.
Usage: Copy this script into the main MPAS-Analysis directory (up one level).
'''
"""

import numpy
import xarray
Expand All @@ -33,7 +33,7 @@


if args.method not in ['bilinear', 'neareststod', 'conserve']:
raise ValueError('Unexpected method {}'.format(args.method))
raise ValueError(f'Unexpected method {args.method}')

dsIn = xarray.open_dataset(args.inFileName)

Expand All @@ -43,7 +43,7 @@
Lx = int((x[-1] - x[0])/1000.)
Ly = int((y[-1] - y[0])/1000.)

inMeshName = '{}x{}km_{}km_Antarctic_stereo'.format(Lx, Ly, dx)
inMeshName = f'{Lx}x{Ly}km_{dx}km_Antarctic_stereo'

projection = get_antarctic_stereographic_projection()

Expand All @@ -58,13 +58,12 @@
yOut = y[0] + outRes*numpy.arange(nyOut)


outMeshName = '{}x{}km_{}km_Antarctic_stereo'.format(Lx, Ly, args.resolution)
outMeshName = f'{Lx}x{Ly}km_{args.resolution}km_Antarctic_stereo'

outDescriptor = ProjectionGridDescriptor.create(projection, xOut, yOut,
outMeshName)

mappingFileName = 'map_{}_to_{}_{}.nc'.format(inMeshName, outMeshName,
args.method)
mappingFileName = f'map_{inMeshName}_to_{outMeshName}_{args.method}.nc'

remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)

Expand Down

0 comments on commit 8b3640c

Please sign in to comment.