gnuplot:将数据文件 1:1 加载到数据块中

gnuplot: load datafile 1:1 into datablock

如何按原样(或 1:1)将数据文件读入数据块? 我怎么能独立做这个平台呢? 到目前为止我的尝试:

### load datafile "as is" into datablock for different platforms

FILE = 'Test.dat'

if (GPVAL_SYSNAME[:7] eq "Windows") {          # "Windows_NT-6.1" is shown on a Win7 system
    load "< echo $Data ^<^<EOD & type ".FILE
}
if (GPVAL_SYSNAME eq "Linux") {                # that's shown on a Raspberry
    load '< echo "$Data << EOD" & cat '.FILE
}
if (GPVAL_SYSNAME eq "Darwin") {               # this was shown on a MacOS Sierra 10.12.6
    # how to load a datafile into datablock under MacOS?
}

print $Data
### end of code

GPVAL_SYSNAME 在 Win10、其他 Linux 和其他 MacOS 系统上的价值是多少? 我需要多少 if 语句才能涵盖所有常见系统? 至少在 Windows 下,控制台 window 正在闪烁。我怎么可能压制这个?

我将数据读入数据集的想法如下:

  1. 如果您的数据位于非常(!)缓慢的服务器路径上
  2. 如果你的数据文件比较大
  3. 如果您拟合并绘制来自多个文件的多条曲线

例如:

FILE1 = '\SlowServer\blah\BigDataFile.dat'
FILE2 = '\SlowerServer\blah\BiggerDataFile.dat'
FILE3 = '\SlowestServer\blah\BiggestDataFile.dat'
fit f(x) FILE1 u 1:2 via a,c,d,e
fit g(x) FILE2 u 2:3 via f,g,h,i
fit h(x) FILE3 u 2:3 via j,k,l,m
plot FILE1 u 1:2:3 w l, \
     '' u (function()):(function()):3 with <whatever>, \
     FILE2 u 4:5:6 w l, \
     '' u 1:2:3 w l, \
     FILE3 u 7:8:9 w l, \
     '' u 1:2:3 w l , \
     <and more...>

我的问题:

  1. 每次绘制或拟合FILE''时,FILE的内容会被反复加载还是会保留在内存中?
  2. 如果放大,例如在交互式 wxt 终端中,在我看来好像需要再次加载文件。这是真的吗?
  3. 如果文件被一次又一次地加载,最好的做法是在开始时将文件一次加载到数据块中,然后再使用这些数据块吗?

欢迎任何解释、限制、优缺点和评论。

加法:

(部分答案,但有新问题): 对于 Windows、Linux 和 MacOS 系统,以下似乎工作正常。 Linux 和 MacOS 显然是一样的。

if (GPVAL_SYSNAME[:7] eq "Windows") { load '< echo $Data ^<^<EOD & type "Test.dat"' }
if (GPVAL_SYSNAME eq "Linux" )      { load '< echo "$Data << EOD" & cat "Test.dat"' }
if (GPVAL_SYSNAME eq "Darwin")      { load '< echo "$Data << EOD" & cat "Test.dat"' }

但是,如果我想从外部 gnuplot 过程调用此构造 "FileToDatablock.gpp" 它会在 Win7/64 下重现地使 gnuplot 崩溃(还没有机会测试 Linux 或 MacOS ).

"FileToDatablock.gpp"

### Load datafile "as is" 1:1 into datablock for different platforms
# ARG1 = input filename
# ARG2 = output datablock
# usage example: call "FileToDatablock.gpp" "Test.dat" "$Data"

if (ARGC<1) { ARG1 = "Test.dat" }
if (ARGC<2) { ARG2 = "$Data" }
if (GPVAL_SYSNAME[:7] eq "Windows") { load '< echo '.ARG2.' ^<^<EOD & type "'.ARG1.'"' }
if (GPVAL_SYSNAME eq "Linux" ) { load '< echo "\'.ARG2.' << EOD" & cat "'.ARG1.'"' }
if (GPVAL_SYSNAME eq "Darwin") { load '< echo "\'.ARG2.' << EOD" & cat "'.ARG1.'"' }
### end of code

以及调用此过程的文件:

### load datafile 1:1 into datablock
reset session

# this works fine under Win7/64
FILE = "Test.dat"
DATA = "$Data"
load '< echo '.DATA.' ^<^<EOD & type "'.FILE.'"'
print $Data

# this crashes gnuplot under Win7/64
call "tbFileToDatablock.gpp" "Test.dat" "$Data"
print $Data
### end of code

这有什么问题吗?谁能解释为什么以及如何解决这个问题?

只要您知道输入数据格式,就可以将文件读入数据块。例如,您有一个文件 MyFile1,其中 3 列的数字要读入数据块 MyBlock1,然后以 3 种方式绘制:

set table $MyBlock1
   plot "MyFile1" using 1:2:3 with table
unset table
plot $MyBlock1 using 1:2 with points
plot $MyBlock1 using 2:3 with points
plot $MyBlock1 using 1:3 with lines

这避免了多次读取文件,并且应该可以在任何平台上工作。与其这样做,我认为将文件从慢速文件系统复制到本地文件系统会更简单。

想法是将数据文件按原样 (1:1) 放入数据块中,包括注释行或空行等。 据我所知,似乎没有简单直接的平台-“独立”的gnuplot命令。 在某些情况下,将数据存储在数据块中(自 gnuplot 5.0 起可用)可能是有利的,因为您可以简单地按索引寻址行(仅自 gnuplot 5.2 起),例如$Data[7],或向前和向后循环数据,这对于文件中的数据而言无法轻易做到。

终于有一个我可以接受的解决方案,它似乎适用于 Windows 和 Linux(测试了 Windows 7 和 10 以及 Ubuntu 18.04。 4).我无法在 MacOS 上进行测试,但我假设该命令与 Linux 相同,并且它也适用于 MacOS。我不知道其他操作系统(感谢反馈)。

代码:

### load data file as is 1:1 into datablock
reset session

FileToDatablock(f,d) = GPVAL_SYSNAME[1:7] eq "Windows" ? \
                       sprintf('< echo   %s ^<^<EOD  & type "%s"',d,f) : \
                       sprintf('< echo "\%s   <<EOD" & cat  "%s"',d,f)     # Linux/MacOS

FILE = 'Test.dat'
load FileToDatablock(FILE,'$Data')

print $Data
### end of code

数据文件: (Test.dat)

# This is a test file
1.1    1.2
2.1    2.2
3.1    3.2

# another commented line
4.1    4.2
5.1    5.2
# some empty lines will follow


  6.1    6.2   # some spaces at the beginning
7.1    7.3
# end of datafile

结果:(如预期的那样$Data是1:1等于Test.dat

# This is a test file
1.1    1.2
2.1    2.2
3.1    3.2

# another commented line
4.1    4.2
5.1    5.2
# some empty lines will follow


  6.1    6.2   # some spaces at the beginning
7.1    7.3
# end of datafile