Phases are wrong
Currently, the simulation does not recognize any warm phase. Instead they have to be calculated afterwards.
The following code adds phases
:
from functools import reduce
import numpy as np
import os
import pandas as pd
from tqdm.notebook import tqdm
import xarray as xr
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
file_path = "/mnt/workstation/data/project/wcb/b8/Z2_data_gradients/"
files = [f for f in os.listdir(file_path) if os.path.isfile(file_path + f)]
def add_phase(ds):
phase_col = np.full((len(ds["ensemble"]), len(ds["trajectory"]), len(ds["time"])), " ")
def warm(ds):
return ((ds["QC"] > 0) | (ds["QC"] > 0) | (ds["NCCLOUD"] > 0) | (ds["NCRAIN"] > 0))
def cold(ds):
return ((ds["QG"] > 0) | (ds["QH"] > 0) | (ds["NCGRAUPEL"] > 0) | (ds["NCHAIL"] > 0)
| (ds["QI"] > 0) | (ds["QS"] > 0) | (ds["NCICE"] > 0) | (ds["NCSNOW"] > 0))
def warm_phase(ds):
return ((warm(ds)) & (~cold(ds)))
def cold_phase(ds):
return ((~warm(ds)) & (cold(ds)))
def mixed_phase(ds):
return ((warm(ds)) & (cold(ds)))
def neutral_phase(ds):
return ((~warm(ds)) & (~cold(ds)))
phase_col[np.where(warm_phase(ds))] = "warm phase "
phase_col[np.where(cold_phase(ds))] = "ice phase "
phase_col[np.where(mixed_phase(ds))] = "mixed phase "
phase_col[np.where(neutral_phase(ds))] = "neutral phase"
ds["phase"] = (("ensemble", "trajectory", "time"), phase_col)
return ds
for file in files:
ds = xr.open_dataset(file_path + file, decode_times=False, engine="netcdf4")
ds = add_phase(ds)
n_data = {phase: 0 for phase in np.unique(ds["phase"])}
for phase in np.unique(ds["phase"]):
n = np.sum(ds["phase"] == phase)
n_data[phase] += n.values.item()
print(file)
for phase in n_data.keys():
print(f"{phase} occurs in {n_data[phase]} time steps")