gismeteo.ru bash script template (with xml)

Mar 21, 2009 01:46

Шаблон для тех, кто хочет сделать себе прогноз погоды на sh.
Понадобится xmlstarlet. Линк на Вворонеж: http://informer.gismeteo.ru/xml/34122_1.xml (у меня переименован в gismeteo.xml)



#!/bin/sh

echo "Prediction interation 1:"

# FORECAST
# Forecast date:
date=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST \
-v "concat(@day,'.',@month,'.',@year,' ')" gismeteo.xml` )
echo Date: ${date[0]}

# Forecast time:
hour=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST \
-v "concat(@hour,' ')" gismeteo.xml` )
echo "Hour: ${hour[0]}:00 MSK"

# Forecast lead-time in hours:
leadtime=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST \
-v "concat(@predict,' ')" gismeteo.xml` )
echo "Lead-time: ${leadtime[0]} hrs."

# Time of day:
tod_id=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST \
-v "concat(@tod,' ')" gismeteo.xml` )
case "${tod_id[0]}" in
[0] ) tod="Night" ;;
[1] ) tod="Morning" ;;
[2] ) tod="Day" ;;
[3] ) tod="Evening" ;;
esac
echo "Time of day: $tod (id: ${tod_id[0]})"

# PHENOMENA
# Cloudiness:
cloudiness_id=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/PHENOMENA \
-v "concat(@cloudiness,' ')" gismeteo.xml` )
case "${cloudiness_id[0]}" in
[0] ) cloudiness="Fair (ясно)" ;;
[1] ) cloudiness="Fair with some cloud (малооблачно)" ;;
[2] ) cloudiness="Cloudy (облачно)" ;;
[3] ) cloudiness="Dull (пасмурно)" ;;
esac
echo "Cloudiness: $cloudiness (id: ${cloudiness_id[0]})"

# Precipitation:
precipitation_id=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/PHENOMENA \
-v "concat(@precipitation,' ')" gismeteo.xml` )
case "${precipitation_id[0]}" in
[4] ) precipitation="Rain (дождь)" ;;
[5] ) precipitation="Downpour (ливень)" ;;
[6] ) precipitation="Snow (снег)" ;;
[7] ) precipitation="Snow (снег)" ;;
[8] ) precipitation="Storm (гроза)" ;;
[9] ) precipitation="Unknown (нет данных)" ;;
* ) precipitation="Clear (без осадков)" ;;
esac
echo "Precipitation: $precipitation (id: ${precipitation_id[0]})"

# Probability of precipitations
rpower_id=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/PHENOMENA \
-v "concat(@rpower,' ')" gismeteo.xml` )
case "${rpower_id[0]}" in
[0] ) rpower="Possible..." ;;
[1] ) rpower="For sure" ;;
esac
echo "Probability of precipitations: $rpower (id: ${rpower_id[0]})"

# Probability of storm
spower_id=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/PHENOMENA \
-v "concat(@spower,' ')" gismeteo.xml` )
case "${spower_id[0]}" in
[0] ) spower="Possible..." ;;
[1] ) spower="For sure" ;;
esac
echo "Probability of storm: $spower (id: ${spower_id[0]})"

# Pressure:
pressure_max=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/PRESSURE \
-v "concat(@max,' ')" gismeteo.xml` )
echo "Max. Pressure: ${pressure_max[0]}"

pressure_min=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/PRESSURE \
-v "concat(@min,' ')" gismeteo.xml` )
echo "Min. Pressure: ${pressure_min[0]}"

pressure_avg=$((($pressure_max+$pressure_min)/2))
echo "Avg. Pressure: ${pressure_avg}"

# Temperature:
temperature_max=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/TEMPERATURE \
-v "concat(@max,' ')" gismeteo.xml` )
echo "Max. Temperature: ${temperature_max[0]}"

temperature_min=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/TEMPERATURE \
-v "concat(@min,' ')" gismeteo.xml` )
echo "Min. Temperature: ${temperature_min[0]}"

temperature_avg=$((($temperature_max+$temperature_min)/2))
echo "Avg. Temperature: ${temperature_avg}"

# Wind:
wind_max=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/WIND \
-v "concat(@max,' ')" gismeteo.xml` )
echo "Max. Wind: ${wind_max[0]}"

wind_min=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/WIND \
-v "concat(@min,' ')" gismeteo.xml` )
echo "Min. Wind: ${wind_min[0]}"

wind_avg=$((($wind_max+$wind_min)/2))
echo "Avg. Wind: ${wind_avg}"

winddir_id=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/WIND \
-v "concat(@direction,' ')" gismeteo.xml` )
case "${winddir_id[0]}" in
[0] ) winddir="N (северный)" ;;
[1] ) winddir="NE (северно-восточный)" ;;
[2] ) winddir="E (восточный)" ;;
[3] ) winddir="SE (юго-восточный)" ;;
[4] ) winddir="S (южный)" ;;
[5] ) winddir="SW (юго-западный)" ;;
[6] ) winddir="W (западный)" ;;
[7] ) winddir="NW (северо-западный)" ;;
esac
echo "Wind direction: $winddir (id: ${winddir_id[0]})"

# Relwet:
relwet_max=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/RELWET \
-v "concat(@max,' ')" gismeteo.xml` )
echo "Max. Relwet: ${relwet_max[0]}"

relwet_min=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/RELWET \
-v "concat(@min,' ')" gismeteo.xml` )
echo "Min. Relwet: ${relwet_min[0]}"

relwet_avg=$((($relwet_max+$relwet_min)/2))
echo "Avg. Relwet: ${relwet_avg}"

# Heat:
heat_max=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/HEAT \
-v "concat(@max,' ')" gismeteo.xml` )
echo "Max. Heat: ${heat_max[0]}"

heat_min=( `xml sel -T -t -m /MMWEATHER/REPORT/TOWN/FORECAST/HEAT \
-v "concat(@min,' ')" gismeteo.xml` )
echo "Min. Heat: ${heat_min[0]}"

heat_avg=$((($heat_max+$heat_min)/2))
echo "Avg. Heat: ${heat_avg}"

Аутпут такой:

Prediction interation 1:
Date: 20.3.2009
Hour: 21:00 MSK
Lead-time: 18 hrs.
Time of day: Evening (id: 3)
Cloudiness: Fair (ясно) (id: 0)
Precipitation: Clear (без осадков) (id: 10)
Probability of precipitations: Possible... (id: 0)
Probability of storm: Possible... (id: 0)
Max. Pressure: 756
Min. Pressure: 754
Avg. Pressure: 755
Max. Temperature: -3
Min. Temperature: -5
Avg. Temperature: -4
Max. Wind: 5
Min. Wind: 2
Avg. Wind: 3
Wind direction: E (восточный) (id: 2)
Max. Relwet: 97
Min. Relwet: 92
Avg. Relwet: 94
Max. Heat: -6
Min. Heat: -8
Avg. Heat: -7
Previous post Next post
Up