Server-Side Functions. Contents Background on DAP2 and Constraints Writing Server Functions: short version Programming with libdap A Real Server Function.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Written by: Dr. JJ Shepherd
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
ASP.NET Programming with C# and SQL Server First Edition
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
C++ fundamentals.
Basic Elements of C++ Chapter 2.
Intro to Generic Programming Templates and Vectors.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
OOP Languages: Java vs C++
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
OOI CyberInfrastructure: Technology Overview - Hyrax January 2009 Claudiu Farcas OOI CI Architecture & Design Team UCSD/Calit2.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Coverages and the DAP2 Data Model James Gallagher.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Copyright © Curt Hill Sounds, Resource Packs, JSON What more would you want?
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Accessing Remote Datasets using the DAP protocol through the netCDF interface. Dr. Dennis Heimbigner Unidata netCDF Workshop August 3-4, 2009.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Computer Engineering Rabie A. Ramadan Lecture 5.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
XP Tutorial 8 Adding Interactivity with ActionScript.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
OPeNDAP Hyrax Harnessing the power of the BES OPeNDAP Hyrax Back-End Server Patrick West
M1G Introduction to Programming 2 5. Completing the program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
OPeNDAP Developer’s Workshop Feb Server-side Functions for Geo-spatial Selection James Gallagher 22 Feb 2007.
Written by: Dr. JJ Shepherd
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
9/21/04 James Gallagher Server-Side: The Basics This part of the workshop contains an overview of the two servers which OPeNDAP has developed. One uses.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
9/21/04 James Gallagher Server Installation and Testing: Hands-on ● Install the CGI server with the HDF and FreeForm handlers ● Link data so the server.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Java Primer 1: Types, Classes and Operators
Tools to Build Clients.
Programming Language Concepts (CIS 635)
Topics Introduction to File Input and Output
Java Programming Language
Applying OO Concepts Using Java
Python Primer 1: Types and Operators
Object Oriented Programming in java
Topics Introduction to File Input and Output
OPeNDAP/Hyrax Interfaces
Presentation transcript:

Server-Side Functions

Contents Background on DAP2 and Constraints Writing Server Functions: short version Programming with libdap A Real Server Function Write HelloWorld() Advanced Topics

Background on DAP2 and Constraint Expressions DAP2 interface is via the URL The URL includes the –Name of the data set (/data/nc/fnoc1.nc) –Name of the desired response (DAS, …) –Optional Constraint Expression Constraint expression chooses which parts of the dataset (granule) are returned –If the dataset has several variables, chooses among those –If the variables are arrays, subsets those –If the are Structures, chooses the fields –If there are Sequences, selects values –Also runs functions if they are available

Constraint Expression (CE) Syntax URL: ? If is ‘dds’ or ‘dods’ the is used to contrain the response is a [& ]* –is a comma separated list of zero or more: Variables to be returned. Array hyperslabs Structure fields Function calls

CE Projection Examples Using the dataset: –?u,vChooses the variables u and v –?u[0][0:5][0:5] Chooses u and subsamples the array returning a 6 by 6 hyperslab Try these in a browser - you do not need to use the virtual machine’s server; you can substitute test.opendap.org for localhost –e.g., u[0][0:5][0:5] –I used ‘.ascii’ instead of ‘.dods’ so the result is easy to see in a browser

More CEs /data/nc/coads_climatology.nc.dds?SST

Choosing Fields /data/nc/coads_climatology.nc.dds?SST.TIME

CEs for data Replace the ‘.dds’ in the previous URLs with.ascii or.dods (use ascii with a web browser or dods with an application that understands binary data) Look at the results Try various combinations

What About Selections? Selections are used to choose values from relational types Try it: – – – The first constraint is a projection that chooses just the variable ‘day’ while the second constraint includes a selection clause which chooses only those entries where the value of day is 19. The third constraint returns all the fields but only those rows where day is 19

