-
Notifications
You must be signed in to change notification settings - Fork 1
/
lorenzcycletoolkit.py
259 lines (218 loc) · 8.54 KB
/
lorenzcycletoolkit.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# **************************************************************************** #
# #
# ::: :::::::: #
# lorenzcycletoolkit.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: daniloceano <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/04/20 10:05:52 by daniloceano #+# #+# #
# Updated: 2024/07/14 12:46:56 by daniloceano ### ########.fr #
# #
# **************************************************************************** #
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Main script for the Lorenz Energy Cycle (LEC) Program.
This script handles user inputs to open the data, specifies the bounding box for the calculations,
and validates user inputs for processing the Lorenz Energy Cycle using either a fixed or moving framework.
Created by:
Danilo Couto de Souza
Universidade de São Paulo (USP) Instituto de Astronomia, Ciências Atmosféricas e Geociências,
São Paulo - Brazil
Contact: [email protected]
"""
import argparse
import os
import sys
import time
import warnings
import pandas as pd
from metpy.units import units
from src.frameworks.lec_fixed_framework import lec_fixed
from src.frameworks.lec_moving_framework import lec_moving
from src.utils.tools import initialize_logging, prepare_data
# Suppress specific RuntimeWarning from xarray
warnings.filterwarnings(
"ignore", category=RuntimeWarning, module="xarray.backends.plugins"
)
def create_arg_parser():
"""
Create an argument parser for the Lorenz Energy Cycle (LEC) program.
Returns:
argparse.ArgumentParser: The argument parser object.
"""
parser = argparse.ArgumentParser(description="Lorenz Energy Cycle (LEC) program.")
parser.add_argument(
"infile",
help="Input .nc file with temperature, geopotential/geopotential height, and wind components data.",
)
parser.add_argument(
"-r",
"--residuals",
action="store_true",
help="Compute the Dissipation and Generation terms as residuals.",
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-f",
"--fixed",
action="store_true",
help="Compute the energetics for a fixed domain specified by the 'box_limits' file.",
)
group.add_argument(
"-t",
"--track",
action="store_true",
help="Define the domain using a track file.",
)
group.add_argument(
"-c",
"--choose",
action="store_true",
help="Interactively select the domain for each time step.",
)
parser.add_argument(
"-z",
"--zeta",
action="store_true",
help="Use the vorticity from the track file instead of computing it at 850 hPa.",
)
parser.add_argument(
"-m",
"--mpas",
action="store_true",
help="Specify this flag if working with MPAS-A data processed with MPAS-BR routines.",
)
parser.add_argument("-p", "--plots", action="store_true", help="Generate plots.")
parser.add_argument(
"-v", "--verbosity", action="store_true", help="Logger level set to debug mode."
)
parser.add_argument(
"--cdsapi",
action="store_true",
help="Use CDS API for downloading data (experimental).",
)
parser.add_argument(
"--trackfile",
type=str,
default="inputs/track",
help="Specify a custom track file. Default is 'inputs/track'.",
)
parser.add_argument(
"--box_limits",
type=str,
default="inputs/box_limits",
help="Specify a custom box limits file. Default is 'inputs/box_limits'.",
)
parser.add_argument(
"-o", "--outname", type=str, help="Specify an output name for the results."
)
return parser
def setup_results_directory(args, method):
"""
Create and setup the results directory for storing output files and figures.
Args:
args (object): The arguments object containing input file information.
method (str): The method used for processing the input file.
Returns:
tuple: A tuple containing the path to the results directory and the path to the figures directory.
"""
resuts_directory = "./LEC_Results/"
results_subdirectory = os.path.join(
resuts_directory,
"".join(args.infile.split("/")[-1].split(".nc")) + "_" + method,
)
results_subdirectory_vertical_levels = os.path.join(
results_subdirectory, "results_vertical_levels")
figures_directory = os.path.join(results_subdirectory, "Figures")
os.makedirs(figures_directory, exist_ok=True)
os.makedirs(results_subdirectory, exist_ok=True)
os.makedirs(results_subdirectory_vertical_levels, exist_ok=True)
return results_subdirectory, figures_directory, results_subdirectory_vertical_levels
def run_lec_analysis(data, args, results_subdirectory, figures_directory, results_subdirectory_vertical_levels, app_logger):
"""
Runs the LEc analysis on the given data using the specified method and arguments.
Args:
data (pd.DataFrame): The input data for the analysis.
method (str): The method to be used for the analysis.
args (dict): Additional arguments for the analysis.
Returns:
None
Raises:
None
"""
start_time = time.time()
variable_list_df = pd.read_csv("inputs/namelist", sep=";", index_col=0, header=0)
if args.fixed:
lec_fixed(data, variable_list_df, results_subdirectory, results_subdirectory_vertical_levels, app_logger, args)
app_logger.info(
"--- %s seconds running fixed framework ---" % (time.time() - start_time)
)
if args.track or args.choose:
dTdt = data[variable_list_df.loc["Air Temperature"]["Variable"]].differentiate(
variable_list_df.loc["Time"]["Variable"], datetime_unit="s"
) * units("K/s")
lec_moving(
data,
variable_list_df,
dTdt,
results_subdirectory,
figures_directory,
results_subdirectory_vertical_levels,
app_logger,
args,
)
app_logger.info(
"--- %s seconds running moving framework ---" % (time.time() - start_time)
)
def main():
"""
Executes the main function.
This function is the entry point of the program. It creates an argument parser, parses the command line arguments,
initializes logging, prepares the data, and runs the LEC analysis.
Parameters:
None.
Returns:
None.
"""
# Parse command line arguments
if len(sys.argv) > 1:
parser = create_arg_parser()
args = parser.parse_args()
else:
# Example usage for debugging
parser = create_arg_parser()
print(
"----------------------------------------------------------------------------"
)
print("WARNING: USING EXAMPLE ARGUMENTS")
import shutil
shutil.copy("inputs/namelist_NCEP-R2", "inputs/namelist")
shutil.copy("inputs/track_testdata_NCEP-R2", "inputs/track")
args = parser.parse_args(
["samples/testdata_NCEP-R2.nc", "-t", "-r", "-p", "-v"]
)
# args = parser.parse_args(['20070536_ERA5_sliced.nc', '-t', '-r', '-p', '-v', '-g', '--cdsapi', '--trackfile',
# 'inputs/track_20070536.csv'])
print(
"----------------------------------------------------------------------------"
)
# Set method
if args.fixed:
method = "fixed"
elif args.track:
method = "track"
elif args.choose:
method = "choose"
# Setup results directory
results_subdirectory, figures_directory, results_subdirectory_vertical_levels = setup_results_directory(args, method)
# Initialize logging
app_logger = initialize_logging(results_subdirectory, args)
app_logger.info("Starting LEC analysis")
app_logger.info(f"Command line arguments: {args}")
# Prepare data
data = prepare_data(args, "inputs/namelist", app_logger)
# Run LEC analysis
run_lec_analysis(data, args, results_subdirectory, figures_directory, results_subdirectory_vertical_levels, app_logger)
if __name__ == "__main__":
main()