WECGridDB

API Reference

SQLite database interface for WEC-Grid simulation data management.

Provides database operations for storing WEC simulation results, device configurations, and time series data. Supports both raw SQL queries and pandas DataFrame integration with multi-software backend support.

Database Schema Overview:

Metadata Tables: - grid_simulations: Grid simulation metadata and parameters - wec_simulations: WEC-Sim simulation parameters and wave conditions - wec_integrations: Links WEC farms to grid connection points

PSS®E Results Tables: - psse_bus_results: Bus voltages, power injections [pu on S_base] - psse_generator_results: Generator outputs [pu on S_base] - psse_load_results: Load demands [pu on S_base] - psse_line_results: Line loadings [% of thermal rating]

PyPSA Results Tables
  • pypsa_bus_results: Same schema as PSS®E for cross-platform comparison
  • pypsa_generator_results: Same schema as PSS®E
  • pypsa_load_results: Same schema as PSS®E
  • pypsa_line_results: Same schema as PSS®E
WEC Simulation Data
  • wec_simulations: Metadata including wave spectrum, class, and conditions
  • wec_power_results: High-resolution WEC device power output [Watts]
Key Design Features
  • Software-specific tables enable multi-backend comparisons
  • All grid power values in per-unit on system S_base (MVA)
  • GridState DataFrame schema alignment for direct data mapping
  • Optional storage model - persist only when explicitly requested
  • JSON configuration file for database path management
  • User-guided setup for first-time configuration
  • Support for downloaded or cloned database repositories
Database Location

Configured via database_config.json in the same directory as this module. Users can point to downloaded database file, cloned repository, or create new empty database.

Attributes:

Name Type Description
db_path str

Path to SQLite database file (from JSON configuration).

Example

db = WECGridDB(engine) # Uses path from database_config.json

First run will prompt user to configure database path

with db.connection() as conn: ... results = db.query("SELECT * FROM grid_simulations", return_type="df")

Notes

Database path is configured via JSON file on first use. Users are guided through setup process with clear instructions. All database operations are transaction-safe with automatic rollback on errors.

