def insert(): uploaded_file = request.files['file'] if uploaded_file: _insert_file_to_vw(uploaded_file) VW_CLIENT = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) model_run_uuid = str(request.form['uuid']) model_run_record = \ VW_CLIENT.modelrun_search(model_run_id=model_run_uuid).records[0] model_run_uuid = model_run_record['Model Run UUID'] model_run_desc = model_run_record['Description'] model_run_name = model_run_record['Model Run Name'] datasets_res = VW_CLIENT.dataset_search(model_run_uuid=model_run_uuid) records_list = datasets_res.records return render_template('share/files.html', model_run_name=model_run_name, model_run_desc=model_run_desc, model_run_uuid=model_run_uuid, records_list=records_list)
def vwp_push_remove(): if request.method == 'GET': vwpModelId = request.args.get("vwpModelId") modelRunID = request.args.get("modelRunID") # gstore testing - Start # gstore_username = "" # gstore_password = "" # gstore_host_url = "https://vwp-dev.unm.edu/" gstore_username = session['g-uname'] gstore_password = session['g-pass'] gstore_host_url = app.config['GSTORE_HOST'] vwclient = VWClient(gstore_host_url, gstore_username, gstore_password) vwclient.authenticate() result = vwclient.deleteModelRun(vwpModelId); if result==True: # delete from vwp-push-info file also app_root = find_user_folder() vwp_push_info_file = app_root + app.config['VWP_PUSH_INFO'] if os.path.exists(vwp_push_info_file): f = open(vwp_push_info_file,"r") lines = f.readlines() f.close() f = open(vwp_push_info_file,"w") for line in lines: values=line.split('\t') if values[0]!=modelRunID: f.write(line) f.close() return json.dumps(result)
def files(model_run_uuid): if not os.path.exists(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) VW_CLIENT = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) model_run_record = \ VW_CLIENT.modelrun_search(model_run_id=model_run_uuid).records[0] model_run_uuid = model_run_record['Model Run UUID'] model_run_desc = model_run_record['Description'] model_run_name = model_run_record['Model Run Name'] "View of file submission for as yet unselected resource to add to" # model_run_uuid = model_run_uuid datasets_res = VW_CLIENT.dataset_search(model_run_uuid=model_run_uuid) records_list = datasets_res.records return render_template('share/files.html', model_run_name=model_run_name, model_run_desc=model_run_desc, model_run_uuid=model_run_uuid, records_list=records_list)
def _insert_file_to_vw(uploaded_file, model_run_uuid, request): """ to be used in a thread to concurrently upload a file and insert the associated metadata in the virtual watershed data management server """ uploaded_file = request.files['file'] watershed_name = str(request.form['watershed']) model_name = str(request.form['model']) description = str(request.form['description']) model_run_uuid = str(request.form['uuid']) model_set = str(request.form['model_set']) if watershed_name in ['Dry Creek', 'Reynolds Creek']: state = 'Idaho' elif watershed_name == 'Valles Caldera': state = 'New Mexico' elif watershed_name == 'Lehman Creek': state = 'Nevada' _local_vw_client = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) uploaded_file_name = secure_filename(uploaded_file.filename) uploaded_file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file_name) uploaded_file.save(uploaded_file_path) _local_vw_client.upload( model_run_uuid, os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file_name) ) input_file = uploaded_file_name parent_uuid = model_run_uuid start_datetime = '2010-01-01 00:00:00' end_datetime = '2010-01-01 01:00:00' # create XML FGDC-standard metadata that gets included in VW metadata fgdc_metadata = \ make_fgdc_metadata(input_file, None, model_run_uuid, start_datetime, end_datetime, model=model_name) # create VW metadata watershed_metadata = metadata_from_file(uploaded_file_path, parent_uuid, model_run_uuid, description, watershed_name, state, start_datetime=start_datetime, end_datetime=end_datetime, model_name=model_name, fgdc_metadata=fgdc_metadata, model_set=model_set, taxonomy='geoimage', model_set_taxonomy='grid') _local_vw_client.insert_metadata(watershed_metadata) time.sleep(1)
def search(): """ Create model run panels, rectangles on the search/home page that display summary information about the data that the VW has for a particular model run. Returns: (str) HTML string of the model run panel """ panels = [] search_fields = [ 'model_run_name', 'researcher_name', 'model_keywords', 'description' ] search_results = [] form = SearchForm(request.args) vw_client = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) if request.args and not form.validate(): flash('Please fill out at least one field') return render_template('search.html', form=form, panels=panels) if request.args: words = form.model_run_name.data.split() if request.args: for search_field in search_fields: search_args = defaultdict() for w in words: search_args[search_field] = w results = vw_client.modelrun_search(**search_args) search_results += results.records records = search_results if records: # make a panel of each metadata record panels = [_make_panel(rec) for rec in records if rec] panels = {p['model_run_uuid']: p for p in panels}.values() # pass the list of parsed records to the template to generate results page return render_template('search.html', form=form, panels=panels)
def search(): """ Create model run panels, rectangles on the search/home page that display summary information about the data that the VW has for a particular model run. Returns: (str) HTML string of the model run panel """ panels = [] search_fields = ['model_run_name', 'researcher_name', 'model_keywords', 'description'] search_results = [] form = SearchForm(request.args) vw_client = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) if request.args and not form.validate(): flash('Please fill out at least one field') return render_template('search.html', form=form, panels=panels) if request.args: words = form.model_run_name.data.split() if request.args: for search_field in search_fields: search_args = defaultdict() for w in words: search_args[search_field] = w results = vw_client.modelrun_search(**search_args) search_results += results.records records = search_results if records: # make a panel of each metadata record panels = [_make_panel(rec) for rec in records if rec] panels = {p['model_run_uuid']: p for p in panels}.values() # pass the list of parsed records to the template to generate results page return render_template('search.html', form=form, panels=panels)
def list_mr_files(model_run_uuid): # create a local VWClient; avoid any timeout VW_CLIENT = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) if request.method == 'GET': records = VW_CLIENT.dataset_search( model_run_uuid=model_run_uuid ).records files = [ { 'name': rec['name'], 'url': [u for u in rec['downloads'][0].values() if 'original' in u][0], 'last_modified': rec['metadata-modified']['all'], 'uuid': rec['uuid'] } for rec in records ] return jsonify({'files': files}) else: model_run_uuid = str(request.form['modelrunUUID']) uploaded_file = request.files['uploadedFile'] if uploaded_file: _insert_file_to_vw(uploaded_file, model_run_uuid, request) else: return jsonify(400, {'status': 'fail', 'reason': 'no file given'}) return jsonify({ 'status': 'success', 'file_name': uploaded_file.filename })
def setGstoreCred(): # Clearing earlier uname & pwd session.pop('g-uname', None) session.pop('g-pass', None) gstore_username = request.form['gstore-uname'] gstore_password = request.form['gstore-pwd'] # gstore_username = "" # gstore_password = "" # gstore_host_url = "https://vwp-dev.unm.edu/" #TODO: get this from config gstore_host_url = app.config['GSTORE_HOST'] vwclient = VWClient(gstore_host_url, gstore_username, gstore_password) verified = vwclient.authenticate() if verified == True: session['g-uname'] = gstore_username session['g-pass'] = gstore_password return json.dumps({'status':'Success'}); else: return json.dumps({'status':'Failed'});
def list_mr_files(model_run_uuid): # create a local VWClient; avoid any timeout VW_CLIENT = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) if request.method == 'GET': records = VW_CLIENT.dataset_search( model_run_uuid=model_run_uuid).records files = [{ 'name': rec['name'], 'url': [u for u in rec['downloads'][0].values() if 'original' in u][0], 'last_modified': rec['metadata-modified']['all'], 'uuid': rec['uuid'] } for rec in records] return jsonify({'files': files}) else: model_run_uuid = str(request.form['modelrunUUID']) uploaded_file = request.files['uploadedFile'] if uploaded_file: _insert_file_to_vw(uploaded_file, model_run_uuid, request) else: return jsonify(400, {'status': 'fail', 'reason': 'no file given'}) return jsonify({ 'status': 'success', 'file_name': uploaded_file.filename })
def resources(): """" Initialize a resource container for either uploading files or an external resource (FTP, TRHEDDS, eventually more) """ # TODO Display current resources that have been shared by the current user # Display form for sharing a data resource form = ResourceForm() if form.validate_on_submit(): _local_vw_client = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) # initialize: post to virtual watershed common_kwargs = { 'description': form.description.data, 'keywords': form.keywords.data } extra_vw_kwargs = { 'researcher_name': current_user.name, 'model_run_name': form.title.data, } vw_kwargs = {} vw_kwargs.update(common_kwargs) vw_kwargs.update(extra_vw_kwargs) # in VW language, the database focuses on 'model_runs'. # however, modelers don't just want to share data associated with a # particular model import uuid UUID = str(uuid.uuid4()) try: result_of_vwpost = _local_vw_client.initialize_modelrun(**vw_kwargs) UUID = result_of_vwpost except: pass # get UUID and add full record to the 'resources' table in DB along with # user ID url = (form.url.data or _local_vw_client.dataset_search_url + '&model_run_uuid=' + UUID) resource = Resource(user_id=current_user.id, title=form.title.data, uuid=UUID, url=url, **common_kwargs) db.session.add(resource) print resource try: db.session.commit() flash('Your submission has been accepted') form.reset() except: db.session.rollback() flash('Your submission has been rejected') # When it's been submitted, give URL to view on the Virtual Watershed and # add it to the list above (i.e. reload the page) return render_template('share/index.html', form=form)
def _insert_file_to_vw(uploaded_file, model_run_uuid, request): """ to be used in a thread to concurrently upload a file and insert the associated metadata in the virtual watershed data management server """ watershed_name = str(request.form['watershed']) model_name = str(request.form['model']) description = str(request.form['description']) model_set = str(request.form['model_set']) if watershed_name in ['Dry Creek', 'Reynolds Creek']: state = 'Idaho' elif watershed_name == 'Valles Caldera': state = 'New Mexico' elif watershed_name == 'Lehman Creek': state = 'Nevada' _local_vw_client = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) uploaded_file_name = secure_filename(uploaded_file.filename) uploaded_file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file_name) uploaded_file.save(uploaded_file_path) _local_vw_client.upload( model_run_uuid, os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file_name)) input_file = uploaded_file_name parent_uuid = model_run_uuid start_datetime = '2010-01-01 00:00:00' end_datetime = '2010-01-01 01:00:00' # create XML FGDC-standard metadata that gets included in VW metadata fgdc_metadata = \ make_fgdc_metadata(input_file, None, model_run_uuid, start_datetime, end_datetime, model=model_name) # create VW metadata watershed_metadata = metadata_from_file(uploaded_file_path, parent_uuid, model_run_uuid, description, watershed_name, state, start_datetime=start_datetime, end_datetime=end_datetime, model_name=model_name, fgdc_metadata=fgdc_metadata, model_set=model_set, taxonomy='geoimage', model_set_taxonomy='grid') _local_vw_client.insert_metadata(watershed_metadata) time.sleep(1)
def model_vwp_push(model_Id, model_type, model_desc, model_title, controlURL, dataURL, paramURL, statsURL, outURL, animationURL): app_root = find_user_folder() if not os.path.exists(app_root): os.makedirs(app_root) # TODO clean the previous download input files data_file = app_root + app.config['TEMP_DATA'] control_file = app_root + app.config['TEMP_CONTROL'] param_file = app_root + app.config['TEMP_PARAM'] animation_file = app_root + app.config['TEMP_ANIMATION'] output_file = app_root + app.config['TEMP_OUTPUT'] statvar_file = app_root + app.config['TEMP_STAT'] vwp_push_info_file = app_root + app.config['VWP_PUSH_INFO'] # clean up previous download file if os.path.isfile(data_file): os.remove(data_file) if os.path.isfile(control_file): os.remove(control_file) if os.path.isfile(param_file): os.remove(param_file) if os.path.isfile(animation_file): os.remove(animation_file) if os.path.isfile(output_file): os.remove(output_file) if os.path.isfile(statvar_file): os.remove(statvar_file) # download three inputs file based on the urls if controlURL is not None: urllib.urlretrieve(controlURL, control_file) if dataURL is not None: urllib.urlretrieve(dataURL, data_file) if paramURL is not None: urllib.urlretrieve(paramURL, param_file) if animationURL is not None: urllib.urlretrieve(animationURL, animation_file) if outURL is not None: urllib.urlretrieve(outURL, output_file) if statsURL is not None: urllib.urlretrieve(statsURL, statvar_file) # gstore testing - Start gstore_username = session['g-uname'] gstore_password = session['g-pass'] gstore_host_url = app.config['GSTORE_HOST'] vwclient = VWClient(gstore_host_url, gstore_username, gstore_password) vwclient.authenticate() resp = {} failed = [] modeluuid_vwp = vwclient.createNewModelRun(model_Id, model_title, model_type, model_desc) if modeluuid_vwp!= '': # changing the working directory to upload folder path os.chdir(app_root) control_file = app.config['TEMP_CONTROL'].strip("/") data_file = app.config['TEMP_DATA'].strip("/") param_file = app.config['TEMP_PARAM'].strip("/") animation_file = app.config['TEMP_ANIMATION'].strip("/") output_file = app.config['TEMP_OUTPUT'].strip("/") statvar_file = app.config['TEMP_STAT'].strip("/") c = vwclient.uploadModelData_swift(modeluuid_vwp, control_file) #upload control file d = vwclient.uploadModelData_swift(modeluuid_vwp, data_file) #upload data file p = vwclient.uploadModelData_swift(modeluuid_vwp, param_file) #upload parameter file s = vwclient.uploadModelData_swift(modeluuid_vwp, statvar_file) #upload statvar file o = vwclient.uploadModelData_swift(modeluuid_vwp, output_file) #upload output file if animationURL is not None: a = vwclient.uploadModelData_swift(modeluuid_vwp, animation_file) #upload animation file # check for failure in upload if c.status_code !=200: failed.append('control') if d.status_code !=200: failed.append('data') if p.status_code !=200: failed.append('param') if s.status_code !=200: failed.append('statvar') if o.status_code !=200: failed.append('output') if animationURL is not None: if a.status_code !=200: failed.append('animation') current_time = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') resp['modeluuid_vwp'] = modeluuid_vwp resp['pushed_time'] = current_time resp['model_Id'] = model_Id resp['control_file_status_code'] = c.status_code resp['data_file_status_code'] = d.status_code resp['param_file_status_code'] = p.status_code resp['statvar_file_status_code'] = s.status_code resp['output_file_status_code'] = o.status_code if animationURL is not None: resp['animation_file_status_code'] = a.status_code # test # resp['approot'] = app_root # resp['d_file'] = data_file # store it in a file with open(vwp_push_info_file, "a") as infoFile: infoFile.write('{}\t{}\t{}\t{}\t{}\n'.format(model_Id, modeluuid_vwp, current_time, failed, model_title)) return resp
def resources(): """" Initialize a resource container for either uploading files or an external resource (FTP, TRHEDDS, eventually more) """ # TODO Display current resources that have been shared by the current user # Display form for sharing a data resource form = ResourceForm() if form.validate_on_submit(): _local_vw_client = VWClient(app.config['GSTORE_HOST'], app.config['GSTORE_USERNAME'], app.config['GSTORE_PASSWORD']) # initialize: post to virtual watershed common_kwargs = { 'description': form.description.data, 'keywords': form.keywords.data } extra_vw_kwargs = { 'researcher_name': current_user.name, 'model_run_name': form.title.data, } vw_kwargs = {} vw_kwargs.update(common_kwargs) vw_kwargs.update(extra_vw_kwargs) # in VW language, the database focuses on 'model_runs'. # however, modelers don't just want to share data associated with a # particular model import uuid UUID = str(uuid.uuid4()) try: result_of_vwpost = _local_vw_client.initialize_modelrun( **vw_kwargs) UUID = result_of_vwpost except: pass # get UUID and add full record to the 'resources' table in DB along with # user ID url = (form.url.data or _local_vw_client.dataset_search_url + '&model_run_uuid=' + UUID) resource = Resource(user_id=current_user.id, title=form.title.data, uuid=UUID, url=url, **common_kwargs) db.session.add(resource) print resource try: db.session.commit() flash('Your submission has been accepted') form.reset() except: db.session.rollback() flash('Your submission has been rejected') # When it's been submitted, give URL to view on the Virtual Watershed and # add it to the list above (i.e. reload the page) return render_template('share/index.html', form=form)