ValueError when converting ITRS to geodetic on M1 Mac

I’m using astropy to do the simple task of converting ITRF Cartesian coordinates of satellite positions to geodetic coordinates (lat, lon, height). However, since yesterday, after fiddling/fixing some issues with other packages in my pipenv environment, I’m always getting a ValueError when using to_geodetic on my M1 Mac (Apple Silicon). This always used to run fine.
I don’t think it is related to those other packages I fiddled with, because the sample code below, with just astropy and Jupyter lab installed in the same way in a clean pipenv environment still works well on an older Intel MacBook Pro. So although I’m not 100% sure, the error might be isolated to the M1 Mac architecture or the way that astropy is compiled for that platform. If someone knows some further steps to debug this, I will be happy to try.

Here’s my minimal example code that triggers the ValueError:

import astropy
import astropy.coordinates as coord
import astropy.units as u
print(astropy.__version__)
itrs = coord.ITRS([7000, 1000, 1000]*u.km)
geo = itrs.earth_location.to_geodetic()

And here’s the output:

5.2
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 6
      4 print(astropy.__version__)
      5 itrs = coord.ITRS([7000, 1000, 1000]*u.km)
----> 6 geo = itrs.earth_location.to_geodetic()

File ~/.local/share/virtualenvs/test-PbZKQCzX/lib/python3.10/site-packages/astropy/coordinates/earth.py:612, in EarthLocation.to_geodetic(self, ellipsoid)
    610 ellipsoid = _check_ellipsoid(ellipsoid, default=self.ellipsoid)
    611 xyz = self.view(self._array_dtype, u.Quantity)
--> 612 llh = CartesianRepresentation(xyz, xyz_axis=-1, copy=False).represent_as(
    613     ELLIPSOIDS[ellipsoid]
    614 )
    615 return GeodeticLocation(
    616     Longitude(llh.lon, u.deg, wrap_angle=180 * u.deg, copy=False),
    617     llh.lat << u.deg,
    618     llh.height << self.unit,
    619 )

File ~/.local/share/virtualenvs/test-PbZKQCzX/lib/python3.10/site-packages/astropy/coordinates/representation.py:919, in BaseRepresentation.represent_as(self, other_class, differential_class)
    912     raise ValueError(
    913         "Input to a representation's represent_as must be a class, not "
    914         "a string. For strings, use frame objects."
    915     )
    917 if other_class is not self.__class__:
    918     # The default is to convert via cartesian coordinates
--> 919     new_rep = other_class.from_cartesian(self.to_cartesian())
    920 else:
    921     new_rep = self

File ~/.local/share/virtualenvs/test-PbZKQCzX/lib/python3.10/site-packages/astropy/coordinates/earth.py:939, in BaseGeodeticRepresentation.from_cartesian(cls, cart)
    933 @classmethod
    934 def from_cartesian(cls, cart):
    935     """
    936     Converts 3D rectangular cartesian coordinates (assumed geocentric) to
    937     WGS84 geodetic coordinates.
    938     """
--> 939     lon, lat, height = erfa.gc2gd(
    940         getattr(erfa, cls._ellipsoid), cart.get_xyz(xyz_axis=-1)
    941     )
    942     return cls(lon, lat, height, copy=False)

File ~/.local/share/virtualenvs/test-PbZKQCzX/lib/python3.10/site-packages/erfa/core.py:15845, in gc2gd(n, xyz)
  15771 def gc2gd(n, xyz):
  15772     """
  15773     Transform geocentric coordinates to geodetic using the specified
  15774     reference ellipsoid.
   (...)
  15843 
  15844     """
> 15845     elong, phi, height, c_retval = ufunc.gc2gd(n, xyz)
  15846     check_errwarn(c_retval, 'gc2gd')
  15847     return elong, phi, height

File ~/.local/share/virtualenvs/test-PbZKQCzX/lib/python3.10/site-packages/astropy/units/quantity.py:673, in Quantity.__array_ufunc__(self, function, method, *inputs, **kwargs)
    670     arrays.append(converter(input_) if converter else input_)
    672 # Call our superclass's __array_ufunc__
--> 673 result = super().__array_ufunc__(function, method, *arrays, **kwargs)
    674 # If unit is None, a plain array is expected (e.g., comparisons), which
    675 # means we're done.
    676 # We're also done if the result was None (for method 'at') or
    677 # NotImplemented, which can happen if other inputs/outputs override
    678 # __array_ufunc__; hopefully, they can then deal with us.
    679 if unit is None or result is None or result is NotImplemented:

ValueError: Invalid data-type for array

I’ve looked a bit further and it turns out the astropy error was most likely the result of installing my pipenv environment with certain flags required to correctly install apexpy on an Apple Silicon Mac. I followed the instructions on Installation — ApexPy "2.0.0" documentation for that. Specifically, I had set CFLAGS="-falign-functions=8 ${CFLAGS}" CC=/opt/local/bin/gcc-mp-12 FC=/opt/local/bin/gfortran-mp-12. Probably one or several of these flags results in the strange ValueError.