Working with OpenWPS:CP_MaskWithCF

Climate Masking Interface Service

  • OpenWPS:CP_MaskWithCF

This service generates and provides mask data using user's geometry data and options

Parameters

Parameter Description Data Type
inputClimateMaskData Input data for masking climate data in gClimateMaskJSON format application/json
inputRegion Input region in GeoJSON format application/json
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 with mask data

Creating gClimateMaskJSON file using climate data in NetCDF format

In [1]:
import tutorial_util as util
In [2]:
from netCDF4 import Dataset
import numpy as np
In [3]:
nc4file = 'data/pr_SEA-44_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 variable 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]:
# Generating gClimateMaskJSON data using variables in the nc file
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

Reading shape file

In [13]:
lat_min = -12.0
lat_max = 30.0

lon_min = 83.0
lon_max = 156.0
In [14]:
# Thailand (tha)  # tha is ISO-3166-1 alpha-3 code
#shape_file = 'data/shape/tha_simplified'
In [15]:
# Lao (lao) # lao is ISO-3166-1 alpha-3 code
shape_file = 'data/shape/lao_simplified'
In [16]:
# Plotting shape data
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

map = Basemap(projection = 'merc', resolution = 'l',\
            llcrnrlat = lat_min, urcrnrlat = lat_max, \
            llcrnrlon = lon_min, urcrnrlon = lon_max, \
            lat_ts = 0)

#map.drawmapboundary(fill_color = 'aqua')
map.fillcontinents(color = '#ddaa66', lake_color = 'aqua')
#map.drawcoastlines()

map.readshapefile(shape_file, 'country')

plt.show()

Shapefile to GeoJSON

