Skip to content

Commit

Permalink
add get_unix_timestamp_for_day to date_to_unix.py, and import to note…
Browse files Browse the repository at this point in the history
…book
  • Loading branch information
toravest committed May 24, 2025
1 parent 22394b6 commit b6b24bc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 103 deletions.
43 changes: 9 additions & 34 deletions notebooks/notebook_compare_one_day_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,14 @@
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import time\n",
"\n",
"# Makes a function so the start and end date is the same date, with all hours of that date\n",
"def get_unix_timestamps_for_day():\n",
" date_input = input(\"Choose a date (yyyy, mm, dd): \")\n",
" date_components = date_input.split(\",\")\n",
" year = int(date_components[0])\n",
" month = int(date_components[1])\n",
" day = int(date_components[2])\n",
"\n",
" # Goes through all hours of the day, use %Y-%m-%d etc. from pythons strftime to convert datetime into a readable string \n",
" timestamps = []\n",
" for hour in range(24):\n",
" dt = datetime.datetime(year, month, day, hour, 0)\n",
" unix_timestamp = int(time.mktime(dt.timetuple()))\n",
" timestamps.append((unix_timestamp, dt.strftime('%Y-%m-%d %H:%M:%S'))) \n",
" \n",
" # Prevents from getting data for the current day, or the future\n",
" if dt >= datetime.datetime.now():\n",
" print(\"Failed, cant use future dates\")\n",
"\n",
" # If \n",
" raise ValueError\n",
"\n",
" # Prints the date chosen\n",
" print(f\"Selected date: {year}-{month:02d}-{day:02d}\")\n",
"\n",
" # Prints the timestamp and the date an hour of the day after\n",
" for ts, readable in timestamps:\n",
" print(f\"Unix Timestamp: {ts} -> {readable}\")\n",
" \n",
" return date_input, [ts[0] for ts in timestamps]\n",
"import sys\n",
"import os\n",
"\n",
"# Gets the absolute path to the src folder\n",
"sys.path.append(os.path.abspath(\"../src\"))\n",
"\n",
"# Import function\n",
"from my_package.date_to_unix import get_unix_timestamps_for_day\n",
"\n",
"date, timestamps = get_unix_timestamps_for_day()"
]
Expand Down Expand Up @@ -508,7 +483,7 @@
"# Add label for x-axis\n",
"ax3.set_xlabel('Datetime')\n",
"\n",
"# Save the plot to the data/output_fig folder\n",
"# Save the plot to the 'data/figures/output_fig_compare_one_day' folder\n",
"plot_path = os.path.join(output_folder, f\"weather_compare_plot_{city_1}_{city_2}.png\")\n",
"# Save the plot as a PNG file\n",
"plt.savefig(plot_path)\n",
Expand Down
50 changes: 15 additions & 35 deletions notebooks/notebook_interactive_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,13 @@
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import time\n",
"\n",
"# Makes a function so the start and end date is the same date, with all hours of that date\n",
"def get_unix_timestamps_for_day():\n",
" date_input = input(\"Choose a date (yyyy, mm, dd): \")\n",
" date_components = date_input.split(\",\")\n",
" year = int(date_components[0])\n",
" month = int(date_components[1])\n",
" day = int(date_components[2])\n",
"\n",
" # Goes through all hours of the day, use %Y-%m-%d etc. from pythons strftime to convert datetime into a readable string \n",
" timestamps = []\n",
" for hour in range(24):\n",
" dt = datetime.datetime(year, month, day, hour, 0)\n",
" unix_timestamp = int(time.mktime(dt.timetuple()))\n",
" timestamps.append((unix_timestamp, dt.strftime('%Y-%m-%d %H:%M:%S'))) \n",
" \n",
" # Prevents from getting data for the current day, or the future\n",
" if dt >= datetime.datetime.now():\n",
" print(\"Failed, cant use future dates\")\n",
"\n",
" # If \n",
" raise ValueError\n",
"import sys\n",
"import os\n",
"\n",
" # Prints the date chosen\n",
" print(f\"Selected date: {year}-{month:02d}-{day:02d}\")\n",
"# Gets the absolute path to the src folder\n",
"sys.path.append(os.path.abspath(\"../src\"))\n",
"\n",
" # Prints the timestamp and the date an hour of the day after\n",
" for ts, readable in timestamps:\n",
" print(f\"Unix Timestamp: {ts} -> {readable}\")\n",
" \n",
" return date_input, [ts[0] for ts in timestamps]\n",
"from my_package.date_to_unix import get_unix_timestamps_for_day\n",
"\n",
"date, timestamps = get_unix_timestamps_for_day()"
]
Expand Down Expand Up @@ -192,7 +166,7 @@
"import ipywidgets as widgets\n",
"from IPython.display import display, clear_output\n",
"\n",
"# Temperaturedata\n",
"# Temperature data\n",
"temp = df['main.temp']\n",
"x_axis = df.index\n",
"\n",
Expand All @@ -204,6 +178,11 @@
"dot_size = widgets.IntSlider(value=40, min=10, max=100, step=5, description='Punktstørrelse')\n",
"\n",
"def plot_temp(show_mean, show_grid, dot_color, mean_color, dot_size):\n",
" '''\n",
" This function creates an interactive plot using jupyter widgets. It makes a scatter diagram of the temperature with the temerature mean dashed line.\n",
" It gives the user oppurtunity to customize color and size of the dots. And weather to show the grid or not, the same with the mean temperature line.\n",
" '''\n",
" \n",
" # Where the figure should be saved when exported\n",
" output_folder = \"../data/figures/output_fig_interactive\"\n",
"\n",
Expand Down Expand Up @@ -237,18 +216,19 @@
" if show_grid:\n",
" plt.grid()\n",
"\n",
" # Save the plot to the data/output_fig folder\n",
" # Save the plot to the 'data/figures/output_fig_interactive' folder\n",
" plot_path = os.path.join(output_folder, f\"weather_interactive_plot_{city_name}.png\")\n",
" # Save the plot as a PNG file\n",
" plt.savefig(plot_path)\n",
"\n",
" plt.legend(loc='upper right')\n",
" plt.show()\n",
" plt.close()\n",
"\n",
"# Collects widgets in a UI (user interface)\n",
"ui = widgets.VBox([show_mean, show_grid, dot_color, mean_color, dot_size])\n",
"\n",
"# connects widgets to plot-functions\n",
"# Connects widgets to plot-functions\n",
"out = widgets.interactive_output(plot_temp, {\n",
" 'show_mean': show_mean,\n",
" 'show_grid': show_grid,\n",
Expand All @@ -257,7 +237,7 @@
" 'dot_size': dot_size,\n",
"})\n",
"\n",
"# show UI and output\n",
"# Show UI and output\n",
"display(ui, out)\n"
]
}
Expand Down
41 changes: 7 additions & 34 deletions notebooks/notebook_one_day_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,12 @@
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import time\n",
"\n",
"# Makes a function so the start and end date is the same date, with all hours of that date\n",
"def get_unix_timestamps_for_day():\n",
" date_input = input(\"Choose a date (yyyy, mm, dd): \")\n",
" date_components = date_input.split(\",\")\n",
" year = int(date_components[0])\n",
" month = int(date_components[1])\n",
" day = int(date_components[2])\n",
"\n",
" # Goes through all hours of the day, use %Y-%m-%d etc. from pythons strftime to convert datetime into a readable string \n",
" timestamps = []\n",
" for hour in range(24):\n",
" dt = datetime.datetime(year, month, day, hour, 0)\n",
" unix_timestamp = int(time.mktime(dt.timetuple()))\n",
" timestamps.append((unix_timestamp, dt.strftime('%Y-%m-%d %H:%M:%S'))) \n",
" \n",
" # Prevents from getting data for the current day, or the future\n",
" if dt >= datetime.datetime.now():\n",
" print(\"Failed, cant use future dates\")\n",
"\n",
" # If \n",
" raise ValueError\n",
"\n",
" # Prints the date chosen\n",
" print(f\"Selected date: {year}-{month:02d}-{day:02d}\")\n",
"\n",
" # Prints the timestamp and the date an hour of the day after\n",
" for ts, readable in timestamps:\n",
" print(f\"Unix Timestamp: {ts} -> {readable}\")\n",
" \n",
" return date_input, [ts[0] for ts in timestamps]\n",
"import sys\n",
"import os\n",
"\n",
"sys.path.append(os.path.abspath(\"../src\"))\n",
"\n",
"from my_package.date_to_unix import get_unix_timestamps_for_day\n",
"\n",
"date, timestamps = get_unix_timestamps_for_day()"
]
Expand Down Expand Up @@ -456,7 +429,7 @@
"# Adjust layout\n",
"plt.tight_layout()\n",
"\n",
"# Save the plot to the data/output_fig folder\n",
"# Save the plot to the 'data/figures/output_one_day' folder\n",
"plot_path = os.path.join(output_folder, f\"weather_data_plot{city_name}.png\")\n",
"plt.savefig(plot_path) # Save the plot as a PNG file\n",
"\n",
Expand Down
39 changes: 39 additions & 0 deletions src/my_package/date_to_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ def from_unix_timestamp(unix_start, unix_end):

