##########################################################################################################
##########################################################################################################
### Function to calculate and plot bubble size distribution spectral properties 
### (spectral slopes and breakpoints)
##########################################################################################################
###########################################################################################
## created by Marcus Klaus, Swedish University of Agricultural Sciences (SLU), Umeå, Sweden
## contact: marcus.klaus@slu.se
## last update: 2026/04/30
###########################################################################################

## INPUTS:
# Na... Bubble number density (m-3 μm-1)
# a... bubble radius (m)
# a_threshold... threshold bubble radius above and below which spectrum is fitted
# plotting... should spectrum be plotted? (TRUE/FALSE)

## OUTPUTS:
# alphaABS... spectral slope of sub-Hinze bubbles (full spectrum)
# alphaABSs... spectral slope of sub-Hinze bubbles (a<a_threshold)
# alphaABSl... spectral slope of sub-Hinze bubbles (a>=a_threshold)
# R2ABS... coefficient of determination of spectral fit for supra-Hinze bubbles

###########################################################################################

FitMicrobubbleSpectrum <- function(Na,a,a_threshold,plotting=TRUE){

## load helper functions
library(lmtest)
library(robustbase)
  
  
  # linear regression for all data and for radius < / >= a_threshold 
  Naa = log10(Na)
  aa = log10(a)
  Nas = log10(Na)[a<a_threshold/1000000]
  as = log10(a)[a<a_threshold/1000000]
  Nal = log10(Na)[a>=a_threshold/1000000]
  al = log10(a)[a>=a_threshold/1000000]

  
  ## fit linear models
  fits <- tryCatch(lm(Nas ~ as,maxit.scale=500,method = "MM"), error = function(e) NA)
  fitl <- tryCatch(lm(Nal ~ al,maxit.scale=500,method = "MM"), error = function(e) NA)
  fita <- tryCatch(lm(Naa ~ aa), error = function(e) NA)
  
  alphaABS <- tryCatch(summary(fita)$coefficients[2,], error = function(e) rep(NA,4))
  alphaABSs <- tryCatch(summary(fits)$coefficients[2,], error = function(e) rep(NA,4))
  alphaABSl <- tryCatch(summary(fitl)$coefficients[2,], error = function(e) rep(NA,4))
  R2ABS <- summary(fita)$r.squared

if(plotting==TRUE){
  
  ## plot data
  
  windows(width = 4, height = 4)
  layout(matrix(1:1, 1, 1, byrow = TRUE))
  par(mar=c(4.5,5.5,1.5,0.5),las=1,mgp=c(3.6, 1, 0))
  
  plot(Naa~aa,xlim=log10(c(10^-5,10^-3)),col=1,ylim=log10(c(10^1,10^7)),xaxt="n",yaxt="n",ylab=expression(paste("# Bubbles (", m^-3,mu, m^-1,")")),xlab="Bubble radius [m]")
  points(Naa[which(a<a_threshold/1000000)]~aa[which(a<a_threshold/1000000)],xlim=log10(c(10^-5,10^-3)),col=7)
  points(Naa[which(a>=a_threshold/1000000)]~aa[which(a>=a_threshold/1000000)],xlim=log10(c(10^-5,10^-3)),col=6)
  if(length(which(Naa>0))>1){
    aaseq=seq(min(aa),max(aa),0.0001)
    lines(aaseq,predict(fita,newdata = data.frame(aa=aaseq)),col=1,lty=2)
    }
  if(length(which(Nas>0))>1){
    asseq=seq(min(as),max(as),0.0001)
    lines(asseq,predict(fits,newdata = data.frame(as=asseq)),col=7)
    }
  if(length(which(Nal>0))>1){
    alseq=seq(min(al),max(al),0.0001)
    lines(alseq,predict(fitl,newdata = data.frame(al=alseq)),col=6)
    }
  
  
  axis(side=2,at=(log10(c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1))),labels=c(10^10,10^9,10^8,10^7,10^6,10^5,10^4,10^3,10^2,0.1,1),lwd=2)
  axis(side=2,at=(log10(c(seq(0.00000001,0.0000001,0.00000001),seq(0.0000001,0.000001,0.0000001),seq(0.000001,0.00001,0.000001),seq(0.00001,0.0001,0.00001),seq(0.0001,0.001,0.0001),seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100))*1000000)),labels=NA)
  axis(side=2,at=(log10(c(10^-10,10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,0.1,1))),lwd=2,labels=NA)
  
  axis(side=1,at=(log10(c(seq(0.00001,0.0001,0.00001),seq(0.0001,0.001,0.0001),seq(0.001,0.01,0.001),seq(0.01,0.1,0.01),seq(0.1,1,0.1),seq(1,10,1),seq(10,100,10),seq(100,1000,100)))),labels=NA,lwd=2)
  axis(side=1,at=(log10(c(0.00001,0.0001,0.001,0.01,0.1,1,10,100,1000))),lwd=2,labels=c(0.00001,0.0001,0.001,0.01,0.1,1,10,100,1000))
  
  
}

return(list(
  alphaABS=alphaABS,
  alphaABSs=alphaABSs,
  alphaABSl=alphaABSl,
  R2ABS=R2ABS
))
}
