예제 #1
0
def get_xml(request):
    list_catalog = {}
    return_obj = {}

    hs_url = request.GET.get('hs_url')

    site_code = request.GET.get('code')
    network = request.GET.get('network')
    code_variable = request.GET.get('code_variable')
    dates_request = request.GET.getlist('timeFrame[]')
    start_date = dates_request[0]
    end_date = dates_request[1]

    variable_desc = network + ':' + code_variable
    site_desc = network + ':' + site_code

    water = pwml.WaterMLOperations(url=hs_url)

    return_obj['waterml'] = water.GetValues(site_desc,
                                            variable_desc,
                                            start_date,
                                            end_date,
                                            format='waterml')

    return JsonResponse(return_obj)
예제 #2
0
def available_services(request):
    url_catalog = request.GET.get('url')
    hs_services = {}
    url_catalog = unquote(url_catalog)

    if url_catalog:
        try:
            # url_catalog = unquote(url_catalog)
            # print("THIS ", url_catalog)
            url_catalog2 = url_catalog + "?WSDL"
            # client = Client(url_catalog2, timeout= 500)
            # service_info = client.service.GetWaterOneFlowServiceInfo()
            # services = service_info.ServiceInfo
            # views = giveServices(services)
            # hs_services['services'] = views
            water = pwml.WaterMLOperations(url=url_catalog2)
            hs_services['services'] = water.AvailableServices()['available']

        except Exception as e:
            print(e)
            # print("I AM HERE OR NOT")
            # services = parseService(url_catalog)
            # views = giveServices(services)
            # hs_services['services'] = views
            hs_services['services'] = []
    return JsonResponse(hs_services)
예제 #3
0
def create_group(request, app_workspace):
    group_obj = {}
    SessionMaker = app.get_persistent_store_database(Persistent_Store_Name,
                                                     as_sessionmaker=True)
    session = SessionMaker()  # Initiate a session
    # Query DB for hydroservers
    if request.is_ajax() and request.method == 'POST':
        #print("inside first if statement of create group")
        description = request.POST.get('textarea')

        # print(description)
        title = request.POST.get('addGroup-title')
        url_catalog = request.POST.get('url')

        selected_services = []
        for key, value in request.POST.items():
            #print(key)
            if value not in (title, description, url_catalog):
                selected_services.append(value.replace("_", " "))
                # selected_services.append(value)

        # group_obj['title']= title.translate ({ord(c): "_" for c in "!@#$%^&*()[]{};:,./<>?\|`~-=+"})
        group_obj['title'] = title
        group_obj['description'] = description
        # url_catalog = request.POST.get('url')
        group_hydroservers = Groups(title=title, description=description)
        session.add(group_hydroservers)
        session.commit()
        session.close()

        if url_catalog:
            try:
                url_catalog = unquote(url_catalog)
                url_catalog2 = url_catalog + "?WSDL"
                water = pwml.WaterMLOperations(url=url_catalog2)
                services = water.GetWaterOneFlowServicesInfo()
                #print(services)
                if selected_services:
                    views = water.aux._giveServices(
                        services, selected_services)['working']
                    group_obj['views'] = addMultipleViews(request,
                                                          hs_list=views,
                                                          group=title)
                else:
                    views = water.aux._giveServices(services)['working']
                    group_obj['views'] = addMultipleViews(request,
                                                          hs_list=views,
                                                          group=title)

            except Exception as e:
                print(e)
                group_obj['views'] = []

    else:
        group_obj['message'] = 'There was an error while adding th group.'

    # print(group_obj['views'])
    return JsonResponse(group_obj)
