<s> import ee <EOL> import os <EOL> import json <EOL> import threading <EOL> import time <EOL> import xml . etree . cElementTree as ET <EOL> import tempfile <EOL> import zipfile <EOL> import urllib2 <EOL> SENSOR_FILE_DIR = os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , '<STR_LIT>' ) <EOL> TEMP_FILE_DIR = tempfile . gettempdir ( ) <EOL> def getExpandingIndices ( length , minIndex = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> maxIndex = length - <NUM_LIT:1> <EOL> if ( maxIndex < <NUM_LIT:0> ) : <EOL> return [ ] <EOL> center = ( minIndex + maxIndex ) / <NUM_LIT:2> <EOL> offset = <NUM_LIT:1> <EOL> output = [ center ] <EOL> while True : <EOL> if len ( output ) == length : <EOL> return output <EOL> thisIndex = center + offset <EOL> output . append ( thisIndex ) <EOL> if offset > <NUM_LIT:0> : <EOL> offset = - offset <EOL> else : <EOL> offset = - offset + <NUM_LIT:1> <EOL> def safe_get_info ( ee_object , max_num_attempts = <NUM_LIT:5> ) : <EOL> '''<STR_LIT>''' <EOL> fail_messages = [ '<STR_LIT>' ] <EOL> num_attempts = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> return ee_object . getInfo ( ) <EOL> except Exception as e : <EOL> for message in fail_messages : <EOL> if message in str ( e ) : <EOL> raise e <EOL> print '<STR_LIT>' % ( e ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> num_attempts += <NUM_LIT:1> <EOL> if max_num_attempts and ( num_attempts >= max_num_attempts ) : <EOL> raise Exception ( '<STR_LIT>' + str ( num_attempts ) + '<STR_LIT>' ) <EOL> class waitForEeResult ( threading . Thread ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , function , finished_function = None ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . function = function <EOL> self . finished_function = finished_function <EOL> self . setDaemon ( True ) <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> self . finished_function ( self . function ( ) ) <EOL> def prettyPrintEE ( eeObjectInfo ) : <EOL> '''<STR_LIT>''' <EOL> print ( json . dumps ( eeObjectInfo , sort_keys = True , indent = <NUM_LIT:2> ) ) <EOL> def get_permanent_water_mask ( ) : <EOL> '''<STR_LIT>''' <EOL> return ee . Image ( "<STR_LIT>" ) . select ( [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def regionIsInUnitedStates ( region ) : <EOL> '''<STR_LIT>''' <EOL> nationList = ee . FeatureCollection ( '<STR_LIT>' ) <EOL> nation = ee . Feature ( nationList . filter ( ee . Filter . eq ( '<STR_LIT>' , '<STR_LIT>' ) ) . first ( ) ) <EOL> nationGeo = ee . Geometry ( nation . geometry ( ) ) <EOL> result = nationGeo . contains ( region , <NUM_LIT:10> ) <EOL> return ( str ( result . getInfo ( ) ) == '<STR_LIT:True>' ) <EOL> def getDefinedSensorNames ( ) : <EOL> '''<STR_LIT>''' <EOL> return [ f [ : - <NUM_LIT:4> ] for f in os . listdir ( SENSOR_FILE_DIR ) if f . endswith ( '<STR_LIT>' ) ] <EOL> def getDateFromSentinel1Info ( info ) : <EOL> '''<STR_LIT>''' <EOL> idString = info [ '<STR_LIT:id>' ] <EOL> parts = idString . split ( '<STR_LIT:_>' ) <EOL> dateString = parts [ <NUM_LIT:5> ] <EOL> return dateString <EOL> def getDateFromLandsatInfo ( info ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in info [ '<STR_LIT>' ] : <EOL> return info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in info [ '<STR_LIT>' ] : <EOL> return info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> else : <EOL> return None <EOL> def getDateFromModisInfo ( info ) : <EOL> '''<STR_LIT>''' <EOL> text = info [ '<STR_LIT:id>' ] <EOL> dateStart1 = text . rfind ( '<STR_LIT>' ) + len ( '<STR_LIT>' ) <EOL> dateStart2 = text . find ( '<STR_LIT:_>' , dateStart1 ) + <NUM_LIT:1> <EOL> this_date = text [ dateStart2 : ] . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> return this_date <EOL> def getDateFromImageInfo ( info ) : <EOL> '''<STR_LIT>''' <EOL> date = getDateFromLandsatInfo ( info ) <EOL> if not date : <EOL> date = getDateFromSentinel1Info ( info ) <EOL> if not date : <EOL> date = getDateFromModisInfo ( info ) <EOL> return date <EOL> def unComputeRectangle ( eeRect ) : <EOL> '''<STR_LIT>''' <EOL> LON = <NUM_LIT:0> <EOL> LAT = <NUM_LIT:1> <EOL> rectCoords = eeRect . getInfo ( ) [ '<STR_LIT>' ] <EOL> minLon = rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ LON ] <EOL> minLat = rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ LAT ] <EOL> maxLon = rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ LON ] <EOL> maxLat = rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ LAT ] <EOL> bbox = [ minLon , minLat , maxLon , maxLat ] <EOL> eeRectFixed = apply ( ee . Geometry . Rectangle , bbox ) <EOL> return eeRectFixed <EOL> def writeModisDomainFile ( domainName , bounds , image_ee_date , outputPath , <EOL> trainingName = None , unfloodedTrainingName = None ) : <EOL> '''<STR_LIT>''' <EOL> end_ee_date = image_ee_date . advance ( <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> startDateStr = image_ee_date . format ( ) . getInfo ( ) [ : <NUM_LIT:10> ] <EOL> endDateStr = end_ee_date . format ( ) . getInfo ( ) [ : <NUM_LIT:10> ] <EOL> root = ET . Element ( "<STR_LIT>" ) <EOL> ET . SubElement ( root , "<STR_LIT:name>" ) . text = domainName <EOL> sensors = ET . SubElement ( root , "<STR_LIT>" ) <EOL> demSensor = ET . SubElement ( sensors , "<STR_LIT>" ) <EOL> if regionIsInUnitedStates ( bounds ) : <EOL> ET . SubElement ( demSensor , "<STR_LIT:name>" ) . text = '<STR_LIT>' <EOL> else : <EOL> ET . SubElement ( demSensor , "<STR_LIT:name>" ) . text = '<STR_LIT>' <EOL> modisSensor = ET . SubElement ( sensors , "<STR_LIT>" ) <EOL> bands = ET . SubElement ( modisSensor , "<STR_LIT>" ) <EOL> source = ET . SubElement ( bands , "<STR_LIT:source>" ) <EOL> ET . SubElement ( modisSensor , "<STR_LIT:name>" ) . text = '<STR_LIT>' <EOL> ET . SubElement ( source , "<STR_LIT>" ) . text = startDateStr <EOL> ET . SubElement ( source , "<STR_LIT>" ) . text = endDateStr <EOL> bbox = ET . SubElement ( root , "<STR_LIT>" ) <EOL> bbox_bl = ET . SubElement ( bbox , "<STR_LIT>" ) <EOL> bbox_tr = ET . SubElement ( bbox , "<STR_LIT>" ) <EOL> rectCoords = bounds . bounds ( ) . getInfo ( ) [ '<STR_LIT>' ] <EOL> ET . SubElement ( bbox_bl , "<STR_LIT>" ) . text = str ( rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> ET . SubElement ( bbox_bl , "<STR_LIT>" ) . text = str ( rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> ET . SubElement ( bbox_tr , "<STR_LIT>" ) . text = str ( rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] ) <EOL> ET . SubElement ( bbox_tr , "<STR_LIT>" ) . text = str ( rectCoords [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) <EOL> ET . SubElement ( root , "<STR_LIT>" ) . text = '<STR_LIT>' <EOL> if trainingName : <EOL> ET . SubElement ( root , "<STR_LIT>" ) . text = trainingName <EOL> if unfloodedTrainingName : <EOL> ET . SubElement ( root , "<STR_LIT>" ) . text = unfloodedTrainingName <EOL> tree = ET . ElementTree ( root ) <EOL> tree . write ( outputPath ) <EOL> def writeDomainFilePair ( domainName , bounds , image_ee_date , training_ee_date , outputFolder ) : <EOL> '''<STR_LIT>''' <EOL> baseName = domainName + '<STR_LIT:_>' + str ( image_ee_date . format ( ) . getInfo ( ) [ : <NUM_LIT:10> ] ) <EOL> testName = baseName + '<STR_LIT>' <EOL> trainName = baseName + '<STR_LIT>' <EOL> testPath = os . path . join ( outputFolder , testName + '<STR_LIT>' ) <EOL> trainPath = os . path . join ( outputFolder , trainName + '<STR_LIT>' ) <EOL> writeModisDomainFile ( domainName , bounds , image_ee_date , testPath , None , trainName ) <EOL> writeModisDomainFile ( domainName , bounds , training_ee_date , trainPath , None , None ) <EOL> return ( testPath , trainPath ) <EOL> def which ( program ) : <EOL> '''<STR_LIT>''' <EOL> import os <EOL> def is_exe ( fpath ) : <EOL> return os . path . isfile ( fpath ) and os . access ( fpath , os . X_OK ) <EOL> fpath , fname = os . path . split ( program ) <EOL> if fpath : <EOL> if is_exe ( program ) : <EOL> return program <EOL> else : <EOL> for path in os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) : <EOL> path = path . strip ( '<STR_LIT:">' ) <EOL> exe_file = os . path . join ( path , program ) <EOL> if is_exe ( exe_file ) : <EOL> return exe_file <EOL> return None <EOL> def downloadEeImage ( eeObject , bbox , scale , file_path , vis_params = None ) : <EOL> '''<STR_LIT>''' <EOL> if not ( which ( '<STR_LIT>' ) and which ( '<STR_LIT>' ) ) : <EOL> print '<STR_LIT>' <EOL> return False <EOL> band_names = [ ] <EOL> if vis_params and ( '<STR_LIT>' in vis_params ) : <EOL> band_names = vis_params [ '<STR_LIT>' ] <EOL> if '<STR_LIT:U+002C>' in band_names : <EOL> band_names = band_names . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> if len ( eeObject . getInfo ( ) [ '<STR_LIT>' ] ) > <NUM_LIT:3> : <EOL> print '<STR_LIT>' <EOL> for b in eeObject . getInfo ( ) [ '<STR_LIT>' ] : <EOL> band_names . append ( b [ '<STR_LIT:id>' ] ) <EOL> if len ( band_names ) == <NUM_LIT:3> : <EOL> break <EOL> if ( len ( band_names ) != <NUM_LIT:3> ) and ( len ( band_names ) != <NUM_LIT:1> ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if vis_params and ( '<STR_LIT>' in vis_params ) and ( '<STR_LIT>' in vis_params ) : <EOL> download_object = eeObject . visualize ( band_names , min = vis_params [ '<STR_LIT>' ] , max = vis_params [ '<STR_LIT>' ] ) <EOL> elif vis_params and ( '<STR_LIT>' in vis_params ) : <EOL> gain_text = vis_params [ '<STR_LIT>' ] . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> gain_vals = [ float ( x ) for x in gain_text ] <EOL> download_object = eeObject . visualize ( band_names , gain_vals ) <EOL> else : <EOL> download_object = eeObject . visualize ( band_names ) <EOL> if isinstance ( bbox , basestring ) or isinstance ( bbox , list ) : <EOL> eeRect = apply ( ee . Geometry . Rectangle , bbox ) <EOL> else : <EOL> eeRect = bbox <EOL> eeGeom = eeRect . toGeoJSONString ( ) <EOL> dummy_name = '<STR_LIT>' <EOL> url = download_object . getDownloadUrl ( { '<STR_LIT:name>' : dummy_name , '<STR_LIT>' : scale , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : eeGeom } ) <EOL> temp_prefix = '<STR_LIT>' + dummy_name <EOL> zip_name = temp_prefix + '<STR_LIT>' <EOL> zip_path = os . path . join ( TEMP_FILE_DIR , zip_name ) <EOL> print '<STR_LIT>' <EOL> data = urllib2 . urlopen ( url ) <EOL> with open ( zip_path , '<STR_LIT:wb>' ) as fp : <EOL> while True : <EOL> chunk = data . read ( <NUM_LIT:16> * <NUM_LIT> ) <EOL> if not chunk : <EOL> break <EOL> fp . write ( chunk ) <EOL> print '<STR_LIT>' <EOL> z = zipfile . ZipFile ( zip_path , '<STR_LIT:r>' ) <EOL> temp_band_files = [ ] <EOL> band_files_string = '<STR_LIT>' <EOL> if len ( band_names ) == <NUM_LIT:1> : <EOL> color_names = [ '<STR_LIT>' ] <EOL> else : <EOL> color_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for b in color_names : <EOL> band_filename = dummy_name + '<STR_LIT:.>' + b + '<STR_LIT>' <EOL> extracted_path = os . path . join ( TEMP_FILE_DIR , band_filename ) <EOL> z . extract ( band_filename , TEMP_FILE_DIR ) <EOL> temp_band_files . append ( extracted_path ) <EOL> band_files_string += '<STR_LIT:U+0020>' + extracted_path <EOL> vrt_path = os . path . join ( TEMP_FILE_DIR , temp_prefix + '<STR_LIT>' ) <EOL> cmd = '<STR_LIT>' + vrt_path + '<STR_LIT:U+0020>' + band_files_string <EOL> os . system ( cmd ) <EOL> if not os . path . exists ( vrt_path ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> cmd = '<STR_LIT>' + vrt_path + '<STR_LIT:U+0020>' + file_path <EOL> os . system ( cmd ) <EOL> if not os . path . exists ( file_path ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' + file_path <EOL> return True </s>
