aghoshpro

ISSUE_01 wcps_rasdaman.py

Jun 19th, 2023 (edited)
56
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | Source Code | 0 0
  1. def wcps_rasdaman(query, ip='saocompute.eurac.edu/sincohmap', file_name=''):
  2.     """
  3.    Sends a WCPS query to a Rasdaman server and wraps the response for further use in Python depending on the
  4.    the response format chosen in the query.
  5.    
  6.    Args:
  7.        query (str) -- WCPS query you want to send to the Rasdaman server
  8.        ip (str) -- IP of Rasdaman server (default saocompute.eurac.edu)
  9.    
  10.    Returns:
  11.        Either one of the following
  12.        - Numpy array for JSON/CSV formatted response
  13.        - Xarray Dataset for a netCDF formatted response
  14.        - Filepath to a TIFF/JPEG/JP2/PNG file saved to disk, in respect to the response image type
  15.        - The response object, when the response could not be processed
  16.    
  17.    Sources:
  18.        http://xarray.pydata.org/en/stable/io.html#netcdf
  19.        http://xarray.pydata.org/en/stable/data-structures.html
  20.        
  21.    Author: Harald Kristen, Alexander Jacob
  22.    Date: 2019-05-29
  23.    """
  24.  
  25.     import requests
  26.     import json
  27.     import werkzeug
  28.     import numpy as np
  29.     import io
  30.     import xarray as xr
  31.     import uuid
  32.     import os
  33.     import xml.etree.ElementTree as ET
  34.     import base64
  35.  
  36.     #set the work directory
  37.     work_directory = ''#os.getcwd()
  38.    
  39.     #print('WCPS init')
  40.    
  41.     if ip == 'saocompute.eurac.edu/sincohmap' or ip == 'saocompute.eurac.edu':
  42.         url = 'http://' + ip + '/rasdaman/ows?SERVICE=WCS&VERSION=2.0.1&REQUEST=ProcessCoverages'  
  43.     else:
  44.         url = 'http://' + ip + ':8080/rasdaman/ows?SERVICE=WCS&VERSION=2.0.1&REQUEST=ProcessCoverages'
  45.    
  46.     #Fix the special characters used the input query like ' ', $ and so on
  47.     query = werkzeug.url_fix(query)
  48.     print('This is the URL, used for the request:\n' + url + '&query=' + query)
  49.     url = url + '&query=' + query
  50.  
  51.     try:
  52.         #Send the request to Rasdaman and save the response in the variable "r"
  53.         r = requests.get(url, stream=True)
  54.     except Exception as ex:
  55.         print(tpye(ex))
  56.         print(ex.args)
  57.         print(ex)
  58.  
  59.     #If there is an error, plot the error message that comes from Rasdaman & exit script
  60.     if r.status_code != requests.codes.ok:
  61.         print('HTTP Error ' + str(r.status_code))
  62.         root = ET.fromstring(r.text)
  63.         for element in root.iter():
  64.             if element.tag == '{http://www.opengis.net/ows/2.0}ExceptionText':
  65.                 print(element.text)
  66.  
  67.     print('currently receiving content of type: ' + r.headers['Content-Type'])
  68.  
  69.     #print('This is the URL, used for the request \n' + r.url)
  70.  
  71.     if r.headers['Content-Type'] == 'text/plain':
  72.         #print('return type is text')
  73.         # Convert CSV or json to NumpyArray
  74.         response_text = r.text()
  75.         output = response_text
  76.         # The JSON version also works for 2D arrays.
  77.         if response_text.startswith("{"):
  78.             loaded = r.json()
  79.             output = np.array(loaded)
  80.         else:
  81.             output = np.fromstring(response_text[1:-1], dtype = float, sep = ',')
  82.  
  83.     elif r.headers['Content-Type'] == 'application/json':
  84.         # Convert JSON to NumpyArray
  85.         loaded = r.json()
  86.         output = np.array(loaded)
  87.  
  88.     elif r.headers['Content-Type'] == 'application/netcdf':
  89.         print(r.headers)
  90.         # create x array dataset from input stream
  91.         if file_name == '':
  92.             file_name = 'wcps_' + str(uuid.uuid4()) + '.nc'
  93.         print('the following file has been saved locally: ' + file_name)
  94.         with io.open(file_name, 'wb') as outfile:
  95.             outfile.write(r.content)        
  96.         output_open = xr.open_dataset(file_name)
  97.         # Xarray is normally lazy loading netCDF files
  98.         # As we want to perform intense computation, we load the file directly in the main memory with Dataset.load()
  99.         output = xr.Dataset.load(output_open)
  100.  
  101.     elif r.headers['Content-Type'] in ['image/tiff', 'image/png', 'image/jp2', 'image/jpeg']:
  102.         # Write response in choosen image format to disk and print filepath
  103.         image_type = r.headers['Content-Type']
  104.         if file_name == '':
  105.             file_ending = image_type[6:]
  106.             # write TIFF to disk and print filepath
  107.             tf = 'wcps_' + str(uuid.uuid4())
  108.             file_name = tf + '.' + file_ending
  109.         with io.open(file_name, 'wb') as outfile:
  110.             outfile.write(r.content)
  111.         print('the following file has been saved locally: ' + file_name)
  112.         output = file_name
  113.  
  114.     else:
  115.         output = r
  116.         output_type = r.headers['Content-Type']
  117.         print('The response could not be processed, as it is a ' + output_type)
  118.  
  119.     return output
Comments
  • aghoshpro
    1 year (edited)
    # HTML 5 0.09 KB | 0 0
    1. Source: https://gitlab.inf.unibz.it/SInCohMap/RoundRobinTutorials/-/blob/master/wcps_rasdaman.py
  • aghoshpro
    1 year (edited)
    # Python 0.48 KB | 0 0
    1. ### ISSUE
    2. /tmp/ipykernel_114914/613252522.py:47: DeprecationWarning: 'werkzeug.urls.url_fix' is deprecated and will be removed in Werkzeug 3.0.
    3.   query = werkzeug.urls.url_fix(query)
    4.  
    5. This is the URL, used for the request:
    6. http://localhost:8080/rasdaman/ows?SERVICE=WCS&VERSION=2.0.1&REQUEST=ProcessCoverages&query=for%20$c%20in%20(Surface_Temperature_Sweden)%20return%20avg($c)
    7. currently receiving content of type:
    8. The response could not be processed, as it is a
    9. <Response [200]>
    10.  
Add Comment
Please, Sign In to add comment