ptolemy of alexandria was a roman astronomer who wrote almagest in second century ad and also grouped 1,022 stars into 48 constellations which was used by copernicus for his primary writing in 1,500 years later .