Los Angeles (AFP) - A California man suffering from violent headaches learned just in time what was making him ill: a tapeworm larvae that had lodged in his brain.

Doctors told local media that the pupal parasite burrowed in the brain of Luis Ortiz, 26, a student in Sacramento, came close to taking his life, news reports said Thursday.

The worm was forming in a cyst that blocked the flow of water to chambers in Ortiz's brain, "like a cork in a bottle," his neurosurgeon Dr. Soren Singel told the Napa Valley Register newspaper.

Ortiz, who had gone to the hospital emergency room complaining of terrible headaches and nausea, eventually slipped into a coma, and required emergency surgery to remove the larvae.

"I was shocked," he later told CBS television as he continued to recover from the ordeal.

"I just couldn't believe something like that would happen to me. I didn't know there was a parasite in my head trying to ruin my life."

He said that during his convalescence, he has had to drop out of school, move back home, and that for now he can't drive or work.

"My memory is like a work in progress," Ortiz said, adding that his overall brain function is slowly improving thanks to therapy.