"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/share/doc/examples/coordinateSystem.cpp" (30 Oct 2019, 3003 Bytes) of package /linux/privat/fimex-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "fimex/coordSys/CoordinateSystem.h"
    2 #include "fimex/CoordinateSystemSliceBuilder.h"
    3 #include "fimex/CDMFileReaderFactory.h"
    4 #include "fimex/CDMReader.h"
    5 
    6 using namespace MetNoFimex;
    7 using namespace std;
    8 
    9 int main(int argc, char* args[]) {
   10     std::shared_ptr<CDMReader> reader = CDMFileReaderFactory::create("netcdf", "coordTest.nc");
   11     // get all coordinate systems from file, usually one, but may be a few (theoretical limit: # of variables)
   12     CoordinateSystem_cp_v coordSys = listCoordinateSystems(reader);
   13     const CDM& cdm = reader->getCDM();
   14 
   15     // find an appropriate coordinate system for a variable
   16     string varName = "air_temperature";
   17     CoordinateSystem_cp_v::iterator varSysIt = find_if(coordSys.begin(), coordSys.end(), CompleteCoordinateSystemForComparator(varName));
   18     if (varSysIt != coordSys.end()) {
   19         if ((*varSysIt)->isSimpleSpatialGridded()) {
   20             CoordinateSystem::ConstAxisPtr xAxis = (*varSysIt)->getGeoXAxis(); // X or Lon
   21             CoordinateSystem::ConstAxisPtr yAxis = (*varSysIt)->getGeoYAxis(); // Y or Lat
   22             CoordinateSystem::ConstAxisPtr tAxis = (*varSysIt)->getTimeAxis(); // time
   23 
   24             CoordinateSystemSliceBuilder sb(cdm, *varSysIt);
   25             // handling of time
   26             if (tAxis.get() != 0) {
   27                 // time-Axis, eventually multi-dimensional, i.e. forecast_reference_time
   28                 if ((*varSysIt)->hasAxisType(CoordinateAxis::ReferenceTime)) {
   29                     CoordinateSystem::ConstAxisPtr rtAxis = (*varSysIt)->findAxisOfType(CoordinateAxis::ReferenceTime);
   30                     DataPtr refTimes = reader->getScaledDataInUnit(rtAxis->getName(),"seconds since 1970-01-01 00:00:00");
   31                     /* do something with the refTimes and select the wanted Position */
   32                     size_t refTimePos = 3; /* or whatever you select between 0 (default) and refTimes->size()-1 */
   33                     sb.setReferenceTimePos(refTimePos);
   34                 }
   35                 DataPtr times = reader->getDataSlice(tAxis->getName(), sb.getTimeVariableSliceBuilder());
   36                 /* select the desired startTime and the sice for the time-slices */
   37                 // fetch the 2nd and 3rd time-step of the 4th run
   38                 sb.setTimeStartAndSize(1, 2); // default is all of ReferenceTimePos
   39             }
   40 
   41 
   42             // further selection of data
   43             // select 3-7 y-points
   44             sb.setStartAndSize(yAxis, 3, 5);
   45             sb.setAll(xAxis);
   46 
   47             // by default, all other dimensions are fetched at maximum size
   48             // here, I reduce them to the first slice
   49             vector<string> dims = sb.getUnsetDimensionNames();
   50             for (vector<string>::iterator dim = dims.begin(); dim != dims.end(); ++dim) {
   51                 sb.setStartAndSize(*dim, 0, 1);
   52             }
   53 
   54             // fetch the data
   55             DataPtr data = reader->getDataSlice(varName, sb);
   56             /* do something with the data */
   57         }
   58     }
   59     return 0;
   60 }
   61