Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
NCWriteDomain.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 // Filename : NCWriteDomain.hpp
3 //
4 // Purpose : Write a generic polygonal mesh out as a CESM domain
5 // NetCDF file. Pairs with NCHelperDomain on the read side.
6 //
7 // On-disk schema (matches NCHelperDomain's reader):
8 // dimensions:
9 // n = ni * nj (number of cells)
10 // ni = i-extent (set to the total cell count for unstructured input)
11 // nj = j-extent (set to 1 for unstructured input)
12 // nv = max vertices per cell
13 // variables:
14 // double xc(nj, ni) ; long_name = "longitude of grid cell center"
15 // ; units = "degrees_east"
16 // ; bounds = "xv"
17 // double yc(nj, ni) ; long_name = "latitude of grid cell center"
18 // ; units = "degrees_north"
19 // ; bounds = "yv"
20 // double xv(nj, ni, nv) ; long_name = "longitude of grid cell verticies"
21 // ; units = "degrees_east"
22 // double yv(nj, ni, nv) ; long_name = "latitude of grid cell verticies"
23 // ; units = "degrees_north"
24 // int mask(nj, ni) ; long_name = "domain mask"
25 // double area(nj, ni) ; units = "radian2"
26 // double frac(nj, ni) ; fraction of grid cell that is active
27 //
28 // Layout note: this first cut treats any input mesh as a flat
29 // ni = total-cells, nj = 1 layout. The sample CESM domain files
30 // for unstructured inputs (e.g. ne4np4_oQU240 with 866 cells, ni=866,
31 // nj=1) follow exactly this convention, so reloading the output via
32 // NCHelperDomain produces the original cell count. Genuine
33 // rectangular (ni > 1, nj > 1) output is a follow-up that needs
34 // structured-mesh topology detection from the input.
35 //
36 // Creator : Vijay Mahadevan, 2026-06-13
37 //-------------------------------------------------------------------------
38 
39 #ifndef NCWRITEDOMAIN_HPP_
40 #define NCWRITEDOMAIN_HPP_
41 
42 #include "NCWriteHelper.hpp"
43 
44 namespace moab
45 {
46 
48 {
49  public:
50  NCWriteDomain( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
51  : NCWriteHelper( writeNC, fileId, opts, fileSet ),
53  mNi( 0 ), mNj( 1 ), mHasAreas( false ), mHasMask( false ), mHasFrac( false ),
54  mDimN( -1 ), mDimNi( -1 ), mDimNj( -1 ), mDimNv( -1 ),
55  mVarXc( -1 ), mVarYc( -1 ), mVarXv( -1 ), mVarYv( -1 ),
56  mVarMask( -1 ), mVarArea( -1 ), mVarFrac( -1 )
57  {
58  }
59 
60  virtual ~NCWriteDomain() override;
61 
62  ErrorCode collect_mesh_info() override;
63  ErrorCode init_file( std::vector< std::string >& var_names,
64  std::vector< std::string >& desired_names,
65  bool _append ) override;
66  ErrorCode write_values( std::vector< std::string >& var_names, std::vector< int >& tstep_nums ) override;
67 
68  protected:
69  ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas,
70  std::vector< int >& tstep_nums ) override;
71 
72  private:
76 
77  //! On-disk grid extents. For now we always emit ni = mGlobalCells,
78  //! nj = 1 — i.e. a flat layout that the CESM domain reader handles
79  //! identically to the rectangular case but with j collapsed.
80  long mNi;
81  long mNj;
82 
83  bool mHasAreas;
84  bool mHasMask;
85  bool mHasFrac;
86 
87  std::vector< int > mLocalGids;
88  std::vector< double > mXc;
89  std::vector< double > mYc;
90  std::vector< double > mXv; // size mLocalCells * mMaxCornersGlobal, row-major
91  std::vector< double > mYv;
92  std::vector< int > mMask;
93  std::vector< double > mAreas;
94  std::vector< double > mFrac;
95 
96  int mDimN;
97  int mDimNi;
98  int mDimNj;
99  int mDimNv;
100  int mVarXc;
101  int mVarYc;
102  int mVarXv;
103  int mVarYv;
104  int mVarMask;
105  int mVarArea;
106  int mVarFrac;
107 };
108 
109 } // namespace moab
110 
111 #endif // NCWRITEDOMAIN_HPP_