Source code in src/wecgrid/util/database.py
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
class WECGridDB:
    """SQLite database interface for WEC-Grid simulation data management.

    Provides database operations for storing WEC simulation results, device
    configurations, and time series data. Supports both raw SQL queries and
    pandas DataFrame integration with multi-software backend support.

    Database Schema Overview:
    ------------------------
    Metadata Tables:
        - grid_simulations: Grid simulation metadata and parameters
        - wec_simulations: WEC-Sim simulation parameters and wave conditions
        - wec_integrations: Links WEC farms to grid connection points

    PSS®E Results Tables:
        - psse_bus_results: Bus voltages, power injections [pu on S_base]
        - psse_generator_results: Generator outputs [pu on S_base]
        - psse_load_results: Load demands [pu on S_base]
        - psse_line_results: Line loadings [% of thermal rating]

    PyPSA Results Tables:
        - pypsa_bus_results: Same schema as PSS®E for cross-platform comparison
        - pypsa_generator_results: Same schema as PSS®E
        - pypsa_load_results: Same schema as PSS®E
        - pypsa_line_results: Same schema as PSS®E

    WEC Simulation Data:
        - wec_simulations: Metadata including wave spectrum, class, and conditions
        - wec_power_results: High-resolution WEC device power output [Watts]

    Key Design Features:
        - Software-specific tables enable multi-backend comparisons
        - All grid power values in per-unit on system S_base (MVA)
        - GridState DataFrame schema alignment for direct data mapping
        - Optional storage model - persist only when explicitly requested
        - JSON configuration file for database path management
        - User-guided setup for first-time configuration
        - Support for downloaded or cloned database repositories

    Database Location:
        Configured via database_config.json in the same directory as this module.
        Users can point to downloaded database file, cloned repository, or create new empty database.

    Attributes:
        db_path (str): Path to SQLite database file (from JSON configuration).

    Example:
        >>> db = WECGridDB(engine)  # Uses path from database_config.json
        >>> # First run will prompt user to configure database path
        >>> with db.connection() as conn:
        ...     results = db.query("SELECT * FROM grid_simulations", return_type="df")

    Notes:
        Database path is configured via JSON file on first use.
        Users are guided through setup process with clear instructions.
        All database operations are transaction-safe with automatic rollback on errors.
    """

    def __init__(self, engine):
        """Initialize database handler.

        Args:
            engine: WEC-GRID engine instance
        """
        self.engine = engine

        # Get database path from config
        self.db_path = get_database_config()
        if self.db_path is None:
            _show_database_setup_message()
            print(
                "Warning: Database not configured. Use engine.database.set_database_path() to configure."
            )
            return  # Allow user to continue and set path later

        # print(f"Using database: {self.db_path}")
        self.check_and_initialize()

    def check_and_initialize(self):
        """Check if database exists and has correct schema, initialize if needed.

        Validates that all required tables exist with proper structure.
        Creates database and initializes schema if missing or incomplete.

        Returns:
            bool: True if database was already valid, False if initialization was needed.
        """
        if self.db_path is None:
            print("Warning: Database path not set. Cannot initialize database.")
            return False

        if not os.path.exists(self.db_path):
            # Ensure directory exists
            os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
            self.initialize_database()
            return False

        # Check if all required tables exist
        required_tables = [
            "grid_simulations",
            "wec_simulations",
            "wec_integrations",
            "psse_bus_results",
            "psse_generator_results",
            "psse_load_results",
            "psse_line_results",
            "pypsa_bus_results",
            "pypsa_generator_results",
            "pypsa_load_results",
            "pypsa_line_results",
            "wec_power_results",
        ]

        with self.connection() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
            existing_tables = {row[0] for row in cursor.fetchall()}

            missing_tables = set(required_tables) - existing_tables
            if missing_tables:
                self.initialize_database()
                return False

        # Check for missing columns in existing tables and migrate
        self._migrate_schema()

        return True

    def _migrate_schema(self):
        """Migrate database schema to add missing columns."""
        with self.connection() as conn:
            cursor = conn.cursor()

            # Check if wave_spectrum and wave_class columns exist in wec_simulations
            cursor.execute("PRAGMA table_info(wec_simulations)")
            columns = [row[1] for row in cursor.fetchall()]

            migrations_applied = False

            if "wave_spectrum" not in columns:
                cursor.execute(
                    "ALTER TABLE wec_simulations ADD COLUMN wave_spectrum TEXT"
                )
                migrations_applied = True

            if "wave_class" not in columns:
                cursor.execute("ALTER TABLE wec_simulations ADD COLUMN wave_class TEXT")
                migrations_applied = True

            if migrations_applied:
                conn.commit()

    @contextmanager
    def connection(self):
        """Context manager for safe database connections.

        Provides transaction safety with automatic commit on success and
        rollback on exceptions. Connection closed automatically.

        """
        if self.db_path is None:
            raise ValueError(
                "Database path not configured. Please use engine.database.set_database_path() to configure."
            )

        conn = sqlite3.connect(self.db_path)
        try:
            yield conn
            conn.commit()
        except:
            conn.rollback()
            raise
        finally:
            conn.close()

    def initialize_database(self, db_path: Optional[str] = None):
        """Initialize database schema with WEC-Grid tables and indexes.

        Args:
            db_path (str, optional): Path where database should be created.
                If provided, creates new database at this location and updates
                the current instance to use it. If None, uses existing database path.

        Creates all required tables according to the finalized WEC-Grid schema:
        - Metadata tables for simulation parameters
        - Software-specific result tables (PSS®E, PyPSA)
        - WEC time-series data tables
        - Performance indexes for efficient queries

        All existing data is preserved if tables already exist.

        Example:
            >>> # Create new database when none is configured
            >>> engine.database.initialize_database("/path/to/new_database.db")

            >>> # Initialize schema on existing configured database
            >>> engine.database.initialize_database()
        """
        if db_path:
            # Convert to absolute path
            db_path = str(Path(db_path).absolute())

            # Ensure directory exists
            os.makedirs(os.path.dirname(db_path), exist_ok=True)

            # Update the instance to use this new database path
            save_database_config(db_path)
            self.db_path = db_path

            # Create the database file if it doesn't exist
            if not os.path.exists(db_path):
                # Touch the file to create it
                conn = sqlite3.connect(db_path)
                conn.close()

        # Verify we have a database path to work with
        if self.db_path is None:
            raise ValueError(
                "No database path configured. Please provide db_path parameter or "
                "use engine.database.set_database_path() to configure a database path first."
            )

        with self.connection() as conn:
            cursor = conn.cursor()

            # ================================================================
            # METADATA TABLES
            # ================================================================

            # Grid simulation metadata
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS grid_simulations (
                    grid_sim_id INTEGER PRIMARY KEY AUTOINCREMENT,
                    sim_name TEXT,
                    case_name TEXT NOT NULL,
                    psse BOOLEAN DEFAULT FALSE,
                    pypsa BOOLEAN DEFAULT FALSE,
                    sbase_mva REAL NOT NULL,
                    sim_start_time TEXT NOT NULL,
                    sim_end_time TEXT,
                    delta_time INTEGER,
                    notes TEXT,
                    created_at TEXT DEFAULT CURRENT_TIMESTAMP
                )
            """
            )

            # WEC simulation parameters
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS wec_simulations (
                    wec_sim_id INTEGER PRIMARY KEY AUTOINCREMENT,
                    model_type TEXT NOT NULL,
                    sim_duration_sec REAL NOT NULL,
                    delta_time REAL NOT NULL,
                    wave_height_m REAL,
                    wave_period_sec REAL,
                    wave_spectrum TEXT,
                    wave_class TEXT,
                    wave_seed INTEGER,
                    simulation_hash TEXT,
                    created_at TEXT DEFAULT CURRENT_TIMESTAMP
                )
            """
            )

            # WEC-Grid integration mapping
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS wec_integrations (
                    integration_id INTEGER PRIMARY KEY AUTOINCREMENT,
                    grid_sim_id INTEGER NOT NULL,
                    wec_sim_id INTEGER NOT NULL,
                    farm_name TEXT NOT NULL,
                    bus_location INTEGER NOT NULL,
                    num_devices INTEGER NOT NULL,
                    created_at TEXT DEFAULT CURRENT_TIMESTAMP,
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE,
                    FOREIGN KEY (wec_sim_id) REFERENCES wec_simulations(wec_sim_id) ON DELETE CASCADE
                )
            """
            )

            # ================================================================
            # PSS®E-SPECIFIC TABLES (GridState Schema Alignment)
            # ================================================================

            # PSS®E Bus Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS psse_bus_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    bus INTEGER NOT NULL,
                    bus_name TEXT,
                    type TEXT,
                    p REAL,
                    q REAL,
                    v_mag REAL,
                    angle_deg REAL,
                    vbase REAL,
                    PRIMARY KEY (grid_sim_id, timestamp, bus),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # PSS®E Generator Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS psse_generator_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    gen INTEGER NOT NULL,
                    gen_name TEXT,
                    bus INTEGER NOT NULL,
                    p REAL,
                    q REAL,
                    mbase REAL,
                    status INTEGER,
                    PRIMARY KEY (grid_sim_id, timestamp, gen),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # PSS®E Load Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS psse_load_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    load INTEGER NOT NULL,
                    load_name TEXT,
                    bus INTEGER NOT NULL,
                    p REAL,
                    q REAL,
                    status INTEGER,
                    PRIMARY KEY (grid_sim_id, timestamp, load),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # PSS®E Line Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS psse_line_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    line INTEGER NOT NULL,
                    line_name TEXT,
                    ibus INTEGER NOT NULL,
                    jbus INTEGER NOT NULL,
                    line_pct REAL,
                    status INTEGER,
                    PRIMARY KEY (grid_sim_id, timestamp, line),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # ================================================================
            # PyPSA-SPECIFIC TABLES (Identical to PSS®E for Cross-Platform Comparison)
            # ================================================================

            # PyPSA Bus Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS pypsa_bus_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    bus INTEGER NOT NULL,
                    bus_name TEXT,
                    type TEXT,
                    p REAL,
                    q REAL,
                    v_mag REAL,
                    angle_deg REAL,
                    vbase REAL,
                    PRIMARY KEY (grid_sim_id, timestamp, bus),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # PyPSA Generator Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS pypsa_generator_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    gen INTEGER NOT NULL,
                    gen_name TEXT,
                    bus INTEGER NOT NULL,
                    p REAL,
                    q REAL,
                    mbase REAL,
                    status INTEGER,
                    PRIMARY KEY (grid_sim_id, timestamp, gen),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # PyPSA Load Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS pypsa_load_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    load INTEGER NOT NULL,
                    load_name TEXT,
                    bus INTEGER NOT NULL,
                    p REAL,
                    q REAL,
                    status INTEGER,
                    PRIMARY KEY (grid_sim_id, timestamp, load),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # PyPSA Line Results
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS pypsa_line_results (
                    grid_sim_id INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    line INTEGER NOT NULL,
                    line_name TEXT,
                    ibus INTEGER NOT NULL,
                    jbus INTEGER NOT NULL,
                    line_pct REAL,
                    status INTEGER,
                    PRIMARY KEY (grid_sim_id, timestamp, line),
                    FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
                )
            """
            )

            # ================================================================
            # WEC TIME-SERIES DATA
            # ================================================================

            # WEC Power Results (High-Resolution Time Series)
            cursor.execute(
                """
                CREATE TABLE IF NOT EXISTS wec_power_results (
                    wec_sim_id INTEGER NOT NULL,
                    time_sec REAL NOT NULL,
                    device_index INTEGER NOT NULL,
                    p_w REAL,
                    q_var REAL,
                    wave_elevation_m REAL,
                    PRIMARY KEY (wec_sim_id, time_sec, device_index),
                    FOREIGN KEY (wec_sim_id) REFERENCES wec_simulations(wec_sim_id) ON DELETE CASCADE
                )
            """
            )

            # ================================================================
            # PERFORMANCE INDEXES
            # ================================================================

            # Grid simulation indexes
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_grid_sim_time ON grid_simulations(sim_start_time)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_grid_sim_case ON grid_simulations(case_name)"
            )

            # PSS®E result indexes
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_psse_bus_time ON psse_bus_results(grid_sim_id, timestamp)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_psse_gen_time ON psse_generator_results(grid_sim_id, timestamp)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_psse_load_time ON psse_load_results(grid_sim_id, timestamp)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_psse_line_time ON psse_line_results(grid_sim_id, timestamp)"
            )

            # PyPSA result indexes
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_pypsa_bus_time ON pypsa_bus_results(grid_sim_id, timestamp)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_pypsa_gen_time ON pypsa_generator_results(grid_sim_id, timestamp)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_pypsa_load_time ON pypsa_load_results(grid_sim_id, timestamp)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_pypsa_line_time ON pypsa_line_results(grid_sim_id, timestamp)"
            )

            # WEC time-series indexes
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_wec_power_time ON wec_power_results(wec_sim_id, time_sec)"
            )
            cursor.execute(
                "CREATE INDEX IF NOT EXISTS idx_wec_integration ON wec_integrations(grid_sim_id, wec_sim_id)"
            )

    def clean_database(self):
        """Delete the current database and reinitialize with fresh schema.

        WARNING: This will permanently delete all stored simulation data.
        Use with caution - all existing data will be lost.

        Returns:
            bool: True if database was successfully cleaned and reinitialized.

        Notes:
            Wasn't working if my Jupyter Kernal was still going, need to restart then call
        Example:
            >>> engine.database.clean_database()
            WARNING: This will delete all data in the database!
            Database cleaned and reinitialized successfully.
        """
        print("WARNING: This will delete all data in the database!")

        # Close any existing connections by creating a temporary one and closing it
        try:
            conn = sqlite3.connect(self.db_path)
            conn.close()
        except:
            pass

        # Delete the database file if it exists
        if os.path.exists(self.db_path):
            try:
                os.remove(self.db_path)
            except OSError as e:
                print(f"Error deleting database file: {e}")
                return False

        # Reinitialize with fresh schema
        try:
            self.initialize_database()
            return True
        except Exception as e:
            print(f"Error reinitializing database: {e}")
            return False

    def query(self, sql: str, params: tuple = None, return_type: str = "raw"):
        """Execute SQL query with flexible result formatting.

        Args:
            sql (str): SQL query string.
            params (tuple, optional): Query parameters for safe substitution.
            return_type (str): Format for results - 'raw', 'df', or 'dict'.

        Returns:
            Results in specified format:
            - 'raw': List of tuples (default SQLite format)
            - 'df': pandas DataFrame with column names
            - 'dict': List of dictionaries with column names as keys

        Example:
            >>> db.query("SELECT * FROM grid_simulations WHERE case_name = ?",
            ...           params=("IEEE_14_bus",), return_type="df")
        """
        with self.connection() as conn:
            cursor = conn.cursor()
            cursor.execute(sql, params or ())
            result = cursor.fetchall()

            if return_type == "df":
                columns = [desc[0] for desc in cursor.description]
                return pd.DataFrame(result, columns=columns)
            elif return_type == "dict":
                columns = [desc[0] for desc in cursor.description]
                return [dict(zip(columns, row)) for row in result]
            elif return_type == "raw":
                return result
            else:
                raise ValueError(
                    f"Invalid return_type '{return_type}'. Must be 'raw', 'df', or 'dict'."
                )

    def save_sim(self, sim_name: str, notes: str = None) -> int:
        """Save simulation data for all available software backends in the engine.

        Automatically detects and stores data from all active software backends
        (PSS®E, PyPSA) and WEC farms present in the engine object.

        Always creates a new simulation entry - no duplicate checking.
        Users can manage simulation names as needed.

        Args:
            sim_name (str): User-friendly simulation name.
            notes (str, optional): Simulation notes.

        Returns:
            int: grid_sim_id of the created simulation.

        Example:
            >>> sim_id = engine.database.save_sim(
            ...     sim_name="IEEE 30 test",
            ...     notes="testing the database"
            ... )
        """
        # Gather all available software objects from engine
        softwares = []

        # Check for PSS®E
        if hasattr(self.engine, "psse") and hasattr(self.engine.psse, "grid"):
            softwares.append(self.engine.psse.grid)

        # Check for PyPSA
        if hasattr(self.engine, "pypsa") and hasattr(self.engine.pypsa, "grid"):
            softwares.append(self.engine.pypsa.grid)

        if not softwares:
            raise ValueError(
                "No software backends found in engine. Ensure PSS®E or PyPSA models are loaded."
            )

        # Get case name from engine
        case_name = getattr(self.engine, "case_name", "Unknown_Case")

        # Get time manager from engine
        timeManager = getattr(self.engine, "time", None)
        if timeManager is None:
            raise ValueError(
                "No time manager found in engine. Ensure engine.time is properly initialized."
            )

        # Extract software flags and determine sbase
        psse_used = False
        pypsa_used = False
        sbase_mva = None

        for i, software_obj in enumerate(softwares):
            software_name = getattr(software_obj, "software", "")

            software_name = software_name.lower() if software_name else ""

            if software_name == "psse":
                psse_used = True
            elif software_name == "pypsa":
                pypsa_used = True
            else:
                continue  # Skip this software object instead of processing it

            # Get sbase from the first software object
            if sbase_mva is None:
                if hasattr(software_obj, "sbase"):
                    sbase_mva = software_obj.sbase
                else:
                    # Try to get from parent object
                    parent = getattr(software_obj, "_parent", None)
                    if parent and hasattr(parent, "sbase"):
                        sbase_mva = parent.sbase
                    else:
                        sbase_mva = 100.0  # Default fallback

        # Get time information from simulation
        sim_start_time = timeManager.start_time.isoformat()
        sim_end_time = getattr(timeManager, "sim_stop", None)
        if sim_end_time:
            sim_end_time = sim_end_time.isoformat()
        delta_time = timeManager.delta_time

        # Create new grid simulation record (always create new entry)
        with self.connection() as conn:
            cursor = conn.cursor()

            # Insert new simulation - always create new entry
            cursor.execute(
                """
                INSERT INTO grid_simulations 
                (sim_name, case_name, psse, pypsa, sbase_mva, sim_start_time, 
                 sim_end_time, delta_time, notes)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
            """,
                (
                    sim_name,
                    case_name,
                    psse_used,
                    pypsa_used,
                    sbase_mva,
                    sim_start_time,
                    sim_end_time,
                    delta_time,
                    notes,
                ),
            )

            grid_sim_id = cursor.lastrowid

        # Store data for each valid software
        valid_softwares = []
        for software_obj in softwares:
            software_name = getattr(software_obj, "software", "").lower()

            # Only process valid software names
            if software_name in ["psse", "pypsa"]:
                valid_softwares.append((software_obj, software_name))

        for software_obj, software_name in valid_softwares:
            # Store all time-series data from GridState
            self._store_all_gridstate_timeseries(
                grid_sim_id, software_obj, software_name, timeManager
            )

        # Store WEC farm data if available
        if hasattr(self.engine, "wec_farms") and self.engine.wec_farms:
            self._store_wec_farm_data(grid_sim_id)

        # Create summary of used software
        used_software = []
        if psse_used:
            used_software.append("PSS®E")
        if pypsa_used:
            used_software.append("PyPSA")

        print(f"Simulation saved: ID {grid_sim_id} - {sim_name}")

        return grid_sim_id

    def _store_all_gridstate_timeseries(
        self, grid_sim_id: int, grid_state_obj, software: str, timeManager
    ):
        """Store all time-series data from GridState object.

        Args:
            grid_sim_id (int): Grid simulation ID.
            grid_state_obj: GridState object with time-series data.
            software (str): Software name ("psse" or "pypsa").
            timeManager: WECGridTime object.
        """
        # Validate software name
        if software not in ["psse", "pypsa"]:
            raise ValueError(
                f"Invalid software name: '{software}'. Must be 'psse' or 'pypsa'."
            )

        table_prefix = f"{software}_"
        snapshots = timeManager.snapshots

        with self.connection() as conn:
            cursor = conn.cursor()

            # Store bus time-series data
            if hasattr(grid_state_obj, "bus_t") and grid_state_obj.bus_t:
                for timestamp in snapshots:
                    timestamp_str = timestamp.isoformat()

                    # Create bus data for this timestamp
                    if hasattr(grid_state_obj, "bus") and not grid_state_obj.bus.empty:
                        for idx, row in grid_state_obj.bus.iterrows():
                            cursor.execute(
                                f"""
                                INSERT OR REPLACE INTO {table_prefix}bus_results 
                                (grid_sim_id, timestamp, bus, bus_name, type, p, q, v_mag, angle_deg, vbase)
                                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                            """,
                                (
                                    grid_sim_id,
                                    timestamp_str,
                                    row.get("bus"),
                                    row.get("bus_name"),
                                    row.get("type"),
                                    self._get_timeseries_value(
                                        grid_state_obj.bus_t,
                                        "p",
                                        row.get("bus"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.bus_t,
                                        "q",
                                        row.get("bus"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.bus_t,
                                        "v_mag",
                                        row.get("bus"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.bus_t,
                                        "angle_deg",
                                        row.get("bus"),
                                        timestamp,
                                    ),
                                    row.get("vbase"),
                                ),
                            )

            # Store generator time-series data
            if hasattr(grid_state_obj, "gen_t") and grid_state_obj.gen_t:
                for timestamp in snapshots:
                    timestamp_str = timestamp.isoformat()

                    if hasattr(grid_state_obj, "gen") and not grid_state_obj.gen.empty:
                        for idx, row in grid_state_obj.gen.iterrows():
                            cursor.execute(
                                f"""
                                INSERT OR REPLACE INTO {table_prefix}generator_results 
                                (grid_sim_id, timestamp, gen, gen_name, bus, p, q, mbase, status)
                                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
                            """,
                                (
                                    grid_sim_id,
                                    timestamp_str,
                                    row.get("gen"),
                                    row.get("gen_name"),
                                    row.get("bus"),
                                    self._get_timeseries_value(
                                        grid_state_obj.gen_t,
                                        "p",
                                        row.get("gen"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.gen_t,
                                        "q",
                                        row.get("gen"),
                                        timestamp,
                                    ),
                                    row.get("Mbase"),
                                    self._get_timeseries_value(
                                        grid_state_obj.gen_t,
                                        "status",
                                        row.get("gen"),
                                        timestamp,
                                    ),
                                ),
                            )

            # Store load time-series data
            if hasattr(grid_state_obj, "load_t") and grid_state_obj.load_t:
                for timestamp in snapshots:
                    timestamp_str = timestamp.isoformat()

                    if (
                        hasattr(grid_state_obj, "load")
                        and not grid_state_obj.load.empty
                    ):
                        for idx, row in grid_state_obj.load.iterrows():
                            cursor.execute(
                                f"""
                                INSERT OR REPLACE INTO {table_prefix}load_results 
                                (grid_sim_id, timestamp, load, load_name, bus, p, q, status)
                                VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                            """,
                                (
                                    grid_sim_id,
                                    timestamp_str,
                                    row.get("load"),
                                    row.get("load_name"),
                                    row.get("bus"),
                                    self._get_timeseries_value(
                                        grid_state_obj.load_t,
                                        "p",
                                        row.get("load"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.load_t,
                                        "q",
                                        row.get("load"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.load_t,
                                        "status",
                                        row.get("load"),
                                        timestamp,
                                    ),
                                ),
                            )

            # Store line time-series data
            if hasattr(grid_state_obj, "line_t") and grid_state_obj.line_t:
                for timestamp in snapshots:
                    timestamp_str = timestamp.isoformat()

                    if (
                        hasattr(grid_state_obj, "line")
                        and not grid_state_obj.line.empty
                    ):
                        for idx, row in grid_state_obj.line.iterrows():
                            cursor.execute(
                                f"""
                                INSERT OR REPLACE INTO {table_prefix}line_results 
                                (grid_sim_id, timestamp, line, line_name, ibus, jbus, line_pct, status)
                                VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                            """,
                                (
                                    grid_sim_id,
                                    timestamp_str,
                                    row.get("line"),
                                    row.get("line_name"),
                                    row.get("ibus"),
                                    row.get("jbus"),
                                    self._get_timeseries_value(
                                        grid_state_obj.line_t,
                                        "line_pct",
                                        row.get("line"),
                                        timestamp,
                                    ),
                                    self._get_timeseries_value(
                                        grid_state_obj.line_t,
                                        "status",
                                        row.get("line"),
                                        timestamp,
                                    ),
                                ),
                            )

    def _store_wec_farm_data(self, grid_sim_id: int):
        """Store WEC farm data if available in the engine.

        Args:
            grid_sim_id (int): Grid simulation ID to link WEC data to.
        """
        with self.connection() as conn:
            cursor = conn.cursor()

            for farm in self.engine.wec_farms:
                # Store wec_integrations record linking farm to grid simulation
                cursor.execute(
                    """
                    INSERT OR REPLACE INTO wec_integrations 
                    (grid_sim_id, wec_sim_id, farm_name, bus_location, num_devices)
                    VALUES (?, ?, ?, ?, ?)
                """,
                    (
                        grid_sim_id,
                        farm.wec_sim_id,
                        farm.farm_name,
                        farm.bus_location,
                        farm.size,
                    ),
                )

    def _get_timeseries_value(
        self, timeseries_dict, parameter: str, component_id: int, timestamp
    ):
        """Extract time-series value for specific component and timestamp.

        Args:
            timeseries_dict: AttrDict containing time-series DataFrames.
            parameter (str): Parameter name (e.g., 'p', 'q', 'v_mag').
            component_id (int): Component ID.
            timestamp: Timestamp to extract.

        Returns:
            Value at the specified timestamp or None if not available.
        """
        try:
            if parameter in timeseries_dict:
                df = timeseries_dict[parameter]

                # GridState stores time-series with component names as columns, not IDs
                # Try both component_id directly and component name patterns
                possible_columns = [
                    component_id,  # Try direct ID first (fallback case)
                    str(component_id),  # String version of ID
                    f"Bus_{component_id}",  # Bus name pattern
                    f"Gen_{component_id}",  # Generator name pattern
                    f"Line_{component_id}",  # Line name pattern
                    f"Load_{component_id}",  # Load name pattern
                ]

                for col in possible_columns:
                    if col in df.columns and timestamp in df.index:
                        value = df.loc[timestamp, col]
                        # Debug: Print if value is None/NaN for slack bus
                        # if component_id == 1 and parameter in ['p', 'q', 'v_mag', 'angle_deg'] and (pd.isna(value) or value is None):
                        #     print(f"DEBUG: Slack bus {component_id} {parameter} = {value} (column: {col})")
                        #     print(f"Available columns: {list(df.columns)}")
                        #     print(f"Available timestamps: {list(df.index)}")
                        return value

        except (KeyError, AttributeError) as e:
            # Debug: Print error for slack bus
            if component_id == 1:
                print(f"DEBUG: Error getting slack bus {component_id} {parameter}: {e}")
            pass
        return None

    def store_gridstate_data(
        self, grid_sim_id: int, timestamp: str, grid_state, software: str
    ):
        """Store GridState data to appropriate software-specific tables.

        Args:
            grid_sim_id (int): Grid simulation ID.
            timestamp (str): ISO datetime string for this snapshot.
            grid_state: GridState object with bus, gen, load, line DataFrames.
            software (str): Software backend - "PSSE" or "PyPSA".

        Example:
            >>> db.store_gridstate_data(
            ...     grid_sim_id=123,
            ...     timestamp="2025-08-14T10:05:00",
            ...     grid_state=my_grid_state,
            ...     software="PSSE"
            ... )
        """
        software = software.lower()
        table_prefix = f"{software}_"

        with self.connection() as conn:
            cursor = conn.cursor()

            # Store bus results
            if not grid_state.bus.empty:
                for bus_id, row in grid_state.bus.iterrows():
                    cursor.execute(
                        f"""
                        INSERT OR REPLACE INTO {table_prefix}bus_results 
                        (grid_sim_id, timestamp, bus, bus_name, type, p, q, v_mag, angle_deg, vbase)
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                    """,
                        (
                            grid_sim_id,
                            timestamp,
                            bus_id,
                            row.get("bus_name"),
                            row.get("type"),
                            row.get("p"),
                            row.get("q"),
                            row.get("v_mag"),
                            row.get("angle_deg"),
                            row.get("vbase"),
                        ),
                    )

            # Store generator results
            if not grid_state.gen.empty:
                for gen_id, row in grid_state.gen.iterrows():
                    cursor.execute(
                        f"""
                        INSERT OR REPLACE INTO {table_prefix}generator_results 
                        (grid_sim_id, timestamp, gen, gen_name, bus, p, q, mbase, status)
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
                    """,
                        (
                            grid_sim_id,
                            timestamp,
                            gen_id,
                            row.get("gen_name"),
                            row.get("bus"),
                            row.get("p"),
                            row.get("q"),
                            row.get("Mbase"),
                            row.get("status"),
                        ),
                    )

            # Store load results
            if not grid_state.load.empty:
                for load_id, row in grid_state.load.iterrows():
                    cursor.execute(
                        f"""
                        INSERT OR REPLACE INTO {table_prefix}load_results 
                        (grid_sim_id, timestamp, load, load_name, bus, p, q, status)
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                    """,
                        (
                            grid_sim_id,
                            timestamp,
                            load_id,
                            row.get("load_name"),
                            row.get("bus"),
                            row.get("p"),
                            row.get("q"),
                            row.get("status"),
                        ),
                    )

            # Store line results
            if not grid_state.line.empty:
                for line_id, row in grid_state.line.iterrows():
                    cursor.execute(
                        f"""
                        INSERT OR REPLACE INTO {table_prefix}line_results 
                        (grid_sim_id, timestamp, line, line_name, ibus, jbus, line_pct, status)
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                    """,
                        (
                            grid_sim_id,
                            timestamp,
                            line_id,
                            row.get("line_name"),
                            row.get("ibus"),
                            row.get("jbus"),
                            row.get("line_pct"),
                            row.get("status"),
                        ),
                    )

    def get_simulation_info(self, grid_sim_id: int = None) -> pd.DataFrame:
        """Get grid simulation information.

        Args:
            grid_sim_id (int, optional): Specific simulation ID. If None, returns all.

        Returns:
            pd.DataFrame: Simulation metadata.
        """
        if grid_sim_id:
            return self.query(
                "SELECT * FROM grid_simulations WHERE grid_sim_id = ?",
                params=(grid_sim_id,),
                return_type="df",
            )
        else:
            return self.query(
                "SELECT * FROM grid_simulations ORDER BY created_at DESC",
                return_type="df",
            )

    def grid_sims(self) -> pd.DataFrame:
        """Get all grid simulation metadata in a user-friendly format.

        Returns:
            pd.DataFrame: Grid simulations with key metadata columns.

        Example:
            >>> engine.database.grid_sims()
               grid_sim_id     sim_name      case_name  psse  pypsa  sbase_mva  ...
            0           1     Test Run   IEEE_14_bus  True  False      100.0  ...
        """
        return self.query(
            """
            SELECT grid_sim_id, sim_name, case_name, psse, pypsa, sbase_mva,
                   sim_start_time, sim_end_time, delta_time, notes, created_at
            FROM grid_simulations 
            ORDER BY created_at DESC
        """,
            return_type="df",
        )

    def wecsim_runs(self) -> pd.DataFrame:
        """Get all WEC simulation metadata with enhanced wave parameters.

        Returns:
            pd.DataFrame: WEC simulations with parameters and wave conditions including
                wave spectrum type, wave class, and all simulation parameters.

        Example:
            >>> engine.database.wecsim_runs()
               wec_sim_id model_type  sim_duration_sec  delta_time  wave_spectrum  wave_class  ...
            0          1       RM3             600.0        0.1             PM   irregular  ...
        """
        return self.query(
            """
            SELECT wec_sim_id, model_type, sim_duration_sec, delta_time,
                   wave_height_m, wave_period_sec, wave_spectrum, wave_class, wave_seed,
                   simulation_hash, created_at
            FROM wec_simulations 
            ORDER BY created_at DESC
        """,
            return_type="df",
        )

    def pull_sim(self, grid_sim_id: int, software: str = None):
        """Pull simulation data from database and reconstruct GridState object.

        Retrieves all time-series data for a specific simulation and recreates
        the GridState object with both snapshot data and time-series history.

        Args:
            grid_sim_id (int): Grid simulation ID to retrieve.
            software (str, optional): Software backend to pull data for ("psse" or "pypsa").
                If None, automatically detects which software was used based on
                grid_simulations table flags.

        Returns:
            GridState: Reconstructed GridState object with time-series data.

        Raises:
            ValueError: If grid_sim_id not found or software not available for this simulation.

        Example:
            >>> # Pull PSS®E simulation data
            >>> grid_state = engine.database.pull_sim(grid_sim_id=123, software="psse")
            >>> print(f"Buses: {len(grid_state.bus)}")
            >>> print(f"Time series data: {list(grid_state.bus_t.keys())}")

            >>> # Auto-detect software and pull data
            >>> grid_state = engine.database.pull_sim(grid_sim_id=123)
        """
        # Import here to avoid circular import
        from ..modelers.power_system.base import GridState, AttrDict

        # Get simulation metadata
        sim_info = self.query(
            "SELECT * FROM grid_simulations WHERE grid_sim_id = ?",
            params=(grid_sim_id,),
            return_type="df",
        )

        if sim_info.empty:
            raise ValueError(f"Simulation with ID {grid_sim_id} not found in database")

        sim_row = sim_info.iloc[0]

        # Auto-detect software if not specified
        if software is None:
            if sim_row["psse"]:
                software = "psse"
            elif sim_row["pypsa"]:
                software = "pypsa"
            else:
                raise ValueError(
                    f"No software backend data found for simulation {grid_sim_id}"
                )

        # Validate software choice
        if software not in ["psse", "pypsa"]:
            raise ValueError(
                f"Invalid software: '{software}'. Must be 'psse' or 'pypsa'."
            )

        if software == "psse" and not sim_row["psse"]:
            raise ValueError(f"PSS®E data not available for simulation {grid_sim_id}")
        if software == "pypsa" and not sim_row["pypsa"]:
            raise ValueError(f"PyPSA data not available for simulation {grid_sim_id}")

        # Create GridState object
        grid_state = GridState(software=software)

        # Set case name from database metadata
        grid_state.case = sim_row["case_name"]

        # Table prefix for this software
        table_prefix = f"{software}_"
        table_prefix = f"{software}_"

        # Pull bus data
        bus_data = self.query(
            f"""
            SELECT * FROM {table_prefix}bus_results 
            WHERE grid_sim_id = ? 
            ORDER BY timestamp, bus
        """,
            params=(grid_sim_id,),
            return_type="df",
        )

        # Pull generator data
        gen_data = self.query(
            f"""
            SELECT * FROM {table_prefix}generator_results 
            WHERE grid_sim_id = ? 
            ORDER BY timestamp, gen
        """,
            params=(grid_sim_id,),
            return_type="df",
        )

        # Pull load data
        load_data = self.query(
            f"""
            SELECT * FROM {table_prefix}load_results 
            WHERE grid_sim_id = ? 
            ORDER BY timestamp, load
        """,
            params=(grid_sim_id,),
            return_type="df",
        )

        # Pull line data
        line_data = self.query(
            f"""
            SELECT * FROM {table_prefix}line_results 
            WHERE grid_sim_id = ? 
            ORDER BY timestamp, line
        """,
            params=(grid_sim_id,),
            return_type="df",
        )

        # Convert timestamp strings to pandas timestamps
        if not bus_data.empty:
            bus_data["timestamp"] = pd.to_datetime(bus_data["timestamp"])
        if not gen_data.empty:
            gen_data["timestamp"] = pd.to_datetime(gen_data["timestamp"])
        if not load_data.empty:
            load_data["timestamp"] = pd.to_datetime(load_data["timestamp"])
        if not line_data.empty:
            line_data["timestamp"] = pd.to_datetime(line_data["timestamp"])

        # Reconstruct current snapshot data (use latest timestamp)
        if not bus_data.empty:
            latest_time = bus_data["timestamp"].max()
            latest_bus = bus_data[bus_data["timestamp"] == latest_time].copy()
            latest_bus.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
            latest_bus.reset_index(drop=True, inplace=True)
            # Ensure clean column headers
            latest_bus.columns.name = None
            latest_bus.index.name = None
            latest_bus.attrs["df_type"] = "BUS"
            grid_state.bus = latest_bus

        if not gen_data.empty:
            latest_time = gen_data["timestamp"].max()
            latest_gen = gen_data[gen_data["timestamp"] == latest_time].copy()
            latest_gen.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
            latest_gen.reset_index(drop=True, inplace=True)
            # Ensure clean column headers
            latest_gen.columns.name = None
            latest_gen.index.name = None
            latest_gen.attrs["df_type"] = "GEN"
            grid_state.gen = latest_gen

        if not load_data.empty:
            latest_time = load_data["timestamp"].max()
            latest_load = load_data[load_data["timestamp"] == latest_time].copy()
            latest_load.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
            latest_load.reset_index(drop=True, inplace=True)
            # Ensure clean column headers
            latest_load.columns.name = None
            latest_load.index.name = None
            latest_load.attrs["df_type"] = "LOAD"
            grid_state.load = latest_load

        if not line_data.empty:
            latest_time = line_data["timestamp"].max()
            latest_line = line_data[line_data["timestamp"] == latest_time].copy()
            latest_line.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
            latest_line.reset_index(drop=True, inplace=True)
            # Ensure clean column headers
            latest_line.columns.name = None
            latest_line.index.name = None
            latest_line.attrs["df_type"] = "LINE"
            grid_state.line = latest_line

        # Reconstruct time-series data
        def _reconstruct_timeseries(data_df, id_col, component_type):
            """Helper function to reconstruct time-series data for a component type."""
            if data_df.empty:
                return AttrDict()

            ts_data = AttrDict()

            # Get all variable columns (exclude metadata columns)
            exclude_cols = {
                "grid_sim_id",
                "timestamp",
                id_col,
                f"{component_type}_name",
            }
            if component_type == "bus":
                exclude_cols.update(
                    {"bus_name", "vbase"}
                )  # Updated to include bus_name
            elif component_type == "gen":
                exclude_cols.update(
                    {"gen_name", "mbase"}
                )  # Updated to include gen_name
            elif component_type == "line":
                exclude_cols.update(
                    {"line_name", "ibus", "jbus"}
                )  # Updated to include line_name
            elif component_type == "load":
                exclude_cols.add("load_name")  # Added load_name

            var_cols = [col for col in data_df.columns if col not in exclude_cols]

            # For each variable, create a time-series DataFrame
            for var in var_cols:
                if f"{component_type}_name" not in data_df.columns:
                    # Fallback: use component IDs as column names
                    pivot_data = data_df.pivot(
                        index="timestamp", columns=id_col, values=var
                    )
                else:
                    # Pivot data to have timestamps as rows and component names as columns
                    pivot_data = data_df.pivot(
                        index="timestamp", columns=f"{component_type}_name", values=var
                    )

                # Clean up column headers
                pivot_data.columns.name = None
                pivot_data.index.name = None
                ts_data[var] = pivot_data

            return ts_data

        # Reconstruct time-series for each component type
        grid_state.bus_t = _reconstruct_timeseries(bus_data, "bus", "bus")
        grid_state.gen_t = _reconstruct_timeseries(gen_data, "gen", "gen")
        grid_state.load_t = _reconstruct_timeseries(load_data, "load", "load")
        grid_state.line_t = _reconstruct_timeseries(line_data, "line", "line")

        print(
            f"GridState reconstructed: {sim_row['case_name']} ({software.upper()}) - "
            f"{len(grid_state.bus)} buses, {len(grid_state.gen)} generators"
        )

        return grid_state

    def set_database_path(self, db_path):
        """Set database path and reinitialize connection.

        Args:
            db_path (str): Path to WEC-GRID database file.

        Example:
            >>> engine.database.set_database_path("/path/to/wecgrid-database/WEC-GRID.db")
        """
        if not os.path.exists(db_path):
            raise FileNotFoundError(f"Database file not found: {db_path}")

        # Save to config
        save_database_config(db_path)

        # Update current instance
        self.db_path = str(Path(db_path).absolute())

        # Reinitialize
        self.check_and_initialize()

        return self.db_path

check_and_initialize()

Check if database exists and has correct schema, initialize if needed.

Validates that all required tables exist with proper structure. Creates database and initializes schema if missing or incomplete.

Returns:

Name Type Description
bool

True if database was already valid, False if initialization was needed.

Source code in src/wecgrid/util/database.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def check_and_initialize(self):
    """Check if database exists and has correct schema, initialize if needed.

    Validates that all required tables exist with proper structure.
    Creates database and initializes schema if missing or incomplete.

    Returns:
        bool: True if database was already valid, False if initialization was needed.
    """
    if self.db_path is None:
        print("Warning: Database path not set. Cannot initialize database.")
        return False

    if not os.path.exists(self.db_path):
        # Ensure directory exists
        os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
        self.initialize_database()
        return False

    # Check if all required tables exist
    required_tables = [
        "grid_simulations",
        "wec_simulations",
        "wec_integrations",
        "psse_bus_results",
        "psse_generator_results",
        "psse_load_results",
        "psse_line_results",
        "pypsa_bus_results",
        "pypsa_generator_results",
        "pypsa_load_results",
        "pypsa_line_results",
        "wec_power_results",
    ]

    with self.connection() as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        existing_tables = {row[0] for row in cursor.fetchall()}

        missing_tables = set(required_tables) - existing_tables
        if missing_tables:
            self.initialize_database()
            return False

    # Check for missing columns in existing tables and migrate
    self._migrate_schema()

    return True

clean_database()

Delete the current database and reinitialize with fresh schema.

WARNING: This will permanently delete all stored simulation data. Use with caution - all existing data will be lost.

Returns:

Name Type Description
bool

True if database was successfully cleaned and reinitialized.

Notes

Wasn't working if my Jupyter Kernal was still going, need to restart then call

Example: >>> engine.database.clean_database() WARNING: This will delete all data in the database! Database cleaned and reinitialized successfully.

Source code in src/wecgrid/util/database.py
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
def clean_database(self):
    """Delete the current database and reinitialize with fresh schema.

    WARNING: This will permanently delete all stored simulation data.
    Use with caution - all existing data will be lost.

    Returns:
        bool: True if database was successfully cleaned and reinitialized.

    Notes:
        Wasn't working if my Jupyter Kernal was still going, need to restart then call
    Example:
        >>> engine.database.clean_database()
        WARNING: This will delete all data in the database!
        Database cleaned and reinitialized successfully.
    """
    print("WARNING: This will delete all data in the database!")

    # Close any existing connections by creating a temporary one and closing it
    try:
        conn = sqlite3.connect(self.db_path)
        conn.close()
    except:
        pass

    # Delete the database file if it exists
    if os.path.exists(self.db_path):
        try:
            os.remove(self.db_path)
        except OSError as e:
            print(f"Error deleting database file: {e}")
            return False

    # Reinitialize with fresh schema
    try:
        self.initialize_database()
        return True
    except Exception as e:
        print(f"Error reinitializing database: {e}")
        return False

connection()

Context manager for safe database connections.

Provides transaction safety with automatic commit on success and rollback on exceptions. Connection closed automatically.

Source code in src/wecgrid/util/database.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
@contextmanager
def connection(self):
    """Context manager for safe database connections.

    Provides transaction safety with automatic commit on success and
    rollback on exceptions. Connection closed automatically.

    """
    if self.db_path is None:
        raise ValueError(
            "Database path not configured. Please use engine.database.set_database_path() to configure."
        )

    conn = sqlite3.connect(self.db_path)
    try:
        yield conn
        conn.commit()
    except:
        conn.rollback()
        raise
    finally:
        conn.close()

get_simulation_info(grid_sim_id=None)

Get grid simulation information.

Parameters:

Name Type Description Default
grid_sim_id int

Specific simulation ID. If None, returns all.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Simulation metadata.

Source code in src/wecgrid/util/database.py
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
def get_simulation_info(self, grid_sim_id: int = None) -> pd.DataFrame:
    """Get grid simulation information.

    Args:
        grid_sim_id (int, optional): Specific simulation ID. If None, returns all.

    Returns:
        pd.DataFrame: Simulation metadata.
    """
    if grid_sim_id:
        return self.query(
            "SELECT * FROM grid_simulations WHERE grid_sim_id = ?",
            params=(grid_sim_id,),
            return_type="df",
        )
    else:
        return self.query(
            "SELECT * FROM grid_simulations ORDER BY created_at DESC",
            return_type="df",
        )

grid_sims()

Get all grid simulation metadata in a user-friendly format.

Returns:

Type Description
DataFrame

pd.DataFrame: Grid simulations with key metadata columns.

Example

engine.database.grid_sims() grid_sim_id sim_name case_name psse pypsa sbase_mva ... 0 1 Test Run IEEE_14_bus True False 100.0 ...

Source code in src/wecgrid/util/database.py
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
def grid_sims(self) -> pd.DataFrame:
    """Get all grid simulation metadata in a user-friendly format.

    Returns:
        pd.DataFrame: Grid simulations with key metadata columns.

    Example:
        >>> engine.database.grid_sims()
           grid_sim_id     sim_name      case_name  psse  pypsa  sbase_mva  ...
        0           1     Test Run   IEEE_14_bus  True  False      100.0  ...
    """
    return self.query(
        """
        SELECT grid_sim_id, sim_name, case_name, psse, pypsa, sbase_mva,
               sim_start_time, sim_end_time, delta_time, notes, created_at
        FROM grid_simulations 
        ORDER BY created_at DESC
    """,
        return_type="df",
    )

initialize_database(db_path=None)

Initialize database schema with WEC-Grid tables and indexes.

Parameters:

Name Type Description Default
db_path str

Path where database should be created. If provided, creates new database at this location and updates the current instance to use it. If None, uses existing database path.

None

Creates all required tables according to the finalized WEC-Grid schema: - Metadata tables for simulation parameters - Software-specific result tables (PSS®E, PyPSA) - WEC time-series data tables - Performance indexes for efficient queries

All existing data is preserved if tables already exist.

Example

Create new database when none is configured

engine.database.initialize_database("/path/to/new_database.db")

Initialize schema on existing configured database

engine.database.initialize_database()

Source code in src/wecgrid/util/database.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
def initialize_database(self, db_path: Optional[str] = None):
    """Initialize database schema with WEC-Grid tables and indexes.

    Args:
        db_path (str, optional): Path where database should be created.
            If provided, creates new database at this location and updates
            the current instance to use it. If None, uses existing database path.

    Creates all required tables according to the finalized WEC-Grid schema:
    - Metadata tables for simulation parameters
    - Software-specific result tables (PSS®E, PyPSA)
    - WEC time-series data tables
    - Performance indexes for efficient queries

    All existing data is preserved if tables already exist.

    Example:
        >>> # Create new database when none is configured
        >>> engine.database.initialize_database("/path/to/new_database.db")

        >>> # Initialize schema on existing configured database
        >>> engine.database.initialize_database()
    """
    if db_path:
        # Convert to absolute path
        db_path = str(Path(db_path).absolute())

        # Ensure directory exists
        os.makedirs(os.path.dirname(db_path), exist_ok=True)

        # Update the instance to use this new database path
        save_database_config(db_path)
        self.db_path = db_path

        # Create the database file if it doesn't exist
        if not os.path.exists(db_path):
            # Touch the file to create it
            conn = sqlite3.connect(db_path)
            conn.close()

    # Verify we have a database path to work with
    if self.db_path is None:
        raise ValueError(
            "No database path configured. Please provide db_path parameter or "
            "use engine.database.set_database_path() to configure a database path first."
        )

    with self.connection() as conn:
        cursor = conn.cursor()

        # ================================================================
        # METADATA TABLES
        # ================================================================

        # Grid simulation metadata
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS grid_simulations (
                grid_sim_id INTEGER PRIMARY KEY AUTOINCREMENT,
                sim_name TEXT,
                case_name TEXT NOT NULL,
                psse BOOLEAN DEFAULT FALSE,
                pypsa BOOLEAN DEFAULT FALSE,
                sbase_mva REAL NOT NULL,
                sim_start_time TEXT NOT NULL,
                sim_end_time TEXT,
                delta_time INTEGER,
                notes TEXT,
                created_at TEXT DEFAULT CURRENT_TIMESTAMP
            )
        """
        )

        # WEC simulation parameters
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS wec_simulations (
                wec_sim_id INTEGER PRIMARY KEY AUTOINCREMENT,
                model_type TEXT NOT NULL,
                sim_duration_sec REAL NOT NULL,
                delta_time REAL NOT NULL,
                wave_height_m REAL,
                wave_period_sec REAL,
                wave_spectrum TEXT,
                wave_class TEXT,
                wave_seed INTEGER,
                simulation_hash TEXT,
                created_at TEXT DEFAULT CURRENT_TIMESTAMP
            )
        """
        )

        # WEC-Grid integration mapping
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS wec_integrations (
                integration_id INTEGER PRIMARY KEY AUTOINCREMENT,
                grid_sim_id INTEGER NOT NULL,
                wec_sim_id INTEGER NOT NULL,
                farm_name TEXT NOT NULL,
                bus_location INTEGER NOT NULL,
                num_devices INTEGER NOT NULL,
                created_at TEXT DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE,
                FOREIGN KEY (wec_sim_id) REFERENCES wec_simulations(wec_sim_id) ON DELETE CASCADE
            )
        """
        )

        # ================================================================
        # PSS®E-SPECIFIC TABLES (GridState Schema Alignment)
        # ================================================================

        # PSS®E Bus Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS psse_bus_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                bus INTEGER NOT NULL,
                bus_name TEXT,
                type TEXT,
                p REAL,
                q REAL,
                v_mag REAL,
                angle_deg REAL,
                vbase REAL,
                PRIMARY KEY (grid_sim_id, timestamp, bus),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # PSS®E Generator Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS psse_generator_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                gen INTEGER NOT NULL,
                gen_name TEXT,
                bus INTEGER NOT NULL,
                p REAL,
                q REAL,
                mbase REAL,
                status INTEGER,
                PRIMARY KEY (grid_sim_id, timestamp, gen),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # PSS®E Load Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS psse_load_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                load INTEGER NOT NULL,
                load_name TEXT,
                bus INTEGER NOT NULL,
                p REAL,
                q REAL,
                status INTEGER,
                PRIMARY KEY (grid_sim_id, timestamp, load),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # PSS®E Line Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS psse_line_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                line INTEGER NOT NULL,
                line_name TEXT,
                ibus INTEGER NOT NULL,
                jbus INTEGER NOT NULL,
                line_pct REAL,
                status INTEGER,
                PRIMARY KEY (grid_sim_id, timestamp, line),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # ================================================================
        # PyPSA-SPECIFIC TABLES (Identical to PSS®E for Cross-Platform Comparison)
        # ================================================================

        # PyPSA Bus Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS pypsa_bus_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                bus INTEGER NOT NULL,
                bus_name TEXT,
                type TEXT,
                p REAL,
                q REAL,
                v_mag REAL,
                angle_deg REAL,
                vbase REAL,
                PRIMARY KEY (grid_sim_id, timestamp, bus),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # PyPSA Generator Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS pypsa_generator_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                gen INTEGER NOT NULL,
                gen_name TEXT,
                bus INTEGER NOT NULL,
                p REAL,
                q REAL,
                mbase REAL,
                status INTEGER,
                PRIMARY KEY (grid_sim_id, timestamp, gen),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # PyPSA Load Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS pypsa_load_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                load INTEGER NOT NULL,
                load_name TEXT,
                bus INTEGER NOT NULL,
                p REAL,
                q REAL,
                status INTEGER,
                PRIMARY KEY (grid_sim_id, timestamp, load),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # PyPSA Line Results
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS pypsa_line_results (
                grid_sim_id INTEGER NOT NULL,
                timestamp TEXT NOT NULL,
                line INTEGER NOT NULL,
                line_name TEXT,
                ibus INTEGER NOT NULL,
                jbus INTEGER NOT NULL,
                line_pct REAL,
                status INTEGER,
                PRIMARY KEY (grid_sim_id, timestamp, line),
                FOREIGN KEY (grid_sim_id) REFERENCES grid_simulations(grid_sim_id) ON DELETE CASCADE
            )
        """
        )

        # ================================================================
        # WEC TIME-SERIES DATA
        # ================================================================

        # WEC Power Results (High-Resolution Time Series)
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS wec_power_results (
                wec_sim_id INTEGER NOT NULL,
                time_sec REAL NOT NULL,
                device_index INTEGER NOT NULL,
                p_w REAL,
                q_var REAL,
                wave_elevation_m REAL,
                PRIMARY KEY (wec_sim_id, time_sec, device_index),
                FOREIGN KEY (wec_sim_id) REFERENCES wec_simulations(wec_sim_id) ON DELETE CASCADE
            )
        """
        )

        # ================================================================
        # PERFORMANCE INDEXES
        # ================================================================

        # Grid simulation indexes
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_grid_sim_time ON grid_simulations(sim_start_time)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_grid_sim_case ON grid_simulations(case_name)"
        )

        # PSS®E result indexes
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_psse_bus_time ON psse_bus_results(grid_sim_id, timestamp)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_psse_gen_time ON psse_generator_results(grid_sim_id, timestamp)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_psse_load_time ON psse_load_results(grid_sim_id, timestamp)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_psse_line_time ON psse_line_results(grid_sim_id, timestamp)"
        )

        # PyPSA result indexes
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_pypsa_bus_time ON pypsa_bus_results(grid_sim_id, timestamp)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_pypsa_gen_time ON pypsa_generator_results(grid_sim_id, timestamp)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_pypsa_load_time ON pypsa_load_results(grid_sim_id, timestamp)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_pypsa_line_time ON pypsa_line_results(grid_sim_id, timestamp)"
        )

        # WEC time-series indexes
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_wec_power_time ON wec_power_results(wec_sim_id, time_sec)"
        )
        cursor.execute(
            "CREATE INDEX IF NOT EXISTS idx_wec_integration ON wec_integrations(grid_sim_id, wec_sim_id)"
        )

pull_sim(grid_sim_id, software=None)

Pull simulation data from database and reconstruct GridState object.

Retrieves all time-series data for a specific simulation and recreates the GridState object with both snapshot data and time-series history.

Parameters:

Name Type Description Default
grid_sim_id int

Grid simulation ID to retrieve.

required
software str

Software backend to pull data for ("psse" or "pypsa"). If None, automatically detects which software was used based on grid_simulations table flags.

None

Returns:

Name Type Description
GridState

Reconstructed GridState object with time-series data.

Raises:

Type Description
ValueError

If grid_sim_id not found or software not available for this simulation.

Example

Pull PSS®E simulation data

grid_state = engine.database.pull_sim(grid_sim_id=123, software="psse") print(f"Buses: {len(grid_state.bus)}") print(f"Time series data: {list(grid_state.bus_t.keys())}")

Auto-detect software and pull data

grid_state = engine.database.pull_sim(grid_sim_id=123)

Source code in src/wecgrid/util/database.py
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
def pull_sim(self, grid_sim_id: int, software: str = None):
    """Pull simulation data from database and reconstruct GridState object.

    Retrieves all time-series data for a specific simulation and recreates
    the GridState object with both snapshot data and time-series history.

    Args:
        grid_sim_id (int): Grid simulation ID to retrieve.
        software (str, optional): Software backend to pull data for ("psse" or "pypsa").
            If None, automatically detects which software was used based on
            grid_simulations table flags.

    Returns:
        GridState: Reconstructed GridState object with time-series data.

    Raises:
        ValueError: If grid_sim_id not found or software not available for this simulation.

    Example:
        >>> # Pull PSS®E simulation data
        >>> grid_state = engine.database.pull_sim(grid_sim_id=123, software="psse")
        >>> print(f"Buses: {len(grid_state.bus)}")
        >>> print(f"Time series data: {list(grid_state.bus_t.keys())}")

        >>> # Auto-detect software and pull data
        >>> grid_state = engine.database.pull_sim(grid_sim_id=123)
    """
    # Import here to avoid circular import
    from ..modelers.power_system.base import GridState, AttrDict

    # Get simulation metadata
    sim_info = self.query(
        "SELECT * FROM grid_simulations WHERE grid_sim_id = ?",
        params=(grid_sim_id,),
        return_type="df",
    )

    if sim_info.empty:
        raise ValueError(f"Simulation with ID {grid_sim_id} not found in database")

    sim_row = sim_info.iloc[0]

    # Auto-detect software if not specified
    if software is None:
        if sim_row["psse"]:
            software = "psse"
        elif sim_row["pypsa"]:
            software = "pypsa"
        else:
            raise ValueError(
                f"No software backend data found for simulation {grid_sim_id}"
            )

    # Validate software choice
    if software not in ["psse", "pypsa"]:
        raise ValueError(
            f"Invalid software: '{software}'. Must be 'psse' or 'pypsa'."
        )

    if software == "psse" and not sim_row["psse"]:
        raise ValueError(f"PSS®E data not available for simulation {grid_sim_id}")
    if software == "pypsa" and not sim_row["pypsa"]:
        raise ValueError(f"PyPSA data not available for simulation {grid_sim_id}")

    # Create GridState object
    grid_state = GridState(software=software)

    # Set case name from database metadata
    grid_state.case = sim_row["case_name"]

    # Table prefix for this software
    table_prefix = f"{software}_"
    table_prefix = f"{software}_"

    # Pull bus data
    bus_data = self.query(
        f"""
        SELECT * FROM {table_prefix}bus_results 
        WHERE grid_sim_id = ? 
        ORDER BY timestamp, bus
    """,
        params=(grid_sim_id,),
        return_type="df",
    )

    # Pull generator data
    gen_data = self.query(
        f"""
        SELECT * FROM {table_prefix}generator_results 
        WHERE grid_sim_id = ? 
        ORDER BY timestamp, gen
    """,
        params=(grid_sim_id,),
        return_type="df",
    )

    # Pull load data
    load_data = self.query(
        f"""
        SELECT * FROM {table_prefix}load_results 
        WHERE grid_sim_id = ? 
        ORDER BY timestamp, load
    """,
        params=(grid_sim_id,),
        return_type="df",
    )

    # Pull line data
    line_data = self.query(
        f"""
        SELECT * FROM {table_prefix}line_results 
        WHERE grid_sim_id = ? 
        ORDER BY timestamp, line
    """,
        params=(grid_sim_id,),
        return_type="df",
    )

    # Convert timestamp strings to pandas timestamps
    if not bus_data.empty:
        bus_data["timestamp"] = pd.to_datetime(bus_data["timestamp"])
    if not gen_data.empty:
        gen_data["timestamp"] = pd.to_datetime(gen_data["timestamp"])
    if not load_data.empty:
        load_data["timestamp"] = pd.to_datetime(load_data["timestamp"])
    if not line_data.empty:
        line_data["timestamp"] = pd.to_datetime(line_data["timestamp"])

    # Reconstruct current snapshot data (use latest timestamp)
    if not bus_data.empty:
        latest_time = bus_data["timestamp"].max()
        latest_bus = bus_data[bus_data["timestamp"] == latest_time].copy()
        latest_bus.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
        latest_bus.reset_index(drop=True, inplace=True)
        # Ensure clean column headers
        latest_bus.columns.name = None
        latest_bus.index.name = None
        latest_bus.attrs["df_type"] = "BUS"
        grid_state.bus = latest_bus

    if not gen_data.empty:
        latest_time = gen_data["timestamp"].max()
        latest_gen = gen_data[gen_data["timestamp"] == latest_time].copy()
        latest_gen.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
        latest_gen.reset_index(drop=True, inplace=True)
        # Ensure clean column headers
        latest_gen.columns.name = None
        latest_gen.index.name = None
        latest_gen.attrs["df_type"] = "GEN"
        grid_state.gen = latest_gen

    if not load_data.empty:
        latest_time = load_data["timestamp"].max()
        latest_load = load_data[load_data["timestamp"] == latest_time].copy()
        latest_load.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
        latest_load.reset_index(drop=True, inplace=True)
        # Ensure clean column headers
        latest_load.columns.name = None
        latest_load.index.name = None
        latest_load.attrs["df_type"] = "LOAD"
        grid_state.load = latest_load

    if not line_data.empty:
        latest_time = line_data["timestamp"].max()
        latest_line = line_data[line_data["timestamp"] == latest_time].copy()
        latest_line.drop(columns=["grid_sim_id", "timestamp"], inplace=True)
        latest_line.reset_index(drop=True, inplace=True)
        # Ensure clean column headers
        latest_line.columns.name = None
        latest_line.index.name = None
        latest_line.attrs["df_type"] = "LINE"
        grid_state.line = latest_line

    # Reconstruct time-series data
    def _reconstruct_timeseries(data_df, id_col, component_type):
        """Helper function to reconstruct time-series data for a component type."""
        if data_df.empty:
            return AttrDict()

        ts_data = AttrDict()

        # Get all variable columns (exclude metadata columns)
        exclude_cols = {
            "grid_sim_id",
            "timestamp",
            id_col,
            f"{component_type}_name",
        }
        if component_type == "bus":
            exclude_cols.update(
                {"bus_name", "vbase"}
            )  # Updated to include bus_name
        elif component_type == "gen":
            exclude_cols.update(
                {"gen_name", "mbase"}
            )  # Updated to include gen_name
        elif component_type == "line":
            exclude_cols.update(
                {"line_name", "ibus", "jbus"}
            )  # Updated to include line_name
        elif component_type == "load":
            exclude_cols.add("load_name")  # Added load_name

        var_cols = [col for col in data_df.columns if col not in exclude_cols]

        # For each variable, create a time-series DataFrame
        for var in var_cols:
            if f"{component_type}_name" not in data_df.columns:
                # Fallback: use component IDs as column names
                pivot_data = data_df.pivot(
                    index="timestamp", columns=id_col, values=var
                )
            else:
                # Pivot data to have timestamps as rows and component names as columns
                pivot_data = data_df.pivot(
                    index="timestamp", columns=f"{component_type}_name", values=var
                )

            # Clean up column headers
            pivot_data.columns.name = None
            pivot_data.index.name = None
            ts_data[var] = pivot_data

        return ts_data

    # Reconstruct time-series for each component type
    grid_state.bus_t = _reconstruct_timeseries(bus_data, "bus", "bus")
    grid_state.gen_t = _reconstruct_timeseries(gen_data, "gen", "gen")
    grid_state.load_t = _reconstruct_timeseries(load_data, "load", "load")
    grid_state.line_t = _reconstruct_timeseries(line_data, "line", "line")

    print(
        f"GridState reconstructed: {sim_row['case_name']} ({software.upper()}) - "
        f"{len(grid_state.bus)} buses, {len(grid_state.gen)} generators"
    )

    return grid_state

query(sql, params=None, return_type='raw')

Execute SQL query with flexible result formatting.

Parameters:

Name Type Description Default
sql str

SQL query string.

required
params tuple

Query parameters for safe substitution.

None
return_type str

Format for results - 'raw', 'df', or 'dict'.

'raw'

Returns:

Type Description

Results in specified format:

  • 'raw': List of tuples (default SQLite format)
  • 'df': pandas DataFrame with column names
  • 'dict': List of dictionaries with column names as keys
Example

db.query("SELECT * FROM grid_simulations WHERE case_name = ?", ... params=("IEEE_14_bus",), return_type="df")

Source code in src/wecgrid/util/database.py
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
def query(self, sql: str, params: tuple = None, return_type: str = "raw"):
    """Execute SQL query with flexible result formatting.

    Args:
        sql (str): SQL query string.
        params (tuple, optional): Query parameters for safe substitution.
        return_type (str): Format for results - 'raw', 'df', or 'dict'.

    Returns:
        Results in specified format:
        - 'raw': List of tuples (default SQLite format)
        - 'df': pandas DataFrame with column names
        - 'dict': List of dictionaries with column names as keys

    Example:
        >>> db.query("SELECT * FROM grid_simulations WHERE case_name = ?",
        ...           params=("IEEE_14_bus",), return_type="df")
    """
    with self.connection() as conn:
        cursor = conn.cursor()
        cursor.execute(sql, params or ())
        result = cursor.fetchall()

        if return_type == "df":
            columns = [desc[0] for desc in cursor.description]
            return pd.DataFrame(result, columns=columns)
        elif return_type == "dict":
            columns = [desc[0] for desc in cursor.description]
            return [dict(zip(columns, row)) for row in result]
        elif return_type == "raw":
            return result
        else:
            raise ValueError(
                f"Invalid return_type '{return_type}'. Must be 'raw', 'df', or 'dict'."
            )

save_sim(sim_name, notes=None)

Save simulation data for all available software backends in the engine.

Automatically detects and stores data from all active software backends (PSS®E, PyPSA) and WEC farms present in the engine object.

Always creates a new simulation entry - no duplicate checking. Users can manage simulation names as needed.

Parameters:

Name Type Description Default
sim_name str

User-friendly simulation name.

required
notes str

Simulation notes.

None

Returns:

Name Type Description
int int

grid_sim_id of the created simulation.

Example

sim_id = engine.database.save_sim( ... sim_name="IEEE 30 test", ... notes="testing the database" ... )

Source code in src/wecgrid/util/database.py
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
def save_sim(self, sim_name: str, notes: str = None) -> int:
    """Save simulation data for all available software backends in the engine.

    Automatically detects and stores data from all active software backends
    (PSS®E, PyPSA) and WEC farms present in the engine object.

    Always creates a new simulation entry - no duplicate checking.
    Users can manage simulation names as needed.

    Args:
        sim_name (str): User-friendly simulation name.
        notes (str, optional): Simulation notes.

    Returns:
        int: grid_sim_id of the created simulation.

    Example:
        >>> sim_id = engine.database.save_sim(
        ...     sim_name="IEEE 30 test",
        ...     notes="testing the database"
        ... )
    """
    # Gather all available software objects from engine
    softwares = []

    # Check for PSS®E
    if hasattr(self.engine, "psse") and hasattr(self.engine.psse, "grid"):
        softwares.append(self.engine.psse.grid)

    # Check for PyPSA
    if hasattr(self.engine, "pypsa") and hasattr(self.engine.pypsa, "grid"):
        softwares.append(self.engine.pypsa.grid)

    if not softwares:
        raise ValueError(
            "No software backends found in engine. Ensure PSS®E or PyPSA models are loaded."
        )

    # Get case name from engine
    case_name = getattr(self.engine, "case_name", "Unknown_Case")

    # Get time manager from engine
    timeManager = getattr(self.engine, "time", None)
    if timeManager is None:
        raise ValueError(
            "No time manager found in engine. Ensure engine.time is properly initialized."
        )

    # Extract software flags and determine sbase
    psse_used = False
    pypsa_used = False
    sbase_mva = None

    for i, software_obj in enumerate(softwares):
        software_name = getattr(software_obj, "software", "")

        software_name = software_name.lower() if software_name else ""

        if software_name == "psse":
            psse_used = True
        elif software_name == "pypsa":
            pypsa_used = True
        else:
            continue  # Skip this software object instead of processing it

        # Get sbase from the first software object
        if sbase_mva is None:
            if hasattr(software_obj, "sbase"):
                sbase_mva = software_obj.sbase
            else:
                # Try to get from parent object
                parent = getattr(software_obj, "_parent", None)
                if parent and hasattr(parent, "sbase"):
                    sbase_mva = parent.sbase
                else:
                    sbase_mva = 100.0  # Default fallback

    # Get time information from simulation
    sim_start_time = timeManager.start_time.isoformat()
    sim_end_time = getattr(timeManager, "sim_stop", None)
    if sim_end_time:
        sim_end_time = sim_end_time.isoformat()
    delta_time = timeManager.delta_time

    # Create new grid simulation record (always create new entry)
    with self.connection() as conn:
        cursor = conn.cursor()

        # Insert new simulation - always create new entry
        cursor.execute(
            """
            INSERT INTO grid_simulations 
            (sim_name, case_name, psse, pypsa, sbase_mva, sim_start_time, 
             sim_end_time, delta_time, notes)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        """,
            (
                sim_name,
                case_name,
                psse_used,
                pypsa_used,
                sbase_mva,
                sim_start_time,
                sim_end_time,
                delta_time,
                notes,
            ),
        )

        grid_sim_id = cursor.lastrowid

    # Store data for each valid software
    valid_softwares = []
    for software_obj in softwares:
        software_name = getattr(software_obj, "software", "").lower()

        # Only process valid software names
        if software_name in ["psse", "pypsa"]:
            valid_softwares.append((software_obj, software_name))

    for software_obj, software_name in valid_softwares:
        # Store all time-series data from GridState
        self._store_all_gridstate_timeseries(
            grid_sim_id, software_obj, software_name, timeManager
        )

    # Store WEC farm data if available
    if hasattr(self.engine, "wec_farms") and self.engine.wec_farms:
        self._store_wec_farm_data(grid_sim_id)

    # Create summary of used software
    used_software = []
    if psse_used:
        used_software.append("PSS®E")
    if pypsa_used:
        used_software.append("PyPSA")

    print(f"Simulation saved: ID {grid_sim_id} - {sim_name}")

    return grid_sim_id

set_database_path(db_path)

Set database path and reinitialize connection.

Parameters:

Name Type Description Default
db_path str

Path to WEC-GRID database file.

required
Example

engine.database.set_database_path("/path/to/wecgrid-database/WEC-GRID.db")

Source code in src/wecgrid/util/database.py
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
def set_database_path(self, db_path):
    """Set database path and reinitialize connection.

    Args:
        db_path (str): Path to WEC-GRID database file.

    Example:
        >>> engine.database.set_database_path("/path/to/wecgrid-database/WEC-GRID.db")
    """
    if not os.path.exists(db_path):
        raise FileNotFoundError(f"Database file not found: {db_path}")

    # Save to config
    save_database_config(db_path)

    # Update current instance
    self.db_path = str(Path(db_path).absolute())

    # Reinitialize
    self.check_and_initialize()

    return self.db_path

store_gridstate_data(grid_sim_id, timestamp, grid_state, software)

Store GridState data to appropriate software-specific tables.

Parameters:

Name Type Description Default
grid_sim_id int

Grid simulation ID.

required
timestamp str

ISO datetime string for this snapshot.

required
grid_state

GridState object with bus, gen, load, line DataFrames.

required
software str

Software backend - "PSSE" or "PyPSA".

required
Example

db.store_gridstate_data( ... grid_sim_id=123, ... timestamp="2025-08-14T10:05:00", ... grid_state=my_grid_state, ... software="PSSE" ... )

Source code in src/wecgrid/util/database.py
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
def store_gridstate_data(
    self, grid_sim_id: int, timestamp: str, grid_state, software: str
):
    """Store GridState data to appropriate software-specific tables.

    Args:
        grid_sim_id (int): Grid simulation ID.
        timestamp (str): ISO datetime string for this snapshot.
        grid_state: GridState object with bus, gen, load, line DataFrames.
        software (str): Software backend - "PSSE" or "PyPSA".

    Example:
        >>> db.store_gridstate_data(
        ...     grid_sim_id=123,
        ...     timestamp="2025-08-14T10:05:00",
        ...     grid_state=my_grid_state,
        ...     software="PSSE"
        ... )
    """
    software = software.lower()
    table_prefix = f"{software}_"

    with self.connection() as conn:
        cursor = conn.cursor()

        # Store bus results
        if not grid_state.bus.empty:
            for bus_id, row in grid_state.bus.iterrows():
                cursor.execute(
                    f"""
                    INSERT OR REPLACE INTO {table_prefix}bus_results 
                    (grid_sim_id, timestamp, bus, bus_name, type, p, q, v_mag, angle_deg, vbase)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                """,
                    (
                        grid_sim_id,
                        timestamp,
                        bus_id,
                        row.get("bus_name"),
                        row.get("type"),
                        row.get("p"),
                        row.get("q"),
                        row.get("v_mag"),
                        row.get("angle_deg"),
                        row.get("vbase"),
                    ),
                )

        # Store generator results
        if not grid_state.gen.empty:
            for gen_id, row in grid_state.gen.iterrows():
                cursor.execute(
                    f"""
                    INSERT OR REPLACE INTO {table_prefix}generator_results 
                    (grid_sim_id, timestamp, gen, gen_name, bus, p, q, mbase, status)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
                """,
                    (
                        grid_sim_id,
                        timestamp,
                        gen_id,
                        row.get("gen_name"),
                        row.get("bus"),
                        row.get("p"),
                        row.get("q"),
                        row.get("Mbase"),
                        row.get("status"),
                    ),
                )

        # Store load results
        if not grid_state.load.empty:
            for load_id, row in grid_state.load.iterrows():
                cursor.execute(
                    f"""
                    INSERT OR REPLACE INTO {table_prefix}load_results 
                    (grid_sim_id, timestamp, load, load_name, bus, p, q, status)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                """,
                    (
                        grid_sim_id,
                        timestamp,
                        load_id,
                        row.get("load_name"),
                        row.get("bus"),
                        row.get("p"),
                        row.get("q"),
                        row.get("status"),
                    ),
                )

        # Store line results
        if not grid_state.line.empty:
            for line_id, row in grid_state.line.iterrows():
                cursor.execute(
                    f"""
                    INSERT OR REPLACE INTO {table_prefix}line_results 
                    (grid_sim_id, timestamp, line, line_name, ibus, jbus, line_pct, status)
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                """,
                    (
                        grid_sim_id,
                        timestamp,
                        line_id,
                        row.get("line_name"),
                        row.get("ibus"),
                        row.get("jbus"),
                        row.get("line_pct"),
                        row.get("status"),
                    ),
                )

wecsim_runs()

Get all WEC simulation metadata with enhanced wave parameters.

Returns:

Type Description
DataFrame

pd.DataFrame: WEC simulations with parameters and wave conditions including wave spectrum type, wave class, and all simulation parameters.

Example

engine.database.wecsim_runs() wec_sim_id model_type sim_duration_sec delta_time wave_spectrum wave_class ... 0 1 RM3 600.0 0.1 PM irregular ...

Source code in src/wecgrid/util/database.py
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
def wecsim_runs(self) -> pd.DataFrame:
    """Get all WEC simulation metadata with enhanced wave parameters.

    Returns:
        pd.DataFrame: WEC simulations with parameters and wave conditions including
            wave spectrum type, wave class, and all simulation parameters.

    Example:
        >>> engine.database.wecsim_runs()
           wec_sim_id model_type  sim_duration_sec  delta_time  wave_spectrum  wave_class  ...
        0          1       RM3             600.0        0.1             PM   irregular  ...
    """
    return self.query(
        """
        SELECT wec_sim_id, model_type, sim_duration_sec, delta_time,
               wave_height_m, wave_period_sec, wave_spectrum, wave_class, wave_seed,
               simulation_hash, created_at
        FROM wec_simulations 
        ORDER BY created_at DESC
    """,
        return_type="df",
    )