source: CONFIG/publications/IPSLCM6_LMDZOR_ensembles_2023/selection/selection_GKTL_z500.py @ 6957

Last change on this file since 6957 was 6957, checked in by aclsce, 3 months ago

Added the python scripts selection.

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1import xarray as xr
2import numpy as np
3import sys
4
5# Paths
6# create by hand the mean_weights.txt and new_trajectories.txt files
7# bouger le fichier ensemble.txt juste aprÚs l'exécution du script
8# créer un dossier REA dans le submit dir
9
10# Parameters general
11year = int(sys.argv[1])
12name_run = sys.argv[2]
13iteration = int(sys.argv[3]) - 1
14
15path_results = "/ccc/work/cont003/gen6877/noyeller/rea_irene_rome/IPSLCM6.2.2_ENSEMBLES/config/IPSLCM6/" + name_run + "/"
16path_data = "/ccc/scratch/cont003/gen6877/noyeller/IGCM_OUT/LMDZOR/DEVT/amip/" + name_run + "/ATM/Output/DA/"
17
18# Parameters REA
19k = float(sys.argv[4]) # default: -0.05
20N = int(sys.argv[5]) 
21
22# Functions
23def create_name_file(name_run, year, iteration):
24    date_ini_tab = ['0601','0606','0611','0616','0621','0626','0701','0706','0711','0716','0721','0726','0731','0805','0810','0815','0820','0825']
25    date_end_tab = ['0605','0610','0615','0620','0625','0630','0705','0710','0715','0720','0725','0730','0804','0809','0814','0819','0824','0829']
26    return name_run + "_" + str(year) + date_ini_tab[iteration-1] + "_" + str(year) + date_end_tab[iteration-1] + "_1D_histday.nc"
27
28def compute_observable(path_data, name_file):
29    data = xr.open_dataset(path_data+name_file, use_cftime=True)
30    obs = data['z500'].sel(lat=slice(50,49),lon=slice(1.25,3.75)).mean(dim=('lat','lon'))
31    return obs.sum(dim='time_counter').values
32
33def compute_weights(int_obs_points, k):
34    weights = np.exp(k*int_obs_points) # temporal integration in discrete time
35    return weights/np.mean(weights), np.mean(weights)
36
37def compute_cloning(weights):
38    normalized_weights = weights/np.mean(weights)
39    N = normalized_weights.size
40    number_copies = (normalized_weights + np.random.uniform(low=0, high=1, size=N)).astype(int)
41    cloning_table = np.zeros(np.sum(number_copies), dtype=int)
42    indice_start = 0
43    for i in range(N):
44        cloning_table[indice_start:indice_start + number_copies[i]] = i
45        indice_start += number_copies[i]
46    delta_N = np.sum(number_copies) - N
47    if delta_N >= 0 :
48        return np.random.choice(cloning_table, size=N, replace=False)
49    else:
50        return np.random.choice(cloning_table, size=N, replace=True)
51
52def write_ensemble_file(new_trajectories):
53    with open(path_results+'ensemble.txt', 'w') as f:
54        for idm,m in enumerate(new_trajectories):
55            f.write("MEMBER_"+str(idm)+":"+str(m)+":1e-4")
56            f.write('\n')
57    f.close()
58
59# REA
60
61if iteration == 0:
62   
63    # Save mean_weights
64    with open(path_results+'REA/mean_weights.txt', 'w') as f:
65        f.write(str(1))
66    f.close()
67   
68    # Save new trajectories
69    new_trajectories = np.arange(N)
70    with open(path_results+'REA/new_trajectories.txt', 'w') as f:
71        f.write(','.join([str(n) % n for n in new_trajectories]))
72    f.close()
73
74    # Create ensemble.txt file
75    write_ensemble_file(new_trajectories)
76
77else:
78    name_file = create_name_file(name_run, year, iteration)
79    int_obs_points = compute_observable(path_data, name_file) # iteration in the name of name_run
80
81    # Computations
82    weights, mean_weights = compute_weights(int_obs_points, k)
83    new_trajectories = compute_cloning(weights)
84    print(np.max(np.unique(new_trajectories, return_counts=True)[1]))
85
86    # Save mean_weights
87    with open(path_results+'REA/mean_weights.txt', 'a') as f:
88        f.write('\n')
89        f.write(str(mean_weights))
90    f.close()
91
92    # Save new trajectories
93    with open(path_results+'REA/new_trajectories.txt', 'a') as f:
94        f.write('\n')
95        f.write(','.join([str(n) % n for n in new_trajectories]))
96    f.close()
97   
98    # Create ensemble.txt file
99    write_ensemble_file(new_trajectories)
Note: See TracBrowser for help on using the repository browser.