return start_from_unix, end_from_unix

def get_unix_timestamps_for_day():
'''
This function takes an dat input from the user, in this format: 'yyyy, mm, dd'. And then splits the
input into the different elements. Before making a timestamp for each 24 hours of the day. It have an
if-statment that will throw an error if the inputed date is the current or future date, this beacuse
it can not get the data for the whole day. The function will then print all the unix timestamp and
more readable times for all the hours of the wanted day.
'''

date_input = input("Choose a date (yyyy, mm, dd): ")
date_components = date_input.split(",")
year = int(date_components[0])
month = int(date_components[1])
day = int(date_components[2])

# Goes through all hours of the day, use %Y-%m-%d etc. from pythons strftime to convert datetime into a readable string
timestamps = []
for hour in range(24):
dt = datetime.datetime(year, month, day, hour, 0)
unix_timestamp = int(time.mktime(dt.timetuple()))
timestamps.append((unix_timestamp, dt.strftime('%Y-%m-%d %H:%M:%S')))

# Prevents from getting data for the current day, or the future
if dt >= datetime.datetime.now():
print("Failed, cant use future dates")

# If
raise ValueError

# Prints the date chosen
print(f"Selected date: {year}-{month:02d}-{day:02d}")

# Prints the timestamp and the date an hour of the day after
for ts, readable in timestamps:
print(f"Unix Timestamp: {ts} -> {readable}")

return date_input, [ts[0] for ts in timestamps]

# This prints the documentation for the functions written inside '''these'''
# print(get_unix_timestamp.__doc__)
# print(from_unix_timestamp.__doc__)
# print(get_unix_timestamp_for_day.__doc__)

0 comments on commit b6b24bc

Please sign in to comment.