예제 #4
0
def upload_hs(request):
    return_obj = {}
    difference = 0

    if request.is_ajax() and request.method == 'POST':
        specific_group = request.POST.get('group')
        specific_hs = request.POST.get('hs')
        response_obj = {}
        SessionMaker = app.get_persistent_store_database(Persistent_Store_Name,
                                                         as_sessionmaker=True)
        session = SessionMaker()  # Initiate a session
        hydroservers_group = session.query(Groups).filter(
            Groups.title == specific_group)[0].hydroserver
        h1 = session.query(Groups).join("hydroserver")
        hs_list = []
        for hydroservers in hydroservers_group:
            name = hydroservers.title
            url = hydroservers.url
            if name == specific_hs:
                difference = len(json.loads(hydroservers.siteinfo))
                # client = Client(url, timeout= 500)
                water = pwml.WaterMLOperations(url=url)
                sites_object = water.GetSites()
                # print(type(sites))
                # sites_json={}
                # if isinstance(sites, str):
                #     print("here")
                #     sites_dict = xmltodict.parse(sites)
                #     sites_json_object = json.dumps(sites_dict)
                #     sites_json = json.loads(sites_json_object)
                # else:
                #     sites_json_object = suds_to_json(sites)
                #     sites_json = json.loads(sites_json_object)

                # Parsing the sites and creating a sites object. See auxiliary.py
                # print("-------------------------------------")
                # print(sites_json)
                # sites_object = parseJSON(sites_json)
                # print(sites_object)
                # converted_sites_object=[x['sitename'].decode("UTF-8") for x in sites_object]

                # sites_parsed_json = json.dumps(converted_sites_object)
                # sites_parsed_json = json.dumps(sites_object)
                sites_parsed_json = json.dumps(sites_object)
                difference = len(sites_object) - difference
                hydroservers.siteinfo = sites_parsed_json
                return_obj["siteInfo"] = json.loads(sites_parsed_json)
                return_obj["sitesAdded"] = difference
                return_obj["url"] = hydroservers.url

        session.commit()
        session.close()

    else:
        return_obj[
            'message'] = 'This request can only be made through a "POST" AJAX call.'

    return JsonResponse(return_obj)
def available_variables_2(url):
    varaibles_list = {}
    hydroserver_variable_list = []
    hydroserver_variable_code_list = []
    water = pwml.WaterMLOperations(url=url)
    hs_variables = water.GetVariables()['variables']
    for hs_variable in hs_variables:
        hydroserver_variable_list.append(hs_variable['variableName'])
        hydroserver_variable_code_list.append(hs_variable['variableCode'])

    varaibles_list["variables"] = hydroserver_variable_list
    varaibles_list["variables_codes"] = hydroserver_variable_code_list
    return varaibles_list
def get_variables_hs(request):
    list_catalog = {}
    #print("get_variables_hs Function")
    specific_group = request.POST.get('group')
    hs_actual = request.POST.get('hs')
    hs_actual = hs_actual.replace('-', ' ')
    #print("HS", hs_actual)
    SessionMaker = app.get_persistent_store_database(Persistent_Store_Name,
                                                     as_sessionmaker=True)

    session = SessionMaker()  # Initiate a session
    hydroservers_group = session.query(Groups).filter(
        Groups.title == specific_group)[0].hydroserver

    for hydroservers in hydroservers_group:
        name = hydroservers.title
        if hs_actual == name:
            # print(hydroservers.url)
            # layer_obj = {}
            # layer_obj["title"] = hydroservers.title
            # layer_obj["url"] = hydroservers.url.strip()
            # print(layer_obj["url"])
            # layer_obj["siteInfo"] = hydroservers.siteinfo
            # client = Client(url = hydroservers.url.strip(), timeout= 500)
            # keywords = client.service.GetVariables('[:]')
            water = pwml.WaterMLOperations(url=hydroservers.url.strip())
            keywords_response = water.GetVariables()['variables']
            keywords = []
            keywords_name = []
            keywords_abbr_unit = []
            key_timeSupport = []
            timeUnitName = []
            for kyword in keywords_response:
                #print(kyword)
                keywords.append(kyword['variableCode'])
                keywords_name.append(kyword['variableName'])
                keywords_abbr_unit.append(kyword['unitAbbreviation'])
                key_timeSupport.append(kyword['timeSupport'])
                timeUnitName.append(kyword['timeUnitAbbreviation'])
            variables_show = keywords

    list_catalog["variables_code"] = variables_show
    list_catalog["variables_name"] = keywords_name
    list_catalog["variables_unit_abr"] = keywords_abbr_unit
    list_catalog["variables_timesupport"] = key_timeSupport
    list_catalog["variables_time_abr"] = timeUnitName

    #print("Finished get_variables_hs Function")

    return JsonResponse(list_catalog)
