Last active
February 19, 2024 16:17
-
-
Save lwsrbrts/d96092b65f2f7b7952e2f7983a9ee527 to your computer and use it in GitHub Desktop.
Used to obtain the average natural gas calorific value for UK North West region from a set date to now.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$TodayDate = Get-Date -Format "yyyy-MM-dd" | |
$PreviousReading = Read-Host -Prompt 'Enter the date of the previous reading in dd/mm/yyyy format' | |
$PreviousReadingDate = Get-Date $PreviousReading -Format "yyyy-MM-dd" | |
$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>Latest</LatestFlag> | |
<ApplicableForFlag>ApplicableFor</ApplicableForFlag> | |
<FromDate>$($PreviousReadingDate)T00:00:00.000Z</FromDate> | |
<ToDate>$($TodayDate)T00:00:00.000Z</ToDate> | |
<DateType>GASDAY</DateType> | |
<PublicationObjectNameList> | |
<string>Calorific Value, LDZ(NW)</string> | |
</PublicationObjectNameList> | |
</reqObject> | |
</GetPublicationDataWM> | |
</soap12:Body> | |
</soap12:Envelope> | |
"@ | |
$Req = Invoke-WebRequest -Uri "http://mip-prodpull-api.azurewebsites.net/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 | |
$Value = $XML.Envelope.Body.GetPublicationDataWMResponse.GetPublicationDataWMResult.CLSMIPIPublicationObjectBE.PublicationObjectData.CLSPublicationObjectDataBE.Value | Measure-Object -Average | |
$Value.Average | |
[math]::Round($Value.Average, 4) | |
#OR | |
"{0:N2}" -f $Value.Average |
A few tweaks to the script for choosing the period to check.
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"
Thanks for your additions and comments. Always very helpful.
Thanks for your additions and comments. Always very helpful.
👍🏻
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I manipulated the code some more to get just the current CV value but letting the user choose the LDZ