Ground Trackc calculation for a given orbit

How do we calculate the orbits of a ground track?

I cannot help with the calculation, you know more about it then I do.
But I can suggest how to find the error in the calculation.

print out the intermediate values of the calcualtion.

That way you will hopeful spot a value that is incorrect and can then fix that values calculation.