def soap_group(request):
    # logging.basicConfig(level=logging.INFO)
    # logging.getLogger('suds.client').setLevel(logging.DEBUG)
    return_obj = {}
    if request.is_ajax() and request.method == 'POST':
        url = request.POST.get('soap-url')
        title = request.POST.get('soap-title')
        # title = title.replace(" ", "")
        # title = title.translate ({ord(c): "_" for c in "!@#$%^&*()[]{};:,./<>?\|`~-=+"})
        group = request.POST.get('actual-group')
        #print(group)
        description = request.POST.get('textarea')
        # Getting the current map extent
        true_extent = request.POST.get('extent')

        if "?WSDL" not in url:
            url = url + "?WSDL"
        water = pwml.WaterMLOperations(url=url)
        # imp = Import('http://schemas.xmlsoap.org/soap/envelope')
        # doctor = ImportDoctor(imp)
        # water.client = Client(url, doctor = doctor)
        # client = Client(url, timeout= 500)

        # True Extent is on and necessary if the user is trying to add USGS or
        # some of the bigger HydroServers.
        if true_extent == 'on':
            extent_value = request.POST['extent_val']
            return_obj['zoom'] = 'true'
            return_obj['level'] = extent_value
            ext_list = extent_value.split(',')
            sitesByBoundingBox = water.GetSitesByBoxObject(
                ext_list, 'epsg:3857')
            countries_json = available_regions_2(request, sites_parsed_json)
            variable_json = available_variables_2(url)

            return_obj['title'] = title
            return_obj['url'] = url
            return_obj['description'] = description

            return_obj['siteInfo'] = sitesByBoundingBox
            return_obj['group'] = group
            return_obj['status'] = "true"

            SessionMaker = app.get_persistent_store_database(
                Persistent_Store_Name, as_sessionmaker=True)
            session = SessionMaker()
            hydroservers_group = session.query(Groups).filter(
                Groups.title == group)[0]
            # hydroservers_g = session.query(Groups).filter(Groups.title == group)
            hs_one = HydroServer_Individual(title=title,
                                            url=url,
                                            description=description,
                                            siteinfo=sites_parsed_json,
                                            variables=variable_json,
                                            countries=countries_json)
            # siteinfo=sitesByBoundingBoxs)

            hydroservers_group.hydroserver.append(hs_one)
            session.add(hydroservers_group)
            session.commit()
            session.close()

        else:

            return_obj['zoom'] = 'false'
            # sites = water.GetSites()

            sites = GetSites_WHOS(url)
            sites_parsed_json = json.dumps(sites)
            countries_json = json.dumps(
                available_regions_2(request, siteinfo=sites_parsed_json))
            # print(countries_json)

            variable_json = json.dumps(available_variables_2(url))

            return_obj['title'] = title
            return_obj['url'] = url
            return_obj['description'] = description
            return_obj['siteInfo'] = sites
            return_obj['group'] = group
            return_obj['status'] = "true"
            SessionMaker = app.get_persistent_store_database(
                Persistent_Store_Name, as_sessionmaker=True)
            session = SessionMaker()
            hydroservers_group = session.query(Groups).filter(
                Groups.title == group)[0]

            hs_one = HydroServer_Individual(title=title,
                                            url=url,
                                            description=description,
                                            siteinfo=sites_parsed_json,
                                            variables=variable_json,
                                            countries=countries_json)

            hydroservers_group.hydroserver.append(hs_one)
            session.add(hydroservers_group)
            session.commit()
            session.close()

    else:
        return_obj[
            'message'] = 'This request can only be made through a "POST" AJAX call.'

    return JsonResponse(return_obj)
