Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
h2.
scrollbar

Exercise

...

outline

...

We

...

want

...

to

...

download

...

a

...

NetCdf

...

file

...

from

...

the

...

opendap

...

server

...

(

...

http://opendap.deltares.nl

...

),

...

and

...

read

...

the

...

variables,

...

dimensions

...

and

...

data.

...

Download

...

a

...

NetCdf

...

file

...

and

...

read

...

its

...

data

...


Add

...

the

...

following

...

import

...

statements

...

to

...

the

...

region

...

Import

...

libs:

Code Block
languagepython
{color}:

{code:language=python}
# python libs
import urllib
from FmWorkshopHelperFunctions import *

# DeltaShell libs
from DelftTools.Utils.NetCdf import NetCdfFile

These will add the libraries "urllib" (python lib containing the code to download urls), FmWorkshopHelperFunctions (python lib containing helper functions for working with arrays) and NetCdfFile (DeltaShell code for working with NetCdf files).


And add this code to the end of the script :

Code Block
languagepython
{code}
\\

And add this code to the end of the script :

{code:language=python}
#region Read NetCdf file

# Download file from opendap
remote_path = "http://opendap.deltares.nl/static/deltares/delftdashboard/toolboxes/TideStations/iho.nc"
urllib.urlretrieve (remote_path, rootPath + "iho.nc")

file = NetCdfFile.OpenExisting(rootPath + "iho.nc")

# Show all variables
for var in file.GetVariables():
	print file.GetVariableName(var)

# Show all dimentionsdimensions
for dim in file.GetAllDimensions():
	print file.GetDimensionName(dim)

# Get variables
stations = file.GetVariableByName("stations")
components = file.GetVariableByName("components")

# Get dimensions
nrOfStations = file.GetDimensionLength(file.GetDimension("stations"))
nrOfComponents = file.GetDimensionLength(file.GetDimension("components"))
componentNameLength = file.GetDimensionLength(file.GetDimension("component_string_length"))
stationNameLength = file.GetDimensionLength(file.GetDimension("name_string_length"))

# Get data
stationNames = Transpose2DCharArrayToStringList(file.Read(stations),stationNameLength, nrOfStations)
componentNames = Transpose2DCharArrayToStringList(file.Read(components),componentNameLength, nrOfComponents)

latData = file.Read(file.GetVariableByName("lat"))
lonData = file.Read(file.GetVariableByName("lon"))
phaseData = file.Read(file.GetVariableByName("amplitude"))
amplitudeData = file.Read(file.GetVariableByName("amplitude"))

#endregion

{code}

\\
\\


The first statements will copy the iho.nc file to a local directory (in our case rootPath)

Code Block
languagepython

# Download file from opendap
remote_path = "http://opendap.deltares.nl/static/deltares/delftdashboard/toolboxes/TideStations/iho.nc"
urllib.urlretrieve (remote_path, rootPath + "iho.nc")


Then we continue by reading the file using the DeltaShell NetCdfFile class and showing all the variable and dimension names in the message window of DeltaShell by using the print statement

Code Block
languagepython

file = NetCdfFile.OpenExisting(rootPath + "iho.nc")

# Show all variables
for var in file.GetVariables():
	print file.GetVariableName(var)

# Show all dimensions
for dim in file.GetAllDimensions():
	print file.GetDimensionName(dim)


Now that we know what the names of the dimensions and variables are we can use them to retrieve them with this code:

Code Block
languagepython

# Get variables
stations = file.GetVariableByName("stations")
components = file.GetVariableByName("components")

# Get dimensions
nrOfStations = file.GetDimensionLength(file.GetDimension("stations"))
nrOfComponents = file.GetDimensionLength(file.GetDimension("components"))
componentNameLength = file.GetDimensionLength(file.GetDimension("component_string_length"))
stationNameLength = file.GetDimensionLength(file.GetDimension("name_string_length"))


And retrieve the data for the variables :

Code Block
languagepython

# Get data
stationNames = Transpose2DCharArrayToStringList(file.Read(stations),stationNameLength, nrOfStations)
componentNames = Transpose2DCharArrayToStringList(file.Read(components),componentNameLength, nrOfComponents)

latData = file.Read(file.GetVariableByName("lat"))
lonData = file.Read(file.GetVariableByName("lon"))
phaseData = file.Read(file.GetVariableByName("amplitude"))
amplitudeData = file.Read(file.GetVariableByName("amplitude"))



scrollbar