<jupyter_start><jupyter_text>... ***CURRENTLY UNDER DEVELOPMENT*** ...  Obtain synthetic DWTs timeseries inputs required:   * Fitted ALR model of DWT based on seasonality,and  AWT and IWT timeseries   * Synthetic timeseries of AWT (with climate change) and IWT  in this notebook:  * Generate *n* simulations of 1000 years of DWTs timeseries for climate change (using the fitted ALR model)<jupyter_code>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# common
import os
import os.path as op

# pip
import numpy as np
import xarray as xr
import shutil

# DEV: override installed teslakit
import sys
sys.path.insert(0, op.join(os.path.abspath(''), '..', '..','..', '..'))

# teslakit 
from teslakit.database import Database
from teslakit.alr import ALR_WRP
from teslakit.util.time_operations import xds_reindex_daily, xds_common_dates_daily

from teslakit.plotting.estela import Plot_DWTs_Probs
from teslakit.plotting.wts import Plot_Probs_WT_WT, Plot_Probs_WT_WT_anomaly

from teslakit.io.aux_nc import StoreBugXdset<jupyter_output><empty_output><jupyter_text>Database and Site parameters<jupyter_code># Teslakit database

p_data = r'/Users/albacid/Projects/TeslaKit_projects'

# offshore
db = Database(p_data)
db.SetSite('ROI')

# climate change - S5
db_S5 = Database(p_data)
db_S5.SetSite('ROI_CC_S5')

#  copy alr_w folder to climate change path
shutil.copytree(db.paths.site.ESTELA.alrw, db_S5.paths.site.ESTELA.alrw) 
 
# --------------------------------------
# load data and set parameters

MJO_sim_all = db.Load_MJO_sim()           # MJO simulations (daily)
PCs_sim_all = db_S5.Load_SST_PCs_sim_d()  # SST PCs simulations climate change (daily)

# ALR fit parameters
alr_num_clusters = 42
alr_markov_order = 1
alr_seasonality = [2, 4]

# ALR simulation
num_sims = 10  # one simulation for each simulated MJO, SST<jupyter_output><empty_output><jupyter_text>ESTELA Predictor - Autoregressive Logistic Regression Simulation<jupyter_code># --------------------------------------
# Prepare Covariates for ALR simulations

# simulation dates
d_sim = xds_common_dates_daily([MJO_sim_all, PCs_sim_all])

# join covariates for all MJO, PCs simulations
l_cov_sims = []
for i in MJO_sim_all.n_sim: 

    # select simulation
    MJO_sim = MJO_sim_all.sel(n_sim=i)
    PCs_sim = PCs_sim_all.sel(n_sim=i)

    # PCs covar 
    cov_PCs = PCs_sim.sel(time = slice(d_sim[0], d_sim[-1]))
    cov_1 = cov_PCs.PC1.values.reshape(-1,1)
    cov_2 = cov_PCs.PC2.values.reshape(-1,1)
    cov_3 = cov_PCs.PC3.values.reshape(-1,1)

    # MJO covars
    cov_MJO = MJO_sim.sel(time = slice(d_sim[0], d_sim[-1]))
    cov_4 = cov_MJO.rmm1.values.reshape(-1,1)
    cov_5 = cov_MJO.rmm2.values.reshape(-1,1)

    # join covars (do not normalize simulation covariates)
    cov_T_sim = np.hstack((cov_1, cov_2, cov_3, cov_4, cov_5))
    #cov_sim = xr.Dataset(
    #    {
    #        'cov_values': (('time','n_covariates'), cov_T_sim),
    #        'cov_names': (('n_covariates',), ['PC1','PC2','PC3','MJO1','MJO2']),
    #    },
    #    coords = {'time': d_sim}
    #)
    
    cov_sim = xr.Dataset(
        {
            'cov_values': (('time','n_covariates'), cov_T_sim),
        },
        coords = {'time': d_sim}
    )
    
    l_cov_sims.append(cov_sim)

# use "n_sim" name to join covariates (ALR.Simulate() will recognize it)
cov_sims = xr.concat(l_cov_sims, dim='n_sim')
cov_sims = cov_sims.squeeze()

print(cov_sims)

# --------------------------------------
# Autoregressive Logistic Regression

# ALR wrapper
ALRW = ALR_WRP(db_S5.paths.site.ESTELA.alrw)

ALRW.LoadModel()
ALRW.LoadBmus_Fit()

# --------------------------------------
# Autoregressive Logistic Regression - simulate 

# launch simulation
xds_alr = ALRW.Simulate(num_sims, d_sim, cov_sims, overfit_filter=True)

# Store Daily Weather Types
DWT_sim = xds_alr.evbmus_sims.to_dataset()
db_S5.Save_ESTELA_DWT_sim(DWT_sim)

print(DWT_sim)

# show sim report (climate change sims)
ALRW.Report_Sim(py_month_ini=6);

# Plot Simulated DWTs probabilities climate change (with TCs DWTs)
DWT_sim = db.Load_ESTELA_DWT_sim()  

bmus_sim = DWT_sim.isel(n_sim=0).evbmus_sims.values[:]
dbmus_sim = DWT_sim.time.values[:]

Plot_DWTs_Probs(bmus_sim, dbmus_sim, alr_num_clusters);

# Plot AWTs/DWTs Probabilities 

# clusters to plot (no TCs)
n_clusters_AWT = 6
n_clusters_DWT = 42
n_sim = 0  # simulation to plot


# Plot AWTs/DWTs sim - simulated
AWT_sim, DWT_sim = db.Load_AWTs_DWTs_Plots_sim(n_sim=0)
AWT_bmus = AWT_sim.bmus.values[:]
DWT_bmus = DWT_sim.bmus.values[:]

Plot_Probs_WT_WT(
    AWT_bmus, DWT_bmus, n_clusters_AWT, n_clusters_DWT,
    wt_colors=True, ttl = 'DWTs Probabilities by AWTs - Simulation'
);


# Plot AWTs/DWTs sim - simulated Climate Change
AWT_sim, DWT_sim = db_S5.Load_AWTs_DWTs_Plots_sim(n_sim=0)
AWT_bmus = AWT_sim.bmus.values[:]
DWT_bmus = DWT_sim.bmus.values[:]

Plot_Probs_WT_WT(
    AWT_bmus, DWT_bmus, n_clusters_AWT, n_clusters_DWT,
    wt_colors=True, ttl = 'DWTs Probabilities by AWTs Climate Change - Simulation'
);



# Plot AWTs/DWTs sim - simulated
AWT_sim, DWT_sim = db.Load_AWTs_DWTs_Plots_sim(n_sim=0)
AWT_bmus = AWT_sim.bmus.values[:]
DWT_bmus = DWT_sim.bmus.values[:]

Plot_Probs_WT_WT_anomaly(
    AWT_bmus, DWT_bmus, n_clusters_AWT, n_clusters_DWT,
    wt_colors=True, ttl = 'DWTs Probabilities by AWTs - anomalies Simulation'
);


# Plot AWTs/DWTs sim - simulated Climate Change
AWT_sim, DWT_sim = db_S5.Load_AWTs_DWTs_Plots_sim(n_sim=0)
AWT_bmus = AWT_sim.bmus.values[:]
DWT_bmus = DWT_sim.bmus.values[:]

Plot_Probs_WT_WT_anomaly(
    AWT_bmus, DWT_bmus, n_clusters_AWT, n_clusters_DWT,
    wt_colors=True, ttl = 'DWTs Probabilities by AWTs Climate Change - anomalies Simulation'
);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1365870786516854, 'fraction_numerical': 0.011762640449438201, 'mean_word_length': 3.949609035621199, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '262322', 'license': 'MIT', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2340, 'n_words': 449, 'path': 'notebooks/ROI/03_ClimateChange/S5_SLR_ENSO/01_11_SLP_ESTELA_ALR_Covariates.ipynb', 'repo_name': 'teslakit/teslak', 'size': '5696'}