geoschem / gcpy

Python toolkit for GEOS-Chem. Contains basic plotting scripts, plus the suite of GEOS-Chem benchmarking utilities.
https://gcpy.readthedocs.io
Other
51 stars 24 forks source link

TypeError encountered when running file_regrid.py #278

Closed yantosca closed 10 months ago

yantosca commented 10 months ago

Name and Institution (Required)

Name: Bob Yantosca Institution: Harvard + GCST

Confirm you have reviewed the following documentation

Description of your issue or question

When using the file_regrid.py script as described in the documentation, a TypeError is encountered.

To reproduce

$ python -m gcpy.file_regrid                                       \
    --filein         ./GEOSChem.Restart.Hg.20190101_0000z.nc4      \
    --dim_format_in  classic                                       \ 
    --fileout        ./GEOSChem.Restart.Hg.20190101_0000z.c24.nc4  \
    --cs_res_out     24                                            \
    --dim_format_out checkpoint

... etc...

Traceback (most recent call last):
  File "/n/jacob_lab/Users/ryantosca/mamba/envs/gcpy_env/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/n/jacob_lab/Users/ryantosca/mamba/envs/gcpy_env/lib/python3.9/runpy.py", line 87, in _run_code    exec(code, run_globals)
  File "/n/home09/ryantosca/python/gcpy/gcpy/file_regrid.py", line 1565, in <module>
    main()
  File "/n/home09/ryantosca/python/gcpy/gcpy/file_regrid.py", line 1549, in main
    file_regrid(
  File "/n/home09/ryantosca/python/gcpy/gcpy/file_regrid.py", line 174, in file_regrid
    dset = regrid_ll_to_cssg(
  File "/n/home09/ryantosca/python/gcpy/gcpy/file_regrid.py", line 644, in regrid_ll_to_cssg
    regridders = make_regridder_L2S(
  File "/n/home09/ryantosca/python/gcpy/gcpy/regrid.py", line 298, in make_regridder_L2S
    weightsfile = os.path.join(
  File "/n/jacob_lab/Users/ryantosca/mamba/envs/gcpy_env/lib/python3.9/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not bool

Explanation

I have traced this to this block of code where the command-line arguments are parsed:

    parser.add_argument(
        "-w", "--weightsdir",
        metavar="WGT",
        type=str,
        default=False,
        help="Directory where regridding weights are found (or will be created)"
    )
    args = parser.parse_args()

Note that the regridding weights directory (--weightsdir argument) should be a string but it has a default value of False (which is of type bool). This was likely a cut-n-paste error.

Solution

Changing the

default=False

to

default="."

fixes the issue. I will prepare a PR with this fix.

Temporary workaround

This error only happens when the --weightsdir argument is not supplied, which causes the default value to be used. Until we can fix this issue, make sure to call file_regrid.py with e.g. --weightsdir ".".

yantosca commented 10 months ago

This is now fixed by PR #279, which has been included in GCPy 1.4.1.