Skip to content

Update py-bkerror Python scripts from Python 2 to Python 3#93

Draft
Copilot wants to merge 3 commits intodevelopfrom
copilot/update-python-scripts-to-python3
Draft

Update py-bkerror Python scripts from Python 2 to Python 3#93
Copilot wants to merge 3 commits intodevelopfrom
copilot/update-python-scripts-to-python3

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 15, 2026

Summary

Updates all Python scripts in src/NMC_Bkerror/py-bkerror/ for compatibility with Python 3 (target: Python 3.8.6–3.12.0), and fixes related issues in compile.csh.

Changes

All 5 Python scripts

  • Updated shebangs from #!/usr/bin/env python to #!/usr/bin/env python3
  • Converted all print statements to print() function calls
  • Replaced ndarray.tostring() (returns bytes in Python 3) with ndarray.tobytes().decode('ascii') to correctly parse variable names from the binary file header

interp_bkerror.py (additional changes)

  • Replaced np.float with float (np.float was deprecated in NumPy 1.20 and removed in 1.24)
  • Replaced the removed scipy.interpolate.interp2d (deprecated in scipy 1.10, removed in 1.14) with a _interp2d() wrapper backed by scipy.interpolate.RegularGridInterpolator. The wrapper preserves the same call signature (f(x_new, y_new) returning shape (len(y_new), len(x_new))), so all downstream interpolation calls are unchanged. Cubic interpolation requires scipy ≥ 1.9.

compile.csh (build script fixes)

  • Fixed a bug where line 45 used --fcompiler=$FC (e.g., --fcompiler=ifort) instead of --fcompiler=$compiler (e.g., --fcompiler=intel). numpy.distutils only recognizes its own compiler class names — not raw compiler executable names — so this caused f2py to fail. Lines 42–43 already used $compiler correctly; only line 45 was wrong.
  • Added support for the Intel oneAPI ifx Fortran compiler (./compile.csh build ifx), using --f90exec=ifx --f77exec=ifx since numpy.distutils does not have a built-in ifx compiler class.

Copilot AI and others added 2 commits April 15, 2026 13:06
Agent-Logs-Url: https://github.com/NOAA-EMC/GSI-utils/sessions/4102bfae-4d3d-4d95-903c-4e805bd823d0

Co-authored-by: RussTreadon-NOAA <26926959+RussTreadon-NOAA@users.noreply.github.com>
Agent-Logs-Url: https://github.com/NOAA-EMC/GSI-utils/sessions/4102bfae-4d3d-4d95-903c-4e805bd823d0

Co-authored-by: RussTreadon-NOAA <26926959+RussTreadon-NOAA@users.noreply.github.com>
@RussTreadon-NOAA
Copy link
Copy Markdown
Contributor

@copilot : The changes in this PR were tested with the following modules loaded

  1) core/rocoto/1.3.5            21) hdf5/1.14.3                    41) ip/5.1.0                61) py-pybind11/2.13.5
  2) PrgEnv-intel/8.3.3           22) parallel-netcdf/1.12.3         42) gsl-lite/0.37.0         62) py-pycodestyle/2.11.0
  3) intel-oneapi/2024.2          23) netcdf-c/4.9.2                 43) libjpeg/2.1.0           63) py-pyyaml/6.0.2
  4) stack-oneapi/2024.2.1        24) nccmp/1.9.0.1                  44) libpng/1.6.37           64) py-scipy/1.14.1
  5) libfabric/1.20.1             25) netcdf-fortran/4.6.1           45) libxt/1.3.0             65) py-setuptools/63.4.3
  6) craype-network-ofi      (H)  26) nco/5.2.4                      46) libxmu/1.2.1            66) py-tzdata/2023.3
  7) cray-mpich/8.1.29            27) parallelio/2.6.2               47) libxpm/3.5.17           67) py-pytz/2023.3
  8) stack-cray-mpich/8.1.29      28) boost/1.84.0                   48) libxaw/1.0.16           68) py-six/1.16.0
  9) python/3.11.7                29) ecbuild/3.7.2                  49) udunits/2.2.28          69) py-python-dateutil/2.8.2
 10) stack-python/3.11.7          30) eigen/3.4.0                    50) ncview/2.1.9            70) py-pandas/2.2.3
 11) cmake/3.27.9                 31) openblas/0.3.24                51) netcdf-cxx4/4.3.1       71) py-packaging/24.1
 12) craype/2.7.17                32) eckit/1.28.3                   52) prod_util/2.0.14        72) py-xarray/2024.7.0
 13) cray-pals/1.3.2              33) fckit/0.13.2                   53) grib-util/1.4.0         73) py-f90nml/1.4.3
 14) git/2.47.0                   34) fms/2024.02                    54) py-numpy/1.26.4         74) py-pip/23.1.2
 15) git-lfs/3.5.1                35) python-venv/1.0                55) bufr/12.1.0             75) py-click/8.1.7
 16) zstd/1.5.6                   36) intel-oneapi-runtime/2024.2.1  56) py-markupsafe/2.1.3     76) py-wheel/0.41.2
 17) pigz/2.8                     37) glibc/2.31                     57) py-jinja2/3.1.4         77) rocoto/1.3.5
 18) tar/1.34                     38) esmf/8.8.0                     58) py-cftime/1.0.3.4       78) GDAS/wcoss2.intel
 19) gettext/0.22.5               39) atlas/0.40.0                   59) py-certifi/2023.7.22
 20) curl/8.10.1                  40) sp/2.5.0                       60) py-netcdf4/1.7.1.post2

