def test_netcdf_monitor_multiple_times_batched_all_vars(): time_list = [ datetime(2013, 7, 20, 0), datetime(2013, 7, 20, 6), datetime(2013, 7, 20, 12), ] current_state = state.copy() try: assert not os.path.isfile('out.nc') monitor = NetCDFMonitor('out.nc') for time in time_list: current_state['time'] = time monitor.store(current_state) assert not os.path.isfile('out.nc') # not set to write on store monitor.write() assert os.path.isfile('out.nc') with xr.open_dataset('out.nc') as ds: assert len(ds.data_vars.keys()) == 2 assert 'air_temperature' in ds.data_vars.keys() assert ds.data_vars['air_temperature'].attrs['units'] == 'degK' assert tuple( ds.data_vars['air_temperature'].shape) == (len(time_list), nx, ny, nz) assert 'air_pressure' in ds.data_vars.keys() assert ds.data_vars['air_pressure'].attrs['units'] == 'Pa' assert tuple( ds.data_vars['air_pressure'].shape) == (len(time_list), nx, ny, nz) assert len(ds['time']) == len(time_list) assert np.all(ds['time'].values == [np.datetime64(time) for time in time_list]) finally: # make sure we remove the output file if os.path.isfile('out.nc'): os.remove('out.nc')
def test_netcdf_monitor_single_time_all_vars(): try: assert not os.path.isfile('out.nc') monitor = NetCDFMonitor('out.nc') monitor.store(state) assert not os.path.isfile('out.nc') # not set to write on store monitor.write() assert os.path.isfile('out.nc') with xr.open_dataset('out.nc') as ds: assert len(ds.data_vars.keys()) == 2 assert 'air_temperature' in ds.data_vars.keys() assert ds.data_vars['air_temperature'].attrs['units'] == 'degK' assert tuple(ds.data_vars['air_temperature'].shape) == (1, nx, ny, nz) assert 'air_pressure' in ds.data_vars.keys() assert ds.data_vars['air_pressure'].attrs['units'] == 'Pa' assert tuple(ds.data_vars['air_pressure'].shape) == (1, nx, ny, nz) assert len(ds['time']) == 1 assert ds['time'][0] == np.datetime64(state['time']) finally: # make sure we remove the output file if os.path.isfile('out.nc'): os.remove('out.nc')
def test_netcdf_monitor_raises_when_names_change_on_batch_write(): current_state = state.copy() try: assert not os.path.isfile('out.nc') monitor = NetCDFMonitor('out.nc') current_state['time'] = datetime(2013, 7, 20, 0) monitor.store(current_state) assert not os.path.isfile('out.nc') current_state['time'] = datetime(2013, 7, 20, 6) current_state['air_density'] = current_state['air_pressure'] monitor.store(current_state) try: monitor.write() except InvalidStateError: pass except Exception as err: raise err else: raise AssertionError( 'Expected InvalidStateError but was not raised.') finally: # make sure we remove the output file if os.path.isfile('out.nc'): os.remove('out.nc')