-
Notifications
You must be signed in to change notification settings - Fork 1
/
lorenz_cycle.py
167 lines (136 loc) · 7.58 KB
/
lorenz_cycle.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
# **************************************************************************** #
# #
# ::: :::::::: #
# lorenz_cycle.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: daniloceano <danilo.oceano@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/04/20 10:05:52 by daniloceano #+# #+# #
# Updated: 2024/05/21 18:00:05 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: danilo.oceano@gmail.com
"""
import os
import sys
import time
import logging
import argparse
import warnings
import pandas as pd
from metpy.units import units
from src.utils.tools import prepare_data, initialize_logging
from src.frameworks.lec_fixed_framework import lec_fixed
from src.frameworks.lec_moving_framework import lec_moving
# 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.")
parser.add_argument("-g", "--geopotential", action='store_true', help="Use geopotential data instead of geopotential height.")
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)
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, exist_ok=True)
return results_subdirectory, figures_directory
def run_lec_analysis(data, args, results_subdirectory, figures_directory, 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/fvars", sep=';', index_col=0, header=0)
if args.fixed:
lec_fixed(data, variable_list_df, results_subdirectory, 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, 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")
# args = parser.parse_args(['samples/Reg1-Representative_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 = 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/fvars', app_logger)
# Run LEC analysis
run_lec_analysis(data, args, results_subdirectory, figures_directory, app_logger)
if __name__ == "__main__":
main()