#Java Script Object Notation is the format that the Kentucky Mesonet Data is stored in. import json import os from datetime import datetime, timedelta #There are a lot of ways to format and work with time in python, all Mesonet data is stored in the timestamp format "YYYY-MM-DD HH:MM:SS" and will be in UTC/GMT timezone #This variable is handy for exporting timestamps in a consistent way tsdef = '%Y-%m-%d %H:%M:%S' #This gets the absolute dir the script is currently in scriptDir = os.path.dirname(__file__) print(scriptDir) #This is a file that contains tons of meta information about our station locations metafile = open('StationsInfo.json') #This loads the information in the son into a dict metadata = json.load(metafile) #After loading the data we can close the file metafile.close() #for station in metadata: # print(metadata[station]["county"]) #human readable timestamps nice simple copy-pasteable beginTimestamp = "2020-04-01 00:00:00" endTimestamp = "2020-04-14 00:00:00" #timestamps for the computer to work with beginTime = datetime.strptime(beginTimestamp, tsdef) endTime = datetime.strptime(endTimestamp, tsdef) #Find out how many days we will be itterating over dayCount = (endTime - beginTime).days + 1 #Station Abbrevations can be found in the metadata file stationAbbrev = "FARM" #Variiable Abbreviations can be found in the defenition document provided variableAbbrev = "TAIR" #A simple for loop starting at beginTime and then adding 1 day per loop until we have reached the end defined by endTime for dataTime in (beginTime + timedelta(n) for n in range(dayCount)): #Start with existing script location from before and add the variable name filePath=scriptDir +"\\"+variableAbbrev #Add the year filePath = filePath + "\\" + dataTime.strftime("%Y") #Add the month filePath = filePath + "\\" + dataTime.strftime("%m") #Add the day filePath = filePath + "\\" + dataTime.strftime("%d") #Add the data period + station ID + file extension filePath = filePath + "\\" + "05_"+ stationAbbrev +".json" #Open the json file at the location we have specified datafile = open(filePath) #Load the JSON into an object creating a dict currentJson = json.load(datafile) #Close the file as we wont need it anymore datafile.close() #Find the sum of the elements in the file dailySum = sum(currentJson["val"]) #Find the count of the elements in the file (for a complete day of data at the Mesonet it will be 288 (24*60 / 5)) dailyCount = len(currentJson["val"]) #Find the average and do some rounding to make the display nice dailyAverage = round(dailySum/dailyCount,2) #Finally we print out the information print("The average of "+ variableAbbrev +" for "+ stationAbbrev +" on " + dataTime.strftime("%Y-%m-%d") + " was " + str(dailyAverage) +", " + str(dailyCount) + " obs were made.")