Working with OpenWPS:CP_MaskWithISO3166

Climate Masking Interface Service

  • OpenWPS:CP_MaskWithISO3166

This service generates and provides mask data using ISO-3166-1 country code without user's country boundary data

Parameters

Parameter Description Data Type
inputClimateMaskData Input data for masking climate data in gClimateMaskJSON format application/json
inputISOCountryCode Input ISO 3166 2 or 3 country code string
inputMaskingOperator Input masking operator string
inputDistance Input distance of expanded MBR or spatial resolution string
MaskingOperator Description
PtInPy Point in Polygon
PtInMBR Point in MBR(Minimum Bounding Rectangle)
PtInExpMBR Point in Expanded MBR
RectInterPy Rectangle (Cell Boundary) In Polygon

Exercise

Clipping climate data in NetCDF format using a ISO-3166-1 country code

Generating gClimateMaskJSON data using climate data in NetCDF format with climate and forecast (CF) metadata

In [1]:
import tutorial_util as util
In [2]:
from netCDF4 import Dataset
import numpy as np
In [3]:
nc4file = 'data/pr_SEA-55_HadGEM2-AO_rcp85_r1i1p1_WRF_v3-5_day_209101-210012.nc'
In [4]:
ds = Dataset(nc4file, 'r')  # Open netcdf file
In [5]:
lon = ds.variables['lon'][:]  # Get data of lon variable in the nc file
In [6]:
lat = ds.variables['lat'][:]  # Get data of lat variable in the nc file
In [7]:
lon_bounds = ds.variables['lon_bnds'][:]  # Get data of lon_bnds varaible in the nc file
In [8]:
lat_bounds = ds.variables['lat_bnds'][:]   # Get data of lat_bnds variable in the nc file
In [9]:
# for plotting
pr = ds.variables['pr'][0, :]    # time index = 1
pr_units = ds.variables['pr'].units
In [10]:
inputgClimateMask = util.make_gClimateMaskJSON(lat, lon, lat_bounds, lon_bounds)
In [11]:
print (inputgClimateMask)
{

  "climatemask": {
    "version": "0.1",
    "coordinate": {
      "dimension": [73, 147],
      "lat": {
        "type": "Individual",
        "method": "None",
        "data": [-13.0, -12.5, -12.0, -11.5, -11.0, -10.5, -10.0, -9.5, -9.0, -8.5, -8.0, -7.5, -7.0, -6.5, -6.0, -5.5, -5.0, -4.5, -4.0, -3.5, -3.0, -2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0]
      },
      "lon": {
        "type": "Individual",
        "method": "None",
        "data": [83.0, 83.5, 84.0, 84.5, 85.0, 85.5, 86.0, 86.5, 87.0, 87.5, 88.0, 88.5, 89.0, 89.5, 90.0, 90.5, 91.0, 91.5, 92.0, 92.5, 93.0, 93.5, 94.0, 94.5, 95.0, 95.5, 96.0, 96.5, 97.0, 97.5, 98.0, 98.5, 99.0, 99.5, 100.0, 100.5, 101.0, 101.5, 102.0, 102.5, 103.0, 103.5, 104.0, 104.5, 105.0, 105.5, 106.0, 106.5, 107.0, 107.5, 108.0, 108.5, 109.0, 109.5, 110.0, 110.5, 111.0, 111.5, 112.0, 112.5, 113.0, 113.5, 114.0, 114.5, 115.0, 115.5, 116.0, 116.5, 117.0, 117.5, 118.0, 118.5, 119.0, 119.5, 120.0, 120.5, 121.0, 121.5, 122.0, 122.5, 123.0, 123.5, 124.0, 124.5, 125.0, 125.5, 126.0, 126.5, 127.0, 127.5, 128.0, 128.5, 129.0, 129.5, 130.0, 130.5, 131.0, 131.5, 132.0, 132.5, 133.0, 133.5, 134.0, 134.5, 135.0, 135.5, 136.0, 136.5, 137.0, 137.5, 138.0, 138.5, 139.0, 139.5, 140.0, 140.5, 141.0, 141.5, 142.0, 142.5, 143.0, 143.5, 144.0, 144.5, 145.0, 145.5, 146.0, 146.5, 147.0, 147.5, 148.0, 148.5, 149.0, 149.5, 150.0, 150.5, 151.0, 151.5, 152.0, 152.5, 153.0, 153.5, 154.0, 154.5, 155.0, 155.5, 156.0]
      }
    },
    "coordinate_bounds": {
      "lat_bnds": {
        "dimension": [73, 2],
        "type": "Individual",
        "method": "None",
        "data": [-13.25, -12.75, -12.75, -12.25, -12.25, -11.75, -11.75, -11.25, -11.25, -10.75, -10.75, -10.25, -10.25, -9.75, -9.75, -9.25, -9.25, -8.75, -8.75, -8.25, -8.25, -7.75, -7.75, -7.25, -7.25, -6.75, -6.75, -6.25, -6.25, -5.75, -5.75, -5.25, -5.25, -4.75, -4.75, -4.25, -4.25, -3.75, -3.75, -3.25, -3.25, -2.75, -2.75, -2.25, -2.25, -1.75, -1.75, -1.25, -1.25, -0.75, -0.75, -0.25, -0.25, 0.25, 0.25, 0.75, 0.75, 1.25, 1.25, 1.75, 1.75, 2.25, 2.25, 2.75, 2.75, 3.25, 3.25, 3.75, 3.75, 4.25, 4.25, 4.75, 4.75, 5.25, 5.25, 5.75, 5.75, 6.25, 6.25, 6.75, 6.75, 7.25, 7.25, 7.75, 7.75, 8.25, 8.25, 8.75, 8.75, 9.25, 9.25, 9.75, 9.75, 10.25, 10.25, 10.75, 10.75, 11.25, 11.25, 11.75, 11.75, 12.25, 12.25, 12.75, 12.75, 13.25, 13.25, 13.75, 13.75, 14.25, 14.25, 14.75, 14.75, 15.25, 15.25, 15.75, 15.75, 16.25, 16.25, 16.75, 16.75, 17.25, 17.25, 17.75, 17.75, 18.25, 18.25, 18.75, 18.75, 19.25, 19.25, 19.75, 19.75, 20.25, 20.25, 20.75, 20.75, 21.25, 21.25, 21.75, 21.75, 22.25, 22.25, 22.75, 22.75, 23.25]
      },
      "lon_bnds": {
        "dimension": [147, 2],
        "type": "Individual",
        "method": "None",
        "data": [82.75, 83.25, 83.25, 83.75, 83.75, 84.25, 84.25, 84.75, 84.75, 85.25, 85.25, 85.75, 85.75, 86.25, 86.25, 86.75, 86.75, 87.25, 87.25, 87.75, 87.75, 88.25, 88.25, 88.75, 88.75, 89.25, 89.25, 89.75, 89.75, 90.25, 90.25, 90.75, 90.75, 91.25, 91.25, 91.75, 91.75, 92.25, 92.25, 92.75, 92.75, 93.25, 93.25, 93.75, 93.75, 94.25, 94.25, 94.75, 94.75, 95.25, 95.25, 95.75, 95.75, 96.25, 96.25, 96.75, 96.75, 97.25, 97.25, 97.75, 97.75, 98.25, 98.25, 98.75, 98.75, 99.25, 99.25, 99.75, 99.75, 100.25, 100.25, 100.75, 100.75, 101.25, 101.25, 101.75, 101.75, 102.25, 102.25, 102.75, 102.75, 103.25, 103.25, 103.75, 103.75, 104.25, 104.25, 104.75, 104.75, 105.25, 105.25, 105.75, 105.75, 106.25, 106.25, 106.75, 106.75, 107.25, 107.25, 107.75, 107.75, 108.25, 108.25, 108.75, 108.75, 109.25, 109.25, 109.75, 109.75, 110.25, 110.25, 110.75, 110.75, 111.25, 111.25, 111.75, 111.75, 112.25, 112.25, 112.75, 112.75, 113.25, 113.25, 113.75, 113.75, 114.25, 114.25, 114.75, 114.75, 115.25, 115.25, 115.75, 115.75, 116.25, 116.25, 116.75, 116.75, 117.25, 117.25, 117.75, 117.75, 118.25, 118.25, 118.75, 118.75, 119.25, 119.25, 119.75, 119.75, 120.25, 120.25, 120.75, 120.75, 121.25, 121.25, 121.75, 121.75, 122.25, 122.25, 122.75, 122.75, 123.25, 123.25, 123.75, 123.75, 124.25, 124.25, 124.75, 124.75, 125.25, 125.25, 125.75, 125.75, 126.25, 126.25, 126.75, 126.75, 127.25, 127.25, 127.75, 127.75, 128.25, 128.25, 128.75, 128.75, 129.25, 129.25, 129.75, 129.75, 130.25, 130.25, 130.75, 130.75, 131.25, 131.25, 131.75, 131.75, 132.25, 132.25, 132.75, 132.75, 133.25, 133.25, 133.75, 133.75, 134.25, 134.25, 134.75, 134.75, 135.25, 135.25, 135.75, 135.75, 136.25, 136.25, 136.75, 136.75, 137.25, 137.25, 137.75, 137.75, 138.25, 138.25, 138.75, 138.75, 139.25, 139.25, 139.75, 139.75, 140.25, 140.25, 140.75, 140.75, 141.25, 141.25, 141.75, 141.75, 142.25, 142.25, 142.75, 142.75, 143.25, 143.25, 143.75, 143.75, 144.25, 144.25, 144.75, 144.75, 145.25, 145.25, 145.75, 145.75, 146.25, 146.25, 146.75, 146.75, 147.25, 147.25, 147.75, 147.75, 148.25, 148.25, 148.75, 148.75, 149.25, 149.25, 149.75, 149.75, 150.25, 150.25, 150.75, 150.75, 151.25, 151.25, 151.75, 151.75, 152.25, 152.25, 152.75, 152.75, 153.25, 153.25, 153.75, 153.75, 154.25, 154.25, 154.75, 154.75, 155.25, 155.25, 155.75, 155.75, 156.25]
      }
    }
  }
}

