-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aditya Omar <[email protected]>
- Loading branch information
1 parent
261e7d9
commit 5077326
Showing
1 changed file
with
27 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,41 @@ | ||
""" | ||
/****************************************************************************** | ||
This source file is part of the Avogadro project. | ||
This source code is released under the New BSD License, (the "License"). | ||
******************************************************************************/ | ||
""" | ||
import json | ||
class Cjson: | ||
""" | ||
This Class is intended to read Cjson files | ||
with python libraries and perform certain | ||
methods on files and convert them back to Cjson | ||
files as required | ||
""" | ||
def __init__(self): | ||
pass | ||
def __open_file(self, filePath): | ||
'''Use to read CJson formats by converting them to python dictionaries''' | ||
with open(filePath, 'r') as cjsonFile: | ||
py_dict_data = json.load(cjsonFile) | ||
return py_dict_data | ||
def __to_cjson(self, element_coords): | ||
cjson_dict = {"element-coordinates" :element_coords} | ||
cjsonData = json.dumps(cjson_dict, indent=4) | ||
def __to_cjson(self, cjson_dict_file): | ||
'''It converts python dictionaries to CJson format''' | ||
cjsonData = json.dumps(cjson_dict_file, indent=4) | ||
return (cjsonData) | ||
def get_atoms_coords(self,filePath): | ||
""" | ||
It helps to get the co-ordinates of individual elements/atoms in the format | ||
[ | ||
x co-ordinate | ||
y co-ordinate | ||
z co-ordinate | ||
Atomic Number of Element | ||
] | ||
""" | ||
data = self.__open_file(filePath) | ||
coords = data["atoms"]["coords"]["3d"] | ||
elements = data["atoms"]["elements"]["number"] | ||
element_coords = [(*coords[i*3:(i+1)*3], elements[i]) for i in range(0, int(len(coords) / 3))] | ||
return self.__to_cjson(element_coords) | ||
cjson_dict = {"element-coordinates" :element_coords} | ||
return self.__to_cjson(cjson_dict) |