"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/src/metgm/MetGmVerticalTag.cc" (30 Oct 2019, 4627 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. For more information about "MetGmVerticalTag.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Fimex
    3  *
    4  * (C) Copyright 2011, met.no
    5  *
    6  * Project Info:  https://wiki.met.no/fimex/start
    7  *
    8  * This library is free software; you can redistribute it and/or modify it
    9  * under the terms of the GNU Lesser General Public License as published by
   10  * the Free Software Foundation; either version 2.1 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This library is distributed in the hope that it will be useful, but
   14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   16  * License for more details.
   17  *
   18  * You should have received a copy of the GNU Lesser General Public
   19  * License along with this library; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   21  * USA.
   22  */
   23 
   24 // internals
   25 //
   26 #include "MetGmVerticalTag.h"
   27 #include "MetGmGroup3Ptr.h"
   28 #include "MetGmFileHandlePtr.h"
   29 #include "MetGmHandlePtr.h"
   30 #include "MetGmUtils.h"
   31 
   32 
   33 // fimex
   34 //
   35 #include "fimex/CDM.h"
   36 #include "fimex/CDMException.h"
   37 #include "fimex/CDMReader.h"
   38 #include "fimex/CDMVariable.h"
   39 #include "fimex/Data.h"
   40 #include "fimex/StringUtils.h"
   41 #include "fimex/coordSys/CoordinateSystem.h"
   42 
   43 namespace MetNoFimex {
   44 
   45 std::shared_ptr<MetGmVerticalTag> MetGmVerticalTag::createMetGmVerticalTagForWriting(const CDMReader_p pCdmReader, const CDMVariable* pVariable)
   46 {
   47     std::shared_ptr<MetGmVerticalTag> VTag;
   48 
   49     const CoordinateSystem_cp_v coordSys = listCoordinateSystems(pCdmReader);
   50 
   51     CoordinateSystem_cp cs = findCompleteCoordinateSystemFor(coordSys, pVariable->getName());
   52     if (cs.get()) {
   53         if(cs->isSimpleSpatialGridded()) {
   54 
   55             CoordinateAxis_cp zAxis = cs->getGeoZAxis();
   56 
   57             if(!zAxis.get()) {
   58                 return std::shared_ptr<MetGmVerticalTag>();
   59             }
   60 
   61             VTag = std::shared_ptr<MetGmVerticalTag>(new MetGmVerticalTag());
   62 
   63             DataPtr data;
   64 
   65             data = pCdmReader->getData(zAxis->getName());
   66 
   67             /* do something with the data */
   68             if(zAxis->getAxisType() == CoordinateAxis::Pressure) {
   69                 VTag->pr_ = 2;
   70                 data = pCdmReader->getScaledDataInUnit(zAxis->getName(), "hPa");
   71             } else if (zAxis->getAxisType() == CoordinateAxis::Height) {
   72                 VTag->pr_ = 1; // as default
   73                 if (ends_with(pVariable->getName(), "_MSL")) {
   74                     VTag->pr_ = 0;
   75                 }
   76                 data = pCdmReader->getScaledDataInUnit(zAxis->getName(), "m");
   77             }
   78             VTag->pz_ = 1;
   79             VTag->nz_= data->size();
   80             VTag->extractVerticalPoints(data);
   81         }
   82     } else {
   83         /* vertical coordinate not found */
   84     }
   85 
   86     return VTag;
   87 }
   88 
   89 std::shared_ptr<MetGmVerticalTag> MetGmVerticalTag::createMetGmVerticalTagForReading(std::shared_ptr<MetGmGroup3Ptr> pGp3,
   90                                                                                      std::shared_ptr<MetGmVerticalTag> prevTag)
   91 {
   92     std::shared_ptr<MetGmVerticalTag> VTag = std::shared_ptr<MetGmVerticalTag>(new MetGmVerticalTag);
   93 
   94     if (pGp3->pz() == 0) {
   95         /**
   96          * same Z data as in previous parameter do deep copy
   97          */
   98         if (prevTag.get() == 0 || prevTag->nz() == 0 || prevTag->points().get() == 0)
   99             throw CDMException("we have pz = 0 and previous vTag = 0");
  100 
  101         VTag->nz_ = pGp3->nz();
  102         VTag->pz_ = pGp3->pz();
  103         VTag->pr_ = pGp3->pr();
  104 
  105         // but take the data from prev
  106         VTag->points() = make_shared_array<float>(VTag->nz());
  107 
  108         memcpy(VTag->points().get(), prevTag->points().get(), prevTag->nz() * sizeof(float));
  109 
  110         return VTag; // return copy
  111     }
  112 
  113     if (pGp3->p_id() == 0) {
  114 
  115         VTag->nz_ = 1;
  116         VTag->pr_ = 0;
  117         VTag->pz_ = 1;
  118 
  119         VTag->points_ = make_shared_array<float>(VTag->nz());
  120         VTag->points_[0] = 0.0;
  121 
  122         MGM_THROW_ON_ERROR(mgm_skip_group4(*pGp3->mgmHandle()->fileHandle(), *pGp3->mgmHandle()))
  123         return VTag;
  124     }
  125 
  126     VTag->nz_ = pGp3->nz();
  127     VTag->pz_ = pGp3->pz();
  128     VTag->pr_ = pGp3->pr();
  129 
  130     VTag->points_ = make_shared_array<float>(VTag->nz());
  131 
  132     MGM_THROW_ON_ERROR(mgm_read_group4(*pGp3->mgmHandle()->fileHandle(), *pGp3->mgmHandle(), VTag->points_.get()))
  133 
  134     return VTag;
  135     }
  136 
  137     bool MetGmVerticalTag::hasNegativePoints() {
  138         return std::find_if(&points_[0], &points_[nz_], [](float f) { return f <= 0; }) != &points_[nz_];
  139     }
  140 
  141     void MetGmVerticalTag::extractVerticalPoints(const DataPtr& data)
  142     {
  143         points_ = data->asFloat();
  144     }
  145 }