<jupyter_start><jupyter_text>Get data from eartH2Observe OpeNDAP server This small notebook shows how the get data from the eartH2Observe OpeNDAP server using python and the netCDF4 package. To run it make sure you notebook server is started like this:- ipython notebook --pylab=inlineIf you want to run this outside of ipython just download the .py file. IN that case make sure to import numpy and scipy first.See also: https://publicwiki.deltares.nl/display/OET/OPeNDAP+subsetting+with+python Cataloghttp://wci.earth2observe.eu/thredds/catalog.html<jupyter_code>#!/usr/bin/env python
# we need the netcdf4 library. This allows us to read netcdf from OpeNDAP or files
import netCDF4

# Uncomment these if needed
#from numpy import *
#from scipy import *

# specify an url
rainurl = 'http://wci.earth2observe.eu/thredds/dodsC/ecmwf/met_forcing_v0/2012/Rainf_daily_E2OBS_201201.nc'

# create a dataset object
raindataset = netCDF4.Dataset(rainurl)
%pylab inline
def getlat(ncdataset):
    """
    """

    for a in ncdataset.variables:
        if  ncdataset.variables[a].standard_name == 'latitude':
            return ncdataset.variables[a]
    
    return None


def getlon(ncdataset):
    """
    """

    for a in ncdataset.variables:
        if  ncdataset.variables[a].standard_name == 'longitude':
            return ncdataset.variables[a]
    
    return None

def getheigth(ncdataset):
    """
    """

    for a in ncdataset.variables:
        if  ncdataset.variables[a].standard_name == 'height':
            return ncdataset.variables[a]
    
    return None


a= getlon(raindataset)
a.shape
# Get some data ......
# note that with larg set you do not want to read all in memory at once like we do here
precip = raindataset.variables['Rainf'][:]
lat = raindataset.variables['lat'][:]
lon = raindataset.variables['lon'][:]

# Show the max value in the matrix
precip[1,:,:].max()
# Display the units
raindataset.variables['Rainf'].units<jupyter_output><empty_output><jupyter_text>Now let's make a plot to see the contents of the file<jupyter_code>f, ax = plt.subplots(figsize=(16, 9))
Lon,Lat = meshgrid(lon, lat)
mesh = pcolormesh(Lon,Lat,log( 0.000001 + precip[1,:,:]),cmap='hot')<jupyter_output><empty_output><jupyter_text>Extract data for a catchment.<jupyter_code># Select lat and long for our cathcment
# Bounding box for our catchment
BB = dict(
   lon=[ 143, 150],
   lat=[-37, -33]
   )
 
(latidx,) = logical_and(lat >= BB['lat'][0], lat < BB['lat'][1]).nonzero()
(lonidx,) = logical_and(lon >= BB['lon'][0], lon < BB['lon'][1]).nonzero()

# get rid of the non used lat/lon now
lat = lat[latidx]
lon = lon[lonidx]
# Now get the time for the x-axis
time = raindataset.variables['time']
timeObj = netCDF4.num2date(time[:], units=time.units, calendar=time.calendar)
#Now determine area P for each timestep and display in a graph
# first  the mean per area lat, next average those also
# Multiply with timestep in seconds to get mm
p_select = precip[:,latidx.min():latidx.max(),lonidx.min():lonidx.max()]
# PLot the sum over this month for the subcatchment

Lon,Lat = meshgrid(lon, lat)
mesh = pcolormesh(Lon,Lat,p_select.sum(axis=0))
title("Cumulative precipitation")

p_mean = p_select.mean(axis=2).mean(axis=1) * hstack((diff(time[:]),mean(diff(time[:]))))
f, ax = plt.subplots(figsize=(16, 6))
ax.plot(timeObj,p_mean)
title("Precipitation")
#Save to disk

p_select = precip[:,latidx.min():latidx.max(),lonidx.min():lonidx.max()]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13007194244604317, 'fraction_numerical': 0.01553956834532374, 'mean_word_length': 4.535031847133758, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '392098', 'license': 'MIT', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1232, 'n_words': 332, 'path': 'wflow/notebooks/openDAP-e2o.ipynb', 'repo_name': 'quanpands/wflow', 'size': '3475'}