The secret of what causes volcanic lightning - a strange and violent form of lightning that only happens inside the ash clouds of erupting volcanoes - has finally been cracked.

Not only have researchers in Germany figured out where volcanic lightning comes from, they might have also found a new way to measure how much ash a volcano is spewing out in real time, which will help us to predict air quality during and after eruptions.

Before we dive headfirst into volcanic lightning, we need to understand some basics about normal lightning. Lightning forms inside a cloud when a mix of warm and cold air causes a static electrical charge to build, with the top of the cloud having a positive charge and the bottom having a negative one.

"In the early stages of development, air acts as an insulator between the positive and negative charges in the cloud and between the cloud and the ground," explains the National Severe Storms Laboratory (NSSL).

"When the opposite charges builds up enough, this insulating capacity of the air breaks down and there is a rapid discharge of electricity that we know as lightning. The flash of lightning temporarily equalises the charged regions in the atmosphere until the opposite charges build up again."

A similar thing happens in volcanic lightning, with an electrical charge building up, but there are some big differences, too. Volcanic lightning, for example, forms closer to the ground, and doesn't always move downward in the same way that normal lightning does.

And, most importantly, volcanic lightning happens inside plumes of ash, rather than regular water vapour clouds, making them a mysterious force that researchers have figuring out for some time.

Now, a team from Ludwig-Maximillian University has observed these awesome bolts of lightning form in Japan's Mount Sakurajima, one of the most active volcanoes on the planet, using high-speed video footage and acoustic analyses.

After meticulously scrutinising the data they collected, the team found that volcanic lightning typically occurs in the lower section of the ash cloud.

This, they say, is because the churning magma inside the rim of the volcano causes the ash cloud right above it to become electrified. Eventually, this charge builds up similar to the way it does in a normal cloud, producing a lightning strike.

"These data lead us to infer that although volcanic and thundercloud lightning share many common physical characteristics, the conditions presaging the occurrence of electrical discharges at explosive eruptions result from the complex charge distribution within the developing plume," the team reports.

While analysing their findings, the team noticed that that the frequency of the lightning strikes increased with the size of the ash plume.

This correlation, they hope, will offer a new way to gauge how large an eruption is and how much ash is thrown into the air without putting researchers in danger. After all, lightning flashes are pretty easy to count even from far away.

"This is a parameter that can be measured - from a distance of several kilometres away and under conditions of poor visibility," one of the team, Corrado Cimarelli, told Maddie Stone at Gizmodo.

While we might now know how volcanic lightning comes to life, there's likely a lot more to learn about them - like this 2015 discovery, where researchers from the University of Alabama found that volcanic lightning actually creates perfectly round balls of glass.

The findings were published in Geophysical Research Letters.