You can purchase tickets for travelling by train in Morocco up to 90 days in advance.