// // momvdfcreate code snippet // { // // Initialize POP data readers // NetCDFCOllection ncdfc() ncdfc.Initialize(files); VDC vdc(VDC::NETCDF4); // Open the VDC master file for writing. // status = vdc.Initialize(masterpath, "w"); // Set up compression parameters. We will use the same ones for all // variables. Ditto for boundary periodicity // vdc.SetCompressionBlock(opt.bs, opt.wname, opt.wmode, opt.cratios()); vdc.SetPeriodicBoundary(opt.periodic); // Get dimensions from mom files, and define them in the VDC // For VDC and CF files each dimension has a 1D coordinate variable // associated with it. The 1D coordinate variable and the dimension // have the same name. // vector coordvars= ncdfc.GetCoordVarsCF(); for (int i=0; i dimlens = ncdfc.GetDims(coordvars[i]); assert(dimlens.size() == 1); // netCDF "coord vars" are 1D // Figure out which axis // int axis = 0; if (ncdfc.IsLonCoordVar(coordvars[i])) { axis = 0; } else if (ncdfc.IsLatCoordVar(coordvars[i])) { axis = 1; } else if (ncdfc.IsVertCoordVar(coordvars[i])) { axis = 2; } else if (ncdfc.IsTimeCoordVar(coordvars[i])) { axis = 3; } vdc.DefineDimension(coordvar[i], dimlens[0], axis); } // Now get all of the mom coordinate variables with one or more // dimensions. // vector latcoordvars = ncdfc.GetLatCoordVars(); vector loncoordvars = ncdfc.GetlonCoordVars(); vector vertcoordvars = ncdfc.GetVertCoordVars(); vector timecoordvars = ncdfc.GetTimeCoordVars(); // // Define the coordinate variables in the VDC using same dimension // names as in the mom data set. We're lazy and we've hard-coded the units // (e.g. degrees_east). Should get these from ncdfc. Also, we're // redefining any CF "Coordinate variables" implicitly defined by // DefineDimension(). We need to do this because the implicit // definition does defines no units. // for (int i=0; i dimnames = ncdfc.GetDimNames(loncoordvars[i]); vdc.DefineCoordVar( loncoordvars[i], dimnames, "degrees_east", VDC:X, VDC:FLOAT32, false ); } for (int i=0; i dimnames = ncdfc.GetDimNames(latcoordvars[i]); vdc.DefineCoordVar( latcoordvars[i], dimnames, "degrees_north", VDC:Y, VDC:FLOAT32, false ); } for (int i=0; i dimnames = ncdfc.GetDimNames(vertcoordvars[i]); vdc.DefineCoordVar( vertcoordvars[i], dimnames, "meters", VDC:Z, VDC:FLOAT32, false ); } for (int i=0; i dimnames = ncdfc.GetDimNames(timecoordvars[i]); vdc.DefineCoordVar( timecoordvars[i], dimnames, "seconds", VDC:T, VDC:FLOAT32, false ); } // // Find all of the 3d variables in the mom data set and define them // in the VDC // vector vars = ncdfc.GetVariableNames(3); for (int i=0; i dimnames = ncdfc.GetDimNames(vars[i]); vector coordvars; ncdfc.GetVarCoordVarNames(vars[i], coordvars); mysort(coordvars); // coordinate varibles in VDC must be ordered string units; ncdfc.GetVarUnits(vars[i], units); vdc.DefineDataVar( vars[i], dimnames, coordvars, units, VDC::FLOAT32, true ); } vdc.EndDefine(); // // now copy all of the coordinate variables from the mom files // to the VDC. We use the time hyperslice version of PutVar() // to ensure the data volumes aren't too large. This is // because we're assuming MOM and POP coordinate variables // have at most 2 spatial dimensions. If they could be 3D we'd // want to something that uses less memory // float *data; for (int i=0; i dims = ncdfc.GetDims(cvar); // make sure 'data' is big enouth // data = reallocate_if_needed(data, dims); size_t numts = ncdf.GetNumTimeSteps(); for (size_t ts = 0; ts