Create subset of hkl file

This area is a space for discussions about programming, scripting, algorithms... related to crystallography. Bash, Python, C, Fortran are welcome but also R, gnuplot and other scientific scripting languages.
User avatar
Arianna
Sealed Tube
Posts: 38
Joined: 22 Jul 2010, 09:04
Location: Istituto Italiano di Tecnologia

Create subset of hkl file

Post by Arianna » 01 Nov 2017, 13:12

Hi all!
It's nice to come back after a long time.

I have a file1.hkl which has 100% completeness and file2.hkl which is incomplete. I want to select a subset of file1 made only of those reflections that are present in file2.
Is there any software that can do it? Or does any of you by chance already have a script that does it?

Thank you in advance!!
Arianna

User avatar
oleg
Olex2 Boffin
Posts: 678
Joined: 24 Nov 2008, 21:34

Re: Create subset of hkl file

Post by oleg » 01 Nov 2017, 18:40

For simple cases you can use a Python script as below. File3 will have all reflections with indices from file2. The script assumes that these are fixed format HKL files with the first 12 chars being the Miller indices.

Cheers,
Oleg.

Code: Select all

test_set = set()
#generate the set to extract
with open("file2.hkl", "r") as inp:
  for l in inp:
    if len(l) < 12: break
    base_set.add(l[:12])
diff = []
#extract the set from file1
with open("file1.hkl", "r") as inp:
  for l in inp:
    if len(l) < 12: break
    if l[:12] in test_set:
      diff.append(l)
with open("file3.hkl", "w") as out:
  out.writelines(diff)
Last edited by oleg on 02 Nov 2017, 13:02, edited 1 time in total.

User avatar
Arianna
Sealed Tube
Posts: 38
Joined: 22 Jul 2010, 09:04
Location: Istituto Italiano di Tecnologia

Re: Create subset of hkl file

Post by Arianna » 01 Nov 2017, 19:52

Oh thank you very very much!
Arianna

Return to “Scientific Programming”