MARGATE, N.J. — A federal judge on Thursday gave the U.S. government the OK to resume a beach replenishment project in New Jersey that led to ponds of bacteria-laden water after a storm.

Judge Renee Marie Bumb overturned a state judge’s ruling and granted the request by the U.S. Army Corps of Engineers to let the project in Margate continue. But the judge ordered the Army Corps to fence off any areas with ponding of 2 or more inches of water, along with other conditions.

The government went to court Wednesday seeking to have overturned a ruling by a state judge last week that stopped the project while the two sides tried to work out a remedial plan.

“Margate’s grave concerns over public safety and health are understandable and cry out for help,” Bumb wrote. “The measures this Court will put in place, although not perfect, should serve to prevent irreparable harm.”

Bumb ordered the engineers to investigate the root causes of the ponding and possible solutions, pump out future ponding immediately after reaching four inches rather than waiting 24 hours, and construct elevated walkways across the water basins.

The ponds of standing water are exactly what Margate residents had warned of in previous, unsuccessful litigation against the government’s plan to build the protective sand dunes at the coastal town south of Atlantic City.

The work is part of a statewide effort to protect New Jersey’s 127-mile coastline following the devastation that Superstorm Sandy caused in 2012. But Margate has opposed it as unnecessary and harmful to its shoreline, and now a public health hazard.

When storms during the last weekend of July dumped nearly a half foot of rain on Margate, water quickly collected in excavated areas between the dunes and the wooden bulkhead that separates oceanfront homes from the sand. Some were 2 or more feet deep.

Mayor Michael Becker said the city’s testing of water from the ponds found “off the charts” levels of bacterial contamination. He said two lifeguards had contracted bacterial infections by sloshing through the dirty water, and were out sick from work.

Superior Court Judge Julio Mendez last Thursday said he was horrified by photos showing “lake-like” ponds of standing water on the beach in Margate for days following heavy rains. He agreed that residents of the town just south of Atlantic City are suffering irreparable harm and that the situation cannot be allowed to be made worse, and had halted the project for at least a week while the two sides worked out a remedial plan.

“Some of those photos are pretty horrendous,” Mendez said. “It’s lake-like ponding causing tremendous difficulty. Something went really bad here. The bottom line is this needs to be corrected.”