Wednesday, June 12, 2019

Powershell: Invoke-RestMethod retrieve garbled data

When I tried to retrieve CSV data from Japan Meteorological Agency, the data totally get garbled.
So I swithed to use Invoke-WebRequest with [System.Text.Encoding]::Default.GetString method to resolve the issue.

# Retrieve CSV data with following Invoke-RestMethod get garbled data.
PS > $jma = Invoke-RestMethod -Uri "https://www.data.jma.go.jp/obd/stats/data/mdrr/tem_rct/alltable/mxtemsadext00_rct.csv" -Method get


PS > $jma | more
?I?a???O??,?s?1?{?§,?n?_,???U?n?_?O??,?≫?Y????(?N),?≫?Y????(??),?≫?Y????(?u),?≫?Y????(??),?≫?Y????(?a),?!?u?I?A???C?・(??),?!?u?I?A???C?・?I?i???i?n,?!?u?I?A???C?・?N???i???j,?!?u?I?A???C?・?N???i?a?j,?!?u?I?A???C?・?N???I?i???i?n,???N?・?i???j,?O?u?・?i???j,?Y???{?i???j,?Y???{?i?{?j,?E?l?X?V,10?N?¢???A?I?E?l?X?V,?!?N?A??,?!?N?I?A???C?・?i??)?i?d?u?U?A?j,?!?N?I?A???C?・?i?d?u?U?A?j?I?i???i?n,?!?N?I?A???C?・?i?d?u?U?A?j?d?I?a?μ???N?u?i?N?j,?!?N?11001,?k?C?1?@?J?n?u,?@?J?|?i?\?E???~?T?L?j,,2019,06,12,20,00,11.7,5,11,33,5,-2.8,-2.9,5,2,,,0,25.4,4,2019,05,20,31.9,8,



# Invoke-WebRequest with [system.Text.Encoding]::Default.GetString combination works fine!
PS > $jma = [system.Text.Encoding]::Default.GetString((Invoke-WebRequest "https://www.data.jma.go.jp/obd/stats/data/mdrr/tem_rct/alltable/mxtemsadext00_rct.csv").RawContentStream.ToArray())

PS > $jma | more
観測所番号,都道府県,地点,国際地点番号,現在時刻(年),現在時刻(月),現在時刻(日),現在時刻(時),現在時刻(分),今日の最高気温(℃),今日の最高気温の品質情報,今日の最高気温起時(時),今日の最高気温起時(分),今日の最高気温起時の品質情報,平年差(℃), 前日差(℃),該当旬(月),該当旬(旬),極値更新,10年未満での極値更新,今年最高,今年の最高気温(℃)(昨日まで),今年の最高気温(昨日まで)の品質情報,今年の最高気温(昨日まで)を観測した起日(年),今年の最高気温(昨日まで)を観測した起日(月),今年の最高気温(昨日まで)を観測した起日(日),昨日までの観測史上1位の値(℃),昨日までの観測史上1位の値の品質情報,昨日までの観測史上1位の値を観測した起日(年),昨日までの観測史上1位の値を観測した起日(月),昨日までの観測史上1位の値を観測 した起日(日),昨日までの6月の1位の値,昨日までの6月の1位の値の品質情報,昨日までの6月の1位の値の起日(年),昨日までの6月 の1位の値の起日(月),昨日までの6月の1位の値の起日(日),統計開始年
11001,北海道宗谷地方,宗谷岬(ソウヤミサキ),,2019,06,12,21,00,11.7,5,11,33,5,-2.8,-2.9,5,2,,,0,25.4,4,2019,05,20,31.9,8,2000,08,01,26.4,8,2005,06,22,1978
11016,北海道宗谷地方,稚内(ワッカナイ),47401,2019,06,12,21,00,12.5,5,10,53,5,-2.9,-5.5,5,2,,,0,23.4,4,2019,05,31,31.3,8,1946,08,22,26.8,8,1977,06,30,1938


Sunday, June 9, 2019

Powershell: Get JST from Unix epoch time

Let's see how to convert UTC to JST by using System.Timespan object, with sample data retrieved from OpenWeatherMap.

Also refer FromSeconds method.


# Retrieve JSON formatted weather data
PS > $ow = Invoke-RestMethod -Uri "api.openweathermap.org/data/2.5/weather?id=1863967&units=metric&appid=xxxx" -Method Get

PS > $ow

coord : @{lon=130.42; lat=33.61}
weather : {@{id=803; main=Clouds; description=broken clouds; icon=04n}}
base : stations
main : @{temp=19.68; pressure=1004; humidity=77; temp_min=18.89; temp_max=20}
visibility : 10000
wind : @{speed=1.5; deg=270}
clouds : @{all=75}
dt : 1560090033
sys : @{type=1; id=7998; message=0.0064; country=JP; sunrise=1560024473; sunset=1560076033}
timezone : 32400
id : 1863967
name : Fukuoka-shi
cod : 200


# Let's get JST sunrise time from Unix timestamp.
PS > ((Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($ow.sys.sunrise))).AddHours(9)

2019年6月9日 5:07:53