In [1]:
# Create a dataframe from a sample CSV file
import pandas as pd
df = pd.read_csv('./data/dsg/timeseriesProfile.csv', parse_dates=['time'])
df.head()
Out[1]:
In [2]:
# Save dataframe as a netCDF file
import os
import tempfile
from pocean.dsg import OrthogonalMultidimensionalTimeseriesProfile as omtsp
output_fp, output = tempfile.mkstemp()
os.close(output_fp)
axes = {
't': 'time',
'x': 'lon',
'y': 'lat',
'z': 'depth'
}
ncd = omtsp.from_dataframe(
df.reset_index(),
output=output,
axes=axes,
mode='a'
)
ncd.naming_authority = 'ioos'
ncd.id = 'Station1'
print(ncd)
ncd.close()
In [3]:
# Create an archive for the file
import shutil
import bagit
import netCDF4 as nc4
with nc4.Dataset(output) as ncd:
urn = 'urn:ioos:station:{naming_authority}:{id}'.format(**ncd.__dict__)
temp_bagit_folder = tempfile.mkdtemp()
temp_data_folder = os.path.join(temp_bagit_folder, 'data')
# Create a new bag
bag = bagit.make_bag(
temp_bagit_folder,
checksum=['sha256']
)
# Copy netCDF file into the bag
shutil.copy2(output, temp_data_folder + '/parameter1.nc')
# Set bagit metadata
bag_meta = {
'Bag-Count': '1 of 1',
'Bag-Group-Identifier': 'ioos_bagit_testing',
'Contact-Name': 'Kyle Wilcox',
'Contact-Phone': '907-230-0304',
'Contact-Email': 'axiom+ncei@axiomdatascience.com',
'External-Identifier': urn,
'External-Description':
'Sensor data from station {}'.format(urn),
'Internal-Sender-Identifier': urn,
'Internal-Sender-Description':
'Station - URN:{}'.format(urn),
'Organization-address':
'1016 W 6th Ave, Ste. 105, Anchorage, AK 99501, USA',
'Source-Organization': 'Axiom Data Science',
}
# Update existing bag
bag.info.update(bag_meta)
bag.save(manifests=True, processes=4)
In [4]:
!tree $temp_bagit_folder
!cat $temp_bagit_folder/manifest-sha256.txt
In [5]:
# Copy a few more files into the bag
shutil.copy2(output, temp_data_folder + '/parameter2.nc')
shutil.copy2(output, temp_data_folder + '/parameter3.nc')
shutil.copy2(output, temp_data_folder + '/parameter4.nc')
bag.save(manifests=True, processes=4)
In [6]:
!tree $temp_bagit_folder
!cat $temp_bagit_folder/manifest-sha256.txt