Skip to content

Commit 17d3292

Browse files
committed
making single file script for a better approach
1 parent 956d99c commit 17d3292

6 files changed

Lines changed: 47 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ These examples are old, now we use HDF5 files. Kept for reference only.
6767

6868
```sh
6969
from histutils import rawDMCreader
70-
data = rawDMCreader.goRead('myfile.DMCdata')[0]
70+
data = rawDMCreader.read('myfile.DMCdata')[0]
7171
```

examples/Convert2013-04-11.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Convert raw DMCdata from 2013-04-11 to HDF5
4+
"""
5+
6+
import histutils.dio
7+
import histutils.rawDMCreader
8+
9+
# path to the data. This will probably be distinct for your computer.
10+
fn = "~/Google Drive/My Drive/Data/PokerFlat/2013-04-11/hst/raw/2013-04-11T07-00-CamSer1387_frames_402209-1-403708.DMCdata"
11+
12+
# where to store the converted data
13+
outdir = "./"
14+
15+
# TODO: these should be read from XML file.
16+
17+
params = {
18+
"header_bytes": 4, # only 2011-era files have 0 header bytes.
19+
"xy_pixel": (512, 512), # usually, but some files are 256x256.
20+
"xy_bin": (1, 1), # usually, but some files are binned 2x2.
21+
}
22+
23+
_, rawind, finf = histutils.rawDMCreader.read(fn, params)
24+
25+
histutils.dio.vid2h5(None, ut1=finf["ut1"], rawind=rawind, ticks=None, params=params)

src/histutils/convert/__main__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626

2727
#
2828
from ..dio import dir2fn, vid2h5
29-
from ..rawDMCreader import goRead
29+
from ..rawDMCreader import read
3030

3131

32-
def dmclooper(p: dict):
32+
def convert_files(p: dict):
33+
"""
34+
converts .DMCdata files to .h5 files, with metadata
35+
"""
3336

3437
params = {
3538
"kineticsec": p["kineticsec"],
@@ -67,7 +70,7 @@ def dmclooper(p: dict):
6770
f"\n file {i + 1} / {N} {i + 1 / N * 100.0:.1f} % done with {flist[0].parent}"
6871
)
6972

70-
rawImgData, rawind, finf = goRead(fn, params)
73+
rawImgData, rawind, finf = read(fn, params)
7174
# %% convert
7275
vid2h5(None, ut1=finf["ut1"], rawind=rawind, ticks=None, params=params)
7376

@@ -152,4 +155,4 @@ def dmclooper(p: dict):
152155
level=logging.INFO,
153156
)
154157

155-
dmclooper(vars(P))
158+
convert_files(vars(P))

src/histutils/index.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66

77
def getRawInd(fn: Path, finf: dict[str, int]) -> tuple[int, int]:
8+
9+
if not fn.is_file():
10+
raise FileNotFoundError(fn)
11+
812
if not isinstance(finf["nmetadata"], int):
913
raise TypeError(finf["nmetadata"])
1014

src/histutils/rawDMCreader.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
# NHEADBYTES = 4
2222

2323

24-
def goRead(infn: Path, params: dict[str, T.Any]) -> tuple:
24+
def read(infn: str | Path, params: dict[str, T.Any]) -> tuple:
2525

26-
infn = Path(infn).expanduser()
26+
fn = Path(infn).expanduser()
2727
# %% setup data parameters
2828
# preallocate *** LABVIEW USES ROW-MAJOR ORDERING C ORDER
29-
finf = getDMCparam(infn, params)
29+
finf = getDMCparam(fn, params)
3030
write_quota(finf["bytes_frame"] * finf["nframeextract"], params.get("outfn"))
3131

3232
rawFrameInd = np.zeros(finf["nframeextract"], dtype=np.int64)
@@ -41,7 +41,7 @@ def goRead(infn: Path, params: dict[str, T.Any]) -> tuple:
4141
order="C",
4242
)
4343
# %% read
44-
with infn.open("rb") as fid:
44+
with fn.open("rb") as fid:
4545
# j and i are NOT the same in general when not starting from beginning of file!
4646
for j, i in enumerate(finf["frameindrel"]):
4747
D, rawFrameInd[j] = getDMCframe(fid, i, finf)
@@ -67,9 +67,6 @@ def getDMCparam(fn: Path, params: dict[str, T.Any]) -> dict[str, T.Any]:
6767
"header_bytes": params["header_bytes"],
6868
} # FIXME for DMCdata version 1 only
6969

70-
if not fn.is_file(): # leave this here, getsize() doesn't fail on directory
71-
raise FileNotFoundError(fn)
72-
7370
# int() in case we are fed a float or int
7471
finf["super_x"] = int(params["xy_pixel"][0] // params["xy_bin"][0])
7572
finf["super_y"] = int(params["xy_pixel"][1] // params["xy_bin"][1])
@@ -97,6 +94,9 @@ def howbig(params: dict[str, T.Any], finf: dict[str, T.Any]) -> dict[str, int]:
9794

9895
def whichframes(fn: Path, params: dict[str, T.Any], finf: dict[str, T.Any]):
9996

97+
if not fn.is_file():
98+
raise FileNotFoundError(fn)
99+
100100
fileSizeBytes = fn.stat().st_size
101101

102102
if fileSizeBytes < finf["bytes_image"]:

src/histutils/tests/test_all.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33
import numpy as np
44

5-
from histutils.rawDMCreader import goRead
5+
from histutils.rawDMCreader import read
66

77
R = Path(__file__).parent
88

@@ -17,9 +17,9 @@ def test_rawread():
1717
"header_bytes": 4,
1818
}
1919

20-
testframe, testind, finf = goRead(bigfn, params)
20+
testframe, testind, finf = read(bigfn, params)
2121

22-
# these are both tested by goRead
22+
# these are both tested by read
2323
# finf = getDMCparam(bigfn,(512,512),(1,1),None,verbose=2)
2424
# with open(bigfn,'rb') as f:
2525
# testframe,testind = getDMCframe(f,iFrm=1,finf=finf,verbose=2)

0 commit comments

Comments
 (0)