Skip to content

Base

create_grid_file(*args, **kwargs)

Backward-compatible wrapper for creating a grid file.

Source code in isca_tools/time_series/base.py
12
13
14
def create_grid_file(*args, **kwargs):
    """Backward-compatible wrapper for creating a grid file."""
    return _create_grid_file(*args, **kwargs)

create_time_arr(*args, **kwargs)

Backward-compatible wrapper for creating time coordinates.

Source code in isca_tools/time_series/base.py
17
18
19
def create_time_arr(*args, **kwargs):
    """Backward-compatible wrapper for creating time coordinates."""
    return _create_time_arr(*args, **kwargs)

create_time_series_file(file_name, namelist_file, var_name, var_func, time_spacing, exp_dir=None, grid_file=None, pressure_var=None, **kwargs)

Create an Isca time-varying input file from a user-supplied function.

This is a convenience wrapper around :mod:isca_tools.run.create_files.write_var. The callback should produce values for the supplied time, pressure, latitude and longitude arrays, with the time axis first. For example::

def co2_func(days, pressure, lat, lon):
    arr = np.ones((len(days), len(pressure), len(lat), len(lon))) * 600
    arr[days < 360 * 5] = 300
    return arr

Parameters:

Name Type Description Default
file_name str

Output file name, usually ending in .nc.

required
namelist_file str

Path to the experiment namelist.

required
var_name str

Name to store in the NetCDF file.

required
var_func Callable

Function returning the variable values on the chosen grid.

required
time_spacing int

Interval between values in days.

required
exp_dir Optional[str]

Optional override for the experiment directory. Defaults to the namelist directory.

None
grid_file Optional[str]

Optional explicit grid file path. If omitted one is created for the experiment resolution.

None
pressure_var Optional[ndarray]

Optional pressure coordinate override.

None
Source code in isca_tools/time_series/base.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def create_time_series_file(
    file_name: str,
    namelist_file: str,
    var_name: str,
    var_func: Callable,
    time_spacing: int,
    exp_dir: Optional[str] = None,
    grid_file: Optional[str] = None,
    pressure_var: Optional[np.ndarray] = None,
    **kwargs,
) -> str:
    """
    Create an Isca time-varying input file from a user-supplied function.

    This is a convenience wrapper around :mod:`isca_tools.run.create_files.write_var`.
    The callback should produce values for the supplied time, pressure, latitude and longitude arrays,
    with the time axis first. For example::

        def co2_func(days, pressure, lat, lon):
            arr = np.ones((len(days), len(pressure), len(lat), len(lon))) * 600
            arr[days < 360 * 5] = 300
            return arr

    Args:
        file_name: Output file name, usually ending in ``.nc``.
        namelist_file: Path to the experiment namelist.
        var_name: Name to store in the NetCDF file.
        var_func: Function returning the variable values on the chosen grid.
        time_spacing: Interval between values in days.
        exp_dir: Optional override for the experiment directory. Defaults to the namelist directory.
        grid_file: Optional explicit grid file path. If omitted one is created for the experiment resolution.
        pressure_var: Optional pressure coordinate override.
    """
    if exp_dir is None:
        exp_dir = os.path.dirname(os.path.abspath(namelist_file))

    namelist = load_namelist(namelist_file=os.path.join(exp_dir, os.path.basename(namelist_file)))
    res = int(namelist['experiment_details']['resolution'][1:])

    if grid_file is None:
        grid_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'run', 'grid_files', f't{res}_grid.nc')
        if not os.path.exists(grid_file):
            create_grid_file(res=res)

    if os.path.exists(grid_file):
        import xarray as xr
        grid = xr.open_dataset(grid_file)
        lat_var = np.asarray(grid.coords['lat'].values)
        lon_var = np.asarray(grid.coords['lon'].values)
        if pressure_var is None and 'pfull' in grid.coords:
            pressure_var = np.asarray(grid.coords['pfull'].values)
    else:
        raise FileNotFoundError(f"Grid file does not exist: {grid_file}")

    main_nml = namelist.get('main_nml', {})
    days_total = int(namelist['experiment_details']['n_months_total'] * main_nml.get('days', 30))
    time_var = np.arange(0, days_total + 1, time_spacing)

    if pressure_var is not None:
        sample = var_func(time_var, pressure_var, lat_var, lon_var)
        write_var_kwargs = {
            'time_var': time_var,
            'pressure_var': pressure_var,
            'lat_var': lat_var,
            'lon_var': lon_var,
            'var_array': sample,
            'var_name': var_name,
            'namelist_file': os.path.basename(namelist_file),
            'exp_dir': exp_dir,
            'grid_file': grid_file,
        }
    else:
        sample = var_func(time_var, lat_var, lon_var)
        write_var_kwargs = {
            'time_var': time_var,
            'lat_var': lat_var,
            'lon_var': lon_var,
            'var_array': sample,
            'var_name': var_name,
            'namelist_file': os.path.basename(namelist_file),
            'exp_dir': exp_dir,
            'grid_file': grid_file,
        }

    _write_var(file_name=file_name, **write_var_kwargs)
    return os.path.abspath(file_name)

write_var(*args, **kwargs)

Backward-compatible wrapper for the general variable writer.

Source code in isca_tools/time_series/base.py
110
111
112
def write_var(*args, **kwargs):
    """Backward-compatible wrapper for the general variable writer."""
    return _write_var(*args, **kwargs)