This is the temperature for the Yatir Forest (Shani station, see map), between 2 and 5 of January 2022. Data is in intervals of 10 minutes, and was downloaded from the Israel Meteorological Service.
import stuff
import numpy as npimport matplotlib.pyplot as pltimport pandas as pdfrom matplotlib.dates import DateFormatterimport matplotlib.dates as mdatesimport datetime as dtimport matplotlib.ticker as tickerimport warnings# Suppress FutureWarningswarnings.simplefilter(action='ignore', category=FutureWarning)import seaborn as snssns.set(style="ticks", font_scale=1.5) # white graphs, with large and legible lettersimport requestsimport jsonimport os# %matplotlib widget
API to download data from IMS
# read token from filewithopen('../archive/IMS-token.txt', 'r') asfile: TOKEN =file.readline()# 28 = SHANI stationSTATION_NUM =28start ="2022/01/01"end ="2022/01/07"filename ='shani_2022_january.json'# check if the JSON file already exists# if so, then load fileif os.path.exists(filename):withopen(filename, 'r') as json_file: data = json.load(json_file)else:# make the API request if the file doesn't exist url =f"https://api.ims.gov.il/v1/envista/stations/{STATION_NUM}/data/?from={start}&to={end}" headers = {'Authorization': f'ApiToken {TOKEN}'} response = requests.get(url, headers=headers) data = json.loads(response.text.encode('utf8'))# save the JSON data to a filewithopen(filename, 'w') as json_file: json.dump(data, json_file)# show data to see if it's alright# data
load data and pre process it
df = pd.json_normalize(data['data'],record_path=['channels'], meta=['datetime'])df['date'] = (pd.to_datetime(df['datetime']) .dt.tz_localize(None) # ignores time zone information )df = df.pivot(index='date', columns='name', values='value')# df
useful dirty trick
# dirty trick to have dates in the middle of the 24-hour period# make minor ticks in the middle, put the labels there!# from https://matplotlib.org/stable/gallery/ticks/centered_ticklabels.htmldef centered_dates(ax): date_form = DateFormatter("%d %b") # %d 3-letter-Month# major ticks at midnight, every day ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) ax.xaxis.set_major_formatter(date_form)# minor ticks at noon, every day ax.xaxis.set_minor_locator(mdates.HourLocator(byhour=[12]))# erase major tick labels ax.xaxis.set_major_formatter(ticker.NullFormatter())# set minor tick labels as define above ax.xaxis.set_minor_formatter(date_form)# completely erase minor ticks, center tick labelsfor tick in ax.xaxis.get_minor_ticks(): tick.tick1line.set_markersize(0) tick.tick2line.set_markersize(0) tick.label1.set_horizontalalignment('center')
The temperature profile now is much smoother, but when using resample, we lost temporal resolution. Our original data had 10-minute frequency, and now we have a 2-hour frequency.
How can we get a smoother curve without losing resolution?