<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 JPK archive
JPK files are zipped archives of data. 
There is a header file at the top-level
Header files are normal text files, nothing special needed to read them
There is a segments folder
the segments folder contains numbered folders, one per segment  
each folder in segments contains another header file
each folder in segments contains a folder named channels
each channels folder contains several data files
data files contain data in C short format, at least in my example file
there seems to be no header in the .dat files, only pure (integer, i.e. short) data
Reading data from JPK archives
Most of this information on how to read JPKs using python I got from the JPKay module.
1. Open the zipped archive using zipfile
Step1:  It you can get the list of files stored in the zip archive, and you can open files using the instance's open function
Step2:  2. Parse header files to dictionaries
As printed above, the first line of the top-level header.properties file contains date and time, preceded by a '#'.
The following lines contain properties of the form "key=value".
To extract the time, one can use dateutil.parser
Step3:  The remainder of the lines should contain properties following the syntax mentioned above. They can easily be parsed to a dictionary.
Step4:  2.1 Parsing properties into tree-like dictionary
Properties seem to have a tree like structure, with node labels separated by dots. It appears more appropriate to parse them into a dictionary with sub-dictionaries recursively.
Step5:  2.2 In contrast to jpk-force archives, lower level header files appear to have the same beginning with time stamp in first line
No lines need to be skipped.
Step6:  3. Read data from files
Data files (.dat) contain data apparently exclusively in C short format. To convert it to python-compatible integers, use the struct module.
Step7:  According to the JPKay guys, every 4 items make one data point
Step11:  3.1 How to figure out the number of items making up one data point, i.e., how to figure out the data format of the .dat files?
In jpk-force archives it was quite simple
Step12:  But where is this information in a tweezer archive's header?
<ASSISTANT_TASK:>
Python Code:
from zipfile import ZipFile
fname = "../examples/force-save-2016.07.05-10.00.50.062.jpk-nt-force"
z = ZipFile(fname)
Explanation: JPK archive
JPK files are zipped archives of data. 
There is a header file at the top-level
Header files are normal text files, nothing special needed to read them
There is a segments folder
the segments folder contains numbered folders, one per segment  
each folder in segments contains another header file
each folder in segments contains a folder named channels
each channels folder contains several data files
data files contain data in C short format, at least in my example file
there seems to be no header in the .dat files, only pure (integer, i.e. short) data
Reading data from JPK archives
Most of this information on how to read JPKs using python I got from the JPKay module.
1. Open the zipped archive using zipfile
End of explanation
list_of_files = z.filelist
for f in list_of_files:
    print f.filename
print list_of_files[0].filename
f = z.open(list_of_files[0].filename)
lines = f.readlines()
print lines[0]
print lines[1]
print lines[2]
Explanation: It you can get the list of files stored in the zip archive, and you can open files using the instance's open function
End of explanation
from dateutil import parser
t = parser.parse(lines[0][1:])
print t
Explanation: 2. Parse header files to dictionaries
As printed above, the first line of the top-level header.properties file contains date and time, preceded by a '#'.
The following lines contain properties of the form "key=value".
To extract the time, one can use dateutil.parser
End of explanation
_properties = {}
for line in lines[1:]:
    key, value = line.split("=")
    value.strip()
    _properties[key] = value
for p in _properties:
    print p," = ",_properties[p]
Explanation: The remainder of the lines should contain properties following the syntax mentioned above. They can easily be parsed to a dictionary.
End of explanation
properties = {}
for line in lines[1:]:
    key,value = line.split("=")
    value = value.strip()
    
    split_key = key.split(".")
    d = properties
    if len(split_key) > 1:
        for s in split_key[:-1]:
            if d.keys().count(s):
                d = d[s]
            else:
                d[s] = {}
                d = d[s]
    d[split_key[-1]] = value
for p in properties:
    print p, " = ",properties[p]
properties['force-scan-series']['header']['force-settings']['force-baseline-adjust-settings']
Explanation: 2.1 Parsing properties into tree-like dictionary
Properties seem to have a tree like structure, with node labels separated by dots. It appears more appropriate to parse them into a dictionary with sub-dictionaries recursively.
End of explanation
fname = z.filelist[-25].filename
print fname
f = z.open(fname)
lines = f.readlines()
print(lines[0])
print(lines[1])
Explanation: 2.2 In contrast to jpk-force archives, lower level header files appear to have the same beginning with time stamp in first line
No lines need to be skipped.
End of explanation
from struct import unpack
fname = z.filelist[-12].filename
print fname
f = z.open(fname)
content = f.read()
print(len(content))
Explanation: 3. Read data from files
Data files (.dat) contain data apparently exclusively in C short format. To convert it to python-compatible integers, use the struct module.
End of explanation
content[0], content[1], content[2], content[3]
data = unpack(">i", content[0:4])
print data
Explanation: According to the JPKay guys, every 4 items make one data point
End of explanation
_z = ZipFile("force-save-2016.06.15-13.17.08.jpk-force")
fname = _z.filelist[-13].filename
print fname
open header file
f = _z.open(fname)
lines = f.readlines()
parse properties to dict
properties = {}
for line in lines[2:]:
    key,value = line.split("=")
    value = value.strip()
    
    split_key = key.split(".")
    d = properties
    if len(split_key) > 1:
        for s in split_key[:-1]:
            if d.keys().count(s):
                d = d[s]
            else:
                d[s] = {}
                d = d[s]
    d[split_key[-1]] = value
read one channel's data.type property
print properties['channel']['height']['data']['type']
Explanation: 3.1 How to figure out the number of items making up one data point, i.e., how to figure out the data format of the .dat files?
In jpk-force archives it was quite simple:
* open header file
* parse properties to dict
* read channel.data.type
End of explanation
fname = z.filelist[-25].filename
print fname
f = z.open(fname)
lines = f.readlines()
properties = {}
for line in lines[2:]:
    key,value = line.split("=")
    value = value.strip()
    
    split_key = key.split(".")
    d = properties
    if len(split_key) > 1:
        for s in split_key[:-1]:
            if d.keys().count(s):
                d = d[s]
            else:
                d[s] = {}
                d = d[s]
    d[split_key[-1]] = value
for key in properties:
    print key, " = ", properties[key]
Explanation: But where is this information in a tweezer archive's header?
End of explanation
<END_TASK>