def test_check_dataset_attrs_car_attrs_good(capsys, nc_dataset): """check_dataset_attrs prints nothing when reqd var attrs present w/ values """ dataset_attrs = ( ('Conventions', 'CF-1.6'), ('title', 'Test Dataset'), ('institution', 'Unit Tests'), ('source', 'foo'), ('references', 'bar'), ('history', 'was'), ('comment', ''), ) for attr, value in dataset_attrs: nc_dataset.setncattr(attr, value) nc_dataset.createDimension('x', 42) foo = nc_dataset.createVariable('foo', float, ('x', )) reqd_attrs = ( ('units', 'foo'), ('long_name', 'bar'), ) for attr, value in reqd_attrs: foo.setncattr(attr, value) nc_tools.check_dataset_attrs(nc_dataset) out, err = capsys.readouterr() assert out == ''
def test_check_dataset_attrs_reqd_var_attr_values(capsys, nc_dataset): """check_dataset_attrs warns of missing reqd variable attr values """ dataset_attrs = ( ('Conventions', 'CF-1.6'), ('title', 'Test Dataset'), ('institution', 'Unit Tests'), ('source', 'foo'), ('references', 'bar'), ('history', 'was'), ('comment', ''), ) for attr, value in dataset_attrs: nc_dataset.setncattr(attr, value) nc_dataset.createDimension('x', 42) foo = nc_dataset.createVariable('foo', float, ('x', )) reqd_attrs = ( 'units', 'long_name', ) for attr in reqd_attrs: foo.setncattr(attr, '') nc_tools.check_dataset_attrs(nc_dataset) out, err = capsys.readouterr() for line, expected in enumerate(reqd_attrs): assert out.splitlines()[line] == ( 'Missing value for variable attribute for foo: {}'.format(expected) )
def test_check_dataset_attrs_good(capsys, nc_dataset): """check_dataset_attrs prints nothing when all reqd attts present w/ value """ dataset_attrs = ( ('Conventions', 'CF-1.6'), ('title', 'Test Dataset'), ('institution', 'Unit Tests'), ('source', 'foo'), ('references', 'bar'), ('history', 'was'), ('comment', ''), ) for attr, value in dataset_attrs: nc_dataset.setncattr(attr, value) nc_tools.check_dataset_attrs(nc_dataset) out, err = capsys.readouterr() assert out == ''
def test_check_dataset_attrs_reqd_dataset_attrs(capsys, nc_dataset): """check_dataset_attrs warns of missing required dataset attributes """ nc_tools.check_dataset_attrs(nc_dataset) out, err = capsys.readouterr() reqd_attrs = ( 'Conventions', 'title', 'institution', 'source', 'references', 'history', 'comment', ) for line, expected in enumerate(reqd_attrs): assert out.splitlines()[line] == ( 'Missing required dataset attribute: {}'.format(expected))
def test_check_dataset_attrs_reqd_dataset_attr_values(capsys, nc_dataset): """check_dataset_attrs warns of missing reqd dataset attr values """ reqd_attrs = ( 'Conventions', 'title', 'institution', 'source', 'references', 'history', ) for attr in reqd_attrs: nc_dataset.setncattr(attr, '') nc_tools.check_dataset_attrs(nc_dataset) out, err = capsys.readouterr() for line, attr in enumerate(reqd_attrs): assert out.splitlines()[line] == ( 'Missing value for dataset attribute: {}'.format(attr))
def test_check_dataset_attrs_url_reqd(capsys, nc_dataset): """check_dataset_attrs warns of source or references set to REQUIRED """ empty_reqd_attrs = ( 'Conventions', 'title', 'institution', 'references', ) for attr in empty_reqd_attrs: nc_dataset.setncattr(attr, 'foo') REQUIRED_reqd_attrs = ( 'source', 'references', ) for attr in REQUIRED_reqd_attrs: nc_dataset.setncattr(attr, 'REQUIRED') nc_tools.check_dataset_attrs(nc_dataset) out, err = capsys.readouterr() for line, attr in enumerate(REQUIRED_reqd_attrs): assert out.splitlines()[line] == ( 'Missing value for dataset attribute: {}'.format(attr))
def create_northern_tides_contd(Z1, Z2, tidevar, constituent, code, name='SalishSea2'): import netCDF4 as NC import numpy as np from salishsea_tools import nc_tools nemo = NC.Dataset(name + '_' + code + '_North_tide_' + constituent + '_grid_' + tidevar + '.nc', 'w', zlib=True) #start and end points starti = 32 endi = 62 lengthi = endi - starti # dataset attributes nc_tools.init_dataset_attrs( nemo, title='Tidal Boundary Conditions for Northern Boundary', notebook_name='johnstone_tides_contd', nc_filepath= '../../../NEMO-forcing/open_boundaries/north/tides/SalishSea2_North_tide_' + constituent + '_grid_' + tidevar + '.nc', comment= 'Tidal current and amplitude data scaled based on differences between K1/M2 and North observations and webtide.' ) # dimensions (only need x and y, don't need depth or time_counter) nemo.createDimension('xb', lengthi) nemo.createDimension('yb', 1) # variables # nbidta, ndjdta, ndrdta nbidta = nemo.createVariable('nbidta', 'int32', ('yb', 'xb')) nbidta.long_name = 'i grid position' nbidta.units = 1 nbjdta = nemo.createVariable('nbjdta', 'int32', ('yb', 'xb')) nbjdta.long_name = 'j grid position' nbjdta.units = 1 nbrdta = nemo.createVariable('nbrdta', 'int32', ('yb', 'xb')) nbrdta.long_name = 'position from boundary' nbrdta.units = 1 print(nbidta.shape) # add in the counter around the boundary (taken from Susan's code in Prepare Tide Files) xb = nemo.createVariable('xb', 'int32', ('xb', ), zlib=True) xb.units = 'non dim' xb.long_name = 'counter around boundary' yb = nemo.createVariable('yb', 'int32', ('yb', ), zlib=True) yb.units = 'non dim' yb.long_name = 'counter along boundary' yb[0] = 897 xb[:] = np.arange(starti, endi) # values # nbidta, nbjdta nbidta[:] = np.arange(starti, endi) nbjdta[:] = 897 nbrdta[:] = 1 if tidevar == 'T': z1 = nemo.createVariable('z1', 'float32', ('yb', 'xb'), zlib=True) z1.units = 'm' z1.long_name = 'tidal elevation: cosine' z2 = nemo.createVariable('z2', 'float32', ('yb', 'xb'), zlib=True) z2.units = 'm' z2.long_name = 'tidal elevation: sine' z1[0, :] = np.array([Z1] * lengthi) z2[0, :] = np.array([Z2] * lengthi) if tidevar == 'U': u1 = nemo.createVariable('u1', 'float32', ('yb', 'xb'), zlib=True) u1.units = 'm' u1.long_name = 'tidal x-velocity: cosine' u2 = nemo.createVariable('u2', 'float32', ('yb', 'xb'), zlib=True) u2.units = 'm' u2.long_name = 'tidal x-velocity: sine' u1[0, 0:lengthi] = Z1[:, 0] u2[0, 0:lengthi] = Z2[:, 0] if tidevar == 'V': v1 = nemo.createVariable('v1', 'float32', ('yb', 'xb'), zlib=True) v1.units = 'm' v1.long_name = 'tidal y-velocity: cosine' v2 = nemo.createVariable('v2', 'float32', ('yb', 'xb'), zlib=True) v2.units = 'm' v2.long_name = 'tidal y-velocity: sine' v1[0, 0:lengthi] = Z1[:, 0] v2[0, 0:lengthi] = Z2[:, 0] nc_tools.check_dataset_attrs(nemo) nemo.close()