Considering its lamentable state when it was taken in hand two years ago by Prince Charles, The Great Steward of Scotland and head of the Dumfries House Trust, the transformation of the five-acre walled garden at the John and Robert Adam-designed grand house in North Ayrshire is astonishing.

Visitors to the garden on the banks of the Lugar Water, which opens to the public next month, may not realise that the original 18th-century red-brick walls which wrap around it on three sides took two years to bring back from collapse; that the two new airy glasshouses filled with flowers and vegetable seedlings are replicas of the originals; that the magnificent sheltering yew hedge separating this garden from the community education garden beside it is also newly planted; and that the nourishing soil in which the orchard, kitchen garden, flower garden, lawns and decorative topiary grow is also freshly imported. Those walking round the beautiful, peaceful and productive garden would be hard pressed to see that it hadn't been like this for at least a century.

The first difficult steps towards its restoration and regeneration were taken last year by Michael Innes, the Fife designer appointed by Prince Charles to come up with a plan - not only for thousands of visitors, but also for future generations of young gardeners. The walled garden development is a major part of the trust's restoration of the sprawling estate after the prince stepped in to save the historic house and its priceless Chippendale furniture in 2007.

Innes soon discovered a major hurdle: there was no definitive layout he could replicate and some of the early plans he'd found for the garden were never implemented, probably because it was such a difficult site to make productive. He reckoned it hadn't been cultivated in the 20th century, when it was probably used for lambing. His beautiful design took account of a nine-metre difference of levels going east to west: he made a virtue of having higher and lowers levels, and levelled off the sloping bottom half of garden with tons of earth.

Scroll forward to January this year, and new head gardener James Goodman was, quite literally, in the thick of it. "When I first saw the garden, it was mud, rain and more mud," he recalls. "The new greenhouses weren't in, and I was awestruck by the size and scale of it. But it was very exciting."

One of his first tasks was to address the sad loss last winter of a major branch of the 300-year-old sycamore tree in the centre of the garden. Goodman and his advisers took the decision to have its remaining branches carefully lopped back to avoid it losing any more. An indication of how important the tree is to the area is the level of scientific analysis involved. An internal MRI scan was done to gauge the thickness and strength of the trunk to see how much - or how little - it could be reduced without fatally damaging it. Judging by its lush, verdant growth, the tree has clearly overcome any trauma and is thriving on all the TLC.

When we met in April, Goodman's mission was to have the otherwise empty garden fully planted by mid-June with a huge range of vegetable, fruit and flower varieties all grown from seed - a major undertaking. He'd already become familiar with the local pests: with such a blank soil, the volume of larvae within it was "horrendous". They'd found hundreds of leatherjackets, which kill small plants in flower beds and vegetable plots by eating the roots and stem bases, and discovered hares, which like to hide in the greenhouse during the day and come out at night to munch on young brassicas. Non-chemical pest control is de rigueur. In this, as with the rest of the garden, Goodman is working in partnership with Prince Charles. "He gives very good feedback and he has an amazing knowledge of plants," he says.

The project has been notable for evolving in the unusually hot, dry early summer. The garden gets the full sun all day long and even in March, the heat was so significant it had already dried out some of the seedlings in the production glasshouse. But Goodman was unfazed: a new underground irrigation system was part of Michael Innes's design and in the greenhouse, natural water is collected from the gutters in water butts for watering seedlings. When I visited, the warm space was filled with a putative crop of healthy produce, such as Prague celeriac; red deer tongue lettuce; chocolate cherry, stupice and Fahrenheit Blues tomatoes; mammoth and lettuce leaf basil; artichokes, aubergines, greengages, peaches, figs and even vines. These were planted out in a complex and sophisticated rotation system, and all produce will be used in the Dumfries House restaurant, as well as the cook school and for functions such as weddings. "We'll really cram it all in and if there's a glut we'll sell it at the ticket office," says Goodman.

In the lower greenhouse, where flowers are grown for planting out in the display gardens, there are lilies, fuchsias, camellias, geraniums. Lime and pleached fruit trees (trees trained into shapes) will provide shade as the garden matures.

Even so, getting to know the garden and its climatic vagaries will take time. "In season one we have to pay attention to the weather forecast every day. We can't leave anything to chance and are stocking up with fleece and frost aids," says Goodman.

Every seedling and its planting time has been fastidiously recorded, together with temperature variations and growth levels. Goodman is working whatever hours the garden needs and to facilitate his 24-hour presence on site he has moved into the restored gardener's cottage with his wife, an occupational therapy student.

Besides heritage and heirloom varieties, he is interested in experimenting with growing unusual, endangered and exotic species from seed. For example, quinoa, a high-energy grain that is becoming more expensive because it faces a world shortage; amaranthus, for cultivation as a grain; sweet potatoes; oca (or New Zealand yam, a small bright red potato that's salty and citrussy).

"I'd also like to grow Yacon root tuber, which is a natural, non-fattening alternative to sugar that tastes like chestnut," he says. These crops are all alternatives to potato and are blight resistant.

In addition to new varieties, he's also keen to bring back traditional varieties such as rare peas that have gone out of production, simply because they're very tall and don't suit modern machinery; and Sutherland kale, an old crofters' variety that has almost disappeared.

To this end, Goodman's team will be expanded with new staff, complemented with horticultural students. "Everyone's learning from each other and there's so much going on in the horticultural world it's essential to share knowledge," he says.

"To get the opportunity to work with something on the brink of non-existence and bring it all the way back is very rare and even though it's been extremely hard work we are all thrilled with the result."

The walled garden at Dumfries House, Cumnock, opens to the public next month. Visit dumfries-house.org