In [12]:
ds.close()  # Close nc file

Generating mask data

In [13]:
from owslib.wps import WebProcessingService
In [14]:
from owslib.wps import WebProcessingService, monitorExecution
In [15]:
import tutorial_util as util
In [16]:
service_host = 'http://openwps.apcc21.org/wps/wps.wsgi'  # OpenWPS WPS service url
In [17]:
wps_process = 'OpenWPS:CP_MaskWithISO3166'  # WPS process name
In [18]:
wps = WebProcessingService(service_host, verbose = False, skip_caps = True)
In [19]:
inputMaskingOperator = 'RectInterPy' #'PtInExpMBR' #'PtInMBR' #'PtInPy'  #'RectInterPy'
inputDistance = "3.5"
* You can find ISO 3166 country codes in Online Browsing Platform (OBP) (https://www.iso.org/obp/ui/) in ISO website.
In [20]:
inputs = [
          ('inputClimateMaskData', inputgClimateMask),
          ('inputISOCountryCode', 'PHL'), # Phlippines 
          ('inputMaskingOperator', inputMaskingOperator),
          ('inputDistance', inputDistance)
         ]
In [21]:
outputs = [
  ('identifier', 'output'),
  ('reference', False)
]
In [22]:
execution = wps.execute(wps_process, inputs, output=outputs)
In [23]:
monitorExecution(execution)
In [24]:
mask_data = execution.processOutputs[0]
In [25]:
print (mask_data.data)
['{\n  "maskresult" : {\n     "version" : "0.1",\n     "dimension" : [73, 147],\n     "method" : "BinaryGzipBase64",\n     "mask" : "H4sIACoCLFoC/+3YOw6AIBQF0S29u//NaSSAVhirUWcqyhN+IVSZmZmZmZmZmZmZPS1AUgI2IXA7IkeNBiE1S2raEJsbREo/bxkBdhKOlPOYsXCT0M8e5E4aHMgtENS6DUtF0g0TSnQ1SVqbCieStCbBHrq09zcORJwkrkmSpO9+CwC/TyS9k2R/bgMoqZI86ykAAA=="\n  }\n}']
In [26]:
# Importing JSON library
import json
In [27]:
mask_data_json = json.loads(mask_data.data[0])
In [28]:
mask_data_array = util.getMaskData2Array(mask_data_json)
In [29]:
print (mask_data_array, mask_data_array.shape)
[[False False False ..., False False False]
 [False False False ..., False False False]
 [False False False ..., False False False]
 ...,
 [False False False ..., False False False]
 [False False False ..., False False False]
 [False False False ..., False False False]] (73, 147)

Plotting netCDF data with mask data

In [30]:
import matplotlib.pyplot as plt
In [31]:
from mpl_toolkits.basemap import Basemap, cm
In [32]:
lon_0 = lon.mean()
In [33]:
lat_0 = lat.mean()
In [34]:
m = Basemap(projection = 'merc', resolution = 'l',\
            llcrnrlat = lat.min(), urcrnrlat = lat.max(), \
            llcrnrlon = lon.min(), urcrnrlon = lon.max(), \
            lat_ts = lat_0)
            #, lat_0 = lat_0, lon_0 = lon_0)
In [35]:
lon_g, lat_g = np.meshgrid(lon, lat)
xi, yi = m(lon_g, lat_g)
In [36]:
# Plot Data
cs = m.pcolor(xi,yi,np.squeeze(pr))

# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 10.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 10.), labels=[0,0,0,1], fontsize=10)

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()

# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(pr_units)

# Add Title
plt.title('Precipitation')

plt.show()
In [37]:
# Plot Data
masked_pr = np.ma.masked_where(mask_data_array==False, pr)
cs = m.pcolor(xi,yi,np.squeeze(masked_pr))

#clevs = [0,1,2.5,5,7.5,10,15,20,30,40,50,70,100,150,200,250,300,400,500,600,750]
#cs = m.contourf(xi, yi, pr, clevs, cmap=cm.s3pcpn)

# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 10.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 10.), labels=[0,0,0,1], fontsize=10)

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()

# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(pr_units)

# Add Title
plt.title('Precipitation')

plt.show()
In [38]:
masked_pr
Out[38]:
masked_array(data =
 [[-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 ...,
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]],
             mask =
 [[ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 ...,
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]],
       fill_value = 1e+20)