This story is part of an ongoing collaboration between ProPublica and FRONTLINE (PBS).

TEXAS CITY, TEXAS -- Two weeks before the blowout in the Gulf of Mexico, the huge, trouble-plagued BP refinery in this coastal town spewed tens of thousands of pounds of toxic chemicals into the skies.

The release from the BP facility here began April 6 and lasted 40 days. It stemmed from the company's decision to keep producing and selling gasoline while it attempted repairs on a key piece of equipment, according to BP officials and Texas regulators.

BP says it failed to detect the extent of the emissions for several weeks. It discovered the scope of the problem only after analyzing data from a monitor that measures emissions from a flare 300 feet above the ground that was supposed to incinerate the toxic chemicals.

The company now estimates that 538,000 pounds of chemicals escaped from the refinery while it was replacing the equipment. These included 17,000 pounds of benzene, a known carcinogen; 37,000 pounds of nitrogen oxides, which contribute to respiratory problems; and 186,000 pounds of carbon monoxide.

It is unclear whether the pollutants harmed the health of Texas City residents, but the amount of chemicals far exceeds the limits set by Texas and other states.

For years, the BP refinery in this town of 44,000 has been among the company's most dangerous and pollution-prone operations. A 2005 explosion killed 15 workers; four more workers have died in accidents since then. Last year, the U.S. Occupational Safety and Health Administration fined the company $87 million for failing to address safety problems that caused the 2005 blast.

In the weeks since the Deepwater Horizon exploded and sank in the Gulf, BP has insisted that the incident, the nation's worst environmental disaster, was a disastrous but unusual misstep for a company that has done much in recent years to change its ways.

But a look at BP's record in running the Texas City refinery adds to the mounting evidence that the company's corporate culture favors production and profit margins over safety and the environment. The 40-day release echoes in several notable ways the runaway spill in the Gulf. BP officials initially underestimated the problem and took steps in the days leading up to the incident to reduce costs and keep the refinery online.

Former workers and industry experts say BP's handling of the recent release of chemicals was typical of the plant's and company's operating practices.

The 40-day emissions were initially reported by the Daily News of Galveston, Texas, but received little national attention.

The unit was never completely shut down, and if it would have been, the event probably would have received more attention. Any reduction in production for even as little as 24 hours is considered sufficiently important to be reported in the financial press to investors and others.

Michael Marr, a BP spokesman, said the company had invested more than $1 billion since 2005 to improve the refinery.

Marr said BP initially monitored the emissions using a method approved by Texas regulators. It did not show any release in "excess of regulatory exposure limits to workers or the community during anytime." Using what Marr described as a method that "enables us to better understand the unit's emissions," BP found the much higher rate of release and notified Texas regulators on June 4.

Environmental experts say the amount of chemicals released was one of the largest in recent Texas history.

"This was a giant release over that 40-day period," said Neil Carman, who worked for the regulators for 12 years before joining the Sierra Club. "Even 50,000 pounds is big."

Carman said a study he performed showed the BP Texas City Refinery was already releasing more benzene into the atmosphere than any other place in the U.S. from 1997 to 2007.

BP spokesman Marr says the refinery's 2009 emissions dropped 20 percent from 2008, including a 50 percent drop in benzene emissions. BP had also invested in onsite chemical treatment to reduce emissions, Marr said.

"I would already argue that there's too much benzene in the air in Texas City," Carman said, "and then you add this release over 40 days, and it's just unconscionable that BP would do this."

Officials in Texas City, who were not informed of the scale of the release until after it was over, have asked BP to explain how this could have occurred. Marr said the company is now reviewing its procedures.

"I'm like, 'Oh goodness,'" Bruce Clawson, Texas City's coordinator for emergency management, recalls thinking when BP notified him about the release. "I had a lot of questions and they didn't have a lot of answers at that time."

Clawson said he is not yet satisfied. "Obviously, we do not like anything to be released," he said. "We expect better from them."

Marr said the incident began on April 6 when a component of the refinery's ultracracker went offline. The ultracracker, an integral part of the plant's processing of crude oil into gasoline and other petroleum products, processes 65,000 barrels of oil per day. A financial analyst who follows the industry said that each barrel should earn BP $5 to $10 in profits.

The part that malfunctioned, a hydrogen compressor, traps noxious chemicals, which can then be reused for fuel in the plant and other purposes. When the compressor stopped working, BP decided to send the gases to a 300-foot high flare, whose high temperatures turn the dangerous material into carbon dioxide.

The company knew that the burning process was incomplete and that at least trace amounts would escape. Marr said BP believed the plant's existing monitors, which are placed just a few feet above the ground level and approved by Texas regulators, would detect any excess emissions.

According to Marr, BP immediately also received measurements from a separate monitor that took readings from the flare. It was not until June 4, he said, that the company understood that the emissions were far higher than was permitted.

Despite repeated requests for clarification, Marr declined to say how long the company spent analyzing the data from the flare.

Industry experts say BP had reason to believe from the outset that emissions from the flare would be substantial.

Widely circulated industry guidelines assume that at least 2 percent of what is sent to a flare goes unburned and passes into the atmosphere. Because such large quantities of gas move through a refinery, this can amount to tens of thousands of pounds.

Carman of the Sierra Club says that flares also may be substantially less efficient than the industry believes. He said studies have shown that as much as 20 percent of what is sent to flares is released into the atmosphere.

"A 20 percent release from the flare would equal 5 million pounds and the benzene would have been 170,000 pounds," said Carman.

California regulators said that couldn't happen there. In Contra Costa County, home to several refineries, flares are to be used to handle chemical releases only in emergency situations, not regular operations.

"Refineries aren't allowed to do that in the Bay Area," said Randy Sawyer, the director of the hazardous materials programs in Contra Costa County. "If you have an upset and you need to get rid of gases in a hurry, you can send it to a flare. But if you continue to operate and dump a lot of stuff to a flare, that's not what they were designed for and it adds to pollution." California requires refineries to keep backup hydrogen compressors on hand and it stations regulators at the plants who are alert for any unscheduled flaring.

Last year, the Texas Attorney General filed a civil lawsuit against BP for “poor operating and maintenance practices’’ that caused an “egregious amount of emissions.”

That case cited 53 separate incidents that, taken together, are roughly equal to the 538,000 pounds BP calculates it released over the 40 days this year.

If BP had shut down the ultracracker, it would have lacked a key component needed to create gasoline suitable for its customers, said Mark Demark, the department chair of process technology at Alvin Community College.

"It's a big deal to shut the ultracracker down," he said. "It's operating at two to three thousand pounds of pressure, 700 degrees Farenheit; so it would take you a week just to cool that place down."

Demark, who worked for Shell for 33 years, said if he had been faced with that choice, he would probably have halted operations.

"Just from a public relations standpoint, for 40 days to have a flare going, you have to be really inconsiderate to your community," he said.