#!/home/yohai/anaconda3/bin/python
#Last update by <NAME> Oct 7 2008"
print('''
 Program to convert from Gamma parameter file to 
 ROI_PAC .rsc file and to ERMapper .ers file
 For lat/lon (EQA) geocoded images or ungeocoded"

''')
import sys,os,re,time,glob,string,numpy,getopt

def usage():
  sys.exit('''
 Usage: '''+sys.argv[0]+''' [infile] [par_file] <options>
   infile   - data file name
   par_file - Gamma parameter file (as for the dem for geocoded or 
              as for the mli for ungeocoded)
   options:
     -e    - endianess: 0 big, 1 small (default 0)
     -h    - header: 0 ERMapper, 1 roi_pac, 2 both (default 2)
     -i    - 2 byte integer data type (default 4 byte real data type)
     -g     - geocoded (dafault - not geocoded)

''')

try:
  opts,args = getopt.gnu_getopt(sys.argv[1:],'e:h:ig')
except getopt.error:
  print("Wrong arguments!")
  usage()
opts = dict(opts)
if len(args) < 2:
  usage()
# initializing parameters
infile = sys.argv[1]
parfile = sys.argv[2]
if not (os.access(infile,os.F_OK) and os.access(parfile,os.F_OK)):
  print("Can't find "+infile+" or "+parfile)
  usage()
# endianess:
en = int(opts.get('-e','0'))
if not en in (0,1):
  print("Warning! endianess set error")
  usage()
# header types:
hd = int(opts.get('-h','2'))
if not hd in (0,1,2):
  print("Warning! headers set error")
  usage()
# data type:
if '-i' in opts:
  dtype = "Signed16BitInteger"
else:
  dtype = "IEEE4ByteReal"
# geocoded or not:
if '-g' in opts:
  geocoded = True
else:
  geocoded = False



# data collection
print("**** Getting data from parameter file: "+parfile,'geocoded:',geocoded)
if geocoded:
  width = os.popen("grep width: "+parfile).read().split()[1]
  xmax = str(int(width)-1)
  length = os.popen("grep nlines: "+parfile).read().split()[1]
  ymax = str(int(length)-1)
  x_first = os.popen("grep corner_lon: "+parfile).read().split()[1]
  y_first = os.popen("grep corner_lat: "+parfile).read().split()[1]
  x_step = os.popen("grep post_lon: "+parfile).read().split()[1]
  y_step = os.popen("grep post_lat: "+parfile).read().split()[1]
  x_first = str(float(x_first)-float(x_step)/2)
  y_first = str(float(y_first)+float(y_step)/2)
else:
  width = os.popen("grep range_samples: "+parfile).read().split()[1]
  xmax = str(int(width)-1)
  length = os.popen("grep azimuth_lines: "+parfile).read().split()[1]
  ymax = str(int(length)-1)
  x_first = '0'
  y_first = '0'
  x_step = '1'
  y_step = '1'

# output
if hd in (1,2):
  print("**** Writting rsc file")
  RSC = open(infile+'.rsc','w')
  RSC.write("WIDTH         "+width+"\n")
  RSC.write("FILE_LENGTH   "+length+"\n")
  RSC.write("XMIN          0\n")
  RSC.write("XMAX          "+xmax+"\n")
  RSC.write("YMIN          0\n")
  RSC.write("YMAX          "+ymax+"\n")
  RSC.write("X_FIRST       "+x_first +"\n")
  RSC.write("Y_FIRST       "+y_first+"\n")
  RSC.write("X_STEP        "+x_step+"\n")
  RSC.write("Y_STEP        "+y_step+"\n")
  RSC.write("X_UNIT        degres\n")
  RSC.write("Y_UNIT        degres\n")
  RSC.write("Z_OFFSET      0\n")
  RSC.write("Z_SCALE       1\n")
  RSC.write("PROJECTION    LATLON\n")
  RSC.write("DATUM         WGS84\n")
  RSC.close()

if hd in (0,2):
  print("**** Writting ers file")
  ERS = open(infile+'.ers','w')
  ERS.write("DatasetHeader Begin \n")
  ERS.write("        Version         = \"6.4\"\n") 
  ERS.write("        Name            =\""+infile+"\"\n")
  ERS.write("        LastUpdated     = "+time.strftime("%a %b %d %H:%M:%S GMT %Y",time.gmtime(time.time()))+"\n")
  ERS.write("        DataSetType     = ERStorage \n")
  ERS.write("        DataType        = Raster \n")
  if en:
    ERS.write("        ByteOrder       = LSBFirst\n")
  else:
    ERS.write("        ByteOrder       = MSBFirst\n")
  ERS.write("        CoordinateSpace Begin\n")
  ERS.write("                Datum           = \"WGS84\"\n")
  ERS.write("                Projection      = \"GEODETIC\"\n")
  ERS.write("                CoordinateType  = EN\n")
  ERS.write("                Rotation        = 0:0:0.0\n")
  ERS.write("        CoordinateSpace End\n")
  ERS.write("        RasterInfo Begin\n")
  ERS.write("               CellType        = "+dtype+"\n")
  ERS.write("               NullCellValue   = 0\n")
  ERS.write("                CellInfo Begin\n")
  ERS.write("                        Xdimension      = "+x_step+"\n")
  ERS.write("                        Ydimension      = "+x_step+"\n")
  ERS.write("                CellInfo End\n")
  ERS.write("                NrOfLines       = "+length+"\n")
  ERS.write("                NrOfCellsPerLine        = "+width+"\n")
  ERS.write("                RegistrationCoord Begin\n")
  ERS.write("                        Eastings        ="+x_first+"\n")
  ERS.write("                        Northings       = "+y_first+"\n")
  ERS.write("                RegistrationCoord End\n")
  ERS.write("               NrOfBands       = 1\n")
  ERS.write("               BandId Begin\n")
  ERS.write("                       Value           = \""+infile+"\"\n")
  ERS.write("               BandId End\n")
  ERS.write("        RasterInfo End\n")
  ERS.write("DatasetHeader End\n")
  ERS.close()

print("****** Done.\n\n")

#!/home/yohai/anaconda3/bin/python
#Last update by <NAME> Oct 7 2008"
print('''
 Program to convert from Gamma parameter file to 
 ROI_PAC .rsc file and to ERMapper .ers file
 For lat/lon (EQA) geocoded images or ungeocoded"

''')
import sys,os,re,time,glob,string,numpy,getopt

def usage():
  sys.exit('''
 Usage: '''+sys.argv[0]+''' [infile] [par_file] <options>
   infile   - data file name
   par_file - Gamma parameter file (as for the dem for geocoded or 
              as for the mli for ungeocoded)
   options:
     -e    - endianess: 0 big, 1 small (default 0)
     -h    - header: 0 ERMapper, 1 roi_pac, 2 both (default 2)
     -i    - 2 byte integer data type (default 4 byte real data type)
     -g     - geocoded (dafault - not geocoded)

''')

try:
  opts,args = getopt.gnu_getopt(sys.argv[1:],'e:h:ig')
except getopt.error:
  print("Wrong arguments!")
  usage()
opts = dict(opts)
if len(args) < 2:
  usage()
# initializing parameters
infile = sys.argv[1]
parfile = sys.argv[2]
if not (os.access(infile,os.F_OK) and os.access(parfile,os.F_OK)):
  print("Can't find "+infile+" or "+parfile)
  usage()
# endianess:
en = int(opts.get('-e','0'))
if not en in (0,1):
  print("Warning! endianess set error")
  usage()
# header types:
hd = int(opts.get('-h','2'))
if not hd in (0,1,2):
  print("Warning! headers set error")
  usage()
# data type:
if '-i' in opts:
  dtype = "Signed16BitInteger"
else:
  dtype = "IEEE4ByteReal"
# geocoded or not:
if '-g' in opts:
  geocoded = True
else:
  geocoded = False



# data collection
print("**** Getting data from parameter file: "+parfile,'geocoded:',geocoded)
if geocoded:
  width = os.popen("grep width: "+parfile).read().split()[1]
  xmax = str(int(width)-1)
  length = os.popen("grep nlines: "+parfile).read().split()[1]
  ymax = str(int(length)-1)
  x_first = os.popen("grep corner_lon: "+parfile).read().split()[1]
  y_first = os.popen("grep corner_lat: "+parfile).read().split()[1]
  x_step = os.popen("grep post_lon: "+parfile).read().split()[1]
  y_step = os.popen("grep post_lat: "+parfile).read().split()[1]
  x_first = str(float(x_first)-float(x_step)/2)
  y_first = str(float(y_first)+float(y_step)/2)
