begining of automatic measurments algorithm