[yt-svn] commit/yt: ngoldbaum: Merged in WeiguangCui/yt (pull request #2355)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Sep 7 11:12:15 PDT 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/eaabc53f4933/
Changeset:   eaabc53f4933
Branch:      yt
User:        ngoldbaum
Date:        2016-09-07 18:11:48+00:00
Summary:     Merged in WeiguangCui/yt (pull request #2355)

Yt - enabled Gadget format 2
Affected #:  2 files

diff -r 7e7c3fdd9cc79e65a179d277ae64b345dc30411f -r eaabc53f4933c4f46b032631563f6b1af087bd95 yt/frontends/gadget/data_structures.py
--- a/yt/frontends/gadget/data_structures.py
+++ b/yt/frontends/gadget/data_structures.py
@@ -46,10 +46,24 @@
     if isinstance(unit[0], string_types):
         unit = unit[1], unit[0]
     return unit
+    
+def _get_gadget_format(filename):
+    # check and return gadget binary format
+    f = open(filename, 'rb')
+    (rhead,) = struct.unpack('<I',f.read(4))
+    f.close()
+    if (rhead == 134217728) | (rhead == 8):
+        return 2
+    elif (rhead == 65536) | (rhead == 256):
+        return 1
+    else:
+        raise RuntimeError("Incorrect Gadget format %s!" % str(rhead))
 
 class GadgetBinaryFile(ParticleFile):
     def __init__(self, ds, io, filename, file_id):
         with open(filename, "rb") as f:
+            if _get_gadget_format(filename) == 2:
+                f.seek(f.tell()+16)
             self.header = read_record(f, ds._header_spec)
             self._position_offset = f.tell()
             f.seek(0, os.SEEK_END)
@@ -135,6 +149,8 @@
         # in the GADGET-2 user guide.
 
         f = open(self.parameter_filename, 'rb')
+        if _get_gadget_format(self.parameter_filename) == 2:
+            f.seek(f.tell()+16)
         hvals = read_record(f, self._header_spec)
         for i in hvals:
             if len(hvals[i]) == 1:
@@ -281,10 +297,10 @@
     @staticmethod
     def _validate_header(filename):
         '''
-        This method automatically detects whether the Gadget file is big/little endian 
-        and is not corrupt/invalid using the first 4 bytes in the file.  It returns a 
-        tuple of (Valid, endianswap) where Valid is a boolean that is true if the file 
-        is a Gadget binary file, and endianswap is the endianness character '>' or '<'. 
+        This method automatically detects whether the Gadget file is big/little endian
+        and is not corrupt/invalid using the first 4 bytes in the file.  It returns a
+        tuple of (Valid, endianswap) where Valid is a boolean that is true if the file
+        is a Gadget binary file, and endianswap is the endianness character '>' or '<'.
         '''
         try:
             f = open(filename,'rb')
@@ -293,9 +309,9 @@
                 f = open(filename+".0")
             except IOError:
                 return False, 1
-        
+
         # First int32 is 256 for a Gadget2 binary file with SnapFormat=1,
-        # 8 for a Gadget2 binary file with SnapFormat=2 file, 
+        # 8 for a Gadget2 binary file with SnapFormat=2 file,
         # or the byte swapped equivalents (65536 and 134217728).
         # The int32 following the header (first 4+256 bytes) must equal this
         # number.
@@ -311,16 +327,17 @@
             endianswap = '>'
         # Disabled for now (does any one still use SnapFormat=2?)
         # If so, alternate read would be needed based on header.
-        # elif rhead == 8:
-        #     return True, '<'
-        # elif rhead == 134217728:
-        #     return True, '>'
+        # Enabled Format2 here
+        elif rhead == 8:
+            return True, '<'
+        elif rhead == 134217728:
+            return True, '>'
         else:
             f.close()
             return False, 1
         # Read in particle number from header
         np0 = sum(struct.unpack(endianswap+'IIIIII',f.read(6*4)))
-        # Read in size of position block. It should be 4 bytes per float, 
+        # Read in size of position block. It should be 4 bytes per float,
         # with 3 coordinates (x,y,z) per particle. (12 bytes per particle)
         f.seek(4+256+4,0)
         np1 = struct.unpack(endianswap+'I',f.read(4))[0]/(4*3)

diff -r 7e7c3fdd9cc79e65a179d277ae64b345dc30411f -r eaabc53f4933c4f46b032631563f6b1af087bd95 yt/frontends/gadget/io.py
--- a/yt/frontends/gadget/io.py
+++ b/yt/frontends/gadget/io.py
@@ -27,11 +27,13 @@
     compute_morton
 from yt.utilities.logger import ytLogger as mylog
 
+from .data_structures import _get_gadget_format
+
 class IOHandlerGadgetHDF5(IOHandlerOWLS):
     _dataset_type = "gadget_hdf5"
 
 ZeroMass = object()
-    
+
 class IOHandlerGadgetBinary(BaseIOHandler):
     _dataset_type = "gadget_binary"
     _vector_fields = (("Coordinates", 3),
@@ -61,6 +63,8 @@
         self._vector_fields = dict(self._vector_fields)
         self._fields = ds._field_spec
         self._ptypes = ds._ptype_spec
+        self.data_files = set([])
+        self._format =  _get_gadget_format(ds.parameter_filename)#default gadget format 1
         super(IOHandlerGadgetBinary, self).__init__(ds, *args, **kwargs)
 
     @property
@@ -173,7 +177,12 @@
             if not any( (ptype, field) in field_list
                         for ptype in self._ptypes):
                 continue
-            pos += 4
+            if self._format == 2:
+                pos += 20 #skip block header
+            elif self._format == 1:
+                pos += 4
+            else:
+                raise RuntimeError("incorrect Gadget format %s!" % str(self._format))
             any_ptypes = False
             for ptype in self._ptypes:
                 if field == "Mass" and ptype not in self.var_mass:
@@ -189,7 +198,7 @@
             pos += 4
             if not any_ptypes: pos -= 8
         if file_size is not None:
-            if file_size != pos:
+            if (file_size != pos) & (self._format == 1): #ignore the rest of format 2 
                 mylog.warning("Your Gadget-2 file may have extra " +
                               "columns or different precision!" +
                               " (%s file vs %s computed)",

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list