ScubaMath.psm1


<#
        .Synopsis
        Math is hard!
 
        .DESCRIPTION
        Functions for planning your dive. Returns exact numbers, so you can do your own rounding, unlike tables,
        which do the rounding for you.
         
        .NOTES
        Yes. You can dive without a computer, even with Nitrox...
         
        .COMPONENT
        Scuba diving
         
        .ROLE
        Gas @ pressure math
         
        .FUNCTIONALITY
        Saftey calculations, and dive planning
#>



#region NITROX


Function Get-NitroxMOD
{
    <#
            .SYNOPSIS
            Gets the maximum operating depth in feet, based on F02, and desired P02
 
            .DESCRIPTION
            Calculations based on the magic circle method
 
            .PARAMETER P02
            Desired partial pressure for your dive. Warmer water, you can max out @ 1.6... colder water, suggest 1.4
 
            .PARAMETER F02
            Oxygen percentage your tank is filled with, in decimal form
 
            .EXAMPLE
            Get-NitroxMOD -P02 1.4 -F02 .36
            Returns the maximum depth for your dive.
 
            .LINK
            Watch the video https://www.youtube.com/watch?v=Ho3erOkNEXQ
 
            .INPUTS
            Double
 
            .OUTPUTS
            PSObject
    #>



    Param
    (
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Double] $P02 = 1.4,
        
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Alias('EAN','AirMix')]
        [Double] $F02 = .32
    )
    
    # We need to get our absolute atmospheric pressure, then convert it to depth
    $Atmosphere = $P02 / $F02 - 1
    
    $MOD = $Atmosphere * 33
    
    $EAD = Get-EquivalentAirDepth -Depth $MOD -F02 $F02
    
    $properties = [Ordered] @{

        #'MOD' = '{0:N1}' -f $MOD
        'MOD' = $MOD
        'P02' = $P02
        'F02' = $F02
        'EAD' = $EAD
        'Atmospheric_Pressure' = $Atmosphere
    }
    
    $retVal = New-Object -TypeName PSObject -Property $properties
    
    $retVal
}


Function Get-NitroxF02
{
    <#
            .SYNOPSIS
            Gets the fraction of oxygen, based on p02, and planned depth
 
            .DESCRIPTION
            Calculations based on the magic circle method. This method of finding the best EAN mix, is good for
            planning to dive a destination i.e. wreck dive.
 
            .PARAMETER P02
            Desired partial pressure for your dive. Warmer water, you can max out @ 1.6... colder water, suggest 1.4
 
            .PARAMETER MOD
            Maximum Operating Depth at which you plan to dive.
 
            .EXAMPLE
            Get-NitroxMOD -P02 1.6 -Depth 100
            If you planned to dive 100 ft, and your desired partial pressure is 1.6, this would return the optimal
            F02, to fill your tank with
 
            .LINK
            Watch the video https://www.youtube.com/watch?v=Ho3erOkNEXQ
 
            .INPUTS
            Double
 
            .OUTPUTS
            PSObject
    #>

    
    Param
    (
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Double] $P02 = 1.4,
        
        [Parameter( Mandatory = $true, 
                    Position = 1,
                    HelpMessage = 'Max depth you plan to dive',
                    ValueFromPipelineByPropertyName = $true
        )] [Double] $MOD
    )
    
    # We need to get our absolute atmospheric pressure from the depth
    $Atmosphere = $MOD / 33 + 1
    
    $F02 = $P02 / $Atmosphere
    
    $EAD = Get-EquivalentAirDepth -Depth $MOD -F02 $F02
    
    $properties = [Ordered] @{

        #'MOD' = '{0:N1}' -f $MOD
        'MOD' = $MOD
        'P02' = $P02
        'F02' = $F02
        'EAD' = $EAD
        'Atmospheric_Pressure' = $Atmosphere
    }
    
    $retVal = New-Object -TypeName PSObject -Property $properties
    
    $retVal
}


Function Get-NitroxP02
{    
        <#
            .SYNOPSIS
            Gets the partial pressure based on depth and gas mix
 
            .DESCRIPTION
            Calculations based on the magic circle method. Use to double check your P02 based on your tank fill,
            and dive plan. Always ensure you'll be safe... check it twice!
 
            .PARAMETER F02
            Oxygen percentage your tank is filled with, in decimal form
 
            .PARAMETER MOD
            Maximum Operating Depth at which you plan to dive.
 
            .EXAMPLE
            Get-NitroxMOD -F02 .40 -Depth 100
            If you planned to dive 100 ft, and your tank is filled with EAN40, this would return the P02 which you
            would experience at the MOD
 
            .LINK
            Watch the video https://www.youtube.com/watch?v=Ho3erOkNEXQ
 
            .INPUTS
            Double
 
            .OUTPUTS
            PSObject
    #>

    
    Param
    (
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Alias('EAN','AirMix')]
        [Double] $F02 = .32,
        
        [Parameter( Mandatory = $true, 
                    Position = 1,
                    HelpMessage = 'Max depth you plan to dive',
                    ValueFromPipelineByPropertyName = $true
        )] [Double] $MOD
    )
    
    # We need to get our absolute atmospheric pressure from the depth
    $Atmosphere = $MOD / 33 + 1
    
    $P02 = $F02 * $Atmosphere
    
    $EAD = Get-EquivalentAirDepth -Depth $MOD -F02 $F02
    
    $properties = [Ordered] @{

        #'MOD' = '{0:N1}' -f $MOD
        'MOD' = $MOD
        'P02' = $P02
        'F02' = $F02
        'EAD' = $EAD
        'Atmospheric_Pressure' = $Atmosphere
    }
    
    $retVal = New-Object -TypeName PSObject -Property $properties
    
    $retVal
}


Function Script:Get-EquivalentAirDepth
{    
        <#
            .SYNOPSIS
            Helper function. Gets the EAD, based on depth and oxygen levels in your air tank
 
            .DESCRIPTION
            Calculations in feet. Used to plan your decompression stops based on your dive plan.
            Always ensure you'll be safe... check it twice!
 
            .PARAMETER F02
            Oxygen percentage your tank is filled with, in decimal form
 
            .PARAMETER Depth
            Maximum depth at which you plan to dive.
 
            .EXAMPLE
            Get-NitroxMOD -F02 .40 -Depth 100
            If you planned to dive 100 ft, and your tank is filled with EAN40, this would return the P02 which you
            would experience at the MOD
 
            .LINK
            https://en.wikipedia.org/wiki/Equivalent_air_depth
 
            .INPUTS
            Double
 
            .OUTPUTS
            Double
    #>

    
    Param
    (
        [Alias('EAN','AirMix')]
        [Double] $F02 = .32,
        
        [Alias('MOD')]
        [Parameter( Mandatory = $true, 
                    Position = 1,
                    HelpMessage = 'Max depth you plan to dive',
                    ValueFromPipelineByPropertyName = $true
        )] [Double] $Depth
    )
    
    # Get the fraction of nitrogen (N2)
    $FN2 = 1 - $F02
    
    # First Atmosphere, in feet
    [Int] $A1 = 33
    
    # Ambient level of nitrogen in air
    [Double] $N2 = .791
    
    # Calc it up
    [Double] $EAD = ($Depth + $A1) * $FN2 / $N2 - $A1
    
    $EAD
}


#endregion