Search code examples
gnuplot

How to subset data with formatted time when writing to tables?


I have been trying with various formatting combinations in gnuplot (xdata,xrange,etc) methods but I can't get this to work: Suppose I have a 'wind.csv' file (a sample shown below the code). I want to write a file after certain manipulations, but only for those times that are between a certain start and end:

reset session
set datafile separator ","
fmt = '%Y-%m-%d %H:%M:%S'
START=strptime(fmt, "1975-08-14 00:00:00")
END=strptime(fmt, "1975-08-15 00:00:00")
set table "outfile.dat"
plot "wind_.csv" using 3:2:0:(sprintf(strftime(fmt ,timecolumn(1,fmt)))) with table if (($1 > START) && ($1 < END))
unset table

A sample data can be the following.

date,wind_speed,wind_direction
1975-08-13 22:00:00,4.1,23.0
1975-08-13 23:00:00,3.1,59.0
1975-08-14 00:00:00,5.2,30.0
1975-08-14 01:00:00,2.0,70.0
1975-08-14 02:00:00,5.7,87.0
1975-08-14 03:00:00,5.6,105.0
1975-08-14 04:00:00,5.8,116.0
1975-08-14 05:00:00,5.0,116.0
1975-08-14 06:00:00,4.5,123.0
1975-08-14 07:00:00,4.1,137.0
1975-08-14 08:00:00,3.6,151.0
1975-08-14 09:00:00,3.5,153.0
1975-08-14 10:00:00,3.5,180.0
1975-08-14 11:00:00,2.8,189.0
1975-08-14 12:00:00,1.7,183.0
1975-08-14 13:00:00,0.7,172.0
1975-08-14 14:00:00,0.4,252.0
1975-08-14 15:00:00,0.2,325.0
1975-08-14 16:00:00,0.6,53.0
1975-08-14 17:00:00,1.0,37.0
1975-08-14 18:00:00,0.7,73.0
1975-08-14 19:00:00,0.8,26.0
1975-08-14 20:00:00,1.8,56.0
1975-08-14 21:00:00,2.3,64.0
1975-08-14 22:00:00,1.9,67.0
1975-08-14 23:00:00,3.1,12.0
1975-08-15 00:00:00,2.8,73.0
1975-08-15 01:00:00,2.5,66.0
1975-08-15 02:00:00,3.0,83.0
1975-08-15 03:00:00,3.9,109.0
1975-08-15 04:00:00,3.9,109.0
1975-08-15 05:00:00,4.2,127.0
1975-08-15 06:00:00,3.6,146.0

I do not want to use awk or any outside system options for this, if possible.


Solution

  • My apologies. I indeed failed to scroll right to see that you were already using the "if" clause.

    These commands will select the entries you want:

    set datafile separator ","
    fmt = '%Y-%m-%d %H:%M:%S'
    START=strptime(fmt, "1975-08-14 00:00:00")
    END=strptime(fmt, "1975-08-15 00:00:00")
    set table "outfile.dat"
    plot "wind_.csv" using 3:2:0:(t=timecolumn(1,fmt)) with table \
         if ((t > START) && (t < END))
    unset table
    

    and if you want the date printed back out in the original format you could modify this to be

    set datafile separator ","
    fmt = '%Y-%m-%d %H:%M:%S'
    START=strptime(fmt, "1975-08-14 00:00:00")
    END=strptime(fmt, "1975-08-15 00:00:00")
    set table "outfile.dat"
    plot "wind_.csv" using 3:2:0:(t=timecolumn(1,fmt),strcol(1)) \
         with table if ((t > START) && (t < END))
    unset table
    

    I.e., use timecolumn to convert to time t in seconds for the pupose of filtering, but then keep the original string for output.