diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ed49d26..38b8c1f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.12.11 + rev: v0.15.1 hooks: # Run the linter. - id: ruff-check @@ -14,9 +14,10 @@ repos: # Run the formatter. - id: ruff-format - - repo: local + - repo: https://github.com/allganize/ty-pre-commit + rev: v0.0.17 hooks: - - id: type-check + - id: ty-check name: Type check entry: ty check . language: system diff --git a/requirements.txt b/requirements.txt index ab3a3ac..3a2112c 100755 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,5 @@ requests>=2.32.5 scipy>=1.17.0 tqdm>=4.67.3 wget>=3.2 +ty +ruff diff --git a/swvo/io/RBMDataSet/scripts/create_RBSP_line_data.py b/swvo/io/RBMDataSet/scripts/create_RBSP_line_data.py index c8b2081..896f2e7 100644 --- a/swvo/io/RBMDataSet/scripts/create_RBSP_line_data.py +++ b/swvo/io/RBMDataSet/scripts/create_RBSP_line_data.py @@ -201,8 +201,8 @@ def create_RBSP_line_data( rbm_data_set_result.line_data_flux[:, e] = np.squeeze( rbm_data[min_offset_instrument].interp_flux( target_en_single, - target_al[e], - TargetType.TargetPairs, # ty:ignore[not-subscriptable] + target_al[e], # ty:ignore[not-subscriptable] + TargetType.TargetPairs, ) ) diff --git a/swvo/io/dst/wdc.py b/swvo/io/dst/wdc.py index d2ae805..8d6593a 100644 --- a/swvo/io/dst/wdc.py +++ b/swvo/io/dst/wdc.py @@ -57,7 +57,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"WDC Dst data directory: {self.data_dir}") @@ -199,7 +199,7 @@ def _process_single_file(self, file_path: Path, year, month) -> pd.DataFrame: if len(val) > 4: val = val[:4] if not val.startswith("9999") else None try: - dst = float(val) + dst = float(val) # ty:ignore[invalid-argument-type] except: # noqa: E722 continue dt = datetime(year, month, day, hour) diff --git a/swvo/io/f10_7/omni.py b/swvo/io/f10_7/omni.py index c904223..a2e674b 100644 --- a/swvo/io/f10_7/omni.py +++ b/swvo/io/f10_7/omni.py @@ -67,7 +67,7 @@ def read(self, start_time: datetime, end_time: datetime, download: bool = False) f107_df["file_name"] = data_out["file_name"] # we return it just every 24 hours - f107_df = f107_df.drop(f107_df[data_out.index.hour % 24 != 0].index, axis=0) # ty: ignore[possibly-missing-attribute] + f107_df = f107_df.drop(f107_df[data_out.index.hour % 24 != 0].index, axis=0) # ty: ignore[unresolved-attribute] f107_df = f107_df.replace(999.9, np.nan) f107_df = f107_df.truncate( before=start_time - timedelta(hours=23.9999), diff --git a/swvo/io/f10_7/swpc.py b/swvo/io/f10_7/swpc.py index 4390be9..5572386 100644 --- a/swvo/io/f10_7/swpc.py +++ b/swvo/io/f10_7/swpc.py @@ -59,7 +59,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: raise ValueError(msg) data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"SWPC F10.7 data directory: {self.data_dir}") @@ -130,7 +130,7 @@ def download_and_process(self) -> None: logger.debug(f"Updating {file_path}...") existing_data = pd.read_csv(file_path, parse_dates=["date"]) - existing_data["date"] = pd.to_datetime(existing_data["date"]).dt.tz_localize(None) # ty: ignore[unresolved-attribute] + existing_data["date"] = pd.to_datetime(existing_data["date"]).dt.tz_localize(None) combined_data = pd.concat([existing_data, year_data]) combined_data = combined_data.drop_duplicates(subset=["date"], keep="last") combined_data = combined_data.sort_values("date") @@ -199,7 +199,7 @@ def _process_single_file(self, file_path: Path) -> pd.DataFrame: skiprows=13, usecols=[0, 1, 2, 3], names=["year", "month", "day", "f107"], - ) + ) # ty:ignore[no-matching-overload] data["date"] = pd.to_datetime(data[["year", "month", "day"]].assign(hour=0)) data = data[["date", "f107"]] diff --git a/swvo/io/hp/ensemble.py b/swvo/io/hp/ensemble.py index d201d4b..b504d89 100755 --- a/swvo/io/hp/ensemble.py +++ b/swvo/io/hp/ensemble.py @@ -59,7 +59,7 @@ def __init__(self, index: str, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] logger.info(f"{self.index.upper()} Ensemble data directory: {self.data_dir}") @@ -178,7 +178,7 @@ def read_with_horizon(self, start_time: datetime, end_time: datetime, horizon: N if end_time is not None: end_time = enforce_utc_timezone(end_time) - if not (0 <= horizon <= 72): # ty: ignore[unsupported-operator] + if not (0 <= horizon <= 72): raise ValueError("Horizon must be between 0 and 72 hours") if self.index == "hp30": @@ -187,7 +187,7 @@ def read_with_horizon(self, start_time: datetime, end_time: datetime, horizon: N raise ValueError("Horizon for hp30 must be in 0.5 hour increments") elif self.index == "hp60": freq = "1h" - if horizon % 1 != 0: # ty: ignore[unsupported-operator] + if horizon % 1 != 0: raise ValueError("Horizon for hp60 must be in 1 hour increments") align_start_to_hp_hr = start_time.replace(hour=start_time.hour, minute=0, second=0, microsecond=0) diff --git a/swvo/io/hp/gfz.py b/swvo/io/hp/gfz.py index 024f663..92cab79 100755 --- a/swvo/io/hp/gfz.py +++ b/swvo/io/hp/gfz.py @@ -61,7 +61,7 @@ def __init__(self, index: str, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) self.index_number: int = index[2:] @@ -161,7 +161,7 @@ def _download(self, temporary_dir: Path, start_time: datetime, end_time: datetim with open(output_file, "w") as f: json.dump(data, f) - except requests.exceptions.RequestException as e: # ty: ignore[unresolved-attribute] + except requests.exceptions.RequestException as e: logger.error(f"API request failed: {e}") raise diff --git a/swvo/io/kp/ensemble.py b/swvo/io/kp/ensemble.py index 94a329f..f4202cc 100755 --- a/swvo/io/kp/ensemble.py +++ b/swvo/io/kp/ensemble.py @@ -53,7 +53,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] logger.info(f"Kp Ensemble data directory: {self.data_dir}") diff --git a/swvo/io/kp/niemegk.py b/swvo/io/kp/niemegk.py index 365c722..753088b 100755 --- a/swvo/io/kp/niemegk.py +++ b/swvo/io/kp/niemegk.py @@ -59,7 +59,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"Kp Niemegk data directory: {self.data_dir}") diff --git a/swvo/io/kp/omni.py b/swvo/io/kp/omni.py index 0eed9df..4b2a07c 100755 --- a/swvo/io/kp/omni.py +++ b/swvo/io/kp/omni.py @@ -62,7 +62,7 @@ def read(self, start_time: datetime, end_time: datetime, download: bool = False) kp_df["kp"] = data_out["kp"] kp_df["file_name"] = data_out["file_name"] # we return it just every 3 hours - kp_df = kp_df.drop(kp_df[data_out.index.hour % 3 != 0].index, axis=0) # ty: ignore[possibly-missing-attribute] + kp_df = kp_df.drop(kp_df[data_out.index.hour % 3 != 0].index, axis=0) # ty: ignore[unresolved-attribute] kp_df = kp_df.truncate( before=start_time - timedelta(hours=2.9999), after=end_time + timedelta(hours=2.9999), diff --git a/swvo/io/kp/swpc.py b/swvo/io/kp/swpc.py index b1b2ac7..506b596 100755 --- a/swvo/io/kp/swpc.py +++ b/swvo/io/kp/swpc.py @@ -61,7 +61,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"Kp SWPC data directory: {self.data_dir}") @@ -253,7 +253,7 @@ def _process_single_file(self, temporary_dir: Path) -> pd.DataFrame: lines = f.readlines() for line in lines: if ":Issued:" in line: - year = int(re.search(r"(\d{4})", line).group(1)) # ty: ignore[possibly-missing-attribute] + year = int(re.search(r"(\d{4})", line).group(1)) # ty: ignore[unresolved-attribute] break for i, line in enumerate(lines): @@ -286,7 +286,7 @@ def _process_single_file(self, temporary_dir: Path) -> pd.DataFrame: kp.append(kp_data[j][i]) time_in = [timestamp[0]] * 24 - df = pd.DataFrame({"t_forecast": timestamp}, index=time_in) + df = pd.DataFrame({"t_forecast": timestamp}, index=time_in) # ty:ignore[invalid-argument-type] df["kp"] = kp df.loc[round(df["kp"] % 1, 2) == 0.67, "kp"] = round(df.loc[round(df["kp"] % 1, 2) == 0.67, "kp"]) + 2 / 3 diff --git a/swvo/io/omni/omni_high_res.py b/swvo/io/omni/omni_high_res.py index cd05020..8757d2f 100644 --- a/swvo/io/omni/omni_high_res.py +++ b/swvo/io/omni/omni_high_res.py @@ -55,7 +55,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"OMNI high resolution data directory: {self.data_dir}") @@ -296,7 +296,7 @@ def _process_single_month(self, data: list[str]) -> pd.DataFrame: logger.error(msg) raise ValueError(msg) - df = pd.DataFrame([line.split() for line in data_lines], columns=columns) + df = pd.DataFrame([line.split() for line in data_lines], columns=columns) # ty:ignore[invalid-argument-type] df = df.apply(pd.to_numeric) df["timestamp"] = df["YYYY"].map(str).apply(lambda x: x + "-01-01 ") + df["HR"].map(str).apply( diff --git a/swvo/io/omni/omni_low_res.py b/swvo/io/omni/omni_low_res.py index 6fbbc67..8afc0f7 100755 --- a/swvo/io/omni/omni_low_res.py +++ b/swvo/io/omni/omni_low_res.py @@ -114,7 +114,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"OMNI Low Res data directory: {self.data_dir}") diff --git a/swvo/io/solar_wind/ace.py b/swvo/io/solar_wind/ace.py index cd4f725..f703767 100644 --- a/swvo/io/solar_wind/ace.py +++ b/swvo/io/solar_wind/ace.py @@ -64,7 +64,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"ACE data directory: {self.data_dir}") @@ -107,7 +107,7 @@ def download_and_process(self, request_time: datetime) -> None: logger.debug("Processing file ...") processed_df = self._process_single_file(temporary_dir) - unique_dates = np.unique(processed_df.index.date) # ty: ignore[possibly-missing-attribute] + unique_dates = np.unique(processed_df.index.date) # ty: ignore[unresolved-attribute] for date in unique_dates: file_path = self.data_dir / date.strftime("%Y/%m") / f"ACE_SW_NOWCAST_{date.strftime('%Y%m%d')}.csv" diff --git a/swvo/io/solar_wind/dscovr.py b/swvo/io/solar_wind/dscovr.py index c81d14f..ef6477c 100644 --- a/swvo/io/solar_wind/dscovr.py +++ b/swvo/io/solar_wind/dscovr.py @@ -62,7 +62,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] self.data_dir.mkdir(parents=True, exist_ok=True) logger.info(f"DSCOVR data directory: {self.data_dir}") @@ -103,7 +103,7 @@ def download_and_process(self, request_time: datetime) -> None: logger.debug("Processing file ...") processed_df = self._process_single_file(temporary_dir) - unique_dates = np.unique(processed_df.index.date) # ty: ignore[possibly-missing-attribute] + unique_dates = np.unique(processed_df.index.date) # ty: ignore[unresolved-attribute] for date in unique_dates: file_path = self.data_dir / date.strftime("%Y/%m") / f"DSCOVR_SW_NOWCAST_{date.strftime('%Y%m%d')}.csv" diff --git a/swvo/io/solar_wind/swift.py b/swvo/io/solar_wind/swift.py index 027224b..3f9e11f 100644 --- a/swvo/io/solar_wind/swift.py +++ b/swvo/io/solar_wind/swift.py @@ -58,7 +58,7 @@ def __init__(self, data_dir: Optional[Path] = None) -> None: data_dir = os.environ.get(self.ENV_VAR_NAME) # ty: ignore[invalid-assignment] - self.data_dir: Path = Path(data_dir) + self.data_dir: Path = Path(data_dir) # ty:ignore[invalid-argument-type] logger.info(f"SWIFT ensemble data directory: {self.data_dir}") @@ -212,7 +212,7 @@ def _read_single_file(self, file_name, use_old_column_names=False) -> pd.DataFra "uz": uz, "pdyn": pdyn, }, - index=time, + index=time, # ty:ignore[invalid-argument-type] ) else: df = pd.DataFrame( @@ -226,7 +226,7 @@ def _read_single_file(self, file_name, use_old_column_names=False) -> pd.DataFra "bz_gsm": bz, "pdyn": pdyn, }, - index=time, + index=time, # ty:ignore[invalid-argument-type] ) df["file_name"] = file_name diff --git a/swvo/io/utils.py b/swvo/io/utils.py index 47ba4d6..2e03931 100644 --- a/swvo/io/utils.py +++ b/swvo/io/utils.py @@ -214,7 +214,7 @@ def sw_mag_propagation(sw_data: pd.DataFrame) -> pd.DataFrame: Data frame with propagated solar wind data, indexed by time. """ - sw_data["t"] = [t.timestamp() for t in sw_data.index.to_pydatetime()] # ty: ignore[possibly-missing-attribute] + sw_data["t"] = [t.timestamp() for t in sw_data.index.to_pydatetime()] # ty: ignore[unresolved-attribute] sw_data = sw_data.dropna(how="any") distance = 1.5e6