Operating environment
Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
Python 3.6.0 on virtualenv
I want to execute by specifying the runtime argument in the Python script being implemented.
Reference http://qiita.com/munkhbat1900/items/d7f9b11fb0965085964e Reference http://qiita.com/petitviolet/items/b8ed39dd6b0a0545dd36
v0.1 > AttributeError: 'Namespace' object has no attribute 't'
The following implementation was made with reference to the above two.
test_python_170323i.py
import argparse
parser = argparse.ArgumentParser(description = "do something")
parser.add_argument(
'-t',
'--timeIndex',
type = int,
help = 'time index for netCDF file',
required = True)
cmd_args = parser.parse_args()
print(cmd_args.t)
$ python test_python_170323i.py -t 314
Traceback (most recent call last):
File "test_python_170323i.py", line 14, in <module>
print(cmd_args.t)
AttributeError: 'Namespace' object has no attribute 't'
There is no attribute called t.
test_python_170323i.py
import argparse
parser = argparse.ArgumentParser(description = "do something")
parser.add_argument(
'-t',
'--timeIndex',
type = int,
help = 'time index for netCDF file',
required = True)
cmd_args = parser.parse_args()
print(cmd_args.timeIndex)
result
$ python test_python_170323i.py -t 314
314
When --timeIndex
is specified, it seems that .timeIndex
is used instead of .t
when reading.
https://docs.python.jp/3/library/argparse.html It seems to be related.
It seems that you can specify the attribute name by specifying dest.
https://docs.python.jp/3/library/argparse.html 16.4.1.2. Add arguments
The accumulate attribute is specified from the command line if --sum is specified
import argparse
parser = argparse.ArgumentParser(description = "do something")
parser.add_argument(
'-t',
'--timeIndex',
dest='time_index',
type=int,
help='time index for netCDF file',
required=True)
cmd_args = parser.parse_args()
print(cmd_args.time_index)
$ python calc_latlon_avg_std_170323.py --timeIndex -20
total: 3477.89
avg_lat: 40.480
avg_lon: 116.246
std_lat: 0.137
std_lon: 0.103
$ python calc_latlon_avg_std_170323.py --timeIndex 4
total: 3477.89
avg_lat: 40.480
avg_lon: 116.246
std_lat: 0.137
std_lon: 0.103
-20 is accepted when timeIndex is specified. The value is 24, which is wraparound, and the result is the same as when 4 is specified. (The timeIndex is actually specified by [0..23]).
Recommended Posts