Post-processing Heading from Gyro / Compass

We have extensive field data that has been collected while the compass was not correctly calibrated. Thus, the compass only reports values in range of 50-110 degs instead of the 0-360 degs. I have the raw XMag and YMag values extracted, and recalibrated by running a soft iron / hard iron calibration on it. I’d like to merge this back with the ZGyro readings using the same settings as on the pixhawk to get back the filtered heading estimates, is there a starighforward way to achieve this?

If not, can someone point me to where in the code this is performed on the hardware, so that I might replicate it in a Matlab / Python script.