Python3 Script For Getting USHCN Monthly Temperatures

Python3 Script For Getting USHCN Monthly Temperatures

Getting the data:

wget https://ift.tt/2ghXWpe
wget https://ift.tt/31RdtyJ
wget https://ift.tt/1t31WYQ
tar xzvf ushcn.tavg.latest.FLs.52j.tar.gz
tar xzvf ushcn.tavg.latest.tob.tar.gz
tar xzvf ushcn.tavg.latest.raw.tar.gz

Then cd into the new directory, which will have a new name every day. It will be something like ushcn.v2.5.5.20191004

Example usage:
python3 date.py USH00412679 7 1921 “EAGLE PASS”

output:
Raw EAGLE PASS USH00412679 7 1921 37.29 99.12
TOBS EAGLE PASS USH00412679 7 1921 37.16 98.89
Final EAGLE PASS USH00412679 7 1921 35.92 96.66

Last two numbers in each line are Degrees C and Degrees F

You will have to know the station ID of the station you are looking for:
https://ift.tt/1vxXYWo

Script :
——————————————————————-

import sys
 
 filename = sys.argv[1]
 #print(filename)
 #filename = filename.replace("USC", "USH")
 #print(filename)
 month = int(sys.argv[2])
 year = sys.argv[3]
 comment = sys.argv[4]
 
 fd = open(filename + ".raw.tmax")
 
 for line in fd :
 #print(line[12:16])
 if (line[12:16] == year) :
 #print(line)
 offset = 18 + (9 * (month - 1))
 #print(line[offset:offset+4])
 temperature_c = float(line[offset:offset+4]) / 100.0
 temperature_f = (temperature_c * 1.8) + 32.0
 print("Raw", comment, filename, month, year, "%.2f" % temperature_c, "%.2f" % temperature_f)
 
 fd = open(filename + ".tob.tmax")
 
 for line in fd :
 #print(line[12:16])
 if (line[12:16] == year) :
 #print(line)
 offset = 18 + (9 * (month - 1))
 #print(line[offset:offset+4])
 temperature_c = float(line[offset:offset+4]) / 100.0
 temperature_f = (temperature_c * 1.8) + 32.0
 print("TOBS", comment, filename, month, year, "%.2f" % temperature_c, "%.2f" % temperature_f)
 
 fd = open(filename + ".FLs.52j.tmax")
 
 for line in fd :
 #print(line[12:16])
 if (line[12:16] == year) :
 #print(line)
 offset = 18 + (9 * (month - 1))
 #print(line[offset:offset+4])
 temperature_c = float(line[offset:offset+4]) / 100.0
 temperature_f = (temperature_c * 1.8) + 32.0
 print("Final", comment, filename, month, year, "%.2f" % temperature_c, "%.2f" % temperature_f)
 
 

This entry was posted in

Uncategorized

. Bookmark the

permalink

.

via Real Climate Science

https://ift.tt/2MbZGw4

October 6, 2019 at 01:28PM

One thought on “Python3 Script For Getting USHCN Monthly Temperatures”

Leave a reply to uwe.roland.gross Cancel reply