Commit 27662354 authored by BorjaEst's avatar BorjaEst
Browse files

Redit path by dirname to specify directory where to generate netcdf

parent a1e18260
......@@ -50,10 +50,10 @@ def create_empty_netCDF(fname):
root_grp.close()
def to_netcdf(path, name, dataset):
def to_netcdf(dirname, name, dataset):
"""Creates or appends data to named netcdf files"""
years, dsx = zip(*dataset.groupby("time.year"))
fnames = [path + "/" + name + "_%s.nc" % y for y in years]
fnames = [dirname + "/" + name + "_%s.nc" % y for y in years]
logging.info("Save dataset into: %s", fnames)
[create_empty_netCDF(fn) for fn in fnames if not os.path.isfile(fn)]
xr.save_mfdataset(dsx, fnames, mode='a')
......@@ -2,9 +2,8 @@
import xarray as xr
import numpy as np
import netCDF4
import os.path
import datetime
import os
from o3skim import utils
......@@ -37,7 +36,7 @@ def dataset(name, coordinades):
)
def netcdf(path, name, coordinades, **kwarg):
def netcdf(dirname, name, coordinades, **kwarg):
"""Creates or appends data to a mock netcdf file"""
ds = dataset(name, coordinades)
utils.to_netcdf(path, name, ds)
utils.to_netcdf(dirname, name, ds)
......@@ -37,8 +37,9 @@ class TestO3SKIM_sources(unittest.TestCase):
for _, collection in self.config_base.items():
for _, variables in collection.items():
for _, vinfo in variables.items():
os.makedirs(vinfo["dir"], exist_ok=True)
mockup_data.netcdf(vinfo["dir"], **vinfo)
dirname = os.path.dirname(vinfo['paths'])
os.makedirs(dirname, exist_ok=True)
mockup_data.netcdf(dirname, **vinfo)
def create_noise_datasets(self):
"""Creates noise data files according to the noise configuration"""
......@@ -62,9 +63,8 @@ class TestO3SKIM_sources(unittest.TestCase):
self.ds_backup[source] = {}
for model, variables in collection.items():
self.ds_backup[source][model] = {}
for v, vinfo in variables.items():
paths = vinfo["dir"] + "/" + vinfo["name"] + "_????.nc"
with xr.open_mfdataset(paths) as ds:
for v, vinfo in variables.items():
with xr.open_mfdataset(vinfo['paths']) as ds:
self.ds_backup[source][model][v] = ds
def assert_with_backup(self):
......@@ -73,8 +73,7 @@ class TestO3SKIM_sources(unittest.TestCase):
for source, collection in self.config_base.items():
for model, variables in collection.items():
for v, vinfo in variables.items():
paths = vinfo["dir"] + "/" + vinfo["name"] + "_????.nc"
with xr.open_mfdataset(paths) as ds:
with xr.open_mfdataset(vinfo['paths']) as ds:
xr.testing.assert_identical(
self.ds_backup[source][model][v], ds)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment