Skip to content

Instantly share code, notes, and snippets.

@lwsrbrts
Last active February 19, 2024 16:17
Show Gist options
  • Save lwsrbrts/d96092b65f2f7b7952e2f7983a9ee527 to your computer and use it in GitHub Desktop.
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.
$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
@1CM69
Copy link

1CM69 commented Oct 21, 2023

URI has changed.

LN25 should now be:

$Req = Invoke-WebRequest -Uri "http://marketinformation.natgrid.co.uk/MIPIws-public/public/publicwebservice.asmx" -Method 'Post' -ContentType 'application/soap+xml' -Body $Soap

@1CM69
Copy link

1CM69 commented Oct 21, 2023

Thank you very much for your code, I have made a few alterations to customise it for my own use & for getting the data for South Wales.

$TodayDate = Get-Date -Format "yyyy-MM-dd"
#$TodayDateF = Get-Date -Format "dd/MM/yyyy"
$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>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(WS)</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 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")

@1CM69
Copy link

1CM69 commented Oct 22, 2023

I manipulated the code some more to get just the current CV value but letting the user choose the LDZ

cls
$TodayDate = Get-Date -Format "yyyy-MM-dd"
#$TodayDate = "2023-08-15" #for testing

$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] '

$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>Y</LatestFlag>
        <ApplicableForFlag>Y</ApplicableForFlag>
        <FromDate>$($TodayDate)T00:00:00.000Z</FromDate>
        <ToDate>$($TodayDate)T00:00:00.000Z</ToDate>
        <DateType>NORMALDAY</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

[xml]$XML = $Req

$Value = $XML.Envelope.Body.GetPublicationDataWMResponse.GetPublicationDataWMResult.CLSMIPIPublicationObjectBE.PublicationObjectData.CLSPublicationObjectDataBE.Value

"`n`n`nThe current Calorific Value (CV) for LDZ ["+$LDZ.ToUpper()+"] for "+$TodayDate+" is {0:N2}" -f $Value 

"`nMore data available at: [https://data.nationalgas.com/]`n`n`n"

@1CM69
Copy link

1CM69 commented Oct 22, 2023

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"

@lwsrbrts
Copy link
Author

Thanks for your additions and comments. Always very helpful.

@1CM69
Copy link

1CM69 commented Oct 30, 2023

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