From a93272baeb2c7c96d27f5ecd43a5d3a2abe26bd7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 24 Sep 2024 10:27:51 +0800 Subject: [PATCH] Add enums for GMT grid format ID and only add the Conventions for netCDF files --- doc/api/index.rst | 8 ++++++++ pygmt/datatypes/header.py | 10 +++++++++- pygmt/enums.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 pygmt/enums.py diff --git a/doc/api/index.rst b/doc/api/index.rst index 1c80d8d163f..04849715f03 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -242,6 +242,14 @@ In addition, there is also a special function to load XYZ tile maps via .. currentmodule:: pygmt +Enumerations +------------ + +.. autosummary:: + :toctree: generated + + enums + Exceptions ---------- diff --git a/pygmt/datatypes/header.py b/pygmt/datatypes/header.py index 04e10ac0c72..7c4a5028412 100644 --- a/pygmt/datatypes/header.py +++ b/pygmt/datatypes/header.py @@ -6,6 +6,7 @@ from typing import Any, ClassVar import numpy as np +from pygmt.enums import GridID # Constants for lengths of grid header variables. # @@ -203,7 +204,14 @@ def data_attrs(self) -> dict[str, Any]: Attributes for the data variable from the grid header. """ attrs: dict[str, Any] = {} - attrs["Conventions"] = "CF-1.7" + if self.type in { # netCDF format + GridID.GMT_GRID_IS_NB, + GridID.GMT_GRID_IS_NS, + GridID.GMT_GRID_IS_NI, + GridID.GMT_GRID_IS_NF, + GridID.GMT_GRID_IS_ND, + }: + attrs["Conventions"] = "CF-1.7" attrs["title"] = self.title.decode() attrs["history"] = self.command.decode() attrs["description"] = self.remark.decode() diff --git a/pygmt/enums.py b/pygmt/enums.py new file mode 100644 index 00000000000..d7c70658d0b --- /dev/null +++ b/pygmt/enums.py @@ -0,0 +1,39 @@ +""" +Enumerations for PyGMT. +""" + +from enum import IntEnum + + +class GridID(IntEnum): + """ + Enum for the GMT grid format ID. + + These enums are defined in 'gmt_grdio.h'. + """ + + GMT_GRD_UNKNOWN_FMT = 0 # If grid format cannot be auto-detected + GMT_GRID_IS_BF = 1 # GMT native, C-binary format (32-bit float) + GMT_GRID_IS_BS = 2 # GMT native, C-binary format (16-bit integer) + GMT_GRID_IS_RB = 3 # SUN rasterfile format (8-bit standard) + GMT_GRID_IS_BB = 4 # GMT native, C-binary format (8-bit integer) + GMT_GRID_IS_BM = 5 # GMT native, C-binary format (bit-mask) + GMT_GRID_IS_SF = 6 # Golden Software Surfer format 6 (32-bit float) + GMT_GRID_IS_CB = 7 # GMT netCDF format (8-bit integer, depreciated) + GMT_GRID_IS_CS = 8 # GMT netCDF format (16-bit integer, depreciated) + GMT_GRID_IS_CI = 9 # GMT netCDF format (32-bit integer, depreciated) + GMT_GRID_IS_CF = 10 # GMT netCDF format (32-bit float, depreciated) + GMT_GRID_IS_CD = 11 # GMT netCDF format (64-bit float, depreciated) + GMT_GRID_IS_RF = 12 # GEODAS grid format GRD98 (NGDC) + GMT_GRID_IS_BI = 13 # GMT native, C-binary format (32-bit integer) + GMT_GRID_IS_BD = 14 # GMT native, C-binary format (64-bit float) + GMT_GRID_IS_NB = 15 # GMT netCDF format (8-bit integer) + GMT_GRID_IS_NS = 16 # GMT netCDF format (16-bit integer) + GMT_GRID_IS_NI = 17 # GMT netCDF format (32-bit integer) + GMT_GRID_IS_NF = 18 # GMT netCDF format (32-bit float) + GMT_GRID_IS_ND = 19 # GMT netCDF format (64-bit float) + GMT_GRID_IS_SD = 20 # Golden Software Surfer format 7 (64-bit float, read-only) + GMT_GRID_IS_AF = 21 # Atlantic Geoscience Center format AGC (32-bit float) + GMT_GRID_IS_GD = 22 # Import through GDAL + GMT_GRID_IS_EI = 23 # ESRI Arc/Info ASCII Grid Interchange format (ASCII integer) + GMT_GRID_IS_EF = 24 # ESRI Arc/Info ASCII Grid Interchange format (ASCII float)