4

Data files commonly used in gnuplot are generally of this kind:

  # This is my input

###a              b               c                 d        ---etc---
a030067o.fits  2457542.734730    60.00  1.2690   -0.174    0.003    9.871737    
a030068o.fits   ????????????      60.00  1.2650   -1.197    1.682    9.869020   
  a030069o.fits  2457542.736397    60.00     1.2610   -1.320   -0.429    9.865766     
a030070o.fits  2457542.737242  60.00  1.2570   -0.503   -0.192    9.867632   


a030071o.fits  2457542.738075    60.00  1.2530    0.370    0.424    9.868780   
a030072o.fits  2457542.738920     60.00  1.2490   -0.000   -0.003    9.869078    
a030073o.fits  2457542.739753    60.00  1.2450   -1.491    0.117    9.868382     


# Third Dataset
a030074o.fits  2457542.740598    60.00  1.2410   -1.413    0.811    9.867624   
a030075o.fits  2457542.741432    60.00  1.2370    0.363    1.411    9.866734   
  a030076o.fits  2457542.742277    60.00  1.2340   -0.868   -0.115    9.861761    
a030077o.fits  2457542.743110    60.00  1.2300   -0.411    0.206    9.865149   

Basically,

  • groups of lines separated by two new lines;
  • with arbitrary leading/trailing/middle spaces or tabs between columns;
  • with undefined (???) fields;
  • and with comments times to times.

This is very common in science, where gnuplot is very used.

Unfortunately, this format is awful if I want to produce web-based graphs with d3.js

In the case it has just a header line made like that

###a b c    d  e     f

I am able to parse it with

d3.text("file.dat", function (error,data){ 

    data=data.replace(/[ \t\r]+/g,',');
    var data = d3.csvParse(data)

});

but I cannot use it without the header line.

Neither this works (read csv/tsv with no header line in D3):

data=data.replace(/[ \t\r]+/g,',');                     
data = d3.csvParseRows(data).map(function(row) {        
    return row.map(function(value) {                    
        return +value;                                  
    });                                                 
});

Neither this (D3: ignore certain row in .csv):

d3.request("pianeta-ascii.dat").get(function (error,request) {             

    var dirtyCSV = request.responseText;   \s+(?=\s)                       
    dirtyCSV=dirtyCSV.replace(/\s/g,',');                                  
    var cleanCSV = dirtyCSV.split('\n').slice(1).join('\n');               
    var data = d3.csvParseRows(cleanCSV);                      

});

How can I make it work skipping comments and separating data groups?

I would like at least the javascript version of

 cat file.dat | grep -v "\#" |sed '/^\s*$/d' |awk '{print $1","$2","$3","$4","$5}'

and, if it possible, a way to separate the data groups in d3

theozh
  • 22,244
  • 5
  • 28
  • 72
leonard vertighel
  • 1,058
  • 1
  • 18
  • 37

0 Answers0