#ver. 1.1.4: -Fix for recent API changes. Thanks to Filiciss.
authorValter Battioli <valtervb@live.com>
Tue, 8 Feb 2011 21:54:09 +0000 (21:54 +0000)
committerValter Battioli <valtervb@live.com>
Tue, 8 Feb 2011 21:54:09 +0000 (21:54 +0000)
#            -Some code cleaning (PEP8)

io_import_LRO_Lola_MGS_Mola_img.py

index 8ea574205586695d080b25f60ba0afa6681ef27e..b24a939154912fb9f6fc0b7370c2d355862fae7a 100644 (file)
 bl_info = {
     "name": "LRO Lola & MGS Mola img Importer",
     "author": "ValterVB",
-    "version": (1, 1, 3),
+    "version": (1, 1, 4),
     "blender": (2, 5, 6),
-    "api": 34043,
+    "api": 34473,
     "location": "3D window > Tool Shelf",
     "description": "Import DTM from LRO Lola and MGS Mola",
     "warning": "May consume a lot of memory",
     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
         "Scripts/Import-Export/NASA_IMG_Importer",
-    "tracker_url":"http://projects.blender.org/tracker/index.php?"\
+    "tracker_url": "http://projects.blender.org/tracker/index.php?"\
         "func=detail&aid=25462",
     "category": "Import-Export"}
 
