Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
NCHelperEuler.cpp
Go to the documentation of this file.
1 #include "moab/MOABConfig.h"
2 #ifdef WIN32 /* windows */
3 #define _USE_MATH_DEFINES // For M_PI
4 #endif
5 
6 #include "NCHelperEuler.hpp"
7 #include "moab/FileOptions.hpp"
8 
9 #include <cmath>
10 #include <sstream>
11 
12 #ifdef WIN32
13 #ifdef size_t
14 #undef size_t
15 #endif
16 #endif
17 
18 namespace moab
19 {
20 
21 bool NCHelperEuler::can_read_file( ReadNC* readNC, int fileId )
22 {
23  std::vector< std::string >& dimNames = readNC->dimNames;
24 
25  // If dimension names "lon" AND "lat' exist then it could be either the Eulerian Spectral grid
26  // or the FV grid
27  if( ( std::find( dimNames.begin(), dimNames.end(), std::string( "lon" ) ) != dimNames.end() ) &&
28  ( std::find( dimNames.begin(), dimNames.end(), std::string( "lat" ) ) != dimNames.end() ) )
29  {
30  // If dimension names "lon" AND "lat" AND "slon" AND "slat" exist then it should be the FV
31  // grid
32  if( ( std::find( dimNames.begin(), dimNames.end(), std::string( "slon" ) ) != dimNames.end() ) &&
33  ( std::find( dimNames.begin(), dimNames.end(), std::string( "slat" ) ) != dimNames.end() ) )
34  return false;
35 
36  // Make sure it is CAM grid
37  std::map< std::string, ReadNC::AttData >::iterator attIt = readNC->globalAtts.find( "source" );
38  if( attIt == readNC->globalAtts.end() ) return false;
39  unsigned int sz = attIt->second.attLen;
40  std::string att_data;
41  att_data.resize( sz + 1 );
42  att_data[sz] = '\000';
43  int success =
44  NCFUNC( get_att_text )( fileId, attIt->second.attVarId, attIt->second.attName.c_str(), &att_data[0] );
45  if( success ) return false;
46  if( att_data.find( "CAM" ) == std::string::npos ) return false;
47 
48  return true;
49  }
50 
51  return false;
52 }
53 
55 {
56  Interface*& mbImpl = _readNC->mbImpl;
57  std::vector< std::string >& dimNames = _readNC->dimNames;
58  std::vector< int >& dimLens = _readNC->dimLens;
59  std::map< std::string, ReadNC::VarData >& varInfo = _readNC->varInfo;
60  DebugOutput& dbgOut = _readNC->dbgOut;
61  bool& isParallel = _readNC->isParallel;
62  int& partMethod = _readNC->partMethod;
63  ScdParData& parData = _readNC->parData;
64 
65  // Look for names of center i/j dimensions
66  // First i
67  std::vector< std::string >::iterator vit;
68  unsigned int idx;
69  if( ( vit = std::find( dimNames.begin(), dimNames.end(), "lon" ) ) != dimNames.end() )
70  idx = vit - dimNames.begin();
71  else
72  {
73  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lon' dimension" );
74  }
75  iCDim = idx;
76  gCDims[0] = 0;
77  gCDims[3] = dimLens[idx] - 1;
78 
79  // Check i periodicity and set globallyPeriodic[0]
80  std::vector< double > til_vals( 2 );
81  MB_CHK_SET_ERR( read_coordinate( "lon", gCDims[3] - 1, gCDims[3], til_vals ), "Trouble reading 'lon' variable" );
82  if( std::fabs( 2 * til_vals[1] - til_vals[0] - 360 ) < 0.001 ) globallyPeriodic[0] = 1;
83 
84  // Now we can set gDims for i
85  gDims[0] = 0;
86  gDims[3] =
87  gCDims[3] + ( globallyPeriodic[0] ? 0 : 1 ); // Only if not periodic is vertex param max > elem param max
88 
89  // Then j
90  if( ( vit = std::find( dimNames.begin(), dimNames.end(), "lat" ) ) != dimNames.end() )
91  idx = vit - dimNames.begin();
92  else
93  {
94  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lat' dimension" );
95  }
96  jCDim = idx;
97  gCDims[1] = 0;
98  gCDims[4] = dimLens[idx] - 1;
99 
100  // For Eul models, will always be non-periodic in j
101  gDims[1] = 0;
102  gDims[4] = gCDims[4] + 1;
103 
104  // Try a truly 2D mesh
105  gDims[2] = -1;
106  gDims[5] = -1;
107 
108  // Look for time dimension
109  if( ( vit = std::find( dimNames.begin(), dimNames.end(), "time" ) ) != dimNames.end() )
110  idx = vit - dimNames.begin();
111  else if( ( vit = std::find( dimNames.begin(), dimNames.end(), "t" ) ) != dimNames.end() )
112  idx = vit - dimNames.begin();
113  else
114  {
115  MB_SET_ERR( MB_FAILURE, "Couldn't find 'time' or 't' dimension" );
116  }
117  tDim = idx;
118  nTimeSteps = dimLens[idx];
119 
120  // Look for level dimension
121  if( ( vit = std::find( dimNames.begin(), dimNames.end(), "lev" ) ) != dimNames.end() )
122  idx = vit - dimNames.begin();
123  else if( ( vit = std::find( dimNames.begin(), dimNames.end(), "ilev" ) ) != dimNames.end() )
124  idx = vit - dimNames.begin();
125  else
126  {
127  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lev' or 'ilev' dimension" );
128  }
129  levDim = idx;
130  nLevels = dimLens[idx];
131 
132  // Parse options to get subset
133  int rank = 0, procs = 1;
134 #ifdef MOAB_HAVE_MPI
135  if( isParallel )
136  {
137  ParallelComm*& myPcomm = _readNC->myPcomm;
138  rank = myPcomm->proc_config().proc_rank();
139  procs = myPcomm->proc_config().proc_size();
140  }
141 #endif
142  if( procs > 1 )
143  {
144  for( int i = 0; i < 6; i++ )
145  parData.gDims[i] = gDims[i];
146  for( int i = 0; i < 3; i++ )
147  parData.gPeriodic[i] = globallyPeriodic[i];
148  parData.partMethod = partMethod;
149  int pdims[3];
150 
151  MB_CHK_ERR( ScdInterface::compute_partition( procs, rank, parData, lDims, locallyPeriodic, pdims ) );
152  for( int i = 0; i < 3; i++ )
153  parData.pDims[i] = pdims[i];
154 
155  dbgOut.tprintf( 1, "Partition: %dx%d (out of %dx%d)\n", lDims[3] - lDims[0] + 1, lDims[4] - lDims[1] + 1,
156  gDims[3] - gDims[0] + 1, gDims[4] - gDims[1] + 1 );
157  if( 0 == rank )
158  dbgOut.tprintf( 1, "Contiguous chunks of size %d bytes.\n",
159  8 * ( lDims[3] - lDims[0] + 1 ) * ( lDims[4] - lDims[1] + 1 ) );
160  }
161  else
162  {
163  for( int i = 0; i < 6; i++ )
164  lDims[i] = gDims[i];
166  }
167 
168  _opts.get_int_option( "IMIN", lDims[0] );
169  _opts.get_int_option( "IMAX", lDims[3] );
170  _opts.get_int_option( "JMIN", lDims[1] );
171  _opts.get_int_option( "JMAX", lDims[4] );
172 
173  // Now get actual coordinate values for vertices and cell centers
174  lCDims[0] = lDims[0];
175  if( locallyPeriodic[0] )
176  // If locally periodic, doesn't matter what global periodicity is, # vertex coords = # elem
177  // coords
178  lCDims[3] = lDims[3];
179  else
180  lCDims[3] = lDims[3] - 1;
181 
182  // For Eul models, will always be non-periodic in j
183  lCDims[1] = lDims[1];
184  lCDims[4] = lDims[4] - 1;
185 
186  // Resize vectors to store values later
187  if( -1 != lDims[0] ) ilVals.resize( lDims[3] - lDims[0] + 1 );
188  if( -1 != lCDims[0] ) ilCVals.resize( lCDims[3] - lCDims[0] + 1 );
189  if( -1 != lDims[1] ) jlVals.resize( lDims[4] - lDims[1] + 1 );
190  if( -1 != lCDims[1] ) jlCVals.resize( lCDims[4] - lCDims[1] + 1 );
191  if( nTimeSteps > 0 ) tVals.resize( nTimeSteps );
192 
193  // Now read coord values
194  std::map< std::string, ReadNC::VarData >::iterator vmit;
195  if( -1 != lCDims[0] )
196  {
197  if( ( vmit = varInfo.find( "lon" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
198  {
199  MB_CHK_SET_ERR( read_coordinate( "lon", lCDims[0], lCDims[3], ilCVals ), "Trouble reading 'lon' variable" );
200  }
201  else
202  {
203  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lon' variable" );
204  }
205  }
206 
207  if( -1 != lCDims[1] )
208  {
209  if( ( vmit = varInfo.find( "lat" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
210  {
211  MB_CHK_SET_ERR( read_coordinate( "lat", lCDims[1], lCDims[4], jlCVals ), "Trouble reading 'lat' variable" );
212  }
213  else
214  {
215  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lat' variable" );
216  }
217  }
218 
219  if( -1 != lDims[0] )
220  {
221  if( ( vmit = varInfo.find( "lon" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
222  {
223  double dif = ( ilCVals[1] - ilCVals[0] ) / 2;
224  std::size_t i;
225  for( i = 0; i != ilCVals.size(); i++ )
226  ilVals[i] = ilCVals[i] - dif;
227  // The last one is needed only if not periodic
228  if( !locallyPeriodic[0] ) ilVals[i] = ilCVals[i - 1] + dif;
229  }
230  else
231  {
232  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lon' variable" );
233  }
234  }
235 
236  if( -1 != lDims[1] )
237  {
238  if( ( vmit = varInfo.find( "lat" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
239  {
240  if( !isParallel || ( ( gDims[4] - gDims[1] ) == ( lDims[4] - lDims[1] ) ) )
241  {
242  std::string gwName( "gw" );
243  std::vector< double > gwVals( lDims[4] - lDims[1] - 1 );
244  MB_CHK_SET_ERR( read_coordinate( gwName.c_str(), lDims[1], lDims[4] - 2, gwVals ),
245  "Trouble reading 'gw' variable" );
246  // Copy the correct piece
247  jlVals[0] = -( M_PI / 2 ) * 180 / M_PI;
248  std::size_t i = 0;
249  double gwSum = -1;
250  for( i = 1; i != gwVals.size() + 1; i++ )
251  {
252  gwSum += gwVals[i - 1];
253  jlVals[i] = std::asin( gwSum ) * 180 / M_PI;
254  }
255  jlVals[i] = 90.0; // Using value of i after loop exits.
256  }
257  else
258  {
259  std::string gwName( "gw" );
260  double gwSum = 0;
261 
262  // If this is the first row
263  if( lDims[1] == gDims[1] )
264  {
265  std::vector< double > gwVals( lDims[4] );
266  MB_CHK_SET_ERR( read_coordinate( gwName.c_str(), 0, lDims[4] - 1, gwVals ),
267  "Trouble reading 'gw' variable" );
268  // Copy the correct piece
269  jlVals[0] = -( M_PI / 2 ) * 180 / M_PI;
270  gwSum = -1;
271  for( std::size_t i = 1; i != jlVals.size(); i++ )
272  {
273  gwSum += gwVals[i - 1];
274  jlVals[i] = std::asin( gwSum ) * 180 / M_PI;
275  }
276  }
277  // Or if it's the last row
278  else if( lDims[4] == gDims[4] )
279  {
280  std::vector< double > gwVals( lDims[4] - 1 );
281  MB_CHK_SET_ERR( read_coordinate( gwName.c_str(), 0, lDims[4] - 2, gwVals ),
282  "Trouble reading 'gw' variable" );
283  // Copy the correct piece
284  gwSum = -1;
285  for( int j = 0; j != lDims[1] - 1; j++ )
286  gwSum += gwVals[j];
287  std::size_t i = 0;
288  for( ; i != jlVals.size() - 1; i++ )
289  {
290  gwSum += gwVals[lDims[1] - 1 + i];
291  jlVals[i] = std::asin( gwSum ) * 180 / M_PI;
292  }
293  jlVals[i] = 90.0; // Using value of i after loop exits.
294  }
295  // It's in the middle
296  else
297  {
298  int start = lDims[1] - 1;
299  int end = lDims[4] - 1;
300  std::vector< double > gwVals( end );
301  MB_CHK_SET_ERR( read_coordinate( gwName.c_str(), 0, end - 1, gwVals ),
302  "Trouble reading 'gw' variable" );
303  gwSum = -1;
304  for( int j = 0; j != start - 1; j++ )
305  gwSum += gwVals[j];
306  std::size_t i = 0;
307  for( ; i != jlVals.size(); i++ )
308  {
309  gwSum += gwVals[start - 1 + i];
310  jlVals[i] = std::asin( gwSum ) * 180 / M_PI;
311  }
312  }
313  }
314  }
315  else
316  {
317  MB_SET_ERR( MB_FAILURE, "Couldn't find 'lat' variable" );
318  }
319  }
320 
321  // Store time coordinate values in tVals
322  if( nTimeSteps > 0 )
323  {
324  if( ( vmit = varInfo.find( "time" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
325  {
326  MB_CHK_SET_ERR( read_coordinate( "time", 0, nTimeSteps - 1, tVals ), "Trouble reading 'time' variable" );
327  }
328  else if( ( vmit = varInfo.find( "t" ) ) != varInfo.end() && ( *vmit ).second.varDims.size() == 1 )
329  {
330  MB_CHK_SET_ERR( read_coordinate( "t", 0, nTimeSteps - 1, tVals ), "Trouble reading 't' variable" );
331  }
332  else
333  {
334  // If expected time variable is not available, set dummy time coordinate values to tVals
335  for( int t = 0; t < nTimeSteps; t++ )
336  tVals.push_back( (double)t );
337  }
338  }
339 
340  dbgOut.tprintf( 1, "I=%d-%d, J=%d-%d\n", lDims[0], lDims[3], lDims[1], lDims[4] );
341  dbgOut.tprintf( 1, "%d elements, %d vertices\n", ( lDims[3] - lDims[0] ) * ( lDims[4] - lDims[1] ),
342  ( lDims[3] - lDims[0] + 1 ) * ( lDims[4] - lDims[1] + 1 ) );
343 
344  // For each variable, determine the entity location type and number of levels
345  std::map< std::string, ReadNC::VarData >::iterator mit;
346  for( mit = varInfo.begin(); mit != varInfo.end(); ++mit )
347  {
348  ReadNC::VarData& vd = ( *mit ).second;
349 
350  // Default entLoc is ENTLOCSET
351  if( std::find( vd.varDims.begin(), vd.varDims.end(), tDim ) != vd.varDims.end() )
352  {
353  if( ( std::find( vd.varDims.begin(), vd.varDims.end(), iCDim ) != vd.varDims.end() ) &&
354  ( std::find( vd.varDims.begin(), vd.varDims.end(), jCDim ) != vd.varDims.end() ) )
356  }
357 
358  // Default numLev is 0
359  if( std::find( vd.varDims.begin(), vd.varDims.end(), levDim ) != vd.varDims.end() ) vd.numLev = nLevels;
360  }
361 
362  // For Eul models, slon and slat are "virtual" dimensions (not defined in the file header)
363  std::vector< std::string > ijdimNames( 4 );
364  ijdimNames[0] = "__slon";
365  ijdimNames[1] = "__slat";
366  ijdimNames[2] = "__lon";
367  ijdimNames[3] = "__lat";
368 
369  std::string tag_name;
370  Tag tagh;
371 
372  // __<dim_name>_LOC_MINMAX (for virtual slon, virtual slat, lon and lat)
373  for( unsigned int i = 0; i != ijdimNames.size(); i++ )
374  {
375  std::vector< int > val( 2, 0 );
376  if( ijdimNames[i] == "__slon" )
377  {
378  val[0] = lDims[0];
379  val[1] = lDims[3];
380  }
381  else if( ijdimNames[i] == "__slat" )
382  {
383  val[0] = lDims[1];
384  val[1] = lDims[4];
385  }
386  else if( ijdimNames[i] == "__lon" )
387  {
388  val[0] = lCDims[0];
389  val[1] = lCDims[3];
390  }
391  else if( ijdimNames[i] == "__lat" )
392  {
393  val[0] = lCDims[1];
394  val[1] = lCDims[4];
395  }
396  std::stringstream ss_tag_name;
397  ss_tag_name << ijdimNames[i] << "_LOC_MINMAX";
398  tag_name = ss_tag_name.str();
399  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 2, MB_TYPE_INTEGER, tagh,
401  "Trouble creating conventional tag " << tag_name );
402  MB_CHK_SET_ERR( mbImpl->tag_set_data( tagh, &_fileSet, 1, &val[0] ),
403  "Trouble setting data to conventional tag " << tag_name );
404  dbgOut.tprintf( 2, "Conventional tag %s is created.\n", tag_name.c_str() );
405  }
406 
407  // __<dim_name>_LOC_VALS (for virtual slon, virtual slat, lon and lat)
408  // Assume all have the same data type as lon (expected type is float or double)
409  switch( varInfo["lon"].varDataType )
410  {
411  case NC_FLOAT:
412  case NC_DOUBLE:
413  break;
414  default:
415  MB_SET_ERR( MB_FAILURE, "Unexpected variable data type for 'lon'" );
416  }
417 
418  for( unsigned int i = 0; i != ijdimNames.size(); i++ )
419  {
420  void* val = NULL;
421  int val_len = 0;
422  if( ijdimNames[i] == "__slon" )
423  {
424  val = &ilVals[0];
425  val_len = ilVals.size();
426  }
427  else if( ijdimNames[i] == "__slat" )
428  {
429  val = &jlVals[0];
430  val_len = jlVals.size();
431  }
432  else if( ijdimNames[i] == "__lon" )
433  {
434  val = &ilCVals[0];
435  val_len = ilCVals.size();
436  }
437  else if( ijdimNames[i] == "__lat" )
438  {
439  val = &jlCVals[0];
440  val_len = jlCVals.size();
441  }
442 
443  std::stringstream ss_tag_name;
444  ss_tag_name << ijdimNames[i] << "_LOC_VALS";
445  tag_name = ss_tag_name.str();
446  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 0, MB_TYPE_DOUBLE, tagh,
448  "Trouble creating conventional tag " << tag_name );
449  MB_CHK_SET_ERR( mbImpl->tag_set_by_ptr( tagh, &_fileSet, 1, &val, &val_len ),
450  "Trouble setting data to conventional tag " << tag_name );
451  dbgOut.tprintf( 2, "Conventional tag %s is created.\n", tag_name.c_str() );
452  }
453 
454  // __<dim_name>_GLOBAL_MINMAX (for virtual slon, virtual slat, lon and lat)
455  for( unsigned int i = 0; i != ijdimNames.size(); i++ )
456  {
457  std::vector< int > val( 2, 0 );
458  if( ijdimNames[i] == "__slon" )
459  {
460  val[0] = gDims[0];
461  val[1] = gDims[3];
462  }
463  else if( ijdimNames[i] == "__slat" )
464  {
465  val[0] = gDims[1];
466  val[1] = gDims[4];
467  }
468  else if( ijdimNames[i] == "__lon" )
469  {
470  val[0] = gCDims[0];
471  val[1] = gCDims[3];
472  }
473  else if( ijdimNames[i] == "__lat" )
474  {
475  val[0] = gCDims[1];
476  val[1] = gCDims[4];
477  }
478  std::stringstream ss_tag_name;
479  ss_tag_name << ijdimNames[i] << "_GLOBAL_MINMAX";
480  tag_name = ss_tag_name.str();
481  MB_CHK_SET_ERR( mbImpl->tag_get_handle( tag_name.c_str(), 2, MB_TYPE_INTEGER, tagh,
483  "Trouble creating conventional tag " << tag_name );
484  MB_CHK_SET_ERR( mbImpl->tag_set_data( tagh, &_fileSet, 1, &val[0] ),
485  "Trouble setting data to conventional tag " << tag_name );
486  dbgOut.tprintf( 2, "Conventional tag %s is created.\n", tag_name.c_str() );
487  }
488 
489  // Hack: create dummy variables, if needed, for dimensions with no corresponding coordinate
490  // variables
491  MB_CHK_SET_ERR( create_dummy_variables(), "Failed to create dummy variables" );
492 
493  return MB_SUCCESS;
494 }
495 
496 } // namespace moab