./compile.csh build intel generated the following error message

Traceback (most recent call last):
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/bin/f2py", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/f2py/f2py2e.py", line 766, in main
    run_compile()
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/f2py/f2py2e.py", line 738, in run_compile
    builder.compile()
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/f2py/_backends/_distutils.py", line 71, in compile
    setup(ext_modules=[ext])
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/core.py", line 170, in setup
    return old_setup(**new_attr)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/python-3.11.7-uhd5gji/lib/python3.11/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/python-3.11.7-uhd5gji/lib/python3.11/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/python-3.11.7-uhd5gji/lib/python3.11/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/command/build.py", line 62, in run
    old_build.run(self)
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/python-3.11.7-uhd5gji/lib/python3.11/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/python-3.11.7-uhd5gji/lib/python3.11/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/python-3.11.7-uhd5gji/lib/python3.11/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/command/build_ext.py", line 297, in run
    fcompiler.customize(self.distribution)
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/__init__.py", line 515, in customize
    get_flags('opt', oflags)
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/__init__.py", line 506, in get_flags
    flags.extend(getattr(self.flag_vars, tag))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/environment.py", line 40, in __getattr__
    return self._get_var(name, conf_desc)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/environment.py", line 56, in _get_var
    var = self._hook_handler(name, hook)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/__init__.py", line 720, in _environment_hook
    return hook()
           ^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/intel.py", line 60, in get_flags_opt
    v = self.get_version()
        ^^^^^^^^^^^^^^^^^^
  File "/apps/ops/test/spack-stack-nco-1.9/oneapi/2024.2.1/py-numpy-1.26.4-tzjdfvk/lib/python3.11/site-packages/numpy/distutils/fcompiler/__init__.py", line 436, in get_version
    raise CompilerNotFound()
numpy.distutils.fcompiler.CompilerNotFound

How do we fix this?

Copy link
Copy Markdown
Author

Copilot AI commented Apr 15, 2026

@copilot : The changes in this PR were tested with the following modules loaded

  1) core/rocoto/1.3.5            21) hdf5/1.14.3                    41) ip/5.1.0                61) py-pybind11/...

There was a bug in compile.csh line 45 where --fcompiler=$FC was used (expanding to --fcompiler=ifort for the Intel case) instead of --fcompiler=$compiler (--fcompiler=intel). numpy.distutils only recognizes its own compiler class names (gnu95, intel, intelem, etc.) — not raw compiler executable names like ifort or gfortran. Lines 42–43 already used $compiler correctly; only line 45 was wrong. Fixed in a6d7f14.

I also added ifx (Intel oneAPI) support, which uses --f90exec=ifx --f77exec=ifx since numpy.distutils doesn't have a built-in ifx compiler class.

If the f2py error you saw is different from what's described above, could you share the complete traceback? The comment appears to have been cut off after line 8, in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants