<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __BEGIN_LICENSE__
#  Copyright (c) 2009-2013, United States Government as represented by the
#  Administrator of the National Aeronautics and Space Administration. All
#  rights reserved.
#
#  The NGT platform is licensed under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance with the
#  License. You may obtain a copy of the License at
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
# __END_LICENSE__

import sys, os, glob, optparse, re, shutil, subprocess, string, time


def man(option, opt, value, parser):
    print >>sys.stderr, parser.usage
    print >>sys.stderr, '''\
Merge the geo information with an ASU map projected CTX JP2 file.
'''
    sys.exit()

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

#--------------------------------------------------------------------------------

# TODO: Move to IrgFileFunctions!
def replaceFileExtension(inputPath, ext):
    '''Replace the extension on a file'''
    return os.path.splitext(inputPath)[0] + ext

    
def editProjFile(projPath):
    '''Modifies a .prj file so it matches a specific format'''
    
    if not os.path.exists(projPath):
        raise Exception('isis3world.pl script failed to create required output files!')

    # Open files    
    outputPath = projPath + '.edit.prj'
    inputFile  = open(projPath,   'r')
    outputFile = open(outputPath, 'w')
    
    # Replace specific text
    for line in inputFile:
        line = line.replace('D_Mars', 'Mars')
        line = line.replace('PROJECTION["Equidistant_Cylindrical"]',
                            'PROJECTION["Equirectangular"],')
        line = line.replace('D_Mars',              'Mars')
        line = line.replace('Central_Meridian',    'central_meridian')
        line = line.replace('False_Northing',      'false_northing')
        line = line.replace('False_Easting',       'false_easting')
        line = line.replace('UNIT["Meter",1.0]',       'UNIT["meter",1.0,AUTHORITY["EPSG","9001"]]')
        if 'Standard_Parallel_1' in line:
            s1     = line.find('Standard_Parallel_1')
            s2     = line.find(',', s1)
            s3     = line.find(']', s2)
            number = line[s2+1:s3-1]
        line = line.replace('PARAMETER["Standard_Parallel_1",'+number+']',
                            'PARAMETER["standard_parallel_1",'+number+'],PARAMETER["latitude_of_origin",'+number+']')       
        outputFile.write(line)
    
    # Clean up
    inputFile.close()
    outputFile.close()
    return outputPath
    

def addGeoData(inputJp2Path, inputHeaderPath, outputPath, keep=False):
    """Does the actual work of adding the geo data"""

    if not os.path.exists(inputJp2Path):
        raise Exception('Input file ' + inputJp2Path + ' does not exist!')
    if not os.path.exists(inputHeaderPath):
        raise Exception('Input file ' + inputHeaderPath + ' does not exist!')


    # Get the needed paths
    prjPath = replaceFileExtension(inputJp2Path, '.prj')
    vrtPath = replaceFileExtension(inputJp2Path, '.vrt')
    
    # The perl script works best from the input folder
    originalDirectory = os.getcwd()
    print originalDirectory
    inputFolder = os.path.dirname(inputJp2Path)
    print inputFolder
    if inputFolder != '':
        os.chdir(inputFolder)

    # Call perl script, then return to original directory
    cmd = 'isis3world.pl -J -prj ' + os.path.basename(inputHeaderPath)
    print cmd
    os.system(cmd)
    if inputFolder != '':
        os.chdir(originalDirectory)

    # If the first command is not called from the input folder, need to do this
    #mv .j2w <INPUT FOLDER>/B01_009838_2108_XI_30N319W.j2w
    #mv .prj <INPUT FOLDER>/B01_009838_2108_XI_30N319W.prj

    correctedProjPath = editProjFile(prjPath)

    if (not os.path.exists(correctedProjPath)):
        raise Exception('Failed to correct proj file!')
    

    # Next conversion command
    #cmd = 'gdal_translate -of VRT -a_srs ESRI::"'+ prjPath +'" -a_nodata 0  '+ inputJp2Path +' '+ vrtPath

    ## Finish the conversion
    #cmd = 'gdal_translate -of JP2OpenJPEG '+ vrtPath +' '+ outputPath
    #print(cmd)
    #os.system(cmd)
    
    cmd = 'cp '+ inputJp2Path +'  '+ outputPath
    print(cmd)
    os.system(cmd)
    
    prjText=correctedProjPath.read()
    print prjText
    cmd = 'gdal_edit.py -a_srs "'+ prjText +'"  '+ outputPath
    print(cmd)
    os.system(cmd)



    # Clean up temporary files
    if not keep:
        #os.remove(vrtPath)<commit_msg>Bug fix to addGeo<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __BEGIN_LICENSE__
#  Copyright (c) 2009-2013, United States Government as represented by the
#  Administrator of the National Aeronautics and Space Administration. All
#  rights reserved.
#
#  The NGT platform is licensed under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance with the
#  License. You may obtain a copy of the License at
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
# __END_LICENSE__

import sys, os, glob, optparse, re, shutil, subprocess, string, time


def man(option, opt, value, parser):
    print >>sys.stderr, parser.usage
    print >>sys.stderr, '''\
Merge the geo information with an ASU map projected CTX JP2 file.
'''
    sys.exit()

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

#--------------------------------------------------------------------------------

# TODO: Move to IrgFileFunctions!
def replaceFileExtension(inputPath, ext):
    '''Replace the extension on a file'''
    return os.path.splitext(inputPath)[0] + ext

    
def editProjFile(projPath):
    '''Modifies a .prj file so it matches a specific format'''
    
    if not os.path.exists(projPath):
        raise Exception('isis3world.pl script failed to create required output files!')

    # Open files    
    outputPath = projPath + '.edit.prj'
    inputFile  = open(projPath,   'r')
    outputFile = open(outputPath, 'w')
    
    # Replace specific text
    for line in inputFile:
        line = line.replace('D_Mars', 'Mars')
        line = line.replace('PROJECTION["Equidistant_Cylindrical"]',
                            'PROJECTION["Equirectangular"]')
        line = line.replace('D_Mars',              'Mars')
        line = line.replace('Central_Meridian',    'central_meridian')
        line = line.replace('False_Northing',      'false_northing')
        line = line.replace('False_Easting',       'false_easting')
        line = line.replace('UNIT["Meter",1.0]',       'UNIT["meter",1.0,AUTHORITY["EPSG","9001"]]')
        if 'Standard_Parallel_1' in line:
            s1     = line.find('Standard_Parallel_1')
            s2     = line.find(',', s1)
            s3     = line.find(']', s2)
            number = line[s2+1:s3]
            line = line.replace('PARAMETER["Standard_Parallel_1",'+number+']',
                            'PARAMETER["standard_parallel_1",'+number+'],PARAMETER["latitude_of_origin",'+number+']')       
        outputFile.write(line)
    
    # Clean up
    inputFile.close()
    outputFile.close()

    return outputPath
    

def addGeoData(inputJp2Path, inputHeaderPath, outputPath, keep=False):
    """Does the actual work of adding the geo data"""

    if not os.path.exists(inputJp2Path):
        raise Exception('Input file ' + inputJp2Path + ' does not exist!')
    if not os.path.exists(inputHeaderPath):
        raise Exception('Input file ' + inputHeaderPath + ' does not exist!')


    # Get the needed paths
    prjPath = replaceFileExtension(inputJp2Path, '.prj')
    vrtPath = replaceFileExtension(inputJp2Path, '.vrt')
    
    # The perl script works best from the input folder
    originalDirectory = os.getcwd()
    print originalDirectory
    inputFolder = os.path.dirname(inputJp2Path)
    print inputFolder
    if inputFolder != '':
        os.chdir(inputFolder)

    # Call perl script, then return to original directory
    cmd = 'isis3world.pl -J -prj ' + os.path.basename(inputHeaderPath)
    print cmd
    os.system(cmd)
    if inputFolder != '':
        os.chdir(originalDirectory)

    # If the first command is not called from the input folder, need to do this
    #mv .j2w <INPUT FOLDER>/B01_009838_2108_XI_30N319W.j2w
    #mv .prj <INPUT FOLDER>/B01_009838_2108_XI_30N319W.prj

    correctedProjPath = editProjFile(prjPath)

    if (not os.path.exists(correctedProjPath)):
        raise Exception('Failed to correct proj file!')
    

    # Next conversion command
    #cmd = 'gdal_translate -of VRT -a_srs ESRI::"'+ prjPath +'" -a_nodata 0  '+ inputJp2Path +' '+ vrtPath

    ## Finish the conversion
    #cmd = 'gdal_translate -of JP2OpenJPEG '+ vrtPath +' '+ outputPath
    #print(cmd)
    #os.system(cmd)
    
    cmd = 'cp '+ inputJp2Path +'  '+ outputPath
    print(cmd)
    os.system(cmd)
    
    f = open(correctedProjPath, 'r')
    prjText=f.read()
    f.close()
    print prjText
    cmd = 'gdal_edit.py -a_srs "'+ prjText +'"  '+ outputPath
    print(cmd)
    os.system(cmd)



    # Clean up temporary files
    if not keep:
        #os.remove(vrtPath)
{'fraction_non_alphanumeric': 0.1284562799714315, 'fraction_numerical': 0.01703907764513825, 'mean_word_length': 2.772902232486528, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5245481', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3041, 'n_words': 842}