Skip to content

Commit

Permalink
Write history as string, not string array
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed Sep 18, 2023
1 parent 8b3640c commit 4ee8183
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 58 deletions.
9 changes: 2 additions & 7 deletions pyremap/descriptor/lat_lon_2d_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import (
add_history,
create_scrip,
interp_extrap_corners_2d,
round_res,
Expand Down Expand Up @@ -120,11 +119,7 @@ def read(cls, fileName=None, ds=None, latVarName='lat',
descriptor._set_coords(latVarName, lonVarName, ds[latVarName].dims[0],
ds[latVarName].dims[1])

if 'history' in ds.attrs:
descriptor.history = '\n'.join([ds.attrs['history'],
' '.join(sys.argv[:])])
else:
descriptor.history = sys.argv[:]
descriptor.history = add_history(ds=ds)
return descriptor

def to_scrip(self, scripFileName):
Expand Down
11 changes: 3 additions & 8 deletions pyremap/descriptor/lat_lon_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import (
add_history,
create_scrip,
interp_extrap_corner,
round_res,
Expand Down Expand Up @@ -157,11 +156,7 @@ def read(cls, fileName=None, ds=None, latVarName='lat',
descriptor._set_coords(latVarName, lonVarName, ds[latVarName].dims[0],
ds[lonVarName].dims[0])

if 'history' in ds.attrs:
descriptor.history = '\n'.join([ds.attrs['history'],
' '.join(sys.argv[:])])
else:
descriptor.history = sys.argv[:]
descriptor.history = add_history(ds=ds)
return descriptor

@classmethod
Expand Down Expand Up @@ -199,7 +194,7 @@ def create(cls, latCorner, lonCorner, units='degrees', meshName=None,
descriptor.lon = 0.5 * (lonCorner[0:-1] + lonCorner[1:])
descriptor.lat = 0.5 * (latCorner[0:-1] + latCorner[1:])
descriptor.units = units
descriptor.history = sys.argv[:]
descriptor.history = add_history()
descriptor._set_coords('lat', 'lon', 'lat', 'lon')
return descriptor

Expand Down
16 changes: 7 additions & 9 deletions pyremap/descriptor/mpas_cell_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys
import warnings

import netCDF4
import numpy
import xarray

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


class MpasCellMeshDescriptor(MeshDescriptor):
Expand All @@ -32,6 +31,9 @@ class MpasCellMeshDescriptor(MeshDescriptor):
fileName : str
The path of the file containing the MPAS mesh
history : str
The history attribute written to SCRIP files
"""
def __init__(self, fileName, meshName=None, vertices=False):
"""
Expand Down Expand Up @@ -94,6 +96,8 @@ def __init__(self, fileName, meshName=None, vertices=False):
self.dims = ['nCells']
self.dimSize = [ds.dims[dim] for dim in self.dims]

self.history = add_history(ds=ds)

def to_scrip(self, scripFileName):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Expand Down Expand Up @@ -148,13 +152,7 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = grid_corner_lat[:]
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

# Update history attribute of netCDF file
if hasattr(inFile, 'history'):
newhist = '\n'.join([getattr(inFile, 'history'),
' '.join(sys.argv[:])])
else:
newhist = sys.argv[:]
setattr(outFile, 'history', newhist)
setattr(outFile, 'history', self.history)

inFile.close()
outFile.close()
Expand Down
17 changes: 7 additions & 10 deletions pyremap/descriptor/mpas_edge_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy as np
import xarray as xr

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


class MpasEdgeMeshDescriptor(MeshDescriptor):
Expand All @@ -27,6 +25,9 @@ class MpasEdgeMeshDescriptor(MeshDescriptor):
----------
fileName : str
The path of the file containing the MPAS mesh
history : str
The history attribute written to SCRIP files
"""
def __init__(self, fileName, meshName=None):
"""
Expand Down Expand Up @@ -67,6 +68,8 @@ def __init__(self, fileName, meshName=None):
self.dims = ['nEdges']
self.dimSize = [ds.dims[dim] for dim in self.dims]

self.history = add_history(ds=ds)

def to_scrip(self, scripFileName):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Expand Down Expand Up @@ -143,13 +146,7 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = grid_corner_lat[:]
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

# Update history attribute of netCDF file
if hasattr(inFile, 'history'):
newhist = '\n'.join([getattr(inFile, 'history'),
' '.join(sys.argv[:])])
else:
newhist = sys.argv[:]
setattr(outFile, 'history', newhist)
setattr(outFile, 'history', self.history)

inFile.close()
outFile.close()
Expand Down
17 changes: 7 additions & 10 deletions pyremap/descriptor/mpas_vertex_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy as np
import xarray as xr

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


class MpasVertexMeshDescriptor(MeshDescriptor):
Expand All @@ -27,6 +25,9 @@ class MpasVertexMeshDescriptor(MeshDescriptor):
----------
fileName : str
The path of the file containing the MPAS mesh
history : str
The history attribute written to SCRIP files
"""
def __init__(self, fileName, meshName=None):
"""
Expand Down Expand Up @@ -67,6 +68,8 @@ def __init__(self, fileName, meshName=None):
self.dims = ['nVertices']
self.dimSize = [ds.dims[dim] for dim in self.dims]

self.history = add_history(ds=ds)

def to_scrip(self, scripFileName):
"""
Given an MPAS mesh file, create a SCRIP file based on the mesh.
Expand Down Expand Up @@ -147,13 +150,7 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lat'][:] = grid_corner_lat[:]
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

# Update history attribute of netCDF file
if hasattr(inFile, 'history'):
newhist = '\n'.join([getattr(inFile, 'history'),
' '.join(sys.argv[:])])
else:
newhist = sys.argv[:]
setattr(outFile, 'history', newhist)
setattr(outFile, 'history', self.history)

inFile.close()
outFile.close()
10 changes: 6 additions & 4 deletions pyremap/descriptor/point_collection_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import xarray

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


class PointCollectionDescriptor(MeshDescriptor):
Expand All @@ -33,6 +31,9 @@ class PointCollectionDescriptor(MeshDescriptor):
units : {'degrees', 'radians'}
The units of ``lats`` and ``lons``
history : str
The history attribute written to SCRIP files
"""

def __init__(self, lats, lons, collectionName, units='degrees',
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self, lats, lons, collectionName, units='degrees',
'attrs': {'units': units}}}
self.dims = [outDimension]
self.dimSize = [len(self.lat)]
self.history = add_history()

def to_scrip(self, scripFileName):
"""
Expand Down Expand Up @@ -114,7 +116,7 @@ def to_scrip(self, scripFileName):
outFile.variables['grid_corner_lon'][:] = grid_corner_lon[:]

# Update history attribute of netCDF file
setattr(outFile, 'history', ' '.join(sys.argv[:]))
setattr(outFile, 'history', self.history)

outFile.close()

Expand Down
16 changes: 7 additions & 9 deletions pyremap/descriptor/projection_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import pyproj
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import (
add_history,
create_scrip,
interp_extrap_corner,
unwrap_corners,
Expand Down Expand Up @@ -58,6 +57,9 @@ class ProjectionGridDescriptor(MeshDescriptor):
yVarName : str
The name of the y variable
history : str
The history attribute written to SCRIP files
"""
def __init__(self, projection, meshName=None):
"""
Expand All @@ -84,6 +86,7 @@ def __init__(self, projection, meshName=None):
self.history = None
self.xVarName = None
self.yVarName = None
self.history = None

@classmethod
def read(cls, projection, fileName, meshName=None, xVarName='x',
Expand Down Expand Up @@ -130,12 +133,7 @@ def read(cls, projection, fileName, meshName=None, xVarName='x',
descriptor.xCorner = interp_extrap_corner(descriptor.x)
descriptor.yCorner = interp_extrap_corner(descriptor.y)

# Update history attribute of netCDF file
if 'history' in ds.attrs:
descriptor.history = '\n'.join([ds.attrs['history'],
' '.join(sys.argv[:])])
else:
descriptor.history = sys.argv[:]
descriptor.history = add_history(ds=ds)
return descriptor

@classmethod
Expand Down Expand Up @@ -172,7 +170,7 @@ def create(cls, projection, x, y, meshName):
# interp/extrap corners
descriptor.xCorner = interp_extrap_corner(descriptor.x)
descriptor.yCorner = interp_extrap_corner(descriptor.y)
descriptor.history = sys.argv[:]
descriptor.history = add_history()
return descriptor

def to_scrip(self, scripFileName):
Expand Down
15 changes: 14 additions & 1 deletion pyremap/descriptor/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# distributed with this code, or at
# https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE

import sys

import numpy


Expand Down Expand Up @@ -107,6 +109,17 @@ def unwrap_corners(inField):


def round_res(res):
"""Round the resoltuion to a reasonable number for grid names"""
"""Round the resolution to a reasonable number for grid names"""
rounded = numpy.round(res * 1000.) / 1000.
return '{}'.format(rounded)


def add_history(ds=None):
"""Get the history attribute, possibly adding it to existing history"""
history = ' '.join(sys.argv[:])
if ds is not None and 'history' in ds.attrs:
prev_hist = ds.attrs['history']
if isinstance(prev_hist, numpy.ndarray):
prev_hist = '\n'.join(prev_hist)
history = '\n'.join([prev_hist, history])
return history

0 comments on commit 4ee8183

Please sign in to comment.