"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/modules/python/test_pyfimex0_PyCDMReader.py" (30 Oct 2019, 3069 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) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Fimex, modules/python/test_pyfimex0.py
    2 #
    3 # Copyright (C) 2019 met.no
    4 #
    5 # Contact information:
    6 # Norwegian Meteorological Institute
    7 # Box 43 Blindern
    8 # 0313 OSLO
    9 # NORWAY
   10 # email: diana@met.no
   11 #
   12 # Project Info:  https://wiki.met.no/fimex/start
   13 #
   14 # This library is free software; you can redistribute it and/or modify it
   15 # under the terms of the GNU Lesser General Public License as published by
   16 # the Free Software Foundation; either version 2.1 of the License, or
   17 # (at your option) any later version.
   18 #
   19 # This library is distributed in the hope that it will be useful, but
   20 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   21 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   22 # License for more details.
   23 #
   24 # You should have received a copy of the GNU Lesser General Public
   25 # License along with this library; if not, write to the Free Software
   26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   27 # USA.
   28 
   29 import pyfimex0 as pyfi
   30 import numpy as np
   31 import sys
   32 
   33 import unittest
   34 
   35 class PyCDMReader(pyfi.CDMReader):
   36     def __init__(self):
   37         pyfi.CDMReader.__init__(self)
   38         cdm = self.getInternalCDM()
   39         cdm.addDimension(pyfi.CDMDimension('dimx', 12))
   40         cdm.addVariable(pyfi.CDMVariable('varx', pyfi.CDMDataType.FLOAT, ['dimx']))
   41 
   42     def getDataSlice(self, var, uldp):
   43         if var == 'varx':
   44             return pyfi.createData(np.arange(12, dtype=np.float32))
   45         else:
   46             return None
   47 
   48 
   49 class PyCDMFilter(pyfi.CDMReader):
   50     def __init__(self, reader):
   51         pyfi.CDMReader.__init__(self)
   52         self.reader = reader
   53         self.setInternalCDM(reader.getCDM())
   54 
   55     def transform(self, var, data):
   56         if var == 'varx' and data is not None:
   57             v = data.values() * -1
   58             data = pyfi.createData(v)
   59         return data
   60 
   61     def getDataSlice(self, var, uldp):
   62         return self.transform(var, self.reader.getDataSlice(var, uldp))
   63 
   64 class TestPyCDMReader(unittest.TestCase):
   65 
   66     def test_CDM(self):
   67         r = PyCDMReader()
   68         cdm = r.getCDM()
   69         self.assertEqual(["dimx"], cdm.getDimensionNames())
   70         self.assertEqual(["varx"], cdm.getVariableNames())
   71         pyfi.createFileWriter(r, "null", "")
   72 
   73     def test_DataSliceUDP(self):
   74         r = PyCDMReader()
   75         self.assertIsNone(r.getDataSlice("no-such-var", 0))
   76         d = r.getDataSlice("varx", 0)
   77         self.assertEqual(12, d.size())
   78 
   79     def test_DataSliceSB(self):
   80         r = PyCDMReader()
   81         sb = pyfi.SliceBuilder(r.getCDM(), 'varx', True)
   82         sb.setStartAndSize('dimx', 4, 4)
   83         d = r.getDataSliceSB("varx", sb)
   84         self.assertEqual([4,5,6,7], list(d.values()))
   85 
   86     def test_Filter(self):
   87         r = PyCDMReader()
   88         f = PyCDMFilter(r)
   89 
   90         sb = pyfi.SliceBuilder(f.getCDM(), 'varx', True)
   91         sb.setStartAndSize('dimx', 4, 4)
   92         d = f.getDataSliceSB("varx", sb)
   93         self.assertEqual([-4,-5,-6,-7], list(d.values()))
   94         pyfi.createFileWriter(f, "null", "")
   95 
   96 if __name__ == '__main__':
   97     unittest.main()