From 288474cb5c3879324f399242c4762df936036ee8 Mon Sep 17 00:00:00 2001 From: Becky Arnold Date: Wed, 28 Mar 2018 13:40:36 +0100 Subject: [PATCH 1/6] wrote functions to get input from user and do sanity checks. --- .Rhistory | 0 info_from_user.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++ try.py~ | 14 ++++++++ 3 files changed, 101 insertions(+) create mode 100644 .Rhistory create mode 100644 info_from_user.py create mode 100644 try.py~ diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 0000000..e69de29 diff --git a/info_from_user.py b/info_from_user.py new file mode 100644 index 0000000..dbed272 --- /dev/null +++ b/info_from_user.py @@ -0,0 +1,87 @@ +import rasterio +from types import FunctionType +#import matplotlib.pyplot as plt + + + +# This function checks if the doi th user has supplied is invalid +def check_invalid_doi(doi): + + if ('10.' not in doi) or ('/' not in doi): + invalid = True + else: + invalid = False + + return invalid + +''' +This funtction asks the user for: +-The name of the file to be converted +-The doi of the dataset +-The doi of the publication + +It also sanity-check the supplied information e.g. valid filetype, +And keeps asking until a reasonable answer has been provided. +''' +def info_from_user(valid_filetypes): + + # Ask the user for their filename + # Keep asking until a valid file has been given + + ask = True + while ask: + + # Get name from user + filename = input('Input the filename. ')#'sample_data/subset_data_1/subset_data_1.tif' + + # Check a valid response has been given + valid = True + + # Check the input is a file + if '.' not in filename: + print('That is not a file.') + valid = False + + # If it is a file check it's a valid file type + else: + filetype = filename[filename.rfind('.')+1:] + if filetype not in valid_filetypes: + valid = False + print('Invalid filetype. Your filetype is ', filetype, '.\n\nValid filetypes are:', '\n\n\n', valid_filetypes) + + ask = not valid + + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + + # Ask the user for the doi of their dataset and publication + # Keep asking until a valid dois has been given + + ask = True + while ask: + + # Get doi from user + doi_dataset = input('Input the dataset doi. ') + ask = check_invalid_doi(doi_dataset) + + ask = True + while ask: + + # Get doi from user + doi_pub = input('Input the publication doi. ') + ask = check_invalid_doi(doi_pub) + + return filename, doi_dataset, doi_pub + +# List of valid filetypes +valid_filetypes = ['gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', + 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', + 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', + 'jpg', 'jp2', 'j2k','ntf', 'nsf', 'grc', 'tab', 'grd', + 'png', 'ppm', 'pgm', 'prf', 'rik', 'rsw', 'mtw', 'xml', + 'safe', 'ter', 'TIL', 'vrt', 'xpm'] + +# Get required information from the user. +filename, doi_dataset, doi_pub = info_from_user(valid_filetypes) + diff --git a/try.py~ b/try.py~ new file mode 100644 index 0000000..01e1225 --- /dev/null +++ b/try.py~ @@ -0,0 +1,14 @@ +import rasterio +from types import FunctionType +#import matplotlib.pyplot as plt + + +filename = 'sample_data/subset_data_1/subset_data_1.tif' + +dataset = rasterio.open(filename) + +for i in dataset.meta: + + print(i, dataset.meta[i]) + + From 7daa451a6de516d16bcb30cafd5003e72e3ef6ad Mon Sep 17 00:00:00 2001 From: Becky Arnold Date: Wed, 28 Mar 2018 14:18:20 +0100 Subject: [PATCH 2/6] Refactored infor_from_user.py --- info_from_user.py | 84 +++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/info_from_user.py b/info_from_user.py index dbed272..94a381c 100644 --- a/info_from_user.py +++ b/info_from_user.py @@ -1,19 +1,47 @@ -import rasterio -from types import FunctionType -#import matplotlib.pyplot as plt +# This function checks if a valid filename has been provided +def check_invalid_filename(filename): + # List of valid filetypes + valid_filetypes = ['gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', + 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', + 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', + 'jpg', 'jp2', 'j2k','ntf', 'nsf', 'grc', 'tab', 'grd', + 'png', 'ppm', 'pgm', 'prf', 'rik', 'rsw', 'mtw', 'xml', + 'safe', 'ter', 'TIL', 'vrt', 'xpm'] + + invalid = False + + # Check the input is a file + if '.' not in filename: + print('\n\nThat is not a file.') + invalid = True + + # If it is a file check it's a valid file type + else: + filetype = filename[filename.rfind('.')+1:] + if filetype not in valid_filetypes: + invalid = True + print('\n\nInvalid filetype. Your filetype is ', filetype, '.\n\nValid filetypes are:', '\n\n', valid_filetypes) + + return invalid + + +# ________________________________________________ -# This function checks if the doi th user has supplied is invalid +# This function checks if the doi the user has supplied is invalid def check_invalid_doi(doi): if ('10.' not in doi) or ('/' not in doi): + print('\n\nThat is an invalid doi, please supply a valid doi.') invalid = True else: invalid = False return invalid +# ________________________________________________ + ''' This funtction asks the user for: -The name of the file to be converted @@ -23,7 +51,7 @@ def check_invalid_doi(doi): It also sanity-check the supplied information e.g. valid filetype, And keeps asking until a reasonable answer has been provided. ''' -def info_from_user(valid_filetypes): +def info_from_user(): # Ask the user for their filename # Keep asking until a valid file has been given @@ -32,56 +60,40 @@ def info_from_user(valid_filetypes): while ask: # Get name from user - filename = input('Input the filename. ')#'sample_data/subset_data_1/subset_data_1.tif' - - # Check a valid response has been given - valid = True - - # Check the input is a file - if '.' not in filename: - print('That is not a file.') - valid = False - - # If it is a file check it's a valid file type - else: - filetype = filename[filename.rfind('.')+1:] - if filetype not in valid_filetypes: - valid = False - print('Invalid filetype. Your filetype is ', filetype, '.\n\nValid filetypes are:', '\n\n\n', valid_filetypes) + filename = input('\n\nInput the filename. ') - ask = not valid + ask = check_invalid_filename(filename) - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # Ask the user for the doi of their dataset and publication - # Keep asking until a valid dois has been given + # Ask the user for the doi of their dataset + # Keep asking until a valid doi has been given ask = True while ask: # Get doi from user - doi_dataset = input('Input the dataset doi. ') + doi_dataset = input('\n\nInput the dataset doi. ') ask = check_invalid_doi(doi_dataset) + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + + # Ask the user for the doi of their publication + # Keep asking until a valid doi has been given ask = True while ask: # Get doi from user - doi_pub = input('Input the publication doi. ') + doi_pub = input('\n\nInput the publication doi. ') ask = check_invalid_doi(doi_pub) return filename, doi_dataset, doi_pub -# List of valid filetypes -valid_filetypes = ['gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', - 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', - 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', - 'jpg', 'jp2', 'j2k','ntf', 'nsf', 'grc', 'tab', 'grd', - 'png', 'ppm', 'pgm', 'prf', 'rik', 'rsw', 'mtw', 'xml', - 'safe', 'ter', 'TIL', 'vrt', 'xpm'] # Get required information from the user. -filename, doi_dataset, doi_pub = info_from_user(valid_filetypes) +filename, doi_dataset, doi_pub = info_from_user() From b19ad04eba3e7b23f821d29e2f0b536a6113d92e Mon Sep 17 00:00:00 2001 From: Anastasis Georgoulas Date: Wed, 28 Mar 2018 14:38:25 +0100 Subject: [PATCH 3/6] Clean up style and typos. --- info_from_user.py | 53 ++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/info_from_user.py b/info_from_user.py index 94a381c..fddb383 100644 --- a/info_from_user.py +++ b/info_from_user.py @@ -2,16 +2,17 @@ def check_invalid_filename(filename): # List of valid filetypes - valid_filetypes = ['gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', - 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', - 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', - 'jpg', 'jp2', 'j2k','ntf', 'nsf', 'grc', 'tab', 'grd', + valid_filetypes = [ + 'gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', + 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', + 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', + 'jpg', 'jp2', 'j2k', 'ntf', 'nsf', 'grc', 'tab', 'grd', 'png', 'ppm', 'pgm', 'prf', 'rik', 'rsw', 'mtw', 'xml', 'safe', 'ter', 'TIL', 'vrt', 'xpm'] invalid = False - # Check the input is a file + # Check the input is a file if '.' not in filename: print('\n\nThat is not a file.') invalid = True @@ -21,8 +22,9 @@ def check_invalid_filename(filename): filetype = filename[filename.rfind('.')+1:] if filetype not in valid_filetypes: invalid = True - print('\n\nInvalid filetype. Your filetype is ', filetype, '.\n\nValid filetypes are:', '\n\n', valid_filetypes) - + print('\n\nInvalid filetype. Your filetype is ', filetype, + '.\n\nValid filetypes are:', '\n\n', valid_filetypes) + return invalid @@ -42,58 +44,53 @@ def check_invalid_doi(doi): # ________________________________________________ -''' -This funtction asks the user for: --The name of the file to be converted --The doi of the dataset --The doi of the publication -It also sanity-check the supplied information e.g. valid filetype, -And keeps asking until a reasonable answer has been provided. -''' def info_from_user(): - + ''' + This function asks the user for: + -The name of the file to be converted + -The doi of the dataset + -The doi of the publication + + It also sanity-checks the supplied information e.g. valid filetype, + and keeps asking until a reasonable answer has been provided. + ''' # Ask the user for their filename # Keep asking until a valid file has been given - + ask = True while ask: # Get name from user - filename = input('\n\nInput the filename. ') + filename = input('Input the filename: ') ask = check_invalid_filename(filename) - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # Ask the user for the doi of their dataset # Keep asking until a valid doi has been given - ask = True while ask: # Get doi from user - doi_dataset = input('\n\nInput the dataset doi. ') + doi_dataset = input('Input the dataset doi: ') ask = check_invalid_doi(doi_dataset) - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # Ask the user for the doi of their publication # Keep asking until a valid doi has been given ask = True while ask: # Get doi from user - doi_pub = input('\n\nInput the publication doi. ') + doi_pub = input('Input the publication doi: ') ask = check_invalid_doi(doi_pub) return filename, doi_dataset, doi_pub -# Get required information from the user. -filename, doi_dataset, doi_pub = info_from_user() - +if __name__ == "__main__": + # Get required information from the user. + filename, doi_dataset, doi_pub = info_from_user() From 9364be3bbf627013c406569dcbb0bfa966db3c74 Mon Sep 17 00:00:00 2001 From: Anastasis Georgoulas Date: Wed, 28 Mar 2018 14:42:34 +0100 Subject: [PATCH 4/6] Print metadata to test --- info_from_user.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/info_from_user.py b/info_from_user.py index fddb383..64c6ba0 100644 --- a/info_from_user.py +++ b/info_from_user.py @@ -1,3 +1,6 @@ +import geometa + + # This function checks if a valid filename has been provided def check_invalid_filename(filename): @@ -94,3 +97,5 @@ def info_from_user(): if __name__ == "__main__": # Get required information from the user. filename, doi_dataset, doi_pub = info_from_user() + metadata = geometa.get_meta(filename, doi_dataset, doi_pub) + print(metadata) From 49d3c70079a619c3ab7780fe4d4c291ed47934ba Mon Sep 17 00:00:00 2001 From: Anastasis Georgoulas Date: Wed, 28 Mar 2018 15:06:15 +0100 Subject: [PATCH 5/6] Output a JSON from interactive CLI --- transform.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 transform.py diff --git a/transform.py b/transform.py new file mode 100644 index 0000000..c863e68 --- /dev/null +++ b/transform.py @@ -0,0 +1,101 @@ +import geometa + + +# This function checks if a valid filename has been provided +def check_invalid_filename(filename): + + # List of valid filetypes + valid_filetypes = [ + 'gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', + 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', + 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', + 'jpg', 'jp2', 'j2k', 'ntf', 'nsf', 'grc', 'tab', 'grd', + 'png', 'ppm', 'pgm', 'prf', 'rik', 'rsw', 'mtw', 'xml', + 'safe', 'ter', 'TIL', 'vrt', 'xpm'] + + invalid = False + + # Check the input is a file + if '.' not in filename: + print('\n\nThat is not a file.') + invalid = True + + # If it is a file check it's a valid file type + else: + filetype = filename[filename.rfind('.')+1:] + if filetype not in valid_filetypes: + invalid = True + print('\n\nInvalid filetype. Your filetype is ', filetype, + '.\n\nValid filetypes are:', '\n\n', valid_filetypes) + + return invalid + + +# ________________________________________________ + + +# This function checks if the doi the user has supplied is invalid +def check_invalid_doi(doi): + + if ('10.' not in doi) or ('/' not in doi): + print('\n\nThat is an invalid doi, please supply a valid doi.') + invalid = True + else: + invalid = False + + return invalid + +# ________________________________________________ + + +def info_from_user(): + ''' + This function asks the user for: + -The name of the file to be converted + -The doi of the dataset + -The doi of the publication + + It also sanity-checks the supplied information e.g. valid filetype, + and keeps asking until a reasonable answer has been provided. + ''' + # Ask the user for their filename + # Keep asking until a valid file has been given + + ask = True + while ask: + + # Get name from user + filename = input('Input the filename: ') + + ask = check_invalid_filename(filename) + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + # Ask the user for the doi of their dataset + # Keep asking until a valid doi has been given + ask = True + while ask: + + # Get doi from user + doi_dataset = input('Input the dataset doi: ') + ask = check_invalid_doi(doi_dataset) + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + # Ask the user for the doi of their publication + # Keep asking until a valid doi has been given + ask = True + while ask: + + # Get doi from user + doi_pub = input('Input the publication doi: ') + ask = check_invalid_doi(doi_pub) + + return filename, doi_dataset, doi_pub + + +if __name__ == "__main__": + # Get required information from the user. + filename, doi_dataset, doi_pub = info_from_user() + output_filename = "output.json" + geometa.get_meta(filename, doi_dataset, doi_pub, output_filename) From 88ed9473c63de57cd2c32459a1bcd01c94f65583 Mon Sep 17 00:00:00 2001 From: Anastasis Georgoulas Date: Wed, 28 Mar 2018 15:07:40 +0100 Subject: [PATCH 6/6] Remove unneeded files --- .Rhistory | 0 info_from_user.py | 101 ---------------------------------------------- try.py~ | 14 ------- 3 files changed, 115 deletions(-) delete mode 100644 .Rhistory delete mode 100644 info_from_user.py delete mode 100644 try.py~ diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index e69de29..0000000 diff --git a/info_from_user.py b/info_from_user.py deleted file mode 100644 index 64c6ba0..0000000 --- a/info_from_user.py +++ /dev/null @@ -1,101 +0,0 @@ -import geometa - - -# This function checks if a valid filename has been provided -def check_invalid_filename(filename): - - # List of valid filetypes - valid_filetypes = [ - 'gen', 'thf', 'adf', 'blx', 'xlb', 'bag', 'bmp', 'kap', - 'bt', 'doq', 'dt0', 'dt1', 'dt2', 'ers', 'n1', 'fits', - 'hdr', 'gif', 'grb', 'tif', 'img', 'mpr', 'mpl', 'mem', - 'jpg', 'jp2', 'j2k', 'ntf', 'nsf', 'grc', 'tab', 'grd', - 'png', 'ppm', 'pgm', 'prf', 'rik', 'rsw', 'mtw', 'xml', - 'safe', 'ter', 'TIL', 'vrt', 'xpm'] - - invalid = False - - # Check the input is a file - if '.' not in filename: - print('\n\nThat is not a file.') - invalid = True - - # If it is a file check it's a valid file type - else: - filetype = filename[filename.rfind('.')+1:] - if filetype not in valid_filetypes: - invalid = True - print('\n\nInvalid filetype. Your filetype is ', filetype, - '.\n\nValid filetypes are:', '\n\n', valid_filetypes) - - return invalid - - -# ________________________________________________ - - -# This function checks if the doi the user has supplied is invalid -def check_invalid_doi(doi): - - if ('10.' not in doi) or ('/' not in doi): - print('\n\nThat is an invalid doi, please supply a valid doi.') - invalid = True - else: - invalid = False - - return invalid - -# ________________________________________________ - - -def info_from_user(): - ''' - This function asks the user for: - -The name of the file to be converted - -The doi of the dataset - -The doi of the publication - - It also sanity-checks the supplied information e.g. valid filetype, - and keeps asking until a reasonable answer has been provided. - ''' - # Ask the user for their filename - # Keep asking until a valid file has been given - - ask = True - while ask: - - # Get name from user - filename = input('Input the filename: ') - - ask = check_invalid_filename(filename) - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - # Ask the user for the doi of their dataset - # Keep asking until a valid doi has been given - ask = True - while ask: - - # Get doi from user - doi_dataset = input('Input the dataset doi: ') - ask = check_invalid_doi(doi_dataset) - - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - # Ask the user for the doi of their publication - # Keep asking until a valid doi has been given - ask = True - while ask: - - # Get doi from user - doi_pub = input('Input the publication doi: ') - ask = check_invalid_doi(doi_pub) - - return filename, doi_dataset, doi_pub - - -if __name__ == "__main__": - # Get required information from the user. - filename, doi_dataset, doi_pub = info_from_user() - metadata = geometa.get_meta(filename, doi_dataset, doi_pub) - print(metadata) diff --git a/try.py~ b/try.py~ deleted file mode 100644 index 01e1225..0000000 --- a/try.py~ +++ /dev/null @@ -1,14 +0,0 @@ -import rasterio -from types import FunctionType -#import matplotlib.pyplot as plt - - -filename = 'sample_data/subset_data_1/subset_data_1.tif' - -dataset = rasterio.open(filename) - -for i in dataset.meta: - - print(i, dataset.meta[i]) - -