def run(self): configuration = {} with self.input()[0].open('r') as getConfiguration: configuration = json.load(getConfiguration) getManifestInfo = {} with self.input()[1].open('r') as getManifest: getManifestInfo = json.load(getManifest) manifestLoader = CheckFileExists( filePath=getManifestInfo["manifestFile"]) yield manifestLoader manifest = {} with manifestLoader.output().open('r') as manifestFile: manifest = manifestFile.read() cutLine = {} cutDemPathRoot = wc.createWorkingPath(configuration["workingRoot"], 'dem') cutDemPath = os.path.join(cutDemPathRoot, 'cutDem.tif') cutLinePath = os.path.join(cutDemPathRoot, "cutline.geojson") demPath = os.path.join(self.paths["static"], configuration["demFilename"]) boundingBoxCoords = self.getBoundingBoxCoords(manifest) inputFilePath = configuration["inputFilePath"] cutLine = {"type": "polygon", "coordinates": [boundingBoxCoords]} with open(cutLinePath, "w") as cutlineFile: cutlineFile.write(json.dumps(cutLine, use_decimal=True)) if not self.testProcessing: try: subprocess.check_output( "gdalwarp -of GTiff -crop_to_cutline -overwrite --config CHECK_DISK_FREE_SPACE NO -cutline {} {} {}" .format(cutLinePath, demPath, cutDemPath), stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError as e: errStr = "command '{}' return with error (code {}): {}".format( e.cmd, e.returncode, e.output) log.error(errStr) raise RuntimeError(errStr) else: wc.createTestFile(cutDemPath) with self.output().open("w") as outFile: outFile.write( json.dumps({ 'cutDemPath': cutDemPath, 'cutLine': cutLine }, use_decimal=True, indent=4))
def run(self): configuration = {} with self.input().open('r') as getConfiguration: configuration = json.load(getConfiguration) cutLine = {} cutDemPathRoot = wc.createWorkingPath(configuration["workingRoot"], 'dem') cutDemPath = os.path.join(cutDemPathRoot, 'cutDem.tif') cutLinePath = os.path.join(cutDemPathRoot, "cutline.geojson") demPath = os.path.join(self.paths["static"], configuration["demFilename"]) inputFilePath = configuration["inputFilePath"] if not self.testProcessing: with zipfile.ZipFile(inputFilePath) as productZipFile: with productZipFile.open("{}.SAFE/preview/map-overlay.kml".format(os.path.basename(inputFilePath).replace(".zip", ""))) as overlay: # Grab first latlong element as there should only be one coordinatesXMLElement = xml.etree.ElementTree.fromstring(overlay.read().decode("utf-8")).findall(".//Document/Folder/GroundOverlay/gx:LatLonQuad/coordinates", {"gx": "http://www.google.com/kml/ext/2.2"})[0] coordinates = [] # Push coordinates from XML into array, converting to floats for coord in coordinatesXMLElement.text.split(' '): coordinates.append(list(map(lambda x: float(x), coord.split(',')))) # Copy first coordinate to end of list to complete polygon coordinates.append(coordinates[0]) cutLine = { "type": "polygon", "coordinates": [coordinates] } with open(cutLinePath, "w") as cutlineFile: cutlineFile.write(json.dumps(cutLine)) try: subprocess.check_output( "gdalwarp -of GTiff -crop_to_cutline -overwrite --config CHECK_DISK_FREE_SPACE NO -cutline {} {} {}".format(cutLinePath, demPath, cutDemPath), stderr=subprocess.STDOUT, shell=True) except subprocess.CalledProcessError as e: errStr = "command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output) log.error(errStr) raise RuntimeError(errStr) else: # yield CreateLocalFile(filePath=cutDemPath, content="TEST_FILE") wc.createTestFile(cutDemPath) with self.output().open("w") as outFile: outFile.write(json.dumps({ 'cutDemPath' : cutDemPath, 'cutLine' : cutLine }))
def run(self): cutDemInfo = {} with self.input()[0].open('r') as cutDEM: cutDemInfo = json.load(cutDEM) copyInputFileInfo = {} with self.input()[1].open('r') as copyInputFile: copyInputFileInfo = json.load(copyInputFile) configuration = {} with self.input()[2].open('r') as getConfiguration: configuration = json.load(getConfiguration) tempOutputPath = wc.createWorkingPath(configuration["workingRoot"], "output") log.info('Populating configfile params') # todo these two options are probabably no longer needed and can be removed after code checks configFilePath = "/app/toolchain/scripts/JNCC_S1_GRD_configfile_v.1.1.sh" vmOptionsFilePath = "/app/snap/bin/gpt.vmoptions" processingConfiguration = { "scriptConfigFilePath" : configFilePath, "vmOptionsFilePath" : vmOptionsFilePath, "arguments" : self.spatialConfig["snapRunArguments"], "parameters" : { "s1_ard_main_dir" : configuration["workingRoot"], "s1_ard_basket_dir" : copyInputFileInfo["tempInputPath"], "s1_ard_ext_dem" : cutDemInfo["cutDemPath"], "s1_ard_temp_output_dir" : tempOutputPath, "s1_ard_snap_memory" : str(self.memoryLimit), "s1_ard_utm_proj" : self.spatialConfig["snapConfigUtmProj"], "s1_ard_central_meridian" : self.spatialConfig["snapConfigCentralMeridian"], "s1_ard_false_northing" : self.spatialConfig["snapConfigFalseNorthing"] } } if not os.path.isdir(processingConfiguration["parameters"]["s1_ard_main_dir"]): raise Exception("Invalid working path: Check working path in paths parameter") with self.output().open("w") as outFile: outFile.write(json.dumps(processingConfiguration))
def run(self): configuration = {} with self.input().open('r') as getConfiguration: configuration = json.load(getConfiguration) tempManifestFile = os.path.join( wc.createWorkingPath(configuration["workingRoot"], "manifest"), "source_manifest.txt") rawZip = zipfile.ZipFile(configuration["inputFilePath"], 'r') manifestPath = os.path.join( os.path.splitext(os.path.basename( configuration["inputFilePath"]))[0] + ".SAFE", "manifest.safe") manifest = rawZip.read(manifestPath).decode("utf-8") with open(tempManifestFile, 'w') as manifestFile: manifestFile.write(manifest) with self.output().open('w') as out: out.write(json.dumps({"manifestFile": tempManifestFile}))
def run(self): configuration = {} with self.input().open('r') as getConfiguration: configuration = json.load(getConfiguration) tempInputPath = wc.createWorkingPath(configuration["workingRoot"], "input") inputSource = configuration["inputFilePath"] tempTatget = os.path.join(tempInputPath, os.path.basename(inputSource)) try: shutil.copy(inputSource, tempTatget) log.info("Copied input file to {}".format(tempTatget)) except IOError as e: raise("Unable to copy file. {}".format(e)) except: raise("Unexpected error:", sys.exc_info()) with self.output().open('w') as out: out.write(json.dumps({ "tempInputPath" : tempInputPath, "tempInputFile" : tempTatget }))