But there’s more to Constraint Expressions They can invoke functions The functions can compute and return values They can also return a Boolean value for use in the selection part of the constraint expression

Server Functions Functions are loaded into the Constraint Evaluator Each handler can load its own set of functions Functions can also be loaded by ‘function modules’

Function syntax can be one or more function calls – “(“ “)” –, Functions can be composed. Example of a simple function call: – –Run this example in a browser or command shell using getdap –This is a very simple function and it takes no arguments –It’s intent is both as an example and to provide information about the functions leaded into the server. jimg : ~/src/hyrax_1.8_release $ getdap " Dataset: function_result_fnoc1.nc version, " …

More Useful Functions grid(): sample a Grid variable based on the values of its map vectors geogrid(): just like grid(), but assume that the Grid holds geospatial data linear_scale(): Scale data using y=mx+b These are defined in the ‘functions’ module (see $HOME/src/hyrax- 1.9/src/bes/functions )

Function Documentation Use ?version() to get a listing of installed functions. Use ? () to get help information specific to a particular function Go to docs.opendap.org*

Example Use the geogrid() function to select values from the COADS Climatology data set: –?geogrid(SST,-5,-80,-40,-50)

Observations Note that the lat/lon values from the call match those of the response The arguments are a little odd but help is available by calling the function with no arguments This function’s logic is pretty crude, but the same interface can be used in front of more sophisticated processing (i.e., GIS) Calling the function with no arguments returns version information and the URL of the function’s documentation. Note also that this made no choice about the time information in the SST variable…

Selecting TIME ?geogrid(SST,40,-80,5,-50,\”2000<TIME\”) Try it - be careful with the quotes The general rule for geogrid() is that the first five arguments are required (they specify the Grid variable and the top, left, bottom, right (TLBR) lat/lon box) and then subsequent arguments are relational expressions which include constants and variables which are the names of the Grid’s map vectors. The result is the intersection of the zero or more relational expressions

Writing Server Functions: short version Write a C++ function which uses one of the three server function type signatures Make an instance of the ServerFunction class class Compile/link the new code into a handler An annotated example follows…

A Simple Example Look at the libdap source file VersionFunction.cc and scan the function “function_version”

Create a variable for the return value Name Arguments. Conceptually similar to the arguments passed to main() when writing a C program Set the value value Return the variable

