At 7:30pm local time on 21 November 2016, the corals of the Great Barrier Reef took part in "their annual sex festival" – as Chris Jones of the Great Barrier Reef Marine Park Authority calls it.
"It's a mass coral spawning event," says Jones. "Sperm and eggs are all released into the water at the same time and they join up."
The infant corals then look for an algae-covered surface to land on. There, if conditions are right, they will slowly form a new colony.
Jones and his colleagues were able to predict the timing of the coral spawning down to the half-hour, using knowledge of water temperatures, the moon and tides. But they may not get many more opportunities to do so. The Great Barrier Reef is in deep trouble, thanks to a host of threats – most of them man-made. If the reef does succumb, a history spanning hundreds of thousands of years will have come to an end on our watch.
The Great Barrier Reef is immensely, gigantically, unbelievably huge. Despite having dived on it and flown over it, I did not quite comprehend its size until I started researching this article. I might not quite get it even now.
When a coral dies, it turns to stone
The Reef is 1,430 miles (2,300km) long. To drive it would be the equivalent of driving from London to Romania. It is similar in size to Italy or Germany, with an area bigger than that of the United Kingdom, Switzerland and Holland combined. Made up of 3,000 coral reefs, 600 islands, 300 coral cays and roughly 150 inshore mangrove islands, it is ten times larger than the world's second largest barrier reef, the Belize Barrier Reef. It is even visible from outer space.
"Because of that enormous size, there's enormous diversity," says Jones. "There's old alluvial planes, old riverbeds that are now shaped like coral. You can see what used to be rivers and valleys."
And at the root of it all are the corals.
Corals are relatively small, simple animals that have been around for 500 million years. They feed by munching on algae on the seafloor. When a coral dies, it turns to stone, and it is these dead stone corals that make up the reef.
The core represents 800,000 years of the site's history
Corals can reproduce in a number of ways. Sometimes they clone themselves. Alternatively, if a segment breaks off and lands in a suitable place, it can grow to form a whole new colony.
Despite their global reputation for being vibrantly colourful, the vast majority of corals are a brownish green. "They're just not photographed as much," says Jones.
However, there are hard corals that glow fluorescent pink, purple or yellow. This is a reaction to sunlight, says Jones. The colour acts like sunscreen, protecting the coral from the Sun's ultraviolet radiation.
Corals have been making their home on the Great Barrier Reef for hundreds of thousands of years.
Jody Webster of the University of Sydney in Australia is a coral reef geologist. In his lab, he has a sediment core that spans the entire 210m depth of the reef. It has been chopped up into 1m-long segments for storage.
As the sea rises and falls the corals get turned on and off
The core represents 800,000 years of the site's history. However, Webster says the site has only been a barrier reef for about half of that.
The core is divided into layers called "reef units", each documenting a different period of the reef's history.
"Each one of the layers is produced by a rise and fall of the sea level," says Webster. "Every 100,000 years or so the sea level will rise, and then 100,000 later it will drop by up to 100-120m. When it drops it kills the reef in that position." These dead portions of reef then solidify into limestone caves.
Essentially, as the sea rises and falls the corals get turned on and off. However, no one knows what made the reef turn on in the first place.
Originally scientists thought it was triggered by a period of warmth between 300,000 and 400,000 years ago. However, this was then debunked by the 2005 discovery of rock sediments, which indicated there had not been any large rise in water temperatures.
During ice ages, sea levels fell and the area was dry with flat plains
It remains a mystery. "These are just ideas," says Webster. "We're trying to disentangle these different ideas and figure out why it is."
The reef is thought to have most recently turned back on 4,000-5,000 years ago, when sea levels began changing at a faster rate. The layers are closer together, suggesting there were more rapid shifts in the global climate.
"Every time the sea level goes down the reef dies," says Webster. "So it hasn't been growing for that entire 400,000-year period."
Since its beginnings, the reef has changed a great deal.
During ice ages, sea levels fell and the area was dry with flat plains. Then, as temperatures rose and the ice melted, the water level rose. The corals would then return, invading their old homes from thousands of years before, says Jones.
Since the 1990s, naval aeroplanes have been shooting lasers into the reef to understand its dimensions
Landslides have also shaped the reef. "There have been hundreds of small slides, but the bigger ones are more problematic," says Webster. One, known as the Gloria landslide, is thought to have struck the coast of Cairns 270,000 years ago, creating dips in the reef that are up to 1km deep.
All this history has left the reef with the most incredible structure and shape.
Parts of it look like a sponge or honeycomb, except that each of the individual pits or depressions can be 200-300m across, says Robin Beaman of James Cook University in Cairns, Australia.
Beaman has spent most of the past seven years painstakingly modelling the reef, using data provided by the Australian navy.
"Since the 1990s, naval aeroplanes have been shooting lasers into the reef to understand its dimensions, to improve nautical charts," he says. Their data has allowed Beaman and his colleagues to create high-resolution 3D maps of the reef.
This vegetarian mammal has a long vacuum-like mouth that it uses to suck up sea grass from the ocean floor
They found such "weird shapes", Beaman says, he outsourced the task of naming them to his nine-year-old son. "There are donuts, funguses, craters of the moon that are 20m high," he says. "If you were to scuba-dive on them – and you could because the top is at the 20m mark – you would really struggle to get a sense of how big they are."
The dips are so large, Beaman says he could not swim from one side to the other in one dive.
"No one really got how strange these things were until we got this navy data," he says. "This new technology has given us new eyes to look at the seafloor into a completely different way."
The complexity of the reef means a huge diversity of weird and wonderful animals have made their home on it.
One of these is the dugong, which looks like a cross between a seal, a cow and an elephant. Weighing in at up to half a tonne, this vegetarian mammal has a long vacuum-like mouth that it uses to suck up sea grass from the ocean floor.
"Australia is the last stronghold for dugong," says Jones. "It's an incredibly important species." Dugongs are one of only four remaining species in the Sirenia family, the other three being manatees. "It's a very small, unique group of mammals."
Aboriginal people have been using the reef resources for at least 40,000 and 60,000 years
The reef is also home to estuarine crocodiles, six of the world's seven species of marine turtle, and giant clams.
"The Great Barrier Reef is now one of the last places on Earth where you can see giant clams," says Jones. These molluscs can stretch longer than 1m and weigh 250kg.
Jones says visitors also love the sponges, which hide in dark places underneath coral. "Instead of having organs, they form these random shapes that filter water like a kidney, beating their hairs to create water currents."
Still, Jones says the truly important organisms are those we rarely see. There are some 500 species of worm, and a huge diversity of bacteria. "It's the little things living in and on the reef that are running the ecosystem," he says.
Perhaps the most recent addition to the reef ecosystem are human beings.
"Aboriginal people have been using the reef resources for at least 40,000 and 60,000 years," says Jones. They came from the Torres Strait Islands in the north, and from the area around Bundaberg on mainland Australia.
Britain's Captain James Cook sailed the HMS Endeavour along the length of the reef, before crashing into it
The different indigenous communities interacted with the reef differently. Many sailed vast distances along the reef, and along the coast of the Cape York Peninsula, to hunt and trade with other tribes.
Having a connection to specific land areas is a central part of Aboriginal custom and lore. "Sea country is just as important for them as land country, in terms of spirituality and resource use," says Jones. With this in mind, more than 70 Indigenous groups are still permitted to interact with the reef in their traditional ways, including hunting animals.
In the last few centuries, Europeans have also begun interacting with the reef.
The first documented sighting of the reef was off the coast of Cooktown in Queensland, by French captain Louis de Bougainville in 1768. But de Bougainville had no time to investigate. His crew was hungry and his ship was short on supplies, so he turned north towards Asia, bypassing Australia.
Two years later, Britain's Captain James Cook sailed the HMS Endeavour along the length of the reef, before crashing into it north of Cape Tribulation. He was forced to spend six weeks ashore repairing his ship, and the botanists on board did not get to do much scientific observation.
Nowadays the reef receives 2 million visitors each year
Once the ship was mended, Cook searched for a way out to the open sea. From the vantage point of Lizard Island, he spotted a path large enough to allow the Endeavour to pass. This is now known as Cook's Passage.
Over the next two centuries, many navigators and scientists attempted to survey the reef. This was partly motivated by a desire to connect the penal colony of Sydney with Asia, through the Arafura Sea. There are over 30 shipwrecks on the reef from this early exploration.
By the middle of the 20th Century, the reef was mainly used by old fisherman with glass bottom boats. It was also home to a number of turtle canneries.
But all that changed in the 1980s when tourism exploded. Nowadays the reef receives 2 million visitors each year.
"People love visiting the open wilderness," says Jones.
Jones says the greatest threat to the reef is climate change
He insists tourism does not damage the wildlife. "This is one of the most bewildering ideas that we come across," he says. "Virtually nothing could be further from the truth."
In fact, Jones says the industry is actually trained to support the reef by submitting data and strategically culling the harmful crown-of-thorns starfish.
"The proof in the pudding is when you go to these places that are visited by 300 people every day, and you can't spot any of the damage."
Instead, Jones says the greatest threat to the reef is climate change, followed by poor water quality and the impacts of fishing, including herbicides and pesticides.
Every major reef in the world has been affected
Corals have symbiotic algae living inside them, without which they cannot long survive. But, when the water heats up they sometimes lose their algae.
"When coral becomes stressed, it rejects the algae and pushes it out," says Jones. "That's when it turns transparent, so you can see through to their skeleton [of] calcium carbonate. It makes the coral look white. That's what we call coral bleaching."
Never has the effect of climate change on the reef been more apparent than this year.
Warm water from a strong El Niño, coupled with rises in water temperature due to climate change, has caused the longest and worst coral bleaching event ever recorded. Every major reef in the world has been affected.
Coral bleaching occurs if water temperatures rise by 1C for four weeks. If it lasts more than eight weeks, the corals begin to die.
Some corals cope with warm water better than others
On Queensland's north coast the event lasted for four months in early 2016, causing bleaching on 93% of the reef.
Almost a quarter of the reef is now dead. In some areas the death toll is up to 50%.
On The Conversation, biologist Lesley Hughes of Macquarie University writes: "While pockets of brilliant blue staghorn remain, much of the coral that bleached earlier this year is dead, the white skeletons filmed over by greenish-brown filamentous algae."
IIt sounds dire, and Hughes says it is likely to keep happening. "At present rates of climate change, this level of bleaching could occurevery two years by the 2030s," she writes. "The forlorn, diminished state of Australia's greatest natural treasure must continue to serve as a visible warning of what we stand to lose. The new normal is a very sad place to be."
However, not all researchers think the reef's future is quite so grim.
Some corals cope with warm water better than others. Unlocking the genetic factors that underpin these differences could be the key to helping the corals survive.
"A lot of science is looking at bacteria that can tolerate the thermal stress from global warming," says Jones.
Water quality seems to be very, very important in controlling what ultimately turns off the reef
In Townsville in northern Queensland, the Australian Institute of Marine Science has a huge building, filled with hundreds of tanks. They use these to emulate conditions in the wild. The aim is to find what allows some corals to persist over millennia, even through challenging periods like ice ages.
"There's always some that survive, and then they radiate," says Jones.
The goal is to make the reef resilient, so that the corals are better able to fight the effects of global warming.
"Water quality seems to be very, very important in controlling what ultimately turns off the reef," says Webster. "That has implications in terms of how we manage the modern reef."
Beaman's 3D maps have also offered some hope. In August 2016, he announced that there is a second reef structure behind the Great Barrier Reef.
Scientists knew about this structure as far back as the 1970s, but they thought it only spanned 2,000 sq km. "With the new data we've tripled that," says Jones. It actually covers 6,095 sq km.
The structure also extends a lot further north than had been thought, right up past the World Heritage area and into the Torres Strait.
It is not a barrier reef. Instead the structure is made of bioherms; large, reef-like mounds made up of dead flakes of Halimeda, a common green algae. They are found up to 40m down.
"There are old limestone pinnacles poking up. People are really excited here in North Queensland," says Beaman.
However, Beaman and his colleagues do not yet know what, if anything, climate change will do to the second reef. "We will have to apply a whole new set of tools and technologies to answer this climate change question," he says.
While there is still much to learn, the bioherms provide some hope for the future of coral reefs off Australia's north-east coast, especially as they appear to be growing in size.
"We are currently planning some big expeditions to try and understand these habitats better," says Beaman. "We thought it was all done and dusted 20 years ago, but these new maps have really changed things."
Join over six million BBC Earth fans by liking us on Facebook, or follow us on Twitter and Instagram.
If you liked this story, sign up for the weekly bbc.com features newsletter called "If You Only Read 6 Things This Week". A handpicked selection of stories from BBC Future, Earth, Culture, Capital, Travel and Autos, delivered to your inbox every Friday.