GX Developer

Hi,

I am trying to load a csv into a new database that I have created but i seem to keep getting errors and the data won't load into the database file. I have all lines i want as a single CSV but I can't add it in.

I was wondering if anyone could help with this or knows what is causing it not to write to the database.

My code is below:

import pandas as pd
import numpy as np
from geosoft import gxpy
import geosoft.gxpy.gdb as gxdb
import os
import shutil

csv_path = r"D:\P110-2025\Database\Processed_database\All_Lines_Merged.csv"
output_gdb_path = r"D:\P110-2025\Database\Processed_database\Processed.gdb"
line_name = "Merged_Line"

# Remove old GDB
if os.path.exists(output_gdb_path):
if os.path.isdir(output_gdb_path):
shutil.rmtree(output_gdb_path)
else:
os.remove(output_gdb_path)

# Load CSV
print("Loading CSV...")
df = pd.read_csv(csv_path, dtype=str) # read all as string first

# Keep only numeric columns X, Y, Z
numeric_cols = ["X", "Y", "Z"]
df_numeric = df[numeric_cols].replace('*', np.nan)
df_numeric = df_numeric.apply(pd.to_numeric, errors='coerce')

# Convert to 2D NumPy array with float64 dtype
data = df_numeric.to_numpy(dtype=np.float64)
if data.ndim == 1:
data = data.reshape(-1, len(numeric_cols))

print(f"Prepared data: {data.shape[0]} points, {data.shape[1]} channels, dtype={data.dtype}")

# Write to GDB
with gxpy.gx.GXpy() as gxc:
with gxdb.Geosoft_gdb.new(output_gdb_path, overwrite=True) as gdb:
print(f"Created new output GDB at: {output_gdb_path}")
gdb.write_line(data, line_name)
print(f"✔ CSV loaded into GDB as line '{line_name}'")

Answers

  • Hello @SandTeam I’ve already encountered this kind of issue when importing a CSV into a Geosoft GDB. If the file imports but the data doesn’t go into the database, it’s often because the writing methods don’t correctly create the line structure and the channels in the new database.

    For it to work:

    You must first create the line in the database, then for each column you want to import (X, Y, Z for example), create an associated channel on this line.

    Then you write the data into each channel, column by column, making sure the format is numeric (float64) and without abnormal values (NaN or others).

    Finally, you check that deleting the previous GDB works properly to avoid conflicts or corruption.

    import pandas as pdimport numpy as npimport geosoft.gxpy.gdb as gxdbimport osimport shutilcsv_path = r"D:\P110-2025\Database\Processed_database\All_Lines_Merged.csv"output_gdb_path = r"D:\P110-2025\Database\Processed_database\Processed.gdb"line_name = "Merged_Line"# Clean previous databaseif os.path.exists(output_gdb_path):    if os.path.isdir(output_gdb_path):        shutil.rmtree(output_gdb_path)    else:        os.remove(output_gdb_path)# Load and prepare CSVdf = pd.read_csv(csv_path, dtype=str)numeric_cols = ["X", "Y", "Z"]df_numeric = df[numeric_cols].replace("*", np.nan)df_numeric = df_numeric.apply(pd.to_numeric, errors='coerce')data = df_numeric.to_numpy(dtype=np.float64)if data.ndim == 1:    data = data.reshape(-1, len(numeric_cols))# Create new database and import datawith gxdb.Geosoft_gdb.new(output_gdb_path, overwrite=True) as gdb:    line = gdb.new_line(line_name)    for i, channel_name in enumerate(numeric_cols):        ch = gxdb.Channel.new(line, channel_name)        ch.write(data[:, i])    print(f"CSV import successful into the database on line '{line_name}'")
    

    With this method, you truly import all the values, column by column, exactly where they need to go in the database. If you want to adapt it for more columns or include advanced checks, we can discuss it. Let me know if it works on your side!

    Igor