def test_DataString(self): d = pyfimex0.createData("hello") self.assertEqual(pyfimex0.CDMDataType.STRING, d.getDataType()) self.assertEqual(5, d.size()) self.assertEqual("hello", d.values()) d = pyfimex0.createData(pyfimex0.CDMDataType.STRING, "values") self.assertEqual(pyfimex0.CDMDataType.STRING, d.getDataType()) self.assertEqual(6, d.size()) self.assertEqual("values", d.values())
def test_scaled(self): test_rwfile = 'test_scaled.nc' shutil.copyfile(os.path.join(test_srcdir, 'test_merge_inner.nc'), test_rwfile) addF = 1.0 addK = addF * 5.0 / 9.0 rw = pyfimex0.createFileReaderWriter('netcdf', test_rwfile) self.assertIsNotNone(rw) read1 = rw.getScaledDataSlice("ga_2t_1", 0) self.assertIsNotNone(read1) write1 = rw.getScaledDataSliceInUnit("ga_2t_1", "deg_F", 0) self.assertIsNotNone(write1) wmod1 = pyfimex0.createData(pyfimex0.CDMDataType.DOUBLE, [x + addF for x in write1.values()]) rw.putScaledDataSliceInUnit("ga_2t_1", "deg_F", 0, wmod1) del rw # read back and compare r = pyfimex0.createFileReader('netcdf', test_rwfile) self.assertIsNotNone(r) read2 = r.getScaledDataSlice("ga_2t_1", 0) self.assertIsNotNone(read2) values1 = read1.values() values2 = read2.values() self.assertEqual(len(values1), len(values2)) for i, (v1, v2), in enumerate(zip(values1, values2)): self.assertAlmostEqual(v1 + addK, v2, msg="at index {}".format(i))
def test_AttributeFloat(self): att = pyfimex0.CDMAttribute( "f", pyfimex0.createData(pyfimex0.CDMDataType.FLOAT, [1])) self.assertEqual("f", att.getName()) self.assertEqual("1", att.getStringValue()) self.assertEqual([1.0], att.getData().values()) self.assertEqual(pyfimex0.CDMDataType.FLOAT, att.getDataType())
def test_update(self): test_rwfile = 'test_update.nc' shutil.copyfile(os.path.join(test_srcdir, 'test_merge_inner.nc'), test_rwfile) diff = 10.0 scale = 1.2 rw = pyfimex0.createFileReaderWriter('netcdf', test_rwfile) self.assertIsNotNone(rw) read1 = rw.getDataSlice("ga_2t_1", 0) self.assertIsNotNone(read1) write1 = pyfimex0.createData( pyfimex0.CDMDataType.DOUBLE, [diff + x * scale for x in read1.values()]) rw.putDataSlice("ga_2t_1", 0, write1) del rw r = pyfimex0.createFileReader('netcdf', test_rwfile) self.assertIsNotNone(r) read2 = r.getScaledDataSlice("ga_2t_1", 0) self.assertIsNotNone(read2) values1 = read1.values() values2 = read2.values() self.assertEqual(len(values1), len(values2)) for i, (v1, v2), in enumerate(zip(values1, values2)): self.assertAlmostEqual(diff + v1 * scale, v2, msg="at index {}".format(i))
def test_AttributeUChar(self): l = [0, 12, 234] att = pyfimex0.CDMAttribute( "u", pyfimex0.createData(pyfimex0.CDMDataType.UCHAR, l)) self.assertEqual(" ".join([str(x) for x in l]), att.getStringValue()) d = att.getData() self.assertEqual(len(l), d.size()) v = d.values() self.assertEqual(numpy.uint8, v.dtype) self.assertEqual(l, list(v))
def test_AttributeString(self): att = pyfimex0.CDMAttribute("name", "value") self.assertEqual("name", att.getName()) self.assertEqual("value", att.getStringValue()) self.assertEqual(pyfimex0.CDMDataType.STRING, att.getDataType()) att.setName("navn") self.assertEqual("navn", att.getName()) att.setData(pyfimex0.createData("content")) self.assertEqual("content", att.getStringValue()) self.assertEqual(pyfimex0.CDMDataType.STRING, att.getDataType())
def test_CDM_variables(self): cdm = pyfimex0.CDM() self.assertEqual(0, len(cdm.getVariableNames())) cdm.addVariable( pyfimex0.CDMVariable("varf", pyfimex0.CDMDataType.FLOAT, ['x', 'y'])) self.assertEqual(['varf'], cdm.getVariableNames()) cdm.addAttribute( 'varf', pyfimex0.CDMAttribute( '_FillValue', pyfimex0.createData(pyfimex0.CDMDataType.FLOAT, -1))) self.assertEqual(1, len(cdm.getAttributeNames('varf'))) cdm.removeVariable('varf') self.assertEqual(0, len(cdm.getVariableNames()))
def test_DataFloat(self): d = pyfimex0.createData(pyfimex0.CDMDataType.FLOAT, [1, 2, 3]) self.assertEqual(pyfimex0.CDMDataType.FLOAT, d.getDataType()) self.assertEqual(3, d.size()) self.assertEqual(numpy.float32, d.values().dtype)
def test_DataUChar(self): d = pyfimex0.createData(numpy.arange(5, dtype=numpy.uint8)) self.assertEqual(pyfimex0.CDMDataType.UCHAR, d.getDataType())
def transform(self, var, data): if var == 'varx' and data is not None: v = data.values() * -1 data = pyfi.createData(v) return data
def getDataSlice(self, var, uldp): if var == 'varx': return pyfi.createData(np.arange(12, dtype=np.float32)) else: return None