@@ -38,7 +38,7 @@ bl_info = {
 #            -Check on selected coordinates more complete
 #ver. 1.1.0: -Optimized for less memory
 #ver. 1.1.1: -Correct bug for real value of the High (bad error).
-#             now it's less artistic, bat more real Always possible use 
+#             now it's less artistic, bat more real Always possible use
 #             the old formula. check Magnify (x4)
 #            -Correct the bug for directory with dot in the name
 #            -Add some warning and more information
@@ -46,22 +46,27 @@ bl_info = {
 #ver. 1.1.2: -Word correction
 #            -Correct the problem for Unix (Upper lower case)
 #              If the Ext of the file selected from user is upper
-#              than the second file is Upper and Viceversa. 
+#              than the second file is Upper and Viceversa.
 #              (I can't verify because i have Win)
 #ver. 1.1.3: -Little fix for previous fix on upper/lower case
+#ver. 1.1.4: -Fix for recent API changes. Thanks to Filiciss.
+#            -Some code cleaning (PEP8)
 #************************************************************************
 
-import bpy 
+import bpy
 import os.path
-import math, array,  mathutils
+import math
+import array
+import mathutils
 from mathutils import *
 
-TO_RAD=math.pi/180 #From degrees to radians
+TO_RAD = math.pi / 180  # From degrees to radians
 
 # turning off relative path - it causes an error if it was true
 if bpy.context.user_preferences.filepaths.use_relative_paths == True:
-   bpy.context.user_preferences.filepaths.use_relative_paths = False
-   
+    bpy.context.user_preferences.filepaths.use_relative_paths = False
+
+
 # A very simple "bridge" tool.
 # Connects two equally long vertex rows with faces.
 # Returns a list of the new faces (list of  lists)
@@ -71,8 +76,10 @@ if bpy.context.user_preferences.filepaths.use_relative_paths == True:
 # closed ... Creates a loop (first & last are closed).
 # flipped ... Invert the normal of the face(s).
 #
-# Note: You can set vertIdx1 to a single vertex index to create a fan/star of faces.
-# Note: If both vertex idx list are the same length they have to have at least 2 vertices.
+# Note: You can set vertIdx1 to a single vertex index to create a fan/star
+#       of faces.
+# Note: If both vertex idx list are the same length they have to have at
+#       least 2 vertices.
 def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
     faces = []
 
@@ -126,288 +133,306 @@ def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
                     vertIdx2[num + 1], vertIdx1[num + 1]]
             faces.append(face)
     return faces
-    
+
+
 #Utility Function ********************************************************
 #
 #Input: Latitude Output: Number of the line (1 to n)
 def LatToLine(Latitude):
-    tmpLines=round((MAXIMUM_LATITUDE-Latitude)*MAP_RESOLUTION+1.0)
-    if tmpLines>LINES:tmpLines=LINES
+    tmpLines = round((MAXIMUM_LATITUDE - Latitude) * MAP_RESOLUTION + 1.0)
+    if tmpLines > LINES:
+        tmpLines = LINES
     return tmpLines
-        
-#Input: Number of the line (1 to n) Output: Latitude 
+
+
+#Input: Number of the line (1 to n) Output: Latitude
 def LineToLat(Line):
-    if MAP_RESOLUTION==0:
+    if MAP_RESOLUTION == 0:
         return 0
     else:
-        return float(MAXIMUM_LATITUDE-(Line-1)/MAP_RESOLUTION)
+        return float(MAXIMUM_LATITUDE - (Line - 1) / MAP_RESOLUTION)
+
 
 #Input: Longitude Output: Number of the point (1 to n)
 def LongToPoint(Longitude):
-    tmpPoints=round((Longitude-WESTERNMOST_LONGITUDE)*MAP_RESOLUTION+1.0)
-    if tmpPoints>LINE_SAMPLES:tmpPoints=LINE_SAMPLES
+    tmpPoints = round((Longitude - WESTERNMOST_LONGITUDE) *
+                       MAP_RESOLUTION + 1.0)
+    if tmpPoints > LINE_SAMPLES:
+        tmpPoints = LINE_SAMPLES
     return tmpPoints
 
+
 #Input: Number of the Point (1 to n) Output: Longitude
 def PointToLong(Point):
-    if MAP_RESOLUTION==0:
+    if MAP_RESOLUTION == 0:
         return 0
     else:
-           return float(WESTERNMOST_LONGITUDE+(Point-1)/MAP_RESOLUTION)
+        return float(WESTERNMOST_LONGITUDE + (Point - 1) / MAP_RESOLUTION)
+
 
 #Input: Latitude Output: Neareast real Latitude on grid
 def RealLat(Latitude):
-       return float(LineToLat(LatToLine(Latitude)))
+    return float(LineToLat(LatToLine(Latitude)))
+
 
-#Input: Longitude Output: Neareast real Longitude on grid      
+#Input: Longitude Output: Neareast real Longitude on grid
 def RealLong(Longitude):
-       return float(PointToLong(LongToPoint(Longitude)))
+    return float(PointToLong(LongToPoint(Longitude)))
 #**************************************************************************
 
 
-#Read the LBL file    
+#Read the LBL file
 def ReadLabel(FileName):
     global FileAndPath
-    global LINES, LINE_SAMPLES, SAMPLE_TYPE, SAMPLE_BITS, UNIT, MAP_RESOLUTION, MAXIMUM_LATITUDE, MINIMUM_LATITUDE
-    global WESTERNMOST_LONGITUDE, EASTERNMOST_LONGITUDE, SCALING_FACTOR,OFFSET, RadiusUM, TARGET_NAME
+    global LINES, LINE_SAMPLES, SAMPLE_TYPE, SAMPLE_BITS, UNIT, MAP_RESOLUTION
+    global MAXIMUM_LATITUDE, MINIMUM_LATITUDE, WESTERNMOST_LONGITUDE
+    global EASTERNMOST_LONGITUDE, SCALING_FACTOR, OFFSET, RadiusUM, TARGET_NAME
     global Message
 
-    if FileName=='':
-        LINES=LINE_SAMPLES=SAMPLE_BITS=MAP_RESOLUTION=0
-        MAXIMUM_LATITUDE=MINIMUM_LATITUDE=WESTERNMOST_LONGITUDE=EASTERNMOST_LONGITUDE=OFFSET=SCALING_FACTOR=0.0
-        SAMPLE_TYPE=UNIT=TARGET_NAME=RadiusUM=Message=""
+    if FileName == '':
+        LINES = LINE_SAMPLES = SAMPLE_BITS = MAP_RESOLUTION = 0
+        MAXIMUM_LATITUDE = MINIMUM_LATITUDE = 0.0
+        WESTERNMOST_LONGITUDE = EASTERNMOST_LONGITUDE = 0.0
+        OFFSET = SCALING_FACTOR = 0.0
+        SAMPLE_TYPE = UNIT = TARGET_NAME = RadiusUM = Message = ""
         return
-        
-    FileAndPath=FileName
-    FileAndExt=os.path.splitext(FileAndPath)
+
+    FileAndPath = FileName
+    FileAndExt = os.path.splitext(FileAndPath)
     try:
         #Check for UNIX that is case sensitive
-        #If the Ext of the file selected from user is Upper, than the second file is Upper and Viceversa
-        if FileAndExt[1].isupper(): 
-            f = open(FileAndExt[0] + ".LBL", 'r')  #Open the label file
+        #If the Ext of the file selected from user is Upper, than the second
+        #file is Upper and Viceversa
+        if FileAndExt[1].isupper():
+            f = open(FileAndExt[0] + ".LBL", 'r')  # Open the label file
         else:
-            f = open(FileAndExt[0] + ".lbl", 'r')  #Open the label file
-        Message=""
+            f = open(FileAndExt[0] + ".lbl", 'r')  # Open the label file
+        Message = ""
     except:
-        Message="FILE LBL NOT AVAILABLE OR YOU HAVEN'T SELECTED A FILE"
+        Message = "FILE LBL NOT AVAILABLE OR YOU HAVEN'T SELECTED A FILE"
         return
-    
-    block=""
-    OFFSET=0
+
+    block = ""
+    OFFSET = 0
     for line in f:
-        tmp=line.split("=")
-        if tmp[0].strip()=="OBJECT" and tmp[1].strip()=="IMAGE":       
-            block="IMAGE"
-        elif tmp[0].strip()=="OBJECT" and tmp[1].strip()=="IMAGE_MAP_PROJECTION":      
-            block="IMAGE_MAP_PROJECTION"
-        elif tmp[0].strip()=="END_OBJECT" and tmp[1].strip()=="IMAGE": 
-            block=""
-        elif tmp[0].strip()=="END_OBJECT" and tmp[1].strip()=="IMAGE_MAP_PROJECTION":  
-            block=""
-        elif tmp[0].strip()=="TARGET_NAME":
-            block=""
-            TARGET_NAME=tmp[1].strip()
-        if block=="IMAGE":
-            if line.find("LINES") !=-1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                LINES=int(tmp[1].strip())
+        tmp = line.split("=")
+        if tmp[0].strip() == "OBJECT" and tmp[1].strip() == "IMAGE":
+            block = "IMAGE"
+        elif tmp[0].strip() == "OBJECT" and tmp[1].strip() == "IMAGE_MAP_PROJECTION":
+            block = "IMAGE_MAP_PROJECTION"
+        elif tmp[0].strip() == "END_OBJECT" and tmp[1].strip() == "IMAGE":
+            block = ""
+        elif tmp[0].strip() == "END_OBJECT" and tmp[1].strip() == "IMAGE_MAP_PROJECTION":
+            block = ""
+        elif tmp[0].strip() == "TARGET_NAME":
+            block = ""
+            TARGET_NAME = tmp[1].strip()
+        if block == "IMAGE":
+            if line.find("LINES") != -1 and not(line.startswith("/*")):
+                tmp = line.split("=")
+                LINES = int(tmp[1].strip())
             elif line.find("LINE_SAMPLES") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                LINE_SAMPLES=int(tmp[1].strip())
+                tmp = line.split("=")
+                LINE_SAMPLES = int(tmp[1].strip())
             elif line.find("UNIT") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                UNIT=tmp[1].strip()
+                tmp = line.split("=")
+                UNIT = tmp[1].strip()
             elif line.find("SAMPLE_TYPE") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                SAMPLE_TYPE=tmp[1].strip()
+                tmp = line.split("=")
+                SAMPLE_TYPE = tmp[1].strip()
             elif line.find("SAMPLE_BITS") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                SAMPLE_BITS=int(tmp[1].strip())
+                tmp = line.split("=")
+                SAMPLE_BITS = int(tmp[1].strip())
             elif line.find("SCALING_FACTOR") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                SCALING_FACTOR=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                SCALING_FACTOR = float(tmp[0].replace(" ", ""))
             elif line.find("OFFSET") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                if tmp[0].find("OFFSET")!=-1 and len(tmp) > 1:
-                    tmp=tmp[1].split("<")
-                    tmp[0]=tmp[0].replace(".", "")
-                    OFFSET=float(tmp[0].replace(" ", ""))
-        elif block=="IMAGE_MAP_PROJECTION":
+                tmp = line.split("=")
+                if tmp[0].find("OFFSET") != -1 and len(tmp) > 1:
+                    tmp = tmp[1].split("<")
+                    tmp[0] = tmp[0].replace(".", "")
+                    OFFSET = float(tmp[0].replace(" ", ""))
+        elif block == "IMAGE_MAP_PROJECTION":
             if line.find("A_AXIS_RADIUS") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                A_AXIS_RADIUS=float(tmp[0].replace(" ", ""))
-                RadiusUM=tmp[1].rstrip().replace(">","")
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                A_AXIS_RADIUS = float(tmp[0].replace(" ", ""))
+                RadiusUM = tmp[1].rstrip().replace(">", "")
             elif line.find("B_AXIS_RADIUS") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                B_AXIS_RADIUS=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                B_AXIS_RADIUS = float(tmp[0].replace(" ", ""))
             elif line.find("C_AXIS_RADIUS") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                C_AXIS_RADIUS=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                C_AXIS_RADIUS = float(tmp[0].replace(" ", ""))
             elif line.find("MAXIMUM_LATITUDE") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                MAXIMUM_LATITUDE=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                MAXIMUM_LATITUDE = float(tmp[0].replace(" ", ""))
             elif line.find("MINIMUM_LATITUDE") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                MINIMUM_LATITUDE=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                MINIMUM_LATITUDE = float(tmp[0].replace(" ", ""))
             elif line.find("WESTERNMOST_LONGITUDE") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                WESTERNMOST_LONGITUDE=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                WESTERNMOST_LONGITUDE = float(tmp[0].replace(" ", ""))
             elif line.find("EASTERNMOST_LONGITUDE") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                EASTERNMOST_LONGITUDE=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                EASTERNMOST_LONGITUDE = float(tmp[0].replace(" ", ""))
             elif line.find("MAP_RESOLUTION") != -1 and not(line.startswith("/*")):
-                tmp=line.split("=")
-                tmp=tmp[1].split("<")
-                MAP_RESOLUTION=float(tmp[0].replace(" ", ""))
+                tmp = line.split("=")
+                tmp = tmp[1].split("<")
+                MAP_RESOLUTION = float(tmp[0].replace(" ", ""))
     f.close
-    MAXIMUM_LATITUDE=MAXIMUM_LATITUDE-1/MAP_RESOLUTION/2
-    MINIMUM_LATITUDE=MINIMUM_LATITUDE+1/MAP_RESOLUTION/2
-    WESTERNMOST_LONGITUDE=WESTERNMOST_LONGITUDE+1/MAP_RESOLUTION/2
-    EASTERNMOST_LONGITUDE=EASTERNMOST_LONGITUDE-1/MAP_RESOLUTION/2
-    if OFFSET==0: #When OFFSET isn't available I use the medium of the radius
-        OFFSET=(A_AXIS_RADIUS+B_AXIS_RADIUS+C_AXIS_RADIUS)/3
+    MAXIMUM_LATITUDE = MAXIMUM_LATITUDE - 1 / MAP_RESOLUTION / 2
+    MINIMUM_LATITUDE = MINIMUM_LATITUDE + 1 / MAP_RESOLUTION / 2
+    WESTERNMOST_LONGITUDE = WESTERNMOST_LONGITUDE + 1 / MAP_RESOLUTION / 2
+    EASTERNMOST_LONGITUDE = EASTERNMOST_LONGITUDE - 1 / MAP_RESOLUTION / 2
+    if OFFSET == 0:  # When OFFSET isn't available I use the medium of the radius
+        OFFSET = (A_AXIS_RADIUS + B_AXIS_RADIUS + C_AXIS_RADIUS) / 3
     else:
-        OFFSET=OFFSET/1000 #Convert m to Km
-    if SCALING_FACTOR==0: SCALING_FACTOR=1.0 #When isn'tavailable I set it to 1
+        OFFSET = OFFSET / 1000  # Convert m to Km
+    if SCALING_FACTOR == 0:
+        SCALING_FACTOR = 1.0  # When isn'tavailable I set it to 1
+
 
 def MakeMaterialMars(obj):
     #Copied from io_convert_image_to_mesh_img
     mat = bpy.data.materials.new("Mars")
     mat.diffuse_shader = 'MINNAERT'
-    mat.diffuse_color=(0.426, 0.213, 0.136)
-    mat.darkness=0.8
-    
+    mat.diffuse_color = (0.426, 0.213, 0.136)
+    mat.darkness = 0.8
+
     mat.specular_shader = 'WARDISO'
-    mat.specular_color= (1.000, 0.242, 0.010) 
-    mat.specular_intensity=0.010 
-    mat.specular_slope= 0.100
+    mat.specular_color = (1.000, 0.242, 0.010)
+    mat.specular_intensity = 0.010
+    mat.specular_slope = 0.100
     obj.data.materials.append(mat)
 
+
 def MakeMaterialMoon(obj):
     #Same as Mars Material, but i have canged the color
     mat = bpy.data.materials.new("Moon")
     mat.diffuse_shader = 'MINNAERT'
-    mat.diffuse_color=(0.426, 0.426, 0.426)
-    mat.darkness=0.8
-    
+    mat.diffuse_color = (0.426, 0.426, 0.426)
+    mat.darkness = 0.8
+
     mat.specular_shader = 'WARDISO'
-    mat.specular_color= (0.6, 0.6, 0.6) 
-    mat.specular_intensity=0.010 
-    mat.specular_slope= 0.100
+    mat.specular_color = (0.6, 0.6, 0.6)
+    mat.specular_intensity = 0.010
+    mat.specular_slope = 0.100
     obj.data.materials.append(mat)
-    
+
+
 def clear_properties():
     # can happen on reload
     if bpy.context.scene is None:
         return
 
-    LINES=LINE_SAMPLES=SAMPLE_BITS=0
-    MAXIMUM_LATITUDE=MINIMUM_LATITUDE=WESTERNMOST_LONGITUDE=EASTERNMOST_LONGITUDE=OFFSET=SCALING_FACTOR=MAP_RESOLUTION=0.0
-    SAMPLE_TYPE=UNIT=TARGET_NAME=RadiusUM=Message=""
-        
+    LINES = LINE_SAMPLES = SAMPLE_BITS = MAP_RESOLUTION = 0
+    MAXIMUM_LATITUDE = MINIMUM_LATITUDE = 0.0
+    WESTERNMOST_LONGITUDE = EASTERNMOST_LONGITUDE = 0.0
+    OFFSET = SCALING_FACTOR = 0.0
+    SAMPLE_TYPE = UNIT = TARGET_NAME = RadiusUM = Message = ""
+
     props = ["FromLat", "ToLat", "FromLong", "ToLong", "Scale"]
     for p in props:
         if p in bpy.types.Scene.bl_rna.properties:
-            exec("del bpy.types.Scene."+p)
+            exec("del bpy.types.Scene." + p)
         if p in bpy.context.scene:
             del bpy.context.scene[p]
-            
-#Import the data and draw the planet  
+
+
+#Import the data and draw the planet
 class Import(bpy.types.Operator):
-    bl_idname = 'Import'
+    bl_idname = 'import.lro_and_mgs'
     bl_label = 'Start Import'
     bl_description = 'Import the data'
 
     def execute(self, context):
-        From_Lat=RealLat(bpy.context.scene['FromLat'])
-        To_Lat=RealLat(bpy.context.scene['ToLat'])
-        From_Long=RealLong(bpy.context.scene['FromLong'])
-        To_Long=RealLong(bpy.context.scene['ToLong'])
-        BlenderScale=bpy.context.scene['Scale']
-        Exag=bpy.context.scene['Exaggerate']
-        Vertex=[] #Vertex array
-        Faces=[]  #Faces arrays
-        FirstRow=[]
-        SecondRow=[]
+        From_Lat = RealLat(bpy.context.scene['FromLat'])
+        To_Lat = RealLat(bpy.context.scene['ToLat'])
+        From_Long = RealLong(bpy.context.scene['FromLong'])
+        To_Long = RealLong(bpy.context.scene['ToLong'])
+        BlenderScale = bpy.context.scene['Scale']
+        Exag = bpy.context.scene['Exaggerate']
+        Vertex = []  # Vertex array
+        Faces = []  # Faces arrays
+        FirstRow = []
+        SecondRow = []
         print('*** Start create vertex ***')
-        FileAndPath=bpy.context.scene['fpath']
-        FileAndExt=os.path.splitext(FileAndPath)
+        FileAndPath = bpy.context.scene['fpath']
+        FileAndExt = os.path.splitext(FileAndPath)
         #Check for UNIX that is case sensitive
         #If the Ext of the file selected from user is Upper, than the second file is Upper and Viceversa
-        if FileAndExt[1].isupper(): 
-            FileName=FileAndExt[0] + ".IMG"
-            print("Upper")
+        if FileAndExt[1].isupper():
+            FileName = FileAndExt[0] + ".IMG"
         else:
-            FileName=FileAndExt[0] + ".img"
-            print("Lower")
-        f=open(FileName,'rb')
-        f.seek(int((int(LatToLine(From_Lat))-1)*(LINE_SAMPLES*(SAMPLE_BITS/8))),1)#Skip the first n line of point
-        SkipFirstPoint=int((LongToPoint(From_Long)-1)*(SAMPLE_BITS/8)) #Nunmber of points to skip
-        PointsToRead=(LongToPoint(To_Long)-LongToPoint(From_Long)+1)*(int(SAMPLE_BITS)/8) #Number of points to be read
-        SkipLastPoint=(LINE_SAMPLES*(SAMPLE_BITS/8))-PointsToRead-SkipFirstPoint #Nunmber of points to skip
-        LatToRead=From_Lat
-        while (LatToRead>=To_Lat):
-            f.seek(SkipFirstPoint,1) #Skip the first n point
-            Altitudes=f.read(int(PointsToRead)) #Read all the point
-            Altitudes=array.array("h",Altitudes) #Change to Array of signed short
-            if SAMPLE_TYPE=="MSB_INTEGER":
-                Altitudes.byteswap() #Change from MSB (big endian) to LSB (little endian)
-            LongToRead=From_Long
-            PointToRead=0
-            while (LongToRead<=To_Long):
+            FileName = FileAndExt[0] + ".img"
+        f = open(FileName, 'rb')
+        f.seek(int((int(LatToLine(From_Lat)) - 1) * (LINE_SAMPLES * (SAMPLE_BITS / 8))), 1)  # Skip the first n line of point
+        SkipFirstPoint = int((LongToPoint(From_Long) - 1) * (SAMPLE_BITS / 8))  # Nunmber of points to skip
+        PointsToRead = (LongToPoint(To_Long) - LongToPoint(From_Long) + 1) * (int(SAMPLE_BITS) / 8)  # Number of points to be read
+        SkipLastPoint = (LINE_SAMPLES * (SAMPLE_BITS / 8)) - PointsToRead - SkipFirstPoint  # Nunmber of points to skip
+        LatToRead = From_Lat
+        while (LatToRead >= To_Lat):
+            f.seek(SkipFirstPoint, 1)  # Skip the first n point
+            Altitudes = f.read(int(PointsToRead))  # Read all the point
+            Altitudes = array.array("h", Altitudes)  # Change to Array of signed short
+            if SAMPLE_TYPE == "MSB_INTEGER":
+                Altitudes.byteswap()  # Change from MSB (big endian) to LSB (little endian)
+            LongToRead = From_Long
+            PointToRead = 0
+            while (LongToRead <= To_Long):
                 if Exag:
-                    tmpRadius=(Altitudes[PointToRead]/SCALING_FACTOR/1000+OFFSET)/BlenderScale #Old formula (High*4)
+                    tmpRadius = (Altitudes[PointToRead] / SCALING_FACTOR / 1000 + OFFSET) / BlenderScale  # Old formula (High*4)
                 else:
-                    tmpRadius=((Altitudes[PointToRead]*SCALING_FACTOR)/1000+OFFSET)/BlenderScale #Correct scale
-                CurrentRadius=tmpRadius*(math.cos(LatToRead*TO_RAD))
-                X=CurrentRadius*(math.sin(LongToRead*TO_RAD))
-                Y=tmpRadius*math.sin(LatToRead*TO_RAD)
-                Z=CurrentRadius*math.cos(LongToRead*TO_RAD)
-                Vertex.append(Vector((float(X),float(Y),float(Z))))
-                LongToRead+=(1/MAP_RESOLUTION)
-                PointToRead+=1
-            f.seek(int(SkipLastPoint),1)       
-            LatToRead-=(1/MAP_RESOLUTION)
+                    tmpRadius = ((Altitudes[PointToRead] * SCALING_FACTOR) / 1000 + OFFSET) / BlenderScale  # Correct scale
+                CurrentRadius = tmpRadius * (math.cos(LatToRead * TO_RAD))
+                X = CurrentRadius * (math.sin(LongToRead * TO_RAD))
+                Y = tmpRadius * math.sin(LatToRead * TO_RAD)
+                Z = CurrentRadius * math.cos(LongToRead * TO_RAD)
+                Vertex.append(Vector((float(X), float(Y), float(Z))))
+                LongToRead += (1 / MAP_RESOLUTION)
+                PointToRead += 1
+            f.seek(int(SkipLastPoint), 1)
+            LatToRead -= (1 / MAP_RESOLUTION)
         f.close
         del Altitudes
         print('*** End create Vertex   ***')
 
         print('*** Start create faces ***')
-        LinesToRead=int(LatToLine(To_Lat)-LatToLine(From_Lat)+1)   #Number of the lines to read
-        PointsToRead=int(LongToPoint(To_Long)-LongToPoint(From_Long)+1) #Number of the points to read
-        for Point in range(0,PointsToRead):
+        LinesToRead = int(LatToLine(To_Lat) - LatToLine(From_Lat) + 1)   # Number of the lines to read
+        PointsToRead = int(LongToPoint(To_Long) - LongToPoint(From_Long) + 1)  # Number of the points to read
+        for Point in range(0, PointsToRead):
             FirstRow.append(Point)
-            SecondRow.append(Point+PointsToRead)
-        if int(PointsToRead)==LINE_SAMPLES:
-            FaceTemp=createFaces(FirstRow,SecondRow,closed=True,flipped=True)
+            SecondRow.append(Point + PointsToRead)
+        if int(PointsToRead) == LINE_SAMPLES:
+            FaceTemp = createFaces(FirstRow, SecondRow, closed=True, flipped=True)
         else:
-            FaceTemp=createFaces(FirstRow,SecondRow,closed=False,flipped=True)
+            FaceTemp = createFaces(FirstRow, SecondRow, closed=False, flipped=True)
         Faces.extend(FaceTemp)
-        
-        FaceTemp=[]
-        for Line in range(1,(LinesToRead-1)):
-            FirstRow=SecondRow
-            SecondRow=[] 
-            FacesTemp=[]
-            for Point in range(0,PointsToRead):
-                SecondRow.append(Point+(Line+1)*PointsToRead)
-            if int(PointsToRead)==LINE_SAMPLES:
-                FaceTemp=createFaces(FirstRow,SecondRow,closed=True,flipped=True)
+
+        FaceTemp = []
+        for Line in range(1, (LinesToRead - 1)):
+            FirstRow = SecondRow
+            SecondRow = []
+            FacesTemp = []
+            for Point in range(0, PointsToRead):
+                SecondRow.append(Point + (Line + 1) * PointsToRead)
+            if int(PointsToRead) == LINE_SAMPLES:
+                FaceTemp = createFaces(FirstRow, SecondRow, closed=True, flipped=True)
             else:
-                FaceTemp=createFaces(FirstRow,SecondRow,closed=False,flipped=True)
+                FaceTemp = createFaces(FirstRow, SecondRow, closed=False, flipped=True)
             Faces.extend(FaceTemp)
         del FaceTemp
         print('*** End create faces   ***')
 
-        print ('*** Start draw ***')        
+        print ('*** Start draw ***')
         mesh = bpy.data.meshes.new(TARGET_NAME)
         mesh.from_pydata(Vertex, [], Faces)
         del Faces
@@ -419,17 +444,18 @@ class Import(bpy.types.Operator):
         scene.objects.link(ob_new)
         scene.objects.active = ob_new
         ob_new.select = True
-        print ('*** End draw   ***')        
+        print ('*** End draw   ***')
         print('*** Start Smooth ***')
         bpy.ops.object.shade_smooth()
         print('*** End Smooth   ***')
-        if TARGET_NAME=="MOON":
+        if TARGET_NAME == "MOON":
             MakeMaterialMoon(ob_new)
-        elif TARGET_NAME=="MARS":
+        elif TARGET_NAME == "MARS":
             MakeMaterialMars(ob_new)
         print('*** FINISHED ***')
         return {'FINISHED'}
-        
+
+
 # drawing the user interface
 class Img_Importer(bpy.types.Panel):
     bl_space_type = "VIEW_3D"
@@ -437,12 +463,19 @@ class Img_Importer(bpy.types.Panel):
     bl_label = "LRO Lola & MGS Mola IMG Importer"
 
     def __init__(self, context):
-        LINES=LINE_SAMPLES=SAMPLE_BITS=MAP_RESOLUTION=0
-        MAXIMUM_LATITUDE=MINIMUM_LATITUDE=WESTERNMOST_LONGITUDE=EASTERNMOST_LONGITUDE=OFFSET=SCALING_FACTOR=0.0
-        SAMPLE_TYPE=UNIT=TARGET_NAME=RadiusUM=Message=""
+        LINES = LINE_SAMPLES = SAMPLE_BITS = MAP_RESOLUTION = 0
+        MAXIMUM_LATITUDE = MINIMUM_LATITUDE = 0.0
+        WESTERNMOST_LONGITUDE = EASTERNMOST_LONGITUDE = 0.0
+        OFFSET = SCALING_FACTOR = 0.0
+        SAMPLE_TYPE = UNIT = TARGET_NAME = RadiusUM = Message = ""
 
         # intializing variables
-        props = [("FromLat",0.0),("ToLat",0.0),("FromLong",0.0),("ToLong",0.0),("Scale",1),("Exaggerate",False)]
+        props = [("FromLat", 0.0),
+                 ("ToLat", 0.0),
+                 ("FromLong", 0.0),
+                 ("ToLong", 0.0),
+                 ("Scale", 1),
+                 ("Exaggerate", False)]
         for p, num in props:
             if not p in bpy.context.scene.keys():
                 bpy.context.scene[p] = num
@@ -452,48 +485,48 @@ class Img_Importer(bpy.types.Panel):
         typ = bpy.types.Scene
         var = bpy.props
         typ.fpath = var.StringProperty(name="Import File ", description="Select your img file", subtype="FILE_PATH", default="")
-        
+
         ReadLabel(bpy.context.scene.fpath)
-        
-        typ.FromLat=var.FloatProperty(description="From Latitude", min=float(MINIMUM_LATITUDE), max=float(MAXIMUM_LATITUDE),precision=3)
-        typ.ToLat=var.FloatProperty(description="To Latitude", min=float(MINIMUM_LATITUDE), max=float(MAXIMUM_LATITUDE),precision=3)
-        typ.FromLong=var.FloatProperty(description="From Longitude", min=float(WESTERNMOST_LONGITUDE), max=float(EASTERNMOST_LONGITUDE),precision=3)
-        typ.ToLong=var.FloatProperty(description="To Longitude", min=float(WESTERNMOST_LONGITUDE), max=float(EASTERNMOST_LONGITUDE),precision=3)
-        typ.Scale=var.IntProperty(description="Scale", min=1, max=100,default=1)
-        typ.Exaggerate=var.BoolProperty(description="Magnify", default=False)
+
+        typ.FromLat = var.FloatProperty(description="From Latitude", min=float(MINIMUM_LATITUDE), max=float(MAXIMUM_LATITUDE), precision=3)
+        typ.ToLat = var.FloatProperty(description="To Latitude", min=float(MINIMUM_LATITUDE), max=float(MAXIMUM_LATITUDE), precision=3)
+        typ.FromLong = var.FloatProperty(description="From Longitude", min=float(WESTERNMOST_LONGITUDE), max=float(EASTERNMOST_LONGITUDE), precision=3)
+        typ.ToLong = var.FloatProperty(description="To Longitude", min=float(WESTERNMOST_LONGITUDE), max=float(EASTERNMOST_LONGITUDE), precision=3)
+        typ.Scale = var.IntProperty(description="Scale", min=1, max=100, default=1)
+        typ.Exaggerate = var.BoolProperty(description="Magnify", default=False)
 
         layout = self.layout
         layout.prop(context.scene, "fpath")
-        
+
         col = layout.column()
         split = col.split(align=True)
-        split.label("Minimum Latitude: "+str(MINIMUM_LATITUDE)+" deg")
-        split.label("Maximum Latitude: "+str(MAXIMUM_LATITUDE)+" deg")
-        
+        split.label("Minimum Latitude: " + str(MINIMUM_LATITUDE) + " deg")
+        split.label("Maximum Latitude: " + str(MAXIMUM_LATITUDE) + " deg")
+
         split = col.split(align=True)
-        split.label("Westernmost Longitude: "+str(WESTERNMOST_LONGITUDE)+" deg")
-        split.label("Easternmost Longitude: "+str(EASTERNMOST_LONGITUDE)+" deg")        
+        split.label("Westernmost Longitude: " + str(WESTERNMOST_LONGITUDE) + " deg")
+        split.label("Easternmost Longitude: " + str(EASTERNMOST_LONGITUDE) + " deg")
 
         split = col.split(align=True)
         split.label("Lines: " + str(LINES))
         split.label("Line samples: " + str(LINE_SAMPLES))
-        
+
         split = col.split(align=True)
-        split.label("Sample type: "+str(SAMPLE_TYPE))
-        split.label("Sample bits: "+str(SAMPLE_BITS))
+        split.label("Sample type: " + str(SAMPLE_TYPE))
+        split.label("Sample bits: " + str(SAMPLE_BITS))
 
         split = col.split(align=True)
-        split.label("Unit: "+UNIT)
-        split.label("Map resolution: "+str(MAP_RESOLUTION) + " pix/deg")
-        
+        split.label("Unit: " + UNIT)
+        split.label("Map resolution: " + str(MAP_RESOLUTION) + " pix/deg")
+
         split = col.split(align=True)
-        split.label("Radius: "+str(OFFSET)+ " " + RadiusUM)
-        split.label("Scale: "+str(SCALING_FACTOR))
-        
+        split.label("Radius: " + str(OFFSET) + " " + RadiusUM)
+        split.label("Scale: " + str(SCALING_FACTOR))
+
         split = col.split(align=True)
         split.label("Target: ")
         split.label(TARGET_NAME)
-                
+
         col = layout.column()
         split = col.split(align=True)
         split.prop(context.scene, "FromLat", "Northernmost Lat.")
@@ -502,7 +535,7 @@ class Img_Importer(bpy.types.Panel):
             col = layout.column()
             split = col.split(align=True)
             split.label("Warning: Northernmost must be greater than Southernmost")
-        
+
         col = layout.column()
         split = col.split(align=True)
         split.prop(context.scene, "FromLong", "Westernmost Long.")
@@ -511,47 +544,49 @@ class Img_Importer(bpy.types.Panel):
             col = layout.column()
             split = col.split(align=True)
             split.label("Warning: Easternmost must be greater than Westernmost")
-        
+
         col = layout.column()
         split = col.split(align=True)
         split.prop(context.scene, "Scale", "Scale")
         split.prop(context.scene, "Exaggerate", "Magnify (x4)")
-        if bpy.context.scene.fpath !="":
+        if bpy.context.scene.fpath != "":
             col = layout.column()
             split = col.split(align=True)
-            split.label("1 Blender unit = " + str(bpy.context.scene['Scale']) +RadiusUM)  
-            
-        if Message!="":
+            split.label("1 Blender unit = " + str(bpy.context.scene['Scale']) + RadiusUM)
+
+        if Message != "":
             col = layout.column()
             split = col.split(align=True)
             split.label("Message: " + Message)
-        
-        if bpy.context.scene.fpath.upper().endswith("IMG") or bpy.context.scene.fpath.upper().endswith("LBL"): #Check if is selected the correct file
-            VertNumbers=(((RealLat(bpy.context.scene['FromLat'])-RealLat(bpy.context.scene['ToLat']))*MAP_RESOLUTION)+1)*((RealLong(bpy.context.scene['ToLong'])-RealLong(bpy.context.scene['FromLong']))*MAP_RESOLUTION+1)
+
+        if bpy.context.scene.fpath.upper().endswith("IMG") or bpy.context.scene.fpath.upper().endswith("LBL"):  # Check if is selected the correct file
+            VertNumbers = (((RealLat(bpy.context.scene['FromLat']) - RealLat(bpy.context.scene['ToLat'])) * MAP_RESOLUTION) + 1) * ((RealLong(bpy.context.scene['ToLong']) - RealLong(bpy.context.scene['FromLong'])) * MAP_RESOLUTION + 1)
         else:
-            VertNumbers=0
+            VertNumbers = 0
         #If I have 4 or plus vertex and at least 2 row and at least 2 point, I can import
-        if VertNumbers>3 and (RealLat(bpy.context.scene['FromLat'])> RealLat(bpy.context.scene['ToLat'])) and (RealLong(bpy.context.scene['FromLong'])<RealLong(bpy.context.scene['ToLong'])): #If I have 4 or plus vertex I can import
+        if VertNumbers > 3 and (RealLat(bpy.context.scene['FromLat']) > RealLat(bpy.context.scene['ToLat'])) and (RealLong(bpy.context.scene['FromLong']) < RealLong(bpy.context.scene['ToLong'])):  # If I have 4 or plus vertex I can import
             split = col.split(align=True)
             split.label("Map resolution on the equator: ")
-            split.label(str(2*math.pi*OFFSET/360/MAP_RESOLUTION) + " " + RadiusUM + "/pix")
+            split.label(str(2 * math.pi * OFFSET / 360 / MAP_RESOLUTION) + " " + RadiusUM + "/pix")
             col = layout.column()
             split = col.split(align=True)
-            split.label("Real Northernmost Lat.: " + str(RealLat(bpy.context.scene['FromLat']))+" deg")
-            split.label("Real Southernmost Long.: " + str(RealLat(bpy.context.scene['ToLat']))+" deg")
+            split.label("Real Northernmost Lat.: " + str(RealLat(bpy.context.scene['FromLat'])) + " deg")
+            split.label("Real Southernmost Long.: " + str(RealLat(bpy.context.scene['ToLat'])) + " deg")
             split = col.split(align=True)
-            split.label("Real Westernmost Long.: " + str(RealLong(bpy.context.scene['FromLong']))+" deg")
-            split.label("Real Easternmost Long.: " + str(RealLong(bpy.context.scene['ToLong']))+"deg")
+            split.label("Real Westernmost Long.: " + str(RealLong(bpy.context.scene['FromLong'])) + " deg")
+            split.label("Real Easternmost Long.: " + str(RealLong(bpy.context.scene['ToLong'])) + "deg")
             split = col.split(align=True)
-            VertNumbers=(((RealLat(bpy.context.scene['FromLat'])-RealLat(bpy.context.scene['ToLat']))*MAP_RESOLUTION)+1)*((RealLong(bpy.context.scene['ToLong'])-RealLong(bpy.context.scene['FromLong']))*MAP_RESOLUTION+1)
-            split.label("Numbers of vertex to be imported: "+ str(int(VertNumbers)))
+            #VertNumbers = (((RealLat(bpy.context.scene['FromLat']) - RealLat(bpy.context.scene['ToLat'])) * MAP_RESOLUTION) + 1) * ((RealLong(bpy.context.scene['ToLong']) - RealLong(bpy.context.scene['FromLong'])) * MAP_RESOLUTION + 1)
+            split.label("Numbers of vertex to be imported: " + str(int(VertNumbers)))
             col.separator()
-            col.operator('Import', text='Import')
-    
+            col.operator('import.lro_and_mgs', text='Import')
+
+
 # registering the script
 def register():
     pass
 
+
 def unregister():
     clear_properties()