subroutine omcosflag read in task parameters open OM OSW Image file determine file type from datamode in primary header read in science window keywords from the primary header extract window data from MODES binary table extension open OM Tracking History Data Auxiliary file determine file type from datamode in primary header extract tracking history data from OMTHX1 binary table extension check for poor tracking solution and warn extract bad pixel map from CAL calculate maximum drift in x and y extract localised bad pixel map allowing for maximum drifts over sample localised bad pixel map loop over tracking frames to construct oversampled quality array loop over discretised tracking steps logical OR oversampled quality array with shifted oversampled bad pixel map end loop end loop add QUALITY extension to OM OSW Image file resize and bin quality array with appropriate binning factors write 16-bit quality array to QUALITY extension of OM OSW Image file end subroutine omcosflag