mockup_data.py 1.13 KB
Newer Older
BorjaEst's avatar
BorjaEst committed
1
2
3
4
"""This modules creates mockup data for testing"""

import xarray as xr
import numpy as np
5
6
import netCDF4
import os.path
7
import datetime
8
from o3skim import utils
BorjaEst's avatar
BorjaEst committed
9
10


11
12
base = datetime.datetime(2000, 1, 1)
indexes = {
13
    'time': [base + datetime.timedelta(days=10*i) for i in range(99)],
14
15
16
17
    'plev': [x for x in range(1, 1000, 100)],
    'lat':  [x for x in range(-90, 90, 10)],
    'lon':  [x for x in range(-180, 180, 20)]
}
BorjaEst's avatar
BorjaEst committed
18
19
20


def data_vars(coordinades):
21
    """Creates a mock n-array with coordinade values"""
BorjaEst's avatar
BorjaEst committed
22
    alias = tuple(coordinades.values())
23
    dim = tuple(len(indexes[c]) for c in coordinades)
BorjaEst's avatar
BorjaEst committed
24
25
26
27
    return alias, np.ones(dim),


def map_coord(coordinades):
28
    """Creates a mock array by coordinade"""
29
    return {alias: indexes[c] for c, alias in coordinades.items()}
BorjaEst's avatar
BorjaEst committed
30
31


32
33
34
def dataset(name, coordinades):
    """Creates a dataset acording to the global module indexes"""
    return xr.Dataset(
BorjaEst's avatar
BorjaEst committed
35
36
37
        {name: data_vars(coordinades)},
        coords=map_coord(coordinades)
    )
38
39


40
41
42
def netcdf(path, name, coordinades, **kwarg):
    """Creates or appends data to a mock netcdf file"""
    ds = dataset(name, coordinades)
43
    utils.to_netcdf(path, name, ds)