Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
NCWriteScrip.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 // Filename : NCWriteScrip.hpp
3 //
4 // Purpose : Write a generic polygonal mesh out in SCRIP grid format.
5 //
6 // Counterpart of NCHelperScrip — same wire format on disk, inverse
7 // direction. Drives off any 2-D unstructured mesh in MOAB; not tied
8 // to a particular source format. Selected via the WriteNC option
9 // "WRITE_FORMAT=SCRIP", which lets us convert from arbitrary inputs
10 // (MPAS, HOMME, h5m, exodus, ...) into SCRIP without first having
11 // to go through the climate read-modify-write template.
12 //
13 // On-disk schema (matches NCHelperScrip's reader):
14 // dimensions:
15 // grid_size = number of cells
16 // grid_corners = max vertices per cell (smaller cells are padded
17 // with the first corner, per SCRIP convention)
18 // grid_rank = 1 (unstructured)
19 // variables:
20 // int grid_dims(grid_rank)
21 // double grid_center_lat(grid_size) ; units = "degrees"
22 // double grid_center_lon(grid_size) ; units = "degrees"
23 // double grid_corner_lat(grid_size, grid_corners) ; units = "degrees"
24 // double grid_corner_lon(grid_size, grid_corners) ; units = "degrees"
25 // int grid_imask(grid_size)
26 // (optional) double grid_area(grid_size) ; units = "radians^2"
27 //
28 // Creator : Vijay Mahadevan, 2026-06-13
29 //-------------------------------------------------------------------------
30 
31 #ifndef NCWRITESCRIP_HPP_
32 #define NCWRITESCRIP_HPP_
33 
34 #include "NCWriteHelper.hpp"
35 
36 namespace moab
37 {
38 
40 {
41  public:
42  NCWriteScrip( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
43  : NCWriteHelper( writeNC, fileId, opts, fileSet ),
44  mLocalCells( 0 ), mGlobalCells( 0 ), mMaxCornersGlobal( 0 ), mHasAreas( false ),
45  mDimGridSize( -1 ), mDimGridCorners( -1 ), mDimGridRank( -1 ),
46  mVarGridDims( -1 ), mVarCenterLon( -1 ), mVarCenterLat( -1 ), mVarCornerLon( -1 ),
47  mVarCornerLat( -1 ), mVarImask( -1 ), mVarArea( -1 )
48  {
49  }
50 
51  virtual ~NCWriteScrip() override;
52 
53  // ----- NCWriteHelper interface --------------------------------------
54 
55  //! Collect cells + compute SCRIP-ready center/corner lat/lon arrays.
56  ErrorCode collect_mesh_info() override;
57 
58  //! Override: synthesize the SCRIP schema directly — no need for
59  //! the climate var_names / dim plumbing that the base init_file
60  //! walks through.
61  ErrorCode init_file( std::vector< std::string >& var_names,
62  std::vector< std::string >& desired_names,
63  bool _append ) override;
64 
65  //! Override: write SCRIP grid arrays (cells gathered to rank 0
66  //! and written serially via the dispatch layer).
67  ErrorCode write_values( std::vector< std::string >& var_names, std::vector< int >& tstep_nums ) override;
68 
69  protected:
70  //! Required by the abstract base; no climate "nonset" variables
71  //! exist in a SCRIP grid file, so this is a no-op.
72  ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas,
73  std::vector< int >& tstep_nums ) override;
74 
75  private:
76  //! Per-rank cell count and the global total (set in collect_mesh_info).
79 
80  //! Global max corners per cell. Used to size the corner arrays.
82 
83  //! True if a cell-area tag was present on the input mesh and we'll
84  //! emit grid_area; false otherwise.
85  bool mHasAreas;
86 
87  //! Per-cell local data (size mLocalCells; corners are
88  //! mLocalCells * mMaxCornersGlobal, row-major).
89  std::vector< int > mLocalGids;
90  std::vector< double > mCenterLon;
91  std::vector< double > mCenterLat;
92  std::vector< double > mCornerLon;
93  std::vector< double > mCornerLat;
94  std::vector< int > mImask;
95  std::vector< double > mAreas; // empty if !mHasAreas
96 
97  //! NetCDF dimension and variable handles populated by init_file().
107  int mVarArea;
108 };
109 
110 } // namespace moab
111 
112 #endif // NCWRITESCRIP_HPP_