Created
June 7, 2024 07:54
-
-
Save gmaze/3cbe3e5997a28c47caf1c3499abc4b9f to your computer and use it in GitHub Desktop.
Read Argo index or data from a DOI snapshot download without untar/decompress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tarfile | |
import gzip | |
import xarray as xr | |
import pandas as pd | |
# Work with a BGC-S doi snapshot | |
# Snapshot was downloaded from: https://www.seanoe.org/data/00311/42182/data/110195.tar.gz | |
file = '/Users/gmaze/Downloads/110195.tar.gz' | |
# Read one of the index file as a pandas dataframe: | |
with tarfile.open(file, mode='r:gz') as tar: | |
for member in tar: | |
if member.name.endswith('argo_synthetic-profile_index.txt.gz'): | |
data = tar.extractfile(member) | |
with gzip.open(data) as f: | |
df = pd.read_csv(f, sep=",", index_col=None, header=0, skiprows=8) | |
break | |
# Read one netcdf file as xarray dataset: | |
with tarfile.open(file, mode='r:gz') as tar: | |
for member in tar: | |
if member.name.endswith('dac/aoml/1900722_Sprof.nc'): | |
data = tar.extractfile(member) | |
ds = xr.open_dataset(data, engine='argo') # requires argopy | |
# ds = xr.open_dataset(data, decode_cf=1, use_cftime=0, mask_and_scale=1) # otherwise | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment