def hyp3_login() -> API: """ Takes users information to log in to NASA EarthData, updates .netrc with user's credentials then returns an API object. """ username = "" try: with open('.netrc', 'r') as f: contents = f.read() username = contents.split(' ')[3] password = contents.split(' ')[5].split('\n')[0] except IndexError and FileNotFoundError: pass error = None while True: if error: print(error) print('Please try agian.\n') if username == "": print("Enter your NASA EarthData username: "******"machine urs.earthdata.nasa.gov login {username} password {password}\n" ) return api
def __init__(self, username=None, password=None): """ takes user input to login to NASA Earthdata updates .netrc with user credentials returns an api object note: Earthdata's EULA applies when accessing ASF APIs Hyp3 API handles HTTPError and LoginError """ err = None while True: if err: # Jupyter input handling requires printing login error here to maintain correct order of output. print(err) print("Please Try again.\n") if not username or not password: print(f"Enter your NASA EarthData username:"******"Enter your password:"******"Login successful.") print(f"Welcome {username}.") self.username = username self.password = password self.api = api break
def earthdata_hyp3_login(): """ takes user input to login to NASA Earthdata updates .netrc with user credentials returns an api object note: Earthdata's EULA applies when accessing ASF APIs Hyp3 API handles HTTPError and LoginError """ err = None while True: if err: # Jupyter input handling requires printing login error here to maintain correct order of output. print(err) print("Please Try again.\n") print(f"Enter your NASA EarthData username:"******"Enter your password:"******"Login successful.") print(f"Welcome {username}.") filename = "/home/jovyan/.netrc" with open(filename, 'w+') as f: f.write( f"machine urs.earthdata.nasa.gov login {username} password {password}\n" ) return api
def procS1StackGroupsRTC(outfile=None, infiles=None, path=None, res=None, filter=False, type='dB-byte', scale=[-40, 0], clip=None, shape=None, overlap=False, zipFlag=False, leave=False, thresh=0.4, font=24, hyp=None, keep=None, group=False, aws=None, inamp=False, exclude=False, dates=None, delay=50): if outfile is not None: logFile = "{}_log.txt".format(outfile) else: logFile = "run_log.txt" outfile = "animation" logging.info( "***********************************************************************************" ) logging.info(" STARTING RUN {}".format(outfile)) logging.info( "***********************************************************************************" ) printParameters(outfile, infiles, path, res, filter, type, scale, clip, shape, overlap, zipFlag, leave, thresh, font, hyp, keep, group, aws, inamp, exclude, dates, delay) if hyp: logging.info( "Using Hyp3 subscription named {} to download input files".format( hyp)) username, password = getUsernamePassword() api = API(username) api.login(password=password) download_products(api, sub_name=hyp) hyp = None zipFlag = True path = "hyp3-products" if zipFlag: unzipFiles(path, "hyp3-products-unzipped") zipFlag = False path = "hyp3-products-unzipped" # Catch the case where # no path is specified, # no input files are specified, # we're not using a HyP3 subscription, # we're not using AWS, # and no dates file is specified... if path is None and hyp is None and infiles is None and dates is None and aws is None: path = "hyp3-products-unzipped" if group: if aws is not None: filelist = aws_ls(aws) filelist = filter_file_list(filelist, path, '.tif') for i in xrange(len(filelist)): filelist[i] = "/vsis3/" + aws + "/" + filelist[i] elif (infiles is None or len(infiles) == 0): # Make path into an absolute path if path is not None: if path[0] != "/": root = os.getcwd() path = os.path.join(root, path) if not os.path.isdir(path): logging.error( "ERROR: path {} is not a directory!".format(path)) exit(1) logging.info("Data path is {}".format(path)) else: path = os.getcwd() if zipFlag: filelist = glob.glob("{}/S1*.zip".format(path)) else: filelist = [] logging.debug("Path is {}".format(path)) for myfile in os.listdir(path): if os.path.isdir(os.path.join(path, myfile)): filelist.append(myfile) if len(filelist) == 0: logging.error("ERROR: Unable to find input files") exit(1) classes, filelists = sortByTime(path, filelist, "rtc") logging.debug("aws is {}".format(aws)) for i in range(len(classes)): if len(filelists[i]) > 2: if aws is None: time = classes[i] mydir = "DATA_{}".format(classes[i]) logging.info("Making clean directory {}".format(mydir)) createCleanDir(mydir) for myfile in filelists[i]: newfile = os.path.join(mydir, os.path.basename(myfile)) logging.info("Linking file {} to {}".format( os.path.join(path, myfile), newfile)) os.symlink(os.path.join(path, myfile), newfile) else: mydir = None infiles = filelists[i] output = outfile + "_" + classes[i] procS1StackRTC(outfile=output, infiles=infiles, path=mydir, res=res, filter=filter, type=type, scale=scale, clip=None, shape=None, overlap=True, zipFlag=zipFlag, leave=leave, thresh=thresh, font=font, keep=keep, aws=aws, inamp=inamp, exclude=exclude, datefile=dates, delay=delay) if mydir is not None: shutil.rmtree(mydir) else: procS1StackRTC(outfile=outfile, infiles=infiles, path=path, res=res, filter=filter, type=type, scale=scale, clip=clip, shape=shape, overlap=overlap, zipFlag=zipFlag, leave=leave, thresh=thresh, font=font, keep=keep, aws=aws, inamp=inamp, exclude=exclude, datefile=dates, delay=delay) if not leave and group: for myfile in glob.glob("sorted_*"): shutil.rmtree(myfile) logging.info( "***********************************************************************************" ) logging.info(" END OF RUN {}".format(outfile)) logging.info( "***********************************************************************************" )
def procS1StackGroupsGIANT(type, output, descFile=None, rxy=None, nvalid=0.8, nsbas=False, filt=0.1, path=None, utcTime=None, heading=None, leave=False, train=False, hyp=None, zipFlag=False, group=False, rawFlag=False, mm=None, errorFlag=False, api_key=None): logging.info( "***********************************************************************************" ) logging.info(" STARTING RUN {}".format(output)) logging.info( "***********************************************************************************" ) printParameters(type, output, descFile=descFile, rxy=rxy, nvalid=nvalid, nsbas=nsbas, filt=filt, path=path, utcTime=utcTime, heading=heading, leave=leave, train=train, hyp=hyp, zipFlag=zipFlag, group=group, rawFlag=rawFlag, mm=mm, errorFlag=errorFlag, api_key=api_key) if hyp: logging.info( "Using Hyp3 subscription named {} to download input files".format( hyp)) if api_key is not None: config = configparser.ConfigParser() config.read(api_key) s = 'hyp3-API-credentials' api = API(config.get(s, 'username'), api_key=config.get(s, 'api_key')) else: username, password = getUsernamePassword() api = API(username) api.login(password=password) download_products(api, sub_name=hyp) zipFlag = True path = "hyp3-products" if zipFlag: unzipFiles(path, "hyp3-products-unzipped") zipFlag = False path = "hyp3-products-unzipped" if type == 'hyp' and group: # Make path into an absolute path if path is not None: if os.path.isdir(path): if path[0] != "/": root = os.getcwd() path = os.path.join(root, path) else: logging.error( "ERROR: path {} is not a directory!".format(path)) exit(1) logging.info("Data path is {}".format(path)) else: path = os.getcwd() filelist = [] for myfile in os.listdir(path): if os.path.isdir(os.path.join(path, myfile)): filelist.append(myfile) if len(filelist) == 0: logging.error("ERROR: Unable to find files to process") exit(1) classes, filelists = sortByTime(path, filelist, "insar") for i in range(len(classes)): if len(filelists[i]) > 2: mydir = "DATA_{}".format(classes[i]) createCleanDir(mydir) for myfile in filelists[i]: thisDir = "../sorted_{}".format(classes[i]) inFile = "{}/{}".format(thisDir, os.path.basename(myfile)) outFile = "{}/{}".format(mydir, os.path.basename(myfile)) logging.debug("Linking file {} to {}".format( inFile, outFile)) os.symlink(inFile, outFile) outfile = output + "_" + classes[i] procS1StackGIANT(type, outfile, descFile=descFile, rxy=rxy, nvalid=nvalid, nsbas=nsbas, filt=filt, path=mydir, utcTime=utcTime, heading=heading, leave=leave, train=train, hyp=hyp, rawFlag=rawFlag, mm=mm, errorFlag=errorFlag) shutil.rmtree(mydir) else: procS1StackGIANT(type, output, descFile=descFile, rxy=rxy, nvalid=nvalid, nsbas=nsbas, filt=filt, path=path, utcTime=utcTime, heading=heading, leave=leave, train=train, hyp=hyp, rawFlag=rawFlag, mm=mm, errorFlag=errorFlag) if not leave: if group: for myfile in glob.glob("sorted_*"): shutil.rmtree(myfile) logging.info( "***********************************************************************************" ) logging.info(" END OF RUN {}".format(output)) logging.info( "***********************************************************************************" )
group = parser.add_mutually_exclusive_group() group.add_argument("-s", "--sub_name", help="Name of the subscription to download") group.add_argument("-i", "--id", help="ID of the subscription to download") group.add_argument("-d", "--date", help="Date of the subscription to download") args = parser.parse_args() print("Username: "******"Username is {}".format(username) api = API(username) api.login() download_products(api, directory="hyp3-products/", id=None, sub_id=args.id, sub_name=args.sub_name, creation_date=args.date, verbose=True, threads=0) # download_products(api)