add margin and offset and variance
code runs but offsets are wrong at least
fixed offsetting
changed to casting ccd instead of finding it