2323from amuse .datamodel import Grid
2424from amuse .datamodel import GridPoint
2525from amuse .datamodel import AbstractSet
26+ from amuse .datamodel .base import VectorAttribute
27+
2628
2729from amuse .io import store_v1
2830
@@ -847,6 +849,7 @@ def store_particles(self, particles, extra_attributes = {}, parent=None, mapping
847849 self .hdf5file .flush ()
848850 self .store_collection_attributes (particles , group , extra_attributes , links )
849851 self .store_values (particles , group , links )
852+ self .store_selected_derived_attributes (particles ,group )
850853
851854 mapping_from_setid_to_group [id (particles )] = group
852855
@@ -868,6 +871,7 @@ def store_grid(self, grid, extra_attributes = {}, parent=None, mapping_from_seti
868871 compression_opts = self .compression_opts ,
869872 )
870873
874+ self .store_selected_derived_attributes (grid , group )
871875 self .store_collection_attributes (grid , group , extra_attributes , links )
872876 self .store_values (grid , group , links )
873877
@@ -924,8 +928,15 @@ def store_values(self, container, group, links = []):
924928 )
925929 dataset .attrs ["units" ] = "none" .encode ('ascii' )
926930
931+ def store_selected_derived_attributes (self , container , group ):
932+ saving = dict ()
933+ for key in container ._derived_attributes .keys ():
934+ attr = container ._derived_attributes [key ]
935+ if key not in container .GLOBAL_DERIVED_ATTRIBUTES and isinstance (attr , VectorAttribute ):
936+ saving [key ]= attr
937+ if len (saving ):
938+ group .attrs ["extra_vector_attributes" ]= pickle_to_string (saving )
927939
928-
929940 def store_linked_array (self , attribute , attributes_group , quantity , group , links ):
930941 subgroup = attributes_group .create_group (attribute )
931942 shape = quantity .shape
@@ -1127,8 +1138,9 @@ def load_particles_from_group(self, group):
11271138
11281139 self .mapping_from_groupid_to_set [group .id ] = particles
11291140 self .load_collection_attributes (particles , group )
1130-
1131-
1141+ if "extra_vector_attributes" in group .attrs .keys ():
1142+ self .load_extra_derived_attributes (particles , group )
1143+
11321144 return particles
11331145
11341146 def load_grid_from_group (self , group ):
@@ -1143,9 +1155,16 @@ def load_grid_from_group(self, group):
11431155 container ._private .attribute_storage = HDF5GridAttributeStorage (shape , group , self )
11441156 self .mapping_from_groupid_to_set [group .id ] = container
11451157 self .load_collection_attributes (container , group )
1158+ if "extra_vector_attributes" in group .attrs .keys ():
1159+ self .load_extra_derived_attributes (container , group )
11461160
11471161 return container
11481162
1163+ def load_extra_derived_attributes (self , container , group ):
1164+ attrs = unpickle_from_string (group .attrs ["extra_vector_attributes" ])
1165+ for key , attr in attrs .items ():
1166+ container ._derived_attributes [key ]= attr
1167+
11491168 def load_from_group (self , group ):
11501169 container_type = group .attrs ['type' ] if isinstance (group .attrs ['type' ], str ) else group .attrs ['type' ].decode ('ascii' )
11511170
0 commit comments