From 4ee818366c6eec01727d1063ccde03b0963e2674 Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Mon, 18 Sep 2023 12:09:07 +0200 Subject: [PATCH] Write history as string, not string array --- .../descriptor/lat_lon_2d_grid_descriptor.py | 9 ++------- pyremap/descriptor/lat_lon_grid_descriptor.py | 11 +++-------- pyremap/descriptor/mpas_cell_mesh_descriptor.py | 16 +++++++--------- pyremap/descriptor/mpas_edge_mesh_descriptor.py | 17 +++++++---------- .../descriptor/mpas_vertex_mesh_descriptor.py | 17 +++++++---------- .../descriptor/point_collection_descriptor.py | 10 ++++++---- .../descriptor/projection_grid_descriptor.py | 16 +++++++--------- pyremap/descriptor/utility.py | 15 ++++++++++++++- 8 files changed, 53 insertions(+), 58 deletions(-) diff --git a/pyremap/descriptor/lat_lon_2d_grid_descriptor.py b/pyremap/descriptor/lat_lon_2d_grid_descriptor.py index 0c38596..8e8727a 100644 --- a/pyremap/descriptor/lat_lon_2d_grid_descriptor.py +++ b/pyremap/descriptor/lat_lon_2d_grid_descriptor.py @@ -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, @@ -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): diff --git a/pyremap/descriptor/lat_lon_grid_descriptor.py b/pyremap/descriptor/lat_lon_grid_descriptor.py index cc56ebb..6fb4d2b 100644 --- a/pyremap/descriptor/lat_lon_grid_descriptor.py +++ b/pyremap/descriptor/lat_lon_grid_descriptor.py @@ -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, @@ -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 @@ -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 diff --git a/pyremap/descriptor/mpas_cell_mesh_descriptor.py b/pyremap/descriptor/mpas_cell_mesh_descriptor.py index 71fac7b..904f76d 100644 --- a/pyremap/descriptor/mpas_cell_mesh_descriptor.py +++ b/pyremap/descriptor/mpas_cell_mesh_descriptor.py @@ -9,7 +9,6 @@ # distributed with this code, or at # https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE -import sys import warnings import netCDF4 @@ -17,7 +16,7 @@ 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): @@ -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): """ @@ -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. @@ -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() diff --git a/pyremap/descriptor/mpas_edge_mesh_descriptor.py b/pyremap/descriptor/mpas_edge_mesh_descriptor.py index d2cbf3a..0d385c6 100644 --- a/pyremap/descriptor/mpas_edge_mesh_descriptor.py +++ b/pyremap/descriptor/mpas_edge_mesh_descriptor.py @@ -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): @@ -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): """ @@ -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. @@ -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() diff --git a/pyremap/descriptor/mpas_vertex_mesh_descriptor.py b/pyremap/descriptor/mpas_vertex_mesh_descriptor.py index 536f99d..0e6682f 100644 --- a/pyremap/descriptor/mpas_vertex_mesh_descriptor.py +++ b/pyremap/descriptor/mpas_vertex_mesh_descriptor.py @@ -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): @@ -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): """ @@ -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. @@ -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() diff --git a/pyremap/descriptor/point_collection_descriptor.py b/pyremap/descriptor/point_collection_descriptor.py index 2e8e248..679e7e3 100644 --- a/pyremap/descriptor/point_collection_descriptor.py +++ b/pyremap/descriptor/point_collection_descriptor.py @@ -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): @@ -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', @@ -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): """ @@ -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() diff --git a/pyremap/descriptor/projection_grid_descriptor.py b/pyremap/descriptor/projection_grid_descriptor.py index 3abaf2f..3c6e9ee 100644 --- a/pyremap/descriptor/projection_grid_descriptor.py +++ b/pyremap/descriptor/projection_grid_descriptor.py @@ -9,8 +9,6 @@ # distributed with this code, or at # https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE -import sys - import netCDF4 import numpy import pyproj @@ -18,6 +16,7 @@ from pyremap.descriptor.mesh_descriptor import MeshDescriptor from pyremap.descriptor.utility import ( + add_history, create_scrip, interp_extrap_corner, unwrap_corners, @@ -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): """ @@ -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', @@ -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 @@ -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): diff --git a/pyremap/descriptor/utility.py b/pyremap/descriptor/utility.py index 5a277d7..af481ba 100644 --- a/pyremap/descriptor/utility.py +++ b/pyremap/descriptor/utility.py @@ -9,6 +9,8 @@ # distributed with this code, or at # https://github.com/raw/MPAS-Dev/pyremap/main/LICENSE +import sys + import numpy @@ -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