forked from jouvetg/igm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_reform_names.py
171 lines (160 loc) · 6.46 KB
/
patch_reform_names.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
This utility script is used to update the names of the modules and parameters of igm
after a major update of the code done on Oct 24, 2023. Follow the following procedure:
1) Go to the folder where you have your igm parameter file and user-defined modules,
2) Run the script below with the name of the parameter file and the name of the
modules as arguments. 3) Both will produce a copy of the former (params_old.json,
mymodule_old.py) and the new (reformed) ones (params.json, mymodule.py), check the
code runs with the new files. If yes, you can delete the old files. If not, you must
investigate the differences between the old, possibly correct manually. You can
also look manually the correspondence table in patch_reform_names.py between
old and new parameters and modules names.
```bash
python patch_reform_names.py params.json
python patch_reform_names.py mymodule.py
```
"""
import os, shutil, glob, re, sys, json
# dictionnary listing the changes of the modules names from old to new
module_changes = {
"oggm_data_prep": "oggm_shop",
"load_ncdf_data": "load_ncdf",
"load_tif_data": "load_tif",
"time_step": "time",
"vertical_iceflow": "vert_flow",
"topg_glacial_erosion": "glerosion",
"write_tif_ex": "write_tif",
"write_ncdf_ex": "write_ncdf",
"write_ncdf_ts": "write_ts",
"print_all_comp_info": "print_comp",
"write_plot2d": "plot2d",
"anim3d_from_ncdf_ex": "anim_mayavi",
"anim_plotly_from_ncdf_ex": "anim_plotly",
"anim_mp4_from_ncdf_ex": "anim_video",
}
# dictionnary listing the changes of the parameters names from old to new
param_changes = {
"input_file": "lncd_input_file",
"coarsen_ncdf": "lncd_coarsen",
"crop_ncdf_xmin": "lncd_xmin",
"crop_ncdf_xmax": "lncd_xmax",
"crop_ncdf_ymin": "lncd_ymin",
"crop_ncdf_ymax": "lncd_ymax",
"crop_ncdf": "lncd_crop",
"coarsen_tif": "ltif_coarsen",
"crop_tif_xmin": "ltif_xmin",
"crop_tif_xmax": "ltif_xmax",
"crop_tif_ymin": "ltif_ymin",
"crop_tif_ymax": "ltif_ymax",
"crop_tif": "ltif_crop",
"RGI_ID": "oggm_RGI_ID",
"preprocess": "oggm_preprocess",
"dx": "oggm_dx",
"border": "oggm_border",
"thk_source": "oggm_thk_source",
"vel_source": "oggm_vel_source",
"include_glathida": "oggm_include_glathida",
"path_glathida": "oggm_path_glathida",
"save_input_ncdf": "oggm_save_in_ncdf",
"geology_optimized_file": "opti_save_result_in_ncdf",
"plot2d_live_inversion": "opti_plot2d_live",
"plot2d_inversion": "opti_plot2d",
"write_ncdf_optimize": "opti_save_iterat_in_ncdf",
"editor_plot2d_optimize": "opti_editor_plot2d",
"tracking_method": "part_tracking_method",
"frequency_seeding": "part_frequency_seeding",
"density_seeding": "part_density_seeding",
"speed_rockflow": "rock_flow_speed",
"smb_update_freq": "smb_simple_update_freq",
"cfl": "time_cfl",
"erosion_cst": "glerosion_cst",
"erosion_exp": "glerosion_exp",
"erosion_update_freq": "glerosion_update_freq",
"type_iceflow": "iflo_type",
"emulator": "iflo_emulator",
"init_slidingco": "iflo_init_slidingco",
"init_arrhenius": "iflo_init_arrhenius",
"regu_glen": "iflo_regu_glen",
"regu_weertman": "iflo_regu_weertman",
"exp_glen": "iflo_exp_glen",
"exp_weertman": "iflo_exp_weertman",
"gravity_cst": "iflo_gravity_cst",
"ice_density": "iflo_ice_density",
"new_friction_param": "iflo_new_friction_param",
"Nz": "iflo_Nz",
"vert_spacing": "iflo_vert_spacing",
"thr_ice_thk": "iflo_thr_ice_thk",
"solve_iceflow_step_size": "iflo_solve_step_size",
"solve_iceflow_nbitmax": "iflo_solve_nbitmax",
"stop_if_no_decrease": "iflo_solve_stop_if_no_decrease",
"fieldin": "iflo_fieldin",
"dim_arrhenius": "iflo_dim_arrhenius",
"retrain_iceflow_emulator_freq": "iflo_retrain_emulator_freq",
"retrain_iceflow_emulator_lr": "iflo_retrain_emulator_lr",
"retrain_iceflow_emulator_nbit_init": "iflo_retrain_emulator_nbit_init",
"retrain_iceflow_emulator_nbit": "iflo_retrain_emulator_nbit",
"retrain_iceflow_emulator_framesizemax": "iflo_retrain_emulator_framesizemax",
"multiple_window_size": "iflo_multiple_window_size",
"force_max_velbar": "iflo_force_max_velbar",
"network": "iflo_network",
"activation": "iflo_activation",
"nb_layers": "iflo_nb_layers",
"nb_blocks": "iflo_nb_blocks",
"nb_out_filter": "iflo_nb_out_filter",
"conv_ker_size": "iflo_conv_ker_size",
"dropout_rate": "iflo_dropout_rate",
"exclude_borders_from_iceflow": "iflo_exclude_borders",
"spy": "enth_spy",
"ki": "enth_ki",
"ci": "enth_ci",
"KtdivKc": "enth_KtdivKc",
"claus_clape_cst": "enth_claus_clape",
"melt_temp": "enth_melt_temp",
"ref_temp": "enth_ref_temp",
"till_friction_angle": "enth_till_friction_angle",
"uthreshold": "enth_uthreshold",
"drain_rate": "enth_drain_rate",
"till_wat_max": "enth_till_wat_max",
"drain_ice_column": "enth_drain_ice_column",
"default_bheatflx": "enth_default_bheatflx",
"Lh": "enth_Lh",
"water_density": "enth_water_density",
"output_file_ncdf_ex": "wncd_output_file",
"vars_to_save_ncdf_ex": "wncd_vars_to_save",
"vars_to_save_tif_ex": "wtif_vars_to_save",
"output_file_ncdf_ts": "wts_output_file",
"add_topography_to_particles": "wpar_add_topography",
"editor_plot2d": "plt2d_editor",
"plot_live": "plt2d_live",
"plot_particles": "plt2d_particles",
"varplot": "plt2d_var",
"varplot_max": "plt2d_varmax",
}
l = sys.argv[1]
filename = l.split(".")[0]
ext = l.split(".")[-1]
if os.path.exists(filename + "_old." + ext):
print(
"the Backup file "
+ filename
+ "_old."
+ ext
+ " already exists, please remove it before running this script"
)
else:
shutil.copy(l, filename + "_old." + ext)
with open(l, "r") as input_file:
input_text = input_file.read()
for old_word, new_word in param_changes.items():
if ext == "py":
input_text = re.sub(
r"\b%s\b" % "params." + old_word, "params." + new_word, input_text
)
input_text = input_text.replace("--" + old_word, "--" + new_word)
else:
input_text = re.sub(r"\b%s\b" % old_word, new_word, input_text)
for old_word, new_word in module_changes.items():
input_text = re.sub(r"\b%s\b" % old_word, new_word, input_text)
with open(l, "w") as output_file:
output_file.write(input_text)