I have a number (number unknown) of data files in a directory. Each data file has the following content.
FILE TYPE: 1
COLUMNS: 7
TITLE: TRACK HISTORY
COLUMN TYPE VARIABLE (UNITS)
------ ---- -------- -------
1 2 ParticleResidenceTime (s)
2 10 ParticleID -
3 10 ParticleXPosition (m)
4 10 ParticleYPosition (m)
5 10 ParticleZPosition (m)
6 10 ParticleDiameter (m)
7 10 ParticleDensity (kg/m3)
---------------------------------------------
3.00E-01 1.01E+05 -5.32E-02 -1.19E-01 -4.21E-02 1.28E-04 1.50E+03
3.00E-01 1.36E+05 -5.73E-02 -1.30E-01 -2.69E-02 1.50E-04 1.50E+03
3.00E-01 1.53E+05 -5.53E-02 -8.33E-02 -8.47E-03 1.39E-04 1.50E+03
Each data file has around 300k lines like above. I need to consolidate all these files into one file. with only 3 columns in them and 1 header. The 3 columns i need the columns 3, 4, 5 which are the particle x y z position data. The data starts on the 16th line of each file.
so the eventual merged file would look something like the below.
X Y Z ( i guess i could add this header at the end manually too)
-5.32E-02 -1.19E-01 -4.21E-02
-5.12E-02 -1.39E-01 -4.21E-02
-5.32E-02 -1.19E-01 -4.21E-02
-5.32E-02 -1.19E-01 -4.21E-02
An Empty Line after data from file 1 following which data from file 2 will start
-5.32E-02 -1.19E-01 -4.21E-02
-5.12E-02 -1.39E-01 -4.21E-02
-5.32E-02 -1.19E-01 -4.21E-02
-5.32E-02 -1.19E-01 -4.21E-02
An Empty Line after data from file 3 following which data from file 4 will start
-5.32E-02 -1.19E-01 -4.21E-02
-5.12E-02 -1.39E-01 -4.21E-02
-5.32E-02 -1.19E-01 -4.21E-02
-5.32E-02 -1.19E-01 -4.21E-02
and so on till the data from all the files is put in this one file.
The script needs to do the following.
- first find how many files there are in that directory so that it can use that as the counter for a for loop (if used)
- Open a new file and add the data in columns 3, 4, 5 from line 16 till end from file 1 in the directory.
- add an empty line
- move to next file(since these data are time dependent data and the data needs to be accumulated in the order of time. the files will be sorted in the directory.)
- add columns 3, 4, 5 from the second file from line 16 till end
- add an empty line
- Repeat until the last file in the directory.
I would appreciate if someone showed me how to do this using Python.