In [17]:
inputRegion = util.make_Shapefile2GeoJSON(shape_file)
In [18]:
print (inputRegion)
{
  "type": "FeatureCollection",
  "features": [
    {
      "properties": {
        "NAME_0": "Laos",
        "FIRST_ISO": "LAO",
        "FIRST_NAME": "Laos"
      },
      "type": "Feature",
      "geometry": {
        "coordinates": [
          [
            [
              101.77947235107419,
              22.499179840087976
            ],
            [
              101.87298583984384,
              22.383661270141715
            ],
            [
              102.126350402832,
              22.43523025512698
            ],
            [
              102.61820220947274,
              21.91696548461914
            ],
            [
              102.67988586425784,
              21.655939102172795
            ],
            [
              102.8236999511719,
              21.8425235748291
            ],
            [
              102.87974548339858,
              21.70289993286127
            ],
            [
              102.97441864013675,
              21.745372772216854
            ],
            [
              102.8141250610353,
              21.264078140258846
            ],
            [
              103.11642456054702,
              20.896982192993278
            ],
            [
              103.69066619873055,
              20.660419464111442
            ],
            [
              103.81287384033206,
              20.86406135559082
            ],
            [
              104.10642242431643,
              20.97669792175293
            ],
            [
              104.63877868652341,
              20.669441223144503
            ],
            [
              104.37597656250009,
              20.443260192871094
            ],
            [
              104.47370910644545,
              20.369079589843864
            ],
            [
              104.66021728515639,
              20.475032806396598
            ],
            [
              104.6132965087892,
              20.242906570434627
            ],
            [
              104.99302673339852,
              20.09493064880371
            ],
            [
              104.83023071289065,
              19.79147529602062
            ],
            [
              104.55703735351577,
              19.6103191375733
            ],
            [
              104.06843566894534,
              19.6832218170166
            ],
            [
              104.1189117431642,
              19.501611709594698
            ],
            [
              103.87249755859389,
              19.309160232543917
            ],
            [
              104.73923492431638,
              18.8004989624024
            ],
            [
              105.13322448730477,
              18.716812133789148
            ],
            [
              105.1836853027344,
              18.324893951416044
            ],
            [
              105.36728668212922,
              18.161003112793225
            ],
            [
              105.49047088623055,
              18.203022003173885
            ],
            [
              105.75010681152375,
              17.66859626770031
            ],
            [
              106.54871368408217,
              17.005247116088896
            ],
            [
              106.67546081542983,
              16.4521617889406
            ],
            [
              106.86225128173842,
              16.54108238220209
            ],
            [
              106.96013641357436,
              16.303901672363253
            ],
            [
              107.45212554931655,
              16.08813285827634
            ],
            [
              107.39679718017581,
              15.89005470275876
            ],
            [
              107.21042633056643,
              15.82577705383298
            ],
            [
              107.3817672729493,
              15.493600845336971
            ],
            [
              107.62405395507827,
              15.414702415466323
            ],
            [
              107.4495162963868,
              14.52352046966547
            ],
            [
              107.30459594726577,
              14.583131790161104
            ],
            [
              106.85473632812497,
              14.285511970520119
            ],
            [
              106.54548645019528,
              14.590620994567956
            ],
            [
              106.4121475219728,
              14.44907093048107
            ],
            [
              106.25508880615243,
              14.485301017761287
            ],
            [
              106.22100067138686,
              14.36003017425547
            ],
            [
              106.00232696533206,
              14.367861747741799
            ],
            [
              106.18910980224618,
              14.060380935669059
            ],
            [
              106.10655212402352,
              13.907600402832102
            ],
            [
              105.27787017822263,
              14.173061370849553
            ],
            [
              105.20900726318357,
              14.340272903442482
            ],
            [
              105.53600311279311,
              14.552291870117344
            ],
            [
              105.62233734130857,
              14.974631309509334
            ],
            [
              105.46698760986337,
              15.112990379333539
            ],
            [
              105.58397674560561,
              15.313160896301255
            ],
            [
              105.46964263916024,
              15.347990036010899
            ],
            [
              105.63761138916018,
              15.658711433410758
            ],
            [
              105.39557647705081,
              15.801420211791935
            ],
            [
              105.42530822753903,
              16.003751754760827
            ],
            [
              105.04946136474618,
              16.100139617920036
            ],
            [
              104.74603271484384,
              16.528184890747156
            ],
            [
              104.80197906494138,
              17.382730484008846
            ],
            [
              104.28137969970712,
              17.85069084167486
            ],
            [
              103.97441864013675,
              18.33440971374509
            ],
            [
              103.31276702880862,
              18.430952072143555
            ],
            [
              103.04116821289065,
              17.97764015197751
            ],
            [
              102.67830657958999,
              17.801349639892635
            ],
            [
              102.60465240478524,
              17.955070495605526
            ],
            [
              102.08982086181649,
              18.21780967712411
            ],
            [
              101.16655731201186,
              17.46682167053217
            ],
            [
              100.96009063720717,
              17.57215118408206
            ],
            [
              101.18598175048842,
              18.0636310577392
            ],
            [
              101.18363952636727,
              18.337358474731502
            ],
            [
              101.05596923828128,
              18.439769744873132
            ],
            [
              101.27297973632827,
              18.688631057739343
            ],
            [
              101.35942840576175,
              19.048009872436552
            ],
            [
              101.18895721435544,
              19.395700454712028
            ],
            [
              101.29001617431643,
              19.566310882568416
            ],
            [
              100.89595794677737,
              19.631759643554858
            ],
            [
              100.7738418579103,
              19.485170364379826
            ],
            [
              100.48233032226571,
              19.487171173095675
            ],
            [
              100.40590667724624,
              19.749282836914176
            ],
            [
              100.57982635498061,
              20.16650009155282
            ],
            [
              100.33264160156264,
              20.396419525146484
            ],
            [
              100.1712799072267,
              20.242879867553654
            ],
            [
              100.08676910400399,
              20.350891113281307
            ],
            [
              100.25948333740237,
              20.746959686279354
            ],
            [
              100.64742279052737,
              20.87555122375494
            ],
            [
              100.51558685302749,
              20.897830963134822
            ],
            [
              100.72710418701186,
              21.31212425231928
            ],
            [
              101.15474700927732,
              21.563869476318416
            ],
            [
              101.28504180908206,
              21.178680419921932
            ],
            [
              101.56336975097653,
              21.24845123291027
            ],
            [
              101.78785705566409,
              21.140031814575167
            ],
            [
              101.78145599365249,
              21.832490921020565
            ],
            [
              101.54695892333987,
              22.248069763183764
            ],
            [
              101.77947235107419,
              22.499179840087976
            ]
          ]
        ],
        "type": "Polygon"
      }
    }
  ]
}