Adding the Function into the Constraint Evaluator Constraint Expressions are evaluated using the ConstraintEvaluator C++ class The ConstraintEvaluator::add_function method is used to add a function and bind that function to a symbolic name used in the constraint expression –From the ce_functions.cc source file, find the function register_functions(ConstraintEvaluator &) –To add a function and bind it to a name use add_function() –ce.add_function("version”,function_version);

Functions are added to the ConstraintEvaluator Look in the ConstraintEvaluator.cc and ce_functions.cc source files to see how the base set of functions are added to the default constraint evaluator Note that ‘register_functions’ is a function defined in libdap’s ce_functions.cc file

Functions can be added to handlers, too In the handler’s specialization of the RequestHandler class This class contains the methods that build the response objects Add an explicit call to add_function to the method that returns data: –Bool NCRequestHandler::nc_build_data(BESDataHandlerInterface & dhi)

Programming with libdap The library has classes for the different datatypes (Byte, Int16, …, Structure, Grid) Each of those are children of BaseType –The common OO pattern of using a generic parent class to pass more specific instances is used often by libdap The DDS object in libdap is used to make all of the DAP responses (although there is a ‘DAS’ object in the library for ‘historical reasons’). The DDS holds all of the variables of the data set –The variables in the DDS contain the attribute information but hold no data until either data values are read or until just before the server runs the send_data() method.

Reading and Sending Data Three pieces are involved: The handler; the BES and libdap First the handler builds an instance of DDS that describes the entire dataset Then the BES calls uses a ‘Transmitter’ to run methods in libdap Finally, libdap parses the constraint, evaluates the constraint and serializes the result

What the handler does Initializes some data objects that are specific to the handler –Sets the ConstraintEvaluator instance –Reads flags (aka ‘keys’) for the handler from the bes.conf file Uses the BESDataHandlerInterface to –Get a pointer to the DDS object –Populates that DDS with variables –Adds attribute information How the handler populates the DDS is dependent on the underlying data store. A handler for HDF4 uses HDF4 API calls while a handler for SQL databases uses a very different kind of interface See bool HDF4RequestHandler::hdf4_build_data(BESDataHandlerInterface & dhi) in HDF4RequestHandler.cc

What the BES does The BES uses a ‘Transmitter’ to: –Pass the DDS object built by the handler to libdap –Pass the ConstraintEvaluator instantiated by the BES to libdap –Pass the Constraint Expression from the URL to libdap See class SendDataDDS in BESDapTransmitter.cc

What libdap does Parse the constraint If there are function calls in the constraint, then evaluate them and store the results in the DDS Serialize the DDS – this is the step where the response is actually sent back to the client See void ResponseBuilder::send_data in libdap:ResponseBuilder.cc

More about libdap’s role The variables held by a DDS may contain data (but they do not have to) Data values are read into the variables in a DDS using read() methods which are specialized for each handler. –For example, the NCArray::read() method for the netCDF handler is different than the HDF4Array::read() method in the HDF4 handler By convention, the read() method tests a ‘property’ called ‘read_p’ to see if the read() method has already been run. This keeps software from reading data for a variable more than once.

Implementation of HDFArray::read() Note that the ‘helper’ function sets the ‘read_p’ property when data are read correctly. … The read() method: An Example

How Constraints are Evaluated When libdap makes the data response, it first parses the constraint expression (see parse_constraint() in the class ConstraintEvaluator). This parses the projection information and marks the variables in the DDS which are part of the projection –The ‘send_p’ flag is used for this purpose –Each variable has a send_p flag and the send_p() accessor and set_send_p() mutator can be used to access and alter its value. –It binds any hyperslab operators to the Array variables –And it parses any functions, recording them for later evaluation While the evaluator sets the send_p flag based on the parse of the projection information, other code which has access to the variables can also work with this flag (server functions are one example).

Aside: What happens when there are no server functions? This is actually the typical case… The serialization code iterates over all of the top-level variables and For each variable where send_p is true, –Reads the data values using read() if read_p is not true –Uses XDR to encode the data and –Writes that encoding to the output stream

Server Functions can Expect The DDS will: Contain all of the variables in the dataset Each variable will have its attributes There will be no data in the variables Variables in the project will have the ‘send_p’ property set – but that does not extend to variables passed to the function as arguments

Other steps you’ll need to take Once you’ve written a CE function… –Compile it –Write the software to add it to an instance of ConstraintEvaluator –Recompile/link the relevant code –Restart the server

Where should I put my functions? You can bundle them with libdap - as the base set of functions are You can add them into a handler –The FreeForm handler includes a fairly large set of functions - open that software and look for the mechanism used to load them

Different ways to load CE Functions They can be added by the ConstraintEvaluator constructor in libdap or by a handler as is the case with FreeForm

Overwriting Functions The ConstraintEvaluator::add_function method maintains the function name/code binding so that newer entries with the same name take precedence. Thus a specific handler can override the definition of one of the base-set functions

A ‘Real’ Server Function Look at function_linear_scale in ce_functions.cc Synopsis: –The function does some fancy processing with arguments - if arguments are passed it will use those values, if not it will look at the attributes for the scale factor (m) and add offset (b) –The scaling operation is slightly different for Grids and Arrays –Because the variable name was passed in via a function call list, the expression parser did not set the send_p flag - the function sets it –Then the function reads the data values from the data set into the variable

Argv[0] is first argument to function and is the variable to scale. Here we use the BaseType method Is_vector_type() to determine that argv[0] is an Array and then use a C++ trick to cast that BaseType pointer down the class hierarchy to an Array. This makes it easier to call methods specific to the Array class As the comment says, if the Array is really a Grid map variable, be sure to use Grid::read() to load data values into variable. If not, just call Array::read()

Extract_double_array() is a helper method that reads numerical values from a DAP variable and loads them into a dynamically allocated double array. It’s defined in the ce_functions.cc source file The Array::length() methods returns the number of elements in an array Y=mx+b

More… Then the values are copied from the variable in the DDS to an Array of doubles –This array will hold the result of the scaling operation –The values are scaled –A new DAP array of Float64 is created and the scaled values are stored there –The newly created DAP Float64 array is returned

Create a new Float64 (the DAP type for 8-byte real values) which serves as a template for the type of the array’s values. Use the old variable’s name. Insert the new template type into the Array object Load the data values into the Array - this is the old-style value loading method. It’s better to use set_value()

Write a HelloWorld Function Write the function in the FreeForm CE functions file ff_ce_functions.cc Recompile and relink the FreeForm handler Install the modified handler Test

Open ff_ce_functions.cc

Add the function At this step it’s just a stub The code should still compile - check it by typing ‘make’

Now add the function’s guts Use C++’s string class to hold the value Create a new DAP ‘Str’ (String) variable Load the value Return the Str instance - Str is a child of BaseType

Compile to catch any errors…

Now arrange to register the functions Use ConstraintEvaluator::add_function() If you add this call to ff_register_functions() then you’re all set How else could you add the function?

Compile, Install and … Compile: ‘make’ Install: ‘make install’ –This will copy the freeform handler shared object library (aka ‘module’) to the directory where the BES expects to find it.

…Test Test –Start the BES and use the BES command line tool to test the new function –bescmdln -p h localhost –set container in catalog values f,/data/ff/avhrr.dat; –define d1 as f; –get ascii for d1; Lots of output… –define d2 as f with f.constraint=“HelloWorld()”; –get ascii for d2; Dataset: virtual Hw, “Hello World!”

Now Start Tomcat and Complete the Tests Start up Tomcat - now you can the with the web server Try it in a web browser – orld() –In a browse, append ‘.ascii’ by hand Try the same URL with getdap in a command line tool –getdap -D -c “HelloWorld()” –With getdap, use -D (data) and -c (constraint) –Make sure to quote the CE since parens are a shell meta- character Note that we must call this function with a FreeForm dataset because the function is only added to the ConstraintEvaluator in that handler. If we had added this function to the CE object in libdap, it could be used by any dataset

Advanced Topics Server functions can be ‘polymorphic’ Most responses use completely new variables Many operations transform the original variables’ type Coding tricks

Server Functions can be Polymorphic Well, sort of… In a server function, test the number and types of the arguments If a function can be called with different types of arguments, handler this at the start of the function

Most responses use completely new variables Use double arrays to hold interim results and Either return those doubles in new Array objects, or Copy the values into other types once processing is done Look at how geogrid() is written

Many operations transform the original variables’ type Note that the result of a server function is a value-result parameter Compute a result using regular C/C++ variables Then, at the end of the function, make a new libdap variable (an instance of Array, e.g.), use set_value() to load the result into the Array and return it. To return several Arrays, make a Structure and add the Array instances to it.

Coding Tricks Examples: libdap’s ce_functions.cc and The FreeForm handler’s ff_ce_functions.cc have lots of utility code Use “vector array(n)” instead of “T array = new T[n]” and BaseType:set_value() over the older BaseType:val2buf() – These minimize errors. When reading from HDF4, make sure to use Grid::read() to read the a Grid’s Maps, not the Maps’ read() method. … see function_geogrid() in libdap’s ce_functions.cc.

Summary A Server-side function is a function added to the constraint evaluator The function has access to all of the variables in the data set The return value of the function is –Encapsulated in a DAP variable –Or a Boolean value –Or void We wrote a simple example of the first type