else:
  width = os.popen("grep range_samples: "+parfile).read().split()[1]
  xmax = str(int(width)-1)
  length = os.popen("grep azimuth_lines: "+parfile).read().split()[1]
  ymax = str(int(length)-1)
  x_first = '0'
  y_first = '0'
  x_step = '1'
  y_step = '1'

# output
if hd in (1,2):
  print("**** Writting rsc file")
  RSC = open(infile+'.rsc','w')
  RSC.write("WIDTH         "+width+"\n")
  RSC.write("FILE_LENGTH   "+length+"\n")
  RSC.write("XMIN          0\n")
  RSC.write("XMAX          "+xmax+"\n")
  RSC.write("YMIN          0\n")
  RSC.write("YMAX          "+ymax+"\n")
  RSC.write("X_FIRST       "+x_first +"\n")
  RSC.write("Y_FIRST       "+y_first+"\n")
  RSC.write("X_STEP        "+x_step+"\n")
  RSC.write("Y_STEP        "+y_step+"\n")
  RSC.write("X_UNIT        degres\n")
  RSC.write("Y_UNIT        degres\n")
  RSC.write("Z_OFFSET      0\n")
  RSC.write("Z_SCALE       1\n")
  RSC.write("PROJECTION    LATLON\n")
  RSC.write("DATUM         WGS84\n")
  RSC.close()

if hd in (0,2):
  print("**** Writting ers file")
  ERS = open(infile+'.ers','w')
  ERS.write("DatasetHeader Begin \n")
  ERS.write("        Version         = \"6.4\"\n") 
  ERS.write("        Name            =\""+infile+"\"\n")
  ERS.write("        LastUpdated     = "+time.strftime("%a %b %d %H:%M:%S GMT %Y",time.gmtime(time.time()))+"\n")
  ERS.write("        DataSetType     = ERStorage \n")
  ERS.write("        DataType        = Raster \n")
  if en:
    ERS.write("        ByteOrder       = LSBFirst\n")
  else:
    ERS.write("        ByteOrder       = MSBFirst\n")
  ERS.write("        CoordinateSpace Begin\n")
  ERS.write("                Datum           = \"WGS84\"\n")
  ERS.write("                Projection      = \"GEODETIC\"\n")
  ERS.write("                CoordinateType  = EN\n")
  ERS.write("                Rotation        = 0:0:0.0\n")
  ERS.write("        CoordinateSpace End\n")
  ERS.write("        RasterInfo Begin\n")
  ERS.write("               CellType        = "+dtype+"\n")
  ERS.write("               NullCellValue   = 0\n")
  ERS.write("                CellInfo Begin\n")
  ERS.write("                        Xdimension      = "+x_step+"\n")
  ERS.write("                        Ydimension      = "+x_step+"\n")
  ERS.write("                CellInfo End\n")
  ERS.write("                NrOfLines       = "+length+"\n")
  ERS.write("                NrOfCellsPerLine        = "+width+"\n")
  ERS.write("                RegistrationCoord Begin\n")
  ERS.write("                        Eastings        ="+x_first+"\n")
  ERS.write("                        Northings       = "+y_first+"\n")
  ERS.write("                RegistrationCoord End\n")
  ERS.write("               NrOfBands       = 1\n")
  ERS.write("               BandId Begin\n")
  ERS.write("                       Value           = \""+infile+"\"\n")
  ERS.write("               BandId End\n")
  ERS.write("        RasterInfo End\n")
  ERS.write("DatasetHeader End\n")
  ERS.close()

print("****** Done.\n\n")

#!/home/yohai/anaconda3/bin/python #Last update by <NAME> Oct 7 2008" Program to convert from Gamma parameter file to   ROI_PAC .rsc file and to ERMapper .ers file  For lat/lon (EQA) geocoded images or ungeocoded" Usage: [infile] [par_file] <options>    infile   - data file name    par_file - Gamma parameter file (as for the dem for geocoded or                as for the mli for ungeocoded)    options:      -e    - endianess: 0 big, 1 small (default 0)      -h    - header: 0 ERMapper, 1 roi_pac, 2 both (default 2)      -i    - 2 byte integer data type (default 4 byte real data type)      -g     - geocoded (dafault - not geocoded) # initializing parameters # endianess: # header types: # data type: # geocoded or not: # data collection # output