Making mask data with APCC OpenWPS service

In [19]:
from owslib.wps import WebProcessingService
In [20]:
from owslib.wps import WebProcessingService, monitorExecution
In [21]:
import tutorial_util as util
In [22]:
service_host = 'http://openwps.apcc21.org/wps/wps.wsgi'
In [23]:
wps_process = 'OpenWPS:CP_MaskWithCF'  # WPS process name
In [24]:
wps = WebProcessingService(service_host, verbose = False, skip_caps = True)
In [25]:
inputMaskingOperator = 'RectInterPy' #'PtInExpMBR' #'PtInMBR' #'PtInPy'  #'RectInterPy'
inputDistance = "3.5"
In [26]:
inputs = [
          ('inputClimateMaskData', inputgClimateMask),
          ('inputRegion', inputRegion),
          ('inputMaskingOperator', inputMaskingOperator),
          ('inputDistance', inputDistance)
         ]
In [27]:
outputs = [
  ('identifier', 'output'),
  ('reference', False)
]
In [28]:
execution = wps.execute(wps_process, inputs, output=outputs)
In [29]:
monitorExecution(execution)
In [30]:
mask_data = execution.processOutputs[0]
print(mask_data.data)
['{\n  "maskresult" : {\n     "version" : "0.1",\n     "dimension" : [73, 147],\n     "method" : "BinaryGzipBase64",\n     "mask" : "H4sIAEgCLFoC/+3WwQlAIQxEwZay/Tf3D4JnT58VZyp4mBCcAQAAAAAAAIDXJClMqquSdOXgWlepJqVuk1ZL1dSyTV2TRzooikc6aHK1bznbxX+3f303nN3E6ykAAA=="\n  }\n}']
In [31]:
print (mask_data.data[0])
{
  "maskresult" : {
     "version" : "0.1",
     "dimension" : [73, 147],
     "method" : "BinaryGzipBase64",
     "mask" : "H4sIAEgCLFoC/+3WwQlAIQxEwZay/Tf3D4JnT58VZyp4mBCcAQAAAAAAAIDXJClMqquSdOXgWlepJqVuk1ZL1dSyTV2TRzooikc6aHK1bznbxX+3f303nN3E6ykAAA=="
  }
}
In [32]:
# Importing JSON library
import json
In [33]:
mask_data_json = json.loads(mask_data.data[0])
In [34]:
print (mask_data_json)
{'maskresult': {'mask': 'H4sIAEgCLFoC/+3WwQlAIQxEwZay/Tf3D4JnT58VZyp4mBCcAQAAAAAAAIDXJClMqquSdOXgWlepJqVuk1ZL1dSyTV2TRzooikc6aHK1bznbxX+3f303nN3E6ykAAA==', 'version': '0.1', 'method': 'BinaryGzipBase64', 'dimension': [73, 147]}}
In [35]:
mask_data_array = util.getMaskData2Array(mask_data_json)
In [36]:
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 [37]:
import matplotlib.pyplot as plt
In [38]:
from mpl_toolkits.basemap import Basemap, cm
In [39]:
lon_0 = lon.mean()
In [40]:
lat_0 = lat.mean()
In [41]:
m = Basemap(projection = 'merc', resolution = 'l',\
            llcrnrlat = lat.min(), urcrnrlat = lat.max(), \
            llcrnrlon = lon.min(), urcrnrlon = lon.max(), \
            lat_ts = lat_0)
In [42]:
lon_g, lat_g = np.meshgrid(lon, lat)
xi, yi = m(lon_g, lat_g)
In [43]:
# 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 [44]:
# 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()