Coverage for /Users/Newville/Codes/xraylarch/larch/io/xrd_netcdf.py: 25%
28 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 10:08 -0600
1#!/usr/bin/env python
2"""
3support for netcdf XRD files from **DETECTOR NAME**
4in Epics Mapping Mode -- copied from xmap_netcdf.py (larch plugins)
5mkak 2016.07.06 // updated 2018.03.30 to increase speed and read other
6variables in netcdf file (or return all as dictionary)
7"""
9import os
10from scipy.io import netcdf_file
12def read_xrd_netcdf(fname,verbose=False):
14 ## Reads a netCDF file created for XRD mapping
15 if verbose:
16 print(' reading %s' % fname)
18 ## Reads an XRD netCDF file with the netCDF plugin buffers
19 xrd_data = read_netcdf(fname, keyword='array_data')
20 xrd_data = xrd_data.astype('uint16')
22 ## Forces data into 3D shape
23 xrdshape = xrd_data.shape ## (no_images,pixels_x,pixels_y)
24 if len(xrdshape) == 2:
25 if verbose:
26 print('Reshaping to (%i, %i, %i)' % (1, xrdshape[0], xrdshape[1]))
27 xrd_data.shape = (1, xrdshape[0], xrdshape[1])
29 return xrd_data
31def read_xrd_netcdf_exptime(fname,verbose=False):
32 '''
33 returns header information for provided xrd netcdf file
34 '''
35 return read_netcdf(fname,keyword='Attr_FrameTime')
37def read_netcdf(fname,verbose=False,keyword=None):
38 '''
39 returns dictionary of all information in provided xrd netcdf file
40 unless data from only one key is specified
41 '''
43 with netcdf_file(fname, mmap=False) as fh:
44 if keyword is not None and keyword in fh.variables.keys():
45 return fh.variables[keyword].data
46 else:
47 netcdf_dict = {}
48 for key,val in dict(fh.variables).items():
49 netcdf_dict[key] = val.data
50 return netcdf_dict
52def test_read(fname):
53 print( fname, os.stat(fname))
54 fd = read_xrd_netcdf(fname, verbose=True)
55 print(fd.counts.shape)