def gather_fitsheader_if_needed(header, comm=MPI.COMM_WORLD): """ Combine headers of local arrays into a global one unless the input is global. """ if 'NAXIS' not in header: raise KeyError("The FITS header does not contain the 'NAXIS' keyword.") if 'NAXIS1' not in header: raise KeyError('Scalar FITS headers cannot be combined.') if comm.size == 1: return header if not has_wcs(header): raise ValueError('The input FITS header does not define a world coordin' 'ate system.') naxis = header['NAXIS'] required_same = ['CRVAL1', 'CRVAL2', 'CRTYPE1', 'CRTYPE2', 'CDELT1', 'CDELT2'] required_same += ['NAXIS' + str(i+1) for i in range(naxis-1)] required_same += ['CRPIX' + str(i+1) for i in range(naxis-1)] headers = comm.allgather(header) for keyword in required_same: if keyword not in header: continue values = [h[keyword] for h in headers] if any(v != headers[0][keyword] for v in values): raise ValueError("The FITS keyword '{0}' has different values acros" "s MPI processes: {1}".format(keyword, values)) keyword = 'CRPIX' + str(naxis) if all(h[keyword] == headers[0][keyword] for h in headers): return header return gather_fitsheader(header, comm=comm)
def gather_fitsheader_if_needed(header, comm=MPI.COMM_WORLD): """ Combine headers of local arrays into a global one unless the input is global. """ if 'NAXIS' not in header: raise KeyError("The FITS header does not contain the 'NAXIS' keyword.") if 'NAXIS1' not in header: raise KeyError('Scalar FITS headers cannot be combined.') if comm.size == 1: return header if not has_wcs(header): raise ValueError( 'The input FITS header does not define a world coordin' 'ate system.') naxis = header['NAXIS'] required_same = [ 'CRVAL1', 'CRVAL2', 'CRTYPE1', 'CRTYPE2', 'CDELT1', 'CDELT2' ] required_same += ['NAXIS' + str(i + 1) for i in range(naxis - 1)] required_same += ['CRPIX' + str(i + 1) for i in range(naxis - 1)] headers = comm.allgather(header) for keyword in required_same: if keyword not in header: continue values = [h[keyword] for h in headers] if any(v != headers[0][keyword] for v in values): raise ValueError( "The FITS keyword '{0}' has different values acros" "s MPI processes: {1}".format(keyword, values)) keyword = 'CRPIX' + str(naxis) if all(h[keyword] == headers[0][keyword] for h in headers): return header return gather_fitsheader(header, comm=comm)
def test_has_wcs(): header = create_fitsheader([2, 4]) assert not has_wcs(header) header = create_fitsheader([2, 4], cdelt=1) assert has_wcs(header)