Skip to content

Instantly share code, notes, and snippets.

@1CM69
Forked from lwsrbrts/GetCalorificAverage.ps1
Last active October 29, 2023 07:13
Show Gist options
  • Save 1CM69/4f56f4cc14b7bc11d1e9e9c7c401fd9c to your computer and use it in GitHub Desktop.
Save 1CM69/4f56f4cc14b7bc11d1e9e9c7c401fd9c to your computer and use it in GitHub Desktop.
Find the Average Gas Calorific Value for any LDZ in the UK for a period that you specify, based on : https://gist.github.com/lwsrbrts/d96092b65f2f7b7952e2f7983a9ee527
cls
$TodayDate = Get-Date -Format "yyyy-MM-dd"
$TodayDateF = Get-Date -Format "dd/MM/yyyy"
$LDZ = Read-Host -Prompt 'Enter the Local Distribution Zone (LDZ) that you require info for, see here: [https://www.xoserve.com/xoserve-search?term=Postcode%20Exit%20Zone/LDZ%20mapping%20list] '
$PreviousReading = Read-Host -Prompt 'Enter the date of the previous reading in dd/mm/yyyy format'
$PreviousReadingDate = Get-Date $PreviousReading -Format "yyyy-MM-dd"
$Tab = [char]9
$Soap = @"
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetPublicationDataWM xmlns="http://www.NationalGrid.com/MIPI/">
<reqObject>
<LatestFlag>N</LatestFlag>
<ApplicableForFlag>Y</ApplicableForFlag>
<FromDate>$($PreviousReadingDate)T00:00:00.000Z</FromDate>
<ToDate>$($TodayDate)T00:00:00.000Z</ToDate>
<DateType>GASDAY</DateType>
<PublicationObjectNameList>
<string>Calorific Value, LDZ($LDZ)</string>
</PublicationObjectNameList>
</reqObject>
</GetPublicationDataWM>
</soap12:Body>
</soap12:Envelope>
"@
$Req = Invoke-WebRequest -Uri "http://marketinformation.natgrid.co.uk/MIPIws-public/public/publicwebservice.asmx" -Method 'Post' -ContentType 'application/soap+xml' -Body $Soap
#Set-Content -Path D:\test.xml -Value $Req.Content # If you'd like to save the output to disk?
[xml]$XML = $Req
$Values = $XML.Envelope.Body.GetPublicationDataWMResponse.GetPublicationDataWMResult.CLSMIPIPublicationObjectBE.PublicationObjectData.CLSPublicationObjectDataBE.Value | Measure-Object -Average -Maximum -Minimum
$MEDdata = $XML.Envelope.Body.GetPublicationDataWMResponse.GetPublicationDataWMResult.CLSMIPIPublicationObjectBE.PublicationObjectData.CLSPublicationObjectDataBE.Value
$MODdata = $XML.Envelope.Body.GetPublicationDataWMResponse.GetPublicationDataWMResult.CLSMIPIPublicationObjectBE.PublicationObjectData.CLSPublicationObjectDataBE.Value
$MEDdata = $MEDdata |sort
if ($MEDdata.count%2) {
#odd
$MedianValue = $MEDdata[[math]::Floor($MEDdata.count/2)]
}
else {
#even
$MedianValue = ($MEDdata[$MEDdata.Count/2],$MEDdata[$MEDdata.count/2-1] |measure -Average).average
}
$i=0
$ModeValue = @()
foreach ($group in ($MODdata |group |sort -Descending count)) {
if ($group.count -ge $i) {
$i = $group.count
$ModeValue += $group.Name
}
else {
break
}
}
$Av = [math]::Round($Values.Average,1)
$Ma = [math]::Round($Values.Maximum,1)
$Mi = [math]::Round($Values.Minimum,1)
$Me = [math]::Round($MedianValue,1)
$Mo = $ModeValue[0]
$CV = [math]::Round(((($Av+$Me+$Mo)/3)+($Ma))/2,1)
"`n`nBelow is the Data returned for LDZ ["+$LDZ.ToUpper()+"] covering the period: "+$PreviousReading+" up to "+$TodayDateF
"`n"
"Average:$Tab" + $Av.ToString("0.0")
"Max:$Tab$Tab" + $Ma.ToString("0.0")
"Min:$Tab$Tab" + $Mi.ToString("0.0")
"Median:$Tab$Tab" + $Me.ToString("0.0")
"Mode:$Tab$Tab" + $Mo
"`n`n"
"A good All Round Figure To Use For CV Would Be: " + $CV.ToString("0.0")+"`n`nMore data available at: [https://data.nationalgas.com/]`n`n`n"
read-host “Press ENTER to EXIT”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment