Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://doi.org/10.5281/zenodo.15690037
18 June 2025, 11:59:23 UTC
  • Code
  • Branches (0)
  • Releases (1)
  • Visits
    • Branches
    • Releases
      • 1
      • 1
    • 6d53078
    • /
    • dmaciel123-BRAZA-b9360a9
    • /
    • Scripts
    • /
    • baseline_shift.R
    Raw File Download

    To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
    Select below a type of object currently browsed in order to display its associated SWHID and permalink.

    • content
    • directory
    • snapshot
    • release
    origin badgecontent badge
    swh:1:cnt:285ade4ba8936d8b2f5c4f3ec61fb3822e171e87
    origin badgedirectory badge
    swh:1:dir:800709718798e2ec38f54de29db55bbe58074706
    origin badgesnapshot badge
    swh:1:snp:751ae64375449ec5a7624a34dea2901f039df8e6
    origin badgerelease badge
    swh:1:rel:63f034f46a613d794f3480d38698b1371e3757bc

    This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
    Select below a type of object currently browsed in order to generate citations for them.

    • content
    • directory
    • snapshot
    • release
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    baseline_shift.R
    
    # This script is called by Run.R to calculate the Baseline_shift flag on GLORIA Rrs spectra
    # Refer to README.md for a description of the method.
    
    
    
    
    ### Function for calculating negative slopes
    
    negative_slopes = function(gloria_rrs) {
    
          #Create a new data.frame for including the results based on GLORIA_ID
          results = data.frame(id = gloria_rrs$GLORIA_ID)
          
          
          #Count negative number of spectra
          
          #All wavelengths
          
          results$negative_all =select(gloria_rrs, contains(paste('Rrs_', 400:900, sep = '')))  %>% apply(MARGIN = 1, FUN = function(x){ x = na.omit(x)
          length(x[x<0])})
          
          #NIR (700-900)
          results$negative_nir = select(gloria_rrs, contains(paste('Rrs_', 700:900, sep = ''))) %>% apply(MARGIN = 1, FUN = function(x){ x = na.omit(x)
          length(x[x<0])})
          
          #UV-BLUE (350-450)
          results$negative_blue = select(gloria_rrs, contains(paste('Rrs_', 350:450, sep = ''))) %>% apply(MARGIN = 1, FUN = function(x){ x = na.omit(x)
          length(x[x<0])})
          
          # Number of valid spectra in NIR Region
          results$length_nir = select(gloria_rrs, contains(paste('Rrs_', 700:900, sep = ''))) %>% apply(MARGIN = 1, FUN = function(x){ x = na.omit(x)
          length(na.omit(x))})
          
          #Ratio of negative/total number of valid spectra in NIR region
          results$Percentage_negative = results$negative_nir /results$length_nir *100
          
          
          
          #Slope calculation (765-900 nm)
          
          rrs.slope = select(gloria_rrs, contains(paste('Rrs_', 765:900, sep = '')))
          
          results$slope_nir = NaN
          
          
          print('Calculating Slope')
          
          for(i in 1:nrow(rrs.slope)) {
          
            pt1 = rrs.slope[i,] %>% t() %>% data.frame(WV= 765:900)
            names(pt1) = c('pt', 'WV')
            pt1 = filter(pt1, pt < 1)
          
            N = nrow(pt1)
          
            if(is.na(pt1$pt[2]) == FALSE ) {
          
              results$slope_nir[i] = summary(lm(pt~WV, data  = pt1))$coefficients[2]
            }
          
          }
          
          
          #### Removing data with negative values lower than 20
          
          filtro_neg_20 = filter(results, negative_all > 20)
          
          
          ##Negative slope NIR lower than lower hinge and with N > 50
          
          bx =boxplot(filtro_neg_20$slope_nir, plot = F)
          negative_nir_slope = filter(filtro_neg_20, negative_nir > 50 & slope_nir < bx$stats[2,])
          
          
          
          #Select values when 90% of Rrs in NIR region are negative
          perc_negative_70 = filter(filtro_neg_20, Percentage_negative > 70)
          perc_negative_50 = filter(filtro_neg_20, Percentage_negative > 50 & slope_nir < bx$stats[2,])
          
          
          
          ## Negative for UV-Blue higher than 20
          filtro_blue_neg = filter(filtro_neg_20, negative_blue > 20)
          
          
          ## Merge the results
          results.merge = rbind(negative_nir_slope, filtro_blue_neg,perc_negative_70,perc_negative_50)
          results.merge = results.merge[order(results.merge$id),]
          
          #Remove duplicates
          results2 = results.merge[duplicated(results.merge) == FALSE,]
          
          
          negatives = data.frame(GLORIA_ID = gloria_rrs$GLORIA_ID, negative_slopes = 0)
          negatives[negatives$GLORIA_ID %in% results2$id, 'negative_slopes'] = 1
          
          
          
          return(negatives)
    
    }
    
    ## Function for calculating baseline shift
    
    baseline_shift = function(gloria_rrs) {
    
          #Create a data.frame to store the results
          baseline = data.frame(ID = gloria_rrs$GLORIA_ID)
          
          #Wavelengths used to calculate the baseline
          rrs_counts = paste('Rrs', 400:900, sep = '_')
          
          
          #Min and median spectra functions without NA
          min.na = function(x) {return(min(x, na.rm = T))}
          median.na = function(x) { return(median(x, na.rm = T))}
          
          #Calculate min and median of the spectra
          baseline$min = apply(X = select(gloria_rrs, contains(rrs_counts)), MARGIN = 1, FUN = min.na)
          baseline$median = apply(X = select(gloria_rrs, contains(rrs_counts)), MARGIN = 1, FUN = median.na)
          
          
          #Baseline calculation (min Rrs / median Rrs)  * 100
        
          baseline$BASELINE_by_median = baseline$min/baseline$median*100
          
          #Baseline boxplot calculation
          bx_median = boxplot(baseline$BASELINE_by_median, ylab = '% Difference', plot = FALSE)
          
          #Filter by higher whisker of boxplot
          baseline.filter = filter(baseline, BASELINE_by_median > bx_median$stats[5,])
          
          #Filter by 60%
          baseline.filter_60 = filter(baseline, BASELINE_by_median > 60)
          
          #create the dataframe to store the results 
          baseline.results= data.frame(GLORIA_ID = gloria_rrs$GLORIA_ID, baseline = 0)
          
          #Results
          baseline.results[baseline.results$GLORIA_ID %in% baseline.filter_60$ID, 'baseline'] = 1
          
          return(baseline.results)
    
    }
    
    ## Function to merge both results
    
    merge_baseline_negative = function(GLORIA_ID, baseline, negative) {
      
      Baseline_shift = data.frame(GLORIA_ID = GLORIA_ID,
                                  negatives = baseline,
                                  positives = negative)
      
      Baseline_shift$Baseline_shift = Baseline_shift$negatives+Baseline_shift$positives
      Baseline_shift$Baseline_shift = gsub(x = Baseline_shift$Baseline_shift, pattern = 2, replacement = 1) %>% as.numeric()
      
      Baseline_shift = select(Baseline_shift, c('GLORIA_ID', 'Baseline_shift'))
      
      return(Baseline_shift)
    }
    
    
    

    back to top

    Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
    The source code of Software Heritage itself is available on our development forge.
    The source code files archived by Software Heritage are available under their own copyright and licenses.
    Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API