Skip to content
Snippets Groups Projects
Commit 13ce989d authored by Ilaria Carlomagno's avatar Ilaria Carlomagno
Browse files

Upload New File

parent 27329d3a
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#This script does 2 things:
#1) It checks for beam dumps during MAP acquisition
# In case BMS is < BMS_MIN, it reshapes the map discarding void pixels
# AND
#2) It discards half-filled rows/columns in case of manual interruption of the map
import glob
import h5py
import itertools
import numpy as np
import os
import sys
from mylib import count_steps, check_bms, orientation
BMS_MIN = 1e-2
PRECISION = 5
EXT = "h5"
PATH_SCALAR = "/Measurement/TransientScalarData"
PATH_VECTOR = "/Measurement/TransientVectorData"
NEW_FOLDER = "/cut-reshaped/"
NEWNAME_APP = "_new.h5"
def cut_reshape(in_file, out_fold):
new_map = os.path.join( out_fold, in_file.split('.h5')[0] + NEWNAME_APP)
with h5py.File(in_file, 'r') as f:
move_hor = False
for run in f.keys():
print ("Run: %s" %run)
x = f[run+PATH_SCALAR+"/X"][...]
y = f[run+PATH_SCALAR+"/Y"][...]
# when X moves first, in order to see the map on PyMCA, the number of
# columns to enter is the number of rows. The map will appear rotated.
try:
x, y, move_hor = orientation(x, y)
except ValueError:
print('x x x Size not valid for map. Please check file. x x x \n\n\n')
break
shape_x, shape_y = count_steps(x,y) # was (y,x) because x is vertical axis on IAEA gui; changed now
total_points = shape_x * shape_y
print("Original map size: (%d,%d), total point=%d" % (shape_x, shape_y, total_points) )
# each pixel has several bms value: checking the minimum only
bms = f[run+PATH_VECTOR+"/BMS-T"][...]
bms = bms.min(axis=1)
beam_lost, valid_pixels = check_bms(bms)
if valid_pixels == 0:
print('x x x x x x x No valid pixels, moving on.\n\n\n')
break
if move_hor:
col = shape_y
row = np.round(valid_pixels/shape_y)
else:
row = shape_x
col = np.round(valid_pixels/shape_x)
if valid_pixels < shape_x*shape_y:
print('Valid pixels =', valid_pixels)
print('Original map =', shape_x*shape_y)
if move_hor:
row = valid_pixels/shape_y
else:
col = valid_pixels/shape_x
if beam_lost:
print('Beam dump detected!')
print('Original size:', shape_x, shape_y)
print('New size:', row, col)
if (row*col==shape_x*shape_y):
print('This map is ok! No cutting, just reshaping!\n')
print('Original size:', shape_x, shape_y)
print('New size:', row, col)
with h5py.File(new_map, 'w') as fout:
total_point = row * col
# print("Map size: (%d,%d), total point=%d\n" % (row, col, total_point))
print('\n--> Now reshaping TransientVectorData:')
for vectorData in f[run+PATH_VECTOR].keys():
#print (vectorData)
v = f[run+PATH_VECTOR+"/"+vectorData][...]
v = v[0:total_point]
v = v.reshape((row,col,v.shape[-1]))
if move_hor:
print('Rotating map here to facilitate data viewing on PyMCA.')
v = np.rot90(v, -1, axes=(1,0))
print(v.shape)
#print("New shape = ", v.shape)
fout.create_dataset(run+PATH_VECTOR+"/"+vectorData, data=v, compression = "gzip", shuffle=True)
print('\n--> Done! Now reshaping TransientScalarData:')
for scalarData in f[run+PATH_SCALAR].keys():
#print(scalarData)
s = f[run+PATH_SCALAR+"/"+scalarData][...]
s = s[0:total_point]
if s.shape[0] == total_point:
s = s.reshape(row, col)
if move_hor:
s = np.rot90(s, -1, axes=(1,0))
#print("New shape: ",s.shape)
fout.create_dataset(run+PATH_SCALAR+"/"+scalarData, data=s)
####################################################################
def run():
print('-------------------------------------------------\n')
print('--------- Welcome! ------------\n')
print("--- Let's cut your XRF maps! ---\n")
print('-------------------------------------------------\n')
in_path = './'
out_path = in_path + NEW_FOLDER
# if out_path is read only, uncomment next line
# out_path = str(input('Where do you want to save the reshaped maps?' ))
# checks automatically all the h5 files in the in_path
file_list = glob.glob('{0}/*'.format(in_path)+EXT)
print('I found '+str(len(file_list))+' files matching the extension '+EXT)
print(file_list)
print('\n')
if len(file_list) == 0:
print("--> Can't do much with 0 files! Sorry!")
print("--> Move the maps in the same folder as the program and try again!")
else:
print("--> All these files will be cut and reshaped. \n")
print("--> Don't worry: overwriting is not an option. ;) \n\n\n")
if not os.path.exists(out_path):
os.makedirs(out_path)
for filename, i in zip(file_list, range(len(file_list))):
filename = filename[2:]
cut_reshape(filename, out_path)
print('\n - - - - Map {0}/{1} successfully checked.\n'.format(i+1, len(file_list)))
print('\n --> Have a nice day!')
if __name__ == "__main__":
run()
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment