(CNN) -- Andy Weir had given up on writing as a career at the age of 26 after agents spurned his novel about a jewel heist involving aliens "on the Planet Sephalon."
After burning through severance checks from his layoff by AOL, he went back to work as a programmer in Silicon Valley.
Ten years later, in 2009, Weir decided to try writing again, but just as a hobby. Keeping his day job at a mobile phone software company, he started posting a new book on a personal website, chapter by chapter as he wrote it. This time, there were no aliens and no imaginary planet.
Instead he crafted a story, set a few decades in the future, about an astronaut who mistakenly gets left for dead on Mars when the other members of his crew are forced to make a quick escape from the effects of a devastating sandstorm.
This book found an audience. People starting following the story and it attracted scientists, including some who e-mailed Weir and offered suggestions to make the book's excursions into physics, chemistry and biology true to science.
Today Weir's "The Martian" is on the New York Times hardcover fiction bestseller list and has been optioned by 20th Century Fox for a potential movie, raising the question: How does a space nerd with no track record as a writer craft a compelling work of science fiction?
The book's hero, a cheeky astronaut named Mark Watney, possesses a self-reliance that enables him to jerry-rig NASA equipment in a suspenseful battle to eke out enough air, food and water to survive alone on Mars. In an interview with CNN, Weir said that his lead character is "smarter and braver than I am. The core personality that most people noticed -- that he's a massive smart ass -- that's basically my personality."
Watney finds ways to heal the injury that led his fellow astronauts to abandon him, thinking he was dead; to grow food in the "hab" module that is his home on Mars, to turn hydrogen and oxygen into water, to restore communication with NASA, and to drive his rover on the inhospitable Martian landscape far further than it was designed to go.
And yet critical life-support components keep failing, mishaps keep setting him back, and he keeps concluding that he's certainly about to die.
There's more than enough science and technology for the technically literate, and although he's never worked at NASA, Weir has gotten compliments for the accuracy of his portrait of an enormous bureaucracy's infighting as it struggles to save a man tens of millions of miles away. In the story, Watney's lonely struggle captures the attention of billions on Earth, even spawning a daily half-hour cable news program: "CNN's Mark Watney Report."
The book builds up the kind of narrative tension captured in the Oscar best-picture contender "Gravity," which Weir liked, even though it may have stretched the science. ("It doesn't have to be perfectly physically accurate to be entertaining. Nobody calls out the physics problems in 'Star Wars.'")
Yet accuracy is one of the things that gets cited in praise for Weir's book. Astronaut Chris Hadfield, former commander of the International Space Station, has said the book "has the very rare combination of a good, original story, interestingly real characters, and fascinating technical accuracy," according to Crown, the book's publisher.
Now 41, Weir is the son of a particle physicist -- his father double-checked much of the science in "The Martian" -- and an engineer. He got hooked on Asimov, Clarke, Heinlein and other classic science fiction writers by plucking their paperbacks from his father's shelf.
Predictably, Weir is fascinated by manned spaceflight and intrigued by the idea of a manned mission to Mars. But he's no fan of Mars One, the nonprofit that has gotten 200,000 people to express interest in being selected for a one-way trip to Mars, to take place in 2025.
Weir thinks the budget envisioned for the project is far too small and, "it would be basically a death sentence for the people who are going." He thinks a government-funded mission to Mars is far more likely but not for a long time. The after-effects suffered by astronauts on the International Space Station show the dangers of long-term space flight, he says. "There are a lot of pieces of the puzzle that we need to invent" to make for safe travel to Mars. Near term, he looks forward to a Chinese manned mission to the moon.
As for NASA, Weir says he's "disappointed by the state of our manned spaceflight program," especially the lack of a vehicle to replace the space shuttle. Would Weir want to fly on a space mission? "I am not a brave man ...I do not have the right stuff. Astronauts are really a cut above."
As a computer programmer, the closest Weir got to fame was as a member of the team that worked on the hit game "WarCraft2."
At his current job in Mountain View, California, Weir's bosses know the score, he says.
"I'm working on a pitch for my next novel right now, and if I get an advance, I'm going to quit and be a full time writer, which is the culmination of my dream coming true. I think I have to go sit in a coffee shop when I do that. And wear a neckerchief."