def get_available_sites(request):
    if request.method == 'POST':
        specific_group = request.POST.get('group')
        specific_hydroserver = request.POST.get('hs')
        specific_variables = request.POST.getlist('variables[]')
        safety_check_limit = len(specific_variables)
        safety_check_intial = 0
        list_catalog = {}
        return_obj = {}
        SessionMaker = app.get_persistent_store_database(Persistent_Store_Name,
                                                         as_sessionmaker=True)

        session = SessionMaker()  # Initiate a session
        hydroservers_group = session.query(Groups).filter(
            Groups.title == specific_group)[0].hydroserver
        h1 = session.query(Groups).join("hydroserver")
        hs_list = []

        for hydroservers in hydroservers_group:
            if hydroservers.title == specific_hydroserver:
                water = pwml.WaterMLOperations(url=hydroservers.url.strip())
                sites = json.loads(hydroservers.siteinfo)
                sitesFiltered = water.GetSitesByVariable(
                    specific_variables)['sites']
                hs_list = sitesFiltered
                # print(hs_list)
                # print("this is the one selecting hs")
                # name = hydroservers.title
                # print(name)
                #
                # # print(hydroservers.title)
                # # print(hydroservers.url)
                # layer_obj = {}
                #
                # layer_obj["title"] = hydroservers.title
                # layer_obj["url"] = hydroservers.url.strip()
                # client = Client(layer_obj["url"])  # Connect to the HydroServer endpoint
                # # print(client)
                # keywords = client.service.GetVariables('[:]')
                # keywords_dict = xmltodict.parse(keywords)
                # keywords_dict_object = json.dumps(keywords_dict)
                #
                # keywords_json = json.loads(keywords_dict_object)
                # # print(client)
                # layer_obj["siteInfoJSON"] =json.loads(hydroservers.siteinfo)
                # layer_obj["siteInfo"] =hydroservers.siteinfo
                # for site in layer_obj["siteInfoJSON"]:
                #
                #     sitecode = site['sitecode']
                #     site_name= site['sitename']
                #     network = site["network"]
                #     layer_obj2={}
                #     layer_obj2['sitecode']=sitecode
                #     layer_obj2['sitename']=site_name
                #     layer_obj2['network']=network
                #     layer_obj2['latitude']=site['latitude']
                #     layer_obj2['longitude']=site['longitude']
                #     print("THIS IS THE ACTUAL SITE")
                #     print(layer_obj2['sitename'])
                #     site_desc = network + ":" + sitecode
                #     site_info_Mc = client.service.GetSiteInfo(site_desc)
                #     site_info_Mc_dict = xmltodict.parse(site_info_Mc)
                #     site_info_Mc_json_object = json.dumps(site_info_Mc_dict)
                #     site_info_Mc_json = json.loads(site_info_Mc_json_object)
                #     # print(site_info_Mc_json)
                #     print(site_info_Mc_json['sitesResponse']['site']['seriesCatalog'])
                #
                #     # print("GETSITESINFO FUNCTION")
                #     # print(object_methods)
                #     object_with_methods_and_variables = {}
                #     object_with_descriptions_and_variables = {}
                #     object_with_time_and_variables = {}
                #     object_methods= {}
                #     if 'series' in site_info_Mc_json['sitesResponse']['site']['seriesCatalog']:
                #         object_methods= site_info_Mc_json['sitesResponse']['site']['seriesCatalog']['series']
                #
                #         if(isinstance(object_methods,(dict))):
                #             # print("adding to the methodID as a dict")
                #             variable_name_ = object_methods['variable']['variableName']
                #             ## this part was added for the WHOS plata broker endpoint ##
                #             if 'method' in object_methods:
                #                 object_with_methods_and_variables[variable_name_]= object_methods['method']['@methodID']
                #             else:
                #                 object_with_methods_and_variables[variable_name_]= None
                #             ## end of the part for WHOS plata
                #             object_with_descriptions_and_variables[variable_name_]= object_methods['source'];
                #             object_with_time_and_variables[variable_name_]= object_methods['variableTimeInterval'];
                #             # print(object_with_methods_and_variables)
                #         else:
                #             for object_method in object_methods:
                #                 # print("adding to the methodID as an arraylist")
                #                 variable_name_ = object_method['variable']['variableName']
                #                 if 'method' in object_method:
                #                     object_with_methods_and_variables[variable_name_]= object_method['method']['@methodID']
                #                 else:
                #                     object_with_methods_and_variables[variable_name_]= None
                #                 # print(object_method['source'])
                #                 object_with_descriptions_and_variables[variable_name_]= object_method['source'];
                #                 object_with_time_and_variables[variable_name_]= object_method['variableTimeInterval'];
                #                 # print(object_with_methods_and_variables)
                #
                #
                #
                #     array_variables=keywords_json['variablesResponse']['variables']['variable']
                #     array_keywords_hydroserver=[]
                #     array_variables_codes = []
                #     array_variables_lengths = []
                #     length_values = 0
                #
                #
                #     if isinstance(array_variables,type([])):
                #         print("array type")
                #         ijj = 0
                #         for words in array_variables:
                #             # print(ijj)
                #             print("variable name")
                #             print(words['variableName'])
                #             variable_text = words['variableName']
                #             print("compared to the following")
                #             print(specific_variables)
                #             if variable_text in specific_variables:
                #                 print("TRUE compared")
                #
                #                 safety_check_intial = safety_check_intial + 1
                #                 print("we are in the varaible specified")
                #                 code_variable = words['variableCode']['#text']
                #                 start_date = ""
                #                 end_date = ""
                #                 variable_desc = network + ':' + code_variable
                #
                #                 # print("variable_desc")
                #                 # print("varaible in list")
                #                 # print(variable_desc)
                #                 # print(site_desc)
                #                 try:
                #                     values = client.service.GetValues(
                #                         site_desc, variable_desc, start_date, end_date, "")
                #
                #                     values_dict = xmltodict.parse(values)  # Converting xml to dict
                #                     values_json_object = json.dumps(values_dict)
                #                     values_json = json.loads(values_json_object)
                #                     # print(values_json)
                #                     # print(values_json.keys())
                #                     if 'timeSeriesResponse' in values_json:
                #                     # if values_json['timeSeriesResponse'] is not None:
                #                         times_series = values_json['timeSeriesResponse'][
                #                             'timeSeries']  # Timeseries object for the variable
                #                         # print(times_series)
                #                         if times_series['values'] is not None:
                #                             length_values= len(times_series['values']['value'])
                #                             print("this is the length value")
                #                             print(variable_text," ", length_values )
                #                             hs_list.append(layer_obj2)
                #
                #                         else:
                #                             length_values = 0
                #                             print(variable_text," ", length_values )
                #                     ## Addition for the WHOS PLATA ##
                #                     else:
                #                         times_series = values_json['wml1:timeSeriesResponse'][
                #                             'wml1:timeSeries']  # Timeseries object for the variable
                #                         # print(times_series)
                #                         if times_series['wml1:values'] is not None:
                #                             length_values= len(times_series['wml1:values']['wml1:value'])
                #                             print(variable_text," ", length_values )
                #                             hs_list.append(layer_obj2)
                #
                #                         else:
                #                             length_values = 0
                #                             print(variable_text," ", length_values )
                #
                #
                #                     array_variables_lengths.append(length_values)
                #
                #
                #                     array_keywords_hydroserver.append(words['variableName'])
                #                     array_variables_codes.append(words['variableCode']['#text'])
                #                     ijj=ijj+1
                #
                #                 except Exception as e:
                #                     print("OOPS",e.__class__)
                #             # else:
                #             #     if layer_obj2 in hs_list:
                #             #         hs_list.remove(layer_obj2)
                #
                #             # words_to_search[name] = array_keywords_hydroserver
                #
                #
                #     if isinstance(array_variables,dict):
                #         print("dict")
                #         print("variable_name")
                #         print(array_variables['variableName'])
                #         print("compared to the following")
                #         print(specific_variables)
                #
                #         variable_text = array_variables['variableName']
                #         if variable_text in specific_variables:
                #             print("TRUE compared")
                #             safety_check_intial = safety_check_intial + 1
                #             code_variable = array_variables['variableCode']['#text']
                #             start_date = ""
                #             end_date = ""
                #             variable_desc = network + ':' + code_variable
                #
                #             try:
                #                 values = client.service.GetValues(
                #                     site_desc, variable_desc, start_date, end_date, "")
                #                 # print(values)
                #
                #                 values_dict = xmltodict.parse(values)  # Converting xml to dict
                #                 values_json_object = json.dumps(values_dict)
                #                 values_json = json.loads(values_json_object)
                #                 if 'timeSeriesResponse' in values_json:
                #                 # if values_json['timeSeriesResponse'] is not None:
                #                     times_series = values_json['timeSeriesResponse'][
                #                         'timeSeries']  # Timeseries object for the variable
                #                     # print(times_series)
                #                     if times_series['values'] is not None:
                #                         length_values= len(times_series['values']['value'])
                #                         print(variable_text," ", length_values )
                #                         hs_list.append(layer_obj2)
                #
                #                     else:
                #                         length_values = 0
                #                         # print(variable_text," ", length_values )
                #                 ## Addition for the WHOS PLATA ##
                #                 else:
                #                     times_series = values_json['wml1:timeSeriesResponse'][
                #                         'wml1:timeSeries']  # Timeseries object for the variable
                #                     # print(times_series)
                #                     if times_series['wml1:values'] is not None:
                #                         length_values= len(times_series['wml1:values']['wml1:value'])
                #                         print(variable_text," ", length_values )
                #                         hs_list.append(layer_obj2)
                #
                #                     else:
                #                         length_values = 0
                #                         print(variable_text," ", length_values )
                #
                #
                #                 array_variables_lengths.append(length_values)
                #
                #                 array_keywords_hydroserver.append(array_variables['variableName'])
                #                 array_variables_codes.append(array_variables['variableCode']['#text'])
                #             except Exception as e:
                #                 print("OOPS",e.__class__)
                #
                #     return_obj['variables']=array_keywords_hydroserver
                #     return_obj['codes']=array_variables_codes
                #     return_obj['counts'] = array_variables_lengths
                #     return_obj['methodsIDs']= object_with_methods_and_variables
                #     return_obj['description'] = object_with_descriptions_and_variables
                #     return_obj['times_series'] = object_with_time_and_variables
                #     return_obj['siteInfo']= site_info_Mc_json
        # print("Safety check limit")
        # print(safety_check_limit)
        # print("Safety check initial")
        # print(safety_check_intial)
        # if safety_check_intial >= safety_check_limit:
        # list_catalog["hydroserver"] = hs_list
    list_catalog["hydroserver"] = hs_list

    return JsonResponse(list_catalog)
예제 #9
0
def get_values_graph_hs(request):
    # print("inside the get_values_graph_hs")
    list_catalog = {}
    return_obj = {}
    hs_url = request.GET.get('hs_url')
    site_code = request.GET.get('code')
    network = request.GET.get('network')
    code_variable = request.GET.get('code_variable')
    dates_request = request.GET.getlist('timeFrame[]')
    start_date = dates_request[0]
    end_date = dates_request[1]
    variable_desc = network + ':' + code_variable
    site_desc = network + ':' + site_code
    water = pwml.WaterMLOperations(url=hs_url)
    values = water.GetValues(site_desc,
                             variable_desc,
                             start_date,
                             end_date,
                             format='json')
    # print(values)
    df = pd.DataFrame.from_dict(values['values'])
    # print(df)
    if df.empty:
        return_obj['graphs'] = []
        return_obj['interpolation'] = []
        return_obj['unit_name'] = []
        return_obj['variablename'] = []
        return_obj['timeUnitName'] = []
        return JsonResponse(return_obj)

    variable_name = df['variableName'].tolist()[0]
    unit_name = df['unitAbbreviation'].tolist()[0]
    time_unit_name = df['timeUnitName'].tolist()[0]
    time_series_vals = df['dataValue'].tolist()
    time_series_timeUTC = df['dateTime'].tolist()
    return_obj['graphs'] = list(zip(time_series_timeUTC, time_series_vals))
    return_obj['interpolation'] = water.GetInterpolation(values)
    return_obj['unit_name'] = unit_name
    return_obj['variablename'] = variable_name
    return_obj['timeUnitName'] = time_unit_name
    dict_xml = []

    for gps_ in return_obj['graphs']:
        chunk_xml = {}
        chunk_xml['DateTimeUTC'] = gps_[0]
        chunk_xml['DataValue'] = gps_[1]
        dict_xml.append(chunk_xml)

    current_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    my_vals = values['values'][0]

    context = {
        "data_values": dict_xml,
        "current_date": current_date,
        "init_date": time_series_timeUTC[0],
        "final_date": time_series_timeUTC[-1],
        "network": network,
        "code_variable": code_variable,
        "code_site": site_code,
        "site_name": my_vals["siteName"],
        "unitAbbreviation": my_vals["unitAbbreviation"],
        "latitude_longitude": f'{my_vals["latitude"]} {my_vals["longitude"]}',
        "site_id": my_vals["siteID"],
        "dataType": my_vals["dataType"],
    }

    template_renderizado = render_to_string(
        'water_data_explorer/wml2_values_template.xml', context)
    return_obj['template_renderizado'] = template_renderizado
    return JsonResponse(return_obj)