Skip to content
Snippets Groups Projects
Commit e159c704 authored by Matthias Boljen's avatar Matthias Boljen
Browse files

Add option widths

parent a09c060f
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,10 @@ If output file `OUTFILE` is undefined, the output is directed to `STDOUT`.
Set the filename of the input spreadsheet.
- **-w**, **-\-width** *WIDTHS*
Set comma-separated list of column widths of the input spreadsheet if auto-detection fails.
- **-m**, **-\-method** *METHOD*
Set the interpolation method. Select one of the following: `linear` (default), `nearest`, `cubic`. The following options are available for 2D data only: `nearest-up`, `zero`, `slinear`, `quadratic`, `previous`, `next`.
......@@ -65,44 +69,44 @@ If output file `OUTFILE` is undefined, the output is directed to `STDOUT`.
Assume the following input file `testdata2.csv` containing scattered data points in 2D:
```text
x y
0.005 0.615
0.014 2.935
0.019 4.712
0.038 13.814
0.064 24.658
0.076 29.594
0.095 39.155
0.125 59.091
0.145 77.677
X Y
0.005 0.615
0.014 2.935
0.019 4.712
0.038 13.814
0.064 24.658
0.076 29.594
0.095 39.155
0.125 59.091
0.145 77.677
```
The following command will parse the file above and produce regular gridded data points using cubic spline interpolation along the first column in the interval `[0.0,0.15]` using `15` steps, i.e. every increment of `0.01` a data point is interpolated in the second column.
```bash
$ mkgrid --infile testdata2.csv --range 0:0.15 --steps 15 --method cubic
$ mkgrid -i testdata2.csv -r 0:0.15 -s 15 -m cubic
```
The output will look like the following:
```text
x y
0.000 -0.297
0.010 1.780
0.020 5.116
0.030 9.812
0.040 14.762
0.050 19.119
0.060 23.094
0.070 27.051
0.080 31.418
0.090 36.432
0.100 42.011
0.110 48.205
0.120 55.215
0.130 63.249
0.140 72.516
0.150 83.224
X Y
0.000 -0.297
0.010 1.780
0.020 5.116
0.030 9.812
0.040 14.762
0.050 19.119
0.060 23.094
0.070 27.051
0.080 31.418
0.090 36.432
0.100 42.011
0.110 48.205
0.120 55.215
0.130 63.249
0.140 72.516
0.150 83.224
```
An example plot for the above result would look like this:
......@@ -114,69 +118,74 @@ An example plot for the above result would look like this:
Assume the following input file `testdata3.csv` containing scattered data points in 3D:
```text
x y z
0.000 0.000 0.000
0.019 0.000 4.712
0.042 0.000 15.976
0.092 0.000 37.076
0.109 0.000 47.519
0.136 0.000 68.049
0.009 0.001 2.413
0.026 0.005 11.605
0.094 0.032 43.647
0.106 0.037 51.925
0.134 0.052 76.073
0.004 0.004 2.127
0.025 0.026 17.043
0.059 0.061 30.732
0.093 0.095 52.012
0.102 0.103 59.315
0.113 0.113 68.501
0.132 0.132 87.946
0.003 0.009 2.752
0.007 0.021 8.035
0.011 0.033 12.049
0.022 0.066 18.954
0.040 0.109 27.913
0.046 0.122 31.549
0.052 0.132 34.542
0.000 0.023 5.569
0.000 0.072 11.650
0.000 0.091 13.370
0.000 0.115 15.056
0.000 0.134 16.302
X Y Z
0.000 0.000 0.000
0.019 0.000 4.712
0.042 0.000 15.976
0.092 0.000 37.076
0.109 0.000 47.519
0.136 0.000 68.049
0.009 0.001 2.413
0.026 0.005 11.605
0.094 0.032 43.647
0.106 0.037 51.925
0.134 0.052 76.073
0.004 0.004 2.127
0.025 0.026 17.043
0.059 0.061 30.732
0.093 0.095 52.012
0.102 0.103 59.315
0.113 0.113 68.501
0.132 0.132 87.946
0.003 0.009 2.752
0.007 0.021 8.035
0.011 0.033 12.049
0.022 0.066 18.954
0.040 0.109 27.913
0.046 0.122 31.549
0.052 0.132 34.542
0.000 0.023 5.569
0.000 0.072 11.650
0.000 0.091 13.370
0.000 0.115 15.056
0.000 0.134 16.302
```
The following command will parse the above input file and creates regular gridded data points along data columns 1 and 2 within the interval `[0.0,0.13]` in both dimensions. The corresponding data in column 3 will be interpolated using `13` steps, i.e. `(13+1)×(13+1)=196` records will be created in total.
```bash
$ mkgrid --infile testdata3.csv --range 0:0.13,0:0.13 --steps 13
$ mkgrid -i testdata3.csv -r 0:0.13,0:0.13 -s 13
```
The output will look like the following:
```text
x y z
0.000 0.000 0.000
0.010 0.000 2.480
0.020 0.000 5.202
0.030 0.000 10.099
0.040 0.000 14.997
0.050 0.000 19.352
0.060 0.000 23.572
0.070 0.000 27.792
0.080 0.000 32.012
0.090 0.000 36.232
0.100 0.000 41.990
0.110 0.000 48.279
0.120 0.000 55.883
0.130 0.000 63.487
0.000 0.010 2.421
0.010 0.010 5.438
0.020 0.010 10.451
0.030 0.010 14.423
0.040 0.010 17.364
0.050 0.010 20.807
X Y Z
0.000 0.000 0.000
0.010 0.000 2.480
0.020 0.000 5.202
0.030 0.000 10.099
0.040 0.000 14.997
0.050 0.000 19.352
0.060 0.000 23.572
0.070 0.000 27.792
0.080 0.000 32.012
0.090 0.000 36.232
0.100 0.000 41.990
0.110 0.000 48.279
0.120 0.000 55.883
0.130 0.000 63.487
0.000 0.010 2.421
0.010 0.010 5.438
0.020 0.010 10.451
0.030 0.010 14.423
0.040 0.010 17.364
0.050 0.010 20.807
[...]
```
An example plot for the above would look like this:
......@@ -188,14 +197,14 @@ An example plot for the above would look like this:
Assume an input file `testdata4.csv` containing 4 columns of scattered input data. In this example, columns 1-3 describe spatial coordinates on the surface of a sphere wheras column 4 holds the surface temperature of that point:
```text
x y z T
0.000 0.000 10.000 0.0
2.588 0.000 9.659 7.8
5.000 0.000 8.660 15.0
7.071 0.000 7.071 21.2
8.660 0.000 5.000 26.0
9.659 0.000 2.588 29.0
10.000 0.000 0.000 30.0
X Y Z T
0.000 0.000 10.000 0.0
2.588 0.000 9.659 7.8
5.000 0.000 8.660 15.0
7.071 0.000 7.071 21.2
8.660 0.000 5.000 26.0
9.659 0.000 2.588 29.0
10.000 0.000 0.000 30.0
[...]
```
......
#!/usr/bin/env python3
#
# Created: Mon 2024-07-15 18:40:27 CEST (boljen)
# Modified: So 2024-07-21 12:10:54 CEST (boljen)
# Modified: Tue 2024-08-06 20:17:12 CEST (boljen)
#
# mkgrid:
# Convert scattered data to regular gridded data in 2D, 3D and 4D
......@@ -42,14 +42,15 @@ def execute_main():
description='Convert scattered data to regular gridded data in 2D, 3D and 4D')
# Add options
parser.add_argument('-i', '--infile', help='Input filename', type=str, required=True)
parser.add_argument('-m', '--method', help='Interpolation method', type=str, default='linear', choices=method.keys())
parser.add_argument('-p', '--plot', help='Plot filename', nargs='?', const='***', type=str, default=None)
parser.add_argument('-u', '--usecols', help='Comma-separated list of data columns to use from input file', type=str, default=None)
parser.add_argument('-r', '--range', help='Comma-separated list of range specifiers for output', type=str, default=None)
parser.add_argument('-s', '--steps', help='Comma-separated list of discretization steps', type=str, default=None)
parser.add_argument('-y', '--yes', help='Overwrite existing output file', default=False, action=argparse.BooleanOptionalAction)
parser.add_argument('outfile', help='Output filename', nargs='?', type=str, default=sys.stdout.buffer)
parser.add_argument('-i', '--infile', help='Input filename', type=str, required=True)
parser.add_argument('-m', '--method', help='Interpolation method', type=str, default='linear', choices=method.keys())
parser.add_argument('-p', '--plot', help='Plot filename', nargs='?', const='***', type=str, default=None)
parser.add_argument('-u', '--usecols', help='Comma-separated list of data columns to use from input file', type=str, default=None)
parser.add_argument('-r', '--range', help='Comma-separated list of range specifiers for output', type=str, default=None)
parser.add_argument('-s', '--steps', help='Comma-separated list of discretization steps', type=str, default=None)
parser.add_argument('-w', '--widths', help='Comma-separated list of column widths', type=str, default=None)
parser.add_argument('-y', '--yes', help='Overwrite existing output file', default=False, action=argparse.BooleanOptionalAction)
parser.add_argument('outfile', help='Output filename', nargs='?', type=str, default=sys.stdout.buffer)
# Parse options
args = parser.parse_args()
......@@ -58,14 +59,33 @@ def execute_main():
if args.outfile != sys.stdout.buffer and os.path.exists(args.outfile) and not args.yes:
raise ValueError("File {:s} exists".format(args.outfile))
# Read input file
df = pd.read_fwf(args.infile)
if args.widths is None:
# Initialize list
widths = None
else:
# Split widths string in list of comma-seperated tokens
widths = [ x.strip() for x in args.widths.split(',') ]
# Loop over tokens
for i,x in enumerate(widths):
# Assert boundary values are floating-point data
try:
widths[i] = int(x)
except:
raise ValueError('Invalid token {:s} in widths list'.format(x))
# Read from input file
df = pd.read_fwf(args.infile, widths=widths, comment="#", skip_empty_rows=True)
data = df.to_dict('list')
# Initialize column list
usecols = None
# Parse --usecols
# Parse usecols argument
if args.usecols is None:
# Use all available columns unless otherwise defined
......@@ -75,6 +95,7 @@ def execute_main():
# Use columns specified by comma-separated list only
usecols = [ x.strip() for x in args.usecols.split(',') ]
# Convert strings to integers
for i,x in enumerate(usecols):
try:
......@@ -99,7 +120,7 @@ def execute_main():
# Initialize range lists
myrange = []
# Parse --range
# Parse range argument
if args.range is not None:
# Split comma-separated list of values
......@@ -154,8 +175,12 @@ def execute_main():
except:
raise ValueError('Invalid token {:s} in steps list'.format(x))
# Initialize labels list
# Initialize labels and formats list
label = []
fmtlist = []
# Assemble header
headstr = ''
# Loop over columns
for i,x in enumerate(usecols):
......@@ -163,6 +188,20 @@ def execute_main():
# Assign column label
label.append(df.columns[usecols[i]])
# Auto-determine column width
width = max(len(label[-1]),12)
if widths is not None and widths[usecols[i]] > width:
width = widths[usecols[i]]
# Set format string
fmtstr = '%{:d}.{:d}f'.format(width,width-6)
fmtlist.append(fmtstr)
# Set header
if headstr != '': headstr += ' '
fmtstr = '{:>' + '{:d}s'.format(width) + '}'
headstr += fmtstr.format(df.columns[usecols[i]])
# Auto-detect array value
if i >= len(steps):
# Apply default or last element value
......@@ -254,14 +293,8 @@ def execute_main():
for i,j,k,l in np.nditer([xi,yi,zi,ci]):
xout.append([i,j,k,l])
# Assemble header
headstr = ''
for i in label:
if headstr != '': headstr += ' '
headstr += '{:>16s}'.format(i)
# Save regular gridded data to output file
np.savetxt(args.outfile, xout, header=headstr, comments='', fmt='%16.7f')
np.savetxt(args.outfile, xout, header=headstr, comments='', fmt=fmtlist)
# =================
# Create plot
......
......@@ -5,13 +5,13 @@ OUTFILES = $(addsuffix .out, testdata2 testdata3 testdata4)
default: $(OUTFILES)
testdata2.out: testdata2.csv
$(MKGRID) -y -i $< -p $(subst .csv,.png,$<) -s 15 -r 0:0.15 -m cubic $@
$(MKGRID) -y -i $< -p $(subst .csv,.png,$<) -s 15 -r 0:0.15 -m cubic -w 12,12 $@
testdata3.out: testdata3.csv
$(MKGRID) -y -i $< -p $(subst .csv,.png,$<) -s 13 -r 0:0.13,0:0.13 $@
$(MKGRID) -y -i $< -p $(subst .csv,.png,$<) -s 13 -r 0:0.13,0:0.13 -w 12,12,12 $@
testdata4.out: testdata4.csv
$(MKGRID) -y -i $< -p $(subst .csv,.png,$<) -s 10 -u 4,5,6,7 $@
$(MKGRID) -y -i $< -p $(subst .csv,.png,$<) -s 10 -u 4,5,6,7 -w 10,10,10,10,10,10,10 $@
clean:
$(RM) $(OUTFILES) $(subst .out,.png,$(OUTFILES))
......
x y
0.005 0.615
0.014 2.935
0.019 4.712
0.038 13.814
0.064 24.658
0.076 29.594
0.095 39.155
0.125 59.091
0.145 77.677
x y
0.005 0.615
0.014 2.935
0.019 4.712
0.038 13.814
0.064 24.658
0.076 29.594
0.095 39.155
0.125 59.091
0.145 77.677
x y z
0.000 0.000 0.000
0.019 0.000 4.712
0.042 0.000 15.976
0.092 0.000 37.076
0.109 0.000 47.519
0.136 0.000 68.049
0.009 0.001 2.413
0.026 0.005 11.605
0.094 0.032 43.647
0.106 0.037 51.925
0.134 0.052 76.073
0.004 0.004 2.127
0.025 0.026 17.043
0.059 0.061 30.732
0.093 0.095 52.012
0.102 0.103 59.315
0.113 0.113 68.501
0.132 0.132 87.946
0.003 0.009 2.752
0.007 0.021 8.035
0.011 0.033 12.049
0.022 0.066 18.954
0.040 0.109 27.913
0.046 0.122 31.549
0.052 0.132 34.542
0.000 0.023 5.569
0.000 0.072 11.650
0.000 0.091 13.370
0.000 0.115 15.056
0.000 0.134 16.302
x y z
0.000 0.000 0.000
0.019 0.000 4.712
0.042 0.000 15.976
0.092 0.000 37.076
0.109 0.000 47.519
0.136 0.000 68.049
0.009 0.001 2.413
0.026 0.005 11.605
0.094 0.032 43.647
0.106 0.037 51.925
0.134 0.052 76.073
0.004 0.004 2.127
0.025 0.026 17.043
0.059 0.061 30.732
0.093 0.095 52.012
0.102 0.103 59.315
0.113 0.113 68.501
0.132 0.132 87.946
0.003 0.009 2.752
0.007 0.021 8.035
0.011 0.033 12.049
0.022 0.066 18.954
0.040 0.109 27.913
0.046 0.122 31.549
0.052 0.132 34.542
0.000 0.023 5.569
0.000 0.072 11.650
0.000 0.091 13.370
0.000 0.115 15.056
0.000 0.134 16.302
This diff is collapsed.
test/testdata4.png

168 KiB | W: | H:

test/testdata4.png

166 KiB | W: | H:

test/testdata4.png
test/testdata4.png
test/testdata4.png
test/testdata4.png
  • 2-up
  • Swipe
  • Onion skin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment