-
Notifications
You must be signed in to change notification settings - Fork 2
Update SITCOM-2111 with manual TZ offset #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
b1quint
wants to merge
2
commits into
develop
Choose a base branch
from
tickets/SITCOM-2111
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,8 +22,8 @@ | |||||||||||||||||||||
| "metadata": {}, | ||||||||||||||||||||||
| "outputs": [], | ||||||||||||||||||||||
| "source": [ | ||||||||||||||||||||||
| "day_obs = 20250601\n", | ||||||||||||||||||||||
| "number_of_days = 14" | ||||||||||||||||||||||
| "day_obs_start = 20250501\n", | ||||||||||||||||||||||
| "day_obs_end = 20250515" | ||||||||||||||||||||||
| ] | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
|
|
@@ -65,6 +65,12 @@ | |||||||||||||||||||||
| "m1m3_inside_cell_sal_index = 113\n", | ||||||||||||||||||||||
| "dome_inside_sal_index = 111\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "# Number of columns to consider - Maximum 336\n", | ||||||||||||||||||||||
| "N_COLS = 45\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "# CLT TimeZone Manual Offset\n", | ||||||||||||||||||||||
| "clt_tz_offset = -4 # hours\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "# Set global font size for labels, titles, and ticks\n", | ||||||||||||||||||||||
| "plt.rcParams.update(\n", | ||||||||||||||||||||||
| " {\n", | ||||||||||||||||||||||
|
|
@@ -101,67 +107,6 @@ | |||||||||||||||||||||
| "metadata": {}, | ||||||||||||||||||||||
| "outputs": [], | ||||||||||||||||||||||
| "source": [ | ||||||||||||||||||||||
| "def get_latest_forecast(_df, start_time=None, end_time=None):\n", | ||||||||||||||||||||||
| " \"\"\"\n", | ||||||||||||||||||||||
| " From a reformatted forecast DataFrame, selects forecast rows\n", | ||||||||||||||||||||||
| " where forecast_timestamp falls between two consecutive published timestamps.\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " Parameters\n", | ||||||||||||||||||||||
| " ----------\n", | ||||||||||||||||||||||
| " _df : pd.DataFrame\n", | ||||||||||||||||||||||
| " DataFrame with columns: published_timestamp, forecast_timestamp, temperature, temperatureSpread.\n", | ||||||||||||||||||||||
| " start_time : datetime-like, optional\n", | ||||||||||||||||||||||
| " Minimum forecast_timestamp to include.\n", | ||||||||||||||||||||||
| " end_time : datetime-like, optional\n", | ||||||||||||||||||||||
| " Maximum forecast_timestamp to include.\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " Returns\n", | ||||||||||||||||||||||
| " -------\n", | ||||||||||||||||||||||
| " pd.DataFrame\n", | ||||||||||||||||||||||
| " DataFrame indexed by forecast_timestamp, containing only selected forecasts.\n", | ||||||||||||||||||||||
| " \"\"\"\n", | ||||||||||||||||||||||
| " if isinstance(start_time, Time):\n", | ||||||||||||||||||||||
| " start_time = start_time.to_datetime().replace(tzinfo=None)\n", | ||||||||||||||||||||||
| " if isinstance(end_time, Time):\n", | ||||||||||||||||||||||
| " end_time = end_time.to_datetime().replace(tzinfo=None)\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " _df = _df.copy()\n", | ||||||||||||||||||||||
| " _df[\"forecast_timestamp\"] = pd.to_datetime(_df[\"forecast_timestamp\"])\n", | ||||||||||||||||||||||
| " _df[\"published_timestamp\"] = pd.to_datetime(_df[\"published_timestamp\"])\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " _df = _df.sort_values(\"published_timestamp\")\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Container for filtered rows\n", | ||||||||||||||||||||||
| " filtered = []\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Group forecasts between each pair of published timestamps\n", | ||||||||||||||||||||||
| " for i in range(len(_df.published_timestamp.unique()) - 1):\n", | ||||||||||||||||||||||
| " t0 = _df.published_timestamp.unique()[i]\n", | ||||||||||||||||||||||
| " t1 = _df.published_timestamp.unique()[i + 1]\n", | ||||||||||||||||||||||
| " mask = (\n", | ||||||||||||||||||||||
| " (_df[\"forecast_timestamp\"] >= t0)\n", | ||||||||||||||||||||||
| " & (_df[\"forecast_timestamp\"] < t1)\n", | ||||||||||||||||||||||
| " & (_df[\"published_timestamp\"] == t0)\n", | ||||||||||||||||||||||
| " )\n", | ||||||||||||||||||||||
| " filtered.append(_df[mask])\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " result_df = pd.concat(filtered, ignore_index=True)\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Filter by time range if specified\n", | ||||||||||||||||||||||
| " if start_time is not None:\n", | ||||||||||||||||||||||
| " result_df = result_df[\n", | ||||||||||||||||||||||
| " result_df[\"forecast_timestamp\"] >= pd.to_datetime(start_time)\n", | ||||||||||||||||||||||
| " ]\n", | ||||||||||||||||||||||
| " if end_time is not None:\n", | ||||||||||||||||||||||
| " result_df = result_df[\n", | ||||||||||||||||||||||
| " result_df[\"forecast_timestamp\"] <= pd.to_datetime(end_time)\n", | ||||||||||||||||||||||
| " ]\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Set index and return\n", | ||||||||||||||||||||||
| " result_df = result_df.set_index(\"forecast_timestamp\")\n", | ||||||||||||||||||||||
| " return result_df.sort_index()\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "def query_ess_inside_dome(client, start_time, end_time):\n", | ||||||||||||||||||||||
| " \"\"\"\n", | ||||||||||||||||||||||
| " Query the actual temperature inside the dome.\n", | ||||||||||||||||||||||
|
|
@@ -230,25 +175,22 @@ | |||||||||||||||||||||
| " if day_obs_end is None:\n", | ||||||||||||||||||||||
| " day_obs_end = day_obs_start\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " start_time = getDayObsStartTime(day_obs_start)\n", | ||||||||||||||||||||||
| " end_time = getDayObsEndTime(day_obs_end)\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " NPOINTS = 336\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # The timedelta below helps including the first and last data point\n", | ||||||||||||||||||||||
| " start_time = getDayObsStartTime(day_obs_start) - pd.to_timedelta(\"30min\")\n", | ||||||||||||||||||||||
| " end_time = getDayObsEndTime(day_obs_end) + pd.to_timedelta(\"30min\")\n", | ||||||||||||||||||||||
| " \n", | ||||||||||||||||||||||
| " _df = getEfdData(\n", | ||||||||||||||||||||||
| " client=client,\n", | ||||||||||||||||||||||
| " topic=\"lsst.sal.WeatherForecast.hourlyTrend\",\n", | ||||||||||||||||||||||
| " columns=[f\"timestamp{i}\" for i in range(NPOINTS)]\n", | ||||||||||||||||||||||
| " + [f\"temperature{i}\" for i in range(NPOINTS)]\n", | ||||||||||||||||||||||
| " + [f\"temperatureSpread{i}\" for i in range(NPOINTS)],\n", | ||||||||||||||||||||||
| " columns=[f\"timestamp{i}\" for i in range(N_COLS)]\n", | ||||||||||||||||||||||
| " + [f\"temperature{i}\" for i in range(N_COLS)]\n", | ||||||||||||||||||||||
| " + [f\"temperatureSpread{i}\" for i in range(N_COLS)],\n", | ||||||||||||||||||||||
| " begin=start_time,\n", | ||||||||||||||||||||||
| " end=end_time,\n", | ||||||||||||||||||||||
| " )\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " for i in range(NPOINTS):\n", | ||||||||||||||||||||||
| " _df[f\"timestamp{i}\"] = pd.to_datetime(\n", | ||||||||||||||||||||||
| " _df[f\"timestamp{i}\"], unit=\"s\", utc=True\n", | ||||||||||||||||||||||
| " ).dt.strftime(\"%Y-%m-%dT%H:%M:%S.%fZ\")\n", | ||||||||||||||||||||||
| " for i in range(N_COLS):\n", | ||||||||||||||||||||||
| " _df[f\"timestamp{i}\"] = pd.to_datetime(_df[f\"timestamp{i}\"], unit=\"s\", utc=True)\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " return _df\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
|
|
@@ -269,53 +211,64 @@ | |||||||||||||||||||||
| " _df[\"index\"] = _df[\"key\"].str.extract(r\"(\\d+)$\")[0].astype(\"Int64\")\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Pivot to final table\n", | ||||||||||||||||||||||
| " _df = _df.pivot(index=\"index\", columns=\"field\", values=\"value\").reset_index(\n", | ||||||||||||||||||||||
| " drop=True\n", | ||||||||||||||||||||||
| " )\n", | ||||||||||||||||||||||
| " _df = _df.pivot(index=\"index\", columns=\"field\", values=\"value\").reset_index(drop=True)\n", | ||||||||||||||||||||||
| " _df[\"timestamp\"] = pd.to_datetime(_df[\"timestamp\"])\n", | ||||||||||||||||||||||
| " _df = _df.where(pd.notnull(_df), pd.NA)\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " mask = _df.timestamp < (_df.timestamp[0] + pd.Timedelta(hours=48))\n", | ||||||||||||||||||||||
| " _df = _df[mask]\n", | ||||||||||||||||||||||
| " _df.columns.name = None\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " return _df\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| "def unpack_hourly_weather_forecast_dataframe(df):\n", | ||||||||||||||||||||||
| "def unpack_hourly_weather_forecast_matching_timestamps(_df):\n", | ||||||||||||||||||||||
| " \"\"\"\n", | ||||||||||||||||||||||
| " Reformats a temperature forecast DataFrame into long format.\n", | ||||||||||||||||||||||
| " Use the published data to select the timestamps and temperatures \n", | ||||||||||||||||||||||
| " we want to include in the output dataframe.\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " Parameters\n", | ||||||||||||||||||||||
| " ----------\n", | ||||||||||||||||||||||
| " df : pd.DataFrame\n", | ||||||||||||||||||||||
| " DataFrame with index as publication time and columns temperatureX and temperatureSpreadX.\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " Returns\n", | ||||||||||||||||||||||
| " -------\n", | ||||||||||||||||||||||
| " pd.DataFrame\n", | ||||||||||||||||||||||
| " Reformatted DataFrame with columns:\n", | ||||||||||||||||||||||
| " - published_timestamp\n", | ||||||||||||||||||||||
| " - forecast_timestamp\n", | ||||||||||||||||||||||
| " - temperature\n", | ||||||||||||||||||||||
| " - temperatureSpread\n", | ||||||||||||||||||||||
| " _df : pd.DataFrame\n", | ||||||||||||||||||||||
| " The weather forecast data. \n", | ||||||||||||||||||||||
| " The index must contain the timestamp of the published data. \n", | ||||||||||||||||||||||
| " It must have at least 40 columns for the timestamp,\n", | ||||||||||||||||||||||
| " temperature, and temperatureSpread.\n", | ||||||||||||||||||||||
| " \"\"\"\n", | ||||||||||||||||||||||
| " records = []\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " for published_time, row in df.iterrows():\n", | ||||||||||||||||||||||
| " for i in range(336):\n", | ||||||||||||||||||||||
| " temp = row.get(f\"temperature{i}\", None)\n", | ||||||||||||||||||||||
| " spread = row.get(f\"temperatureSpread{i}\", None)\n", | ||||||||||||||||||||||
| " forecast_time = published_time + pd.Timedelta(hours=i)\n", | ||||||||||||||||||||||
| " records.append(\n", | ||||||||||||||||||||||
| " {\n", | ||||||||||||||||||||||
| " \"published_timestamp\": published_time,\n", | ||||||||||||||||||||||
| " \"forecast_timestamp\": forecast_time,\n", | ||||||||||||||||||||||
| " \"temperature\": temp,\n", | ||||||||||||||||||||||
| " \"temperatureSpread\": spread,\n", | ||||||||||||||||||||||
| " }\n", | ||||||||||||||||||||||
| " )\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " return pd.DataFrame(records)" | ||||||||||||||||||||||
| " # Round published time to make it easier to select good data\n", | ||||||||||||||||||||||
| " _df.index = _df.index.round(\"h\")\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Exclude rows published at times different from expected.\n", | ||||||||||||||||||||||
| " _df = _df[_df.index.hour.isin([4, 16])]\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Columns that starts with `timestamp`\n", | ||||||||||||||||||||||
| " cols = [col for col in _df.columns if col.startswith(\"timestamp\")]\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Create empty dataframe\n", | ||||||||||||||||||||||
| " new_df = None\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Extract time range associated with the time between published times\n", | ||||||||||||||||||||||
| " for published_time, row in _df[cols].iterrows():\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Get the beginning of the data corresponding to this time range\n", | ||||||||||||||||||||||
| " index_start = (row - published_time).abs().argmin()\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " # Number of hours between published forecasts is always the same\n", | ||||||||||||||||||||||
| " index_end = index_start + 12\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " new_cols = (\n", | ||||||||||||||||||||||
| " [f\"timestamp{i}\" for i in range(index_start, index_end)] + \n", | ||||||||||||||||||||||
| " [f\"temperature{i}\" for i in range(index_start, index_end)] + \n", | ||||||||||||||||||||||
| " [f\"temperatureSpread{i}\" for i in range(index_start, index_end)]\n", | ||||||||||||||||||||||
| " )\n", | ||||||||||||||||||||||
| " \n", | ||||||||||||||||||||||
| " sub_df = _df[new_cols]\n", | ||||||||||||||||||||||
| " sub_df = unpack_hourly_weather_forecast(sub_df.loc[published_time])\n", | ||||||||||||||||||||||
| " sub_df[\"published_time\"] = published_time\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " if new_df is not None:\n", | ||||||||||||||||||||||
| " new_df = pd.concat([new_df, sub_df])\n", | ||||||||||||||||||||||
| " else:\n", | ||||||||||||||||||||||
| " new_df = sub_df\n", | ||||||||||||||||||||||
| "\n", | ||||||||||||||||||||||
| " return new_df" | ||||||||||||||||||||||
|
Comment on lines
+266
to
+271
|
||||||||||||||||||||||
| " if new_df is not None:\n", | |
| " new_df = pd.concat([new_df, sub_df])\n", | |
| " else:\n", | |
| " new_df = sub_df\n", | |
| "\n", | |
| " return new_df" | |
| " sub_dfs.append(sub_df)\n", | |
| "\n", | |
| " new_df = pd.concat(sub_dfs, ignore_index=True)\n", | |
| " return new_df\n", |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Computing
index_startthis way returns a column label string instead of an integer offset, which will break the subsequentindex_endcalculation; consider extracting the numeric suffix or using positional indexing.