Linear Interpolation of CSV Columnar Data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linear Interpolation of CSV Columnar Data
# 1  
Old 08-30-2019
Linear Interpolation of CSV Columnar Data

I am trying to perform linear interpolation on three columns in a CSV file.

When I run this code, I do not get any changes in the output (see Expected Output).

What am I doing wrong here?

Awk Code
Code:
'
    function interpolate(lat1, lon1, t1, lat2, lon2, t2,      i) {
        for (i = 1; i <= (t2 - t1); i++) {
            printf "%.2f,%.2f,%d\n",
                lat1 + i * (lat2 - lat1) / (t2 - t1),
                lon1 + i * (lon2 - lon1) / (t2 - t1),
                t1   + i
        }
    }
    NR <= 1 {print}
    NR >= 2 {interpolate(lat, lon, t, $1, $2, $3)}
    {lat = $1; lon = $2; t = $3}
' file.in.csv > file.out.csv

file.in.csv
22.70,-68.00,0
22.77,-68.13,1
22.83,-68.27,2
22.90,-68.40,3
22.97,-68.53,4
23.03,-68.67,5

Expected Output
22.70,-68.00,0
22.74,-68.07,0.5
22.77,-68.13,1
22.80,-68.20,1.5
22.83,-68.27,2
22.87,-68.34,2.5
22.90,-68.40,3
22.94,-68.47,3.5
22.97,-68.53,4
23.00,-68.60,4.5
23.03,-68.67,5
# 2  
Old 08-30-2019
try:
Code:
awk -F, '
function interpolate(lat1, lon1, t1, lat2, lon2, t2, p) {
for (i = 1; i <= (t2 - t1); i++) {
printf "%.2f,%.2f,%.1f\n",
lat1 + (t2 - t1) * (lat2 - lat1) / p,
lon1 + (t2 - t1) * (lon2 - lon1) / p,
t1 + i * (t2 - t1) / p
}
}
NR >= 2 {interpolate(lat, lon, t, $1, $2, $3, ++p); p=0;}
{lat = $1; lon = $2; t = $3; ++p}
{print}
' file.in.csv > file.out.csv


Last edited by RavinderSingh13; 02-28-2020 at 01:56 AM..
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-30-2019
Thank you! This is exactly what I was looking for.

Quote:
Originally Posted by rdrtx1
try:
Code:
awk -F, '
   function interpolate(lat1, lon1, t1, lat2, lon2, t2, p) {
       for (i = 1; i <= (t2 - t1); i++) {
           printf "%.2f,%.2f,%.1f\n",
              lat1 + (t2 - t1) * (lat2 - lat1) / p,
              lon1 + (t2 - t1) * (lon2 - lon1) / p,
              t1 + i * (t2 - t1) / p
       }
   }
   NR >= 2 {interpolate(lat, lon, t, $1, $2, $3, ++p); p=0;}
   {lat = $1; lon = $2; t = $3; ++p}
   {print}
' file.in.csv > file.out.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Joining Columnar heading from 2 lines

Hi, Below is the format of a report generated by a custom reporting solution. I opened the report in Notepad++ and junked data and values as in the image below. https://www.unix.com/attachment.php?attachmentid=7907&stc=1&d=1575507708 I want to convert the values to a PIPE delimited format as... (2 Replies)
Discussion started by: ramkrix
2 Replies

2. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

3. Programming

Find gaps in time data and replace missing time value and column 2 value by interpolation in awk

Dear all, I am kindly seeking assistance on the following issue. I am working with data that is sampled every 0.05 hours (that is 3 minutes intervals) here is a sample data from the file 5.00000 15.5030 5.05000 15.6680 5.10000 16.0100 5.15000 16.3450 5.20000 16.7120 5.25000... (4 Replies)
Discussion started by: malandisa
4 Replies

4. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

5. Programming

Linear hashing implementation in C language

Hi, I'm looking for linear hashing implementation in C language. Please help. PS: I have implement this on Ubuntu 10.04 Linux on 64 bit machine. (1 Reply)
Discussion started by: sajjar
1 Replies

6. Shell Programming and Scripting

File Comparison Columnar?

I'm looking to build up a process which would compare 2 files and show difference, the difference needs to be done in such a way that it also shows which column value is differing. So i think of this. Run a diff between the files , then someway find which columns have different values need to... (6 Replies)
Discussion started by: dinjo_jo
6 Replies

7. Shell Programming and Scripting

Formatting Data - CSV

I want to check whether if any column data has any + , - , = prefixed to it then convert it in such a form that in excel its not read as formula. echo "$DATA" | awk 'BEGIN { OFS="," } -F" " {print $1,$2,$3,$4,$5,$6,$7,$8.$9,$10,$11,$12}' (4 Replies)
Discussion started by: dinjo_jo
4 Replies

8. Shell Programming and Scripting

Linear data to column data..script help seeked

Hello Take a look at following lines. This is giving me an o/p all in one array where as i want the column to be printed.How can i do it? e.g I am getting: 1575028616...... whereas i want 1 5750 28616 I am writing this small piece and trying to get this column o/p in a CSV. ... (1 Reply)
Discussion started by: ak835
1 Replies

9. Shell Programming and Scripting

Converting to columnar output

Hi All, I need some help in script or awk to create a textfile. I have a directory where two subdirectories exists say A and B. Now I need to write a ".txt" file with well arranged(space wraped) three columns in it with the data as follows: 1st column:Serial number(which will increment... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

10. Programming

LInear Addresses

Hi all, Even after reading many explanation the question still haunting me what's the difference between physical and linear addresses.Can we directly access physical addresses .If not then paging circuitry would have ensure contiguous physical addresses regardless of any linear addresses but this... (2 Replies)
Discussion started by: joshighanshyam
2 Replies
Login or Register to Ask a Question