def test_maskandscale(): t = np.linspace(20, 30, 15) t[3] = 100 tm = np.ma.masked_greater(t, 99) fname = pjoin(TEST_DATA_PATH, 'example_2.nc') with netcdf_file(fname, maskandscale=True) as f: Temp = f.variables['Temperature'] assert_equal(Temp.missing_value, 9999) assert_equal(Temp.add_offset, 20) assert_equal(Temp.scale_factor, np.float32(0.01)) found = Temp[:].compressed() del Temp # Remove ref to mmap, so file can be closed. expected = np.round(tm.compressed(), 2) assert_allclose(found, expected) with in_tempdir(): newfname = 'ms.nc' f = netcdf_file(newfname, 'w', maskandscale=True) f.createDimension('Temperature', len(tm)) temp = f.createVariable('Temperature', 'i', ('Temperature',)) temp.missing_value = 9999 temp.scale_factor = 0.01 temp.add_offset = 20 temp[:] = tm f.close() with netcdf_file(newfname, maskandscale=True) as f: Temp = f.variables['Temperature'] assert_equal(Temp.missing_value, 9999) assert_equal(Temp.add_offset, 20) assert_equal(Temp.scale_factor, np.float32(0.01)) expected = np.round(tm.compressed(), 2) found = Temp[:].compressed() del Temp assert_allclose(found, expected)
def test_maskandscale(): t = np.linspace(20, 30, 15) t[3] = 100 tm = np.ma.masked_greater(t, 99) fname = pjoin(TEST_DATA_PATH, 'example_2.nc') with netcdf_file(fname, maskandscale=True) as f: Temp = f.variables['Temperature'] assert_equal(Temp.missing_value, 9999) assert_equal(Temp.add_offset, 20) assert_equal(Temp.scale_factor, np.float32(0.01)) found = Temp[:].compressed() del Temp # Remove ref to mmap, so file can be closed. expected = np.round(tm.compressed(), 2) assert_allclose(found, expected) with in_tempdir(): newfname = 'ms.nc' f = netcdf_file(newfname, 'w', maskandscale=True) f.createDimension('Temperature', len(tm)) temp = f.createVariable('Temperature', 'i', ('Temperature', )) temp.missing_value = 9999 temp.scale_factor = 0.01 temp.add_offset = 20 temp[:] = tm f.close() with netcdf_file(newfname, maskandscale=True) as f: Temp = f.variables['Temperature'] assert_equal(Temp.missing_value, 9999) assert_equal(Temp.add_offset, 20) assert_equal(Temp.scale_factor, np.float32(0.01)) expected = np.round(tm.compressed(), 2) found = Temp[:].compressed() del Temp assert_allclose(found, expected)
def test_in_tempdir(): my_cwd = getcwd() with in_tempdir() as tmpdir: with open('test.txt', 'wt') as f: f.write('some text') assert_(isfile('test.txt')) assert_(isfile(pjoin(tmpdir, 'test.txt'))) assert_(not exists(tmpdir)) assert_equal(getcwd(), my_cwd)
def test_byte_gatts(): # Check that global "string" atts work like they did before py3k # unicode and general bytes confusion with in_tempdir(): filename = 'g_byte_atts.nc' f = netcdf_file(filename, 'w') f._attributes['holy'] = b'grail' f._attributes['witch'] = 'floats' f.close() f = netcdf_file(filename, 'r') assert_equal(f._attributes['holy'], b'grail') assert_equal(f._attributes['witch'], b'floats') f.close()
def test_record_variable_preceeds_scalar_variable(): # ticket #6203 with in_tempdir(): with netcdf_file('6203.nc', "w") as ncfile: nx = 3 data = np.arange(3) ncfile.createDimension('nframes', None) ncfile.createDimension('nx', nx) var_data = ncfile.createVariable("data", 'i', ('nframes', 'nx')) var_lon0 = ncfile.createVariable("lon0", 'i', ()) var_data[0, :] = data var_lon0.assignValue(34) ncfile.close() # If the record variable preceeds the non-record variable, the # constructor here will error out. other_netcdf_impl.Dataset('6203.nc')
def test_open_append(): # open 'w' put one attr with in_tempdir(): filename = 'append_dat.nc' f = netcdf_file(filename, 'w') f._attributes['Kilroy'] = 'was here' f.close() # open again in 'a', read the att and and a new one f = netcdf_file(filename, 'a') assert_equal(f._attributes['Kilroy'], b'was here') f._attributes['naughty'] = b'Zoot' f.close() # open yet again in 'r' and check both atts f = netcdf_file(filename, 'r') assert_equal(f._attributes['Kilroy'], b'was here') assert_equal(f._attributes['naughty'], b'Zoot') f.close()
def test_append_recordDimension(): dataSize = 100 with in_tempdir(): # Create file with record time dimension with netcdf_file('withRecordDimension.nc', 'w') as f: f.createDimension('time', None) f.createVariable('time', 'd', ('time', )) f.createDimension('x', dataSize) x = f.createVariable('x', 'd', ('x', )) x[:] = np.array(range(dataSize)) f.createDimension('y', dataSize) y = f.createVariable('y', 'd', ('y', )) y[:] = np.array(range(dataSize)) f.createVariable('testData', 'i', ('time', 'x', 'y')) f.flush() f.close() for i in range(2): # Open the file in append mode and add data with netcdf_file('withRecordDimension.nc', 'a') as f: f.variables['time'].data = np.append(f.variables["time"].data, i) f.variables['testData'][i, :, :] = np.ones( (dataSize, dataSize)) * i f.flush() # Read the file and check that append worked with netcdf_file('withRecordDimension.nc') as f: assert_equal(f.variables['time'][-1], i) assert_equal(f.variables['testData'][-1, :, :].copy(), np.ones((dataSize, dataSize)) * i) assert_equal(f.variables['time'].data.shape[0], i + 1) assert_equal(f.variables['testData'].data.shape[0], i + 1) # Read the file and check that 'data' was not saved as user defined # attribute of testData variable during append operation with netcdf_file('withRecordDimension.nc') as f: with assert_raises(KeyError) as ar: f.variables['testData']._attributes['data'] ex = ar.exception assert_equal(ex.args[0], 'data')
def test_append_recordDimension(): dataSize = 100 with in_tempdir(): # Create file with record time dimension with netcdf_file('withRecordDimension.nc', 'w') as f: f.createDimension('time', None) f.createVariable('time', 'd', ('time',)) f.createDimension('x', dataSize) x = f.createVariable('x', 'd', ('x',)) x[:] = np.array(range(dataSize)) f.createDimension('y', dataSize) y = f.createVariable('y', 'd', ('y',)) y[:] = np.array(range(dataSize)) f.createVariable('testData', 'i', ('time', 'x', 'y')) f.flush() f.close() for i in range(2): # Open the file in append mode and add data with netcdf_file('withRecordDimension.nc', 'a') as f: f.variables['time'].data = np.append(f.variables["time"].data, i) f.variables['testData'][i, :, :] = np.ones((dataSize, dataSize))*i f.flush() # Read the file and check that append worked with netcdf_file('withRecordDimension.nc') as f: assert_equal(f.variables['time'][-1], i) assert_equal(f.variables['testData'][-1, :, :].copy(), np.ones((dataSize, dataSize))*i) assert_equal(f.variables['time'].data.shape[0], i+1) assert_equal(f.variables['testData'].data.shape[0], i+1) # Read the file and check that 'data' was not saved as user defined # attribute of testData variable during append operation with netcdf_file('withRecordDimension.nc') as f: with assert_raises(KeyError) as ar: f.variables['testData']._attributes['data'] ex = ar.value assert_equal(ex.args[0], 'data')