I have this working code to process a single file:
import pandas as pd
import pygmt
#import table
df = pd.read_table("file1.txt", sep=" ", names=['X', 'Y', 'Z'] )
#min/max
Xmin = df['X'].min()
Xmax = df['X'].max()
Ymin = df['Y'].min()
Ymax = df['Y'].max()
#print(Xmin, Xmax)
#print(Ymin, Ymax)
#gridding with pyGMT
grid = pygmt.surface(data=df, spacing=1, region=[Xmin, Xmax, Ymin, Ymax])
#print(grid)
#export
grid.to_netcdf('file1.nc')
Now I want to repeat this code for all *.txt files in a directory. How can I do that? I tried writing a loop like:
for file in glob.glob("*.txt"):
But how can I make the respective input (.txt) and output (.nc) have the same name?