Accessing netCDF/OPeNDAP data with perl
- read
Code Block #!/usr/bin/env perl use strict; # ========================================================================== # Example script for reading NetCDF files through OPeNDAP with Perl # ========================================================================== # # This script shows some examples of how to read a NetCDF file in Perl. # It makes use of the Perl module PDL::NetCDF, a perl-netCDF interface # based on the use of "piddles" (PDL). # In order to be able to use this script, you need: # - a Perl installation # - the PDL module of Perl # (re. http://cpansearch.perl.org/src/CSOE/PDL-2.4.3/INSTALL) # - NetCDF installation with OPeNDAP support # (re. http://www.unidata.ucar.edu/software/netcdf/index.html) # - the installation of the PDL::NetCDF module # (re. http://search.cpan.org/~dhunt/PDL-NetCDF-4.03/) # # ========================================================================== # -------------------------------------------------------------------------- # Use PDL::NetCDF to read NetCDF files in Perl # -------------------------------------------------------------------------- use PDL; use PDL::NetCDF; # # -------------------------------------------------------------------------- # Open a remote NetCDF file ($nc will contain the NetCDF file object) # Remark 1: the file "201007050850.nc" may have been cleaned up. Consult the # URL below to get a valid file name # Remark 2: instead of a remote NetCDF file you may specify a local file # -------------------------------------------------------------------------- my $url = 'http://www.institution.nl/matroos/partners/opendap/cgi-bin/nph-dods/matroos/data/normal/hmcn_csm8'; my $ncfile = "$url/201007050850.nc"; my $nc = PDL::NetCDF->new($ncfile); # # -------------------------------------------------------------------------- # Get the list of variables in the NetCDF file (object $nc) # -------------------------------------------------------------------------- my $varlist = $nc->getvariablenames(); print "Variables on this file: \n @{$varlist}\n"; print "\n"; # -------------------------------------------------------------------------- # Read the time array # -------------------------------------------------------------------------- my $time = $nc->get('time'); print "Time array:\n $time\n"; print "\n"; # -------------------------------------------------------------------------- # Get the attributes of the time array # -------------------------------------------------------------------------- my $attlist = $nc->getattributenames('time'); print "Time array attributes:\n"; foreach my $attname ( @{$attlist} ) { my $attr = $nc->getatt($attname,'time'); print " $attname: $attr\n"; } print "\n"; # -------------------------------------------------------------------------- # Get dimensions of the water level array SEP # -------------------------------------------------------------------------- my $dims = $nc->getdimensionnames('SEP'); print "Dimension names of SEP:\n @{$dims}\n"; print "\n"; # -------------------------------------------------------------------------- # Get the attributes of the SEP array # -------------------------------------------------------------------------- my $attlist = $nc->getattributenames('SEP'); print "SEP array attributes:\n"; foreach my $attname ( @{$attlist} ) { my $attr = $nc->getatt($attname,'SEP'); print " $attname: $attr\n"; } print "\n"; # -------------------------------------------------------------------------- # Read a slice of the water level array SEP # -------------------------------------------------------------------------- # [0,0,0] is starting point, [1,1,5] is the count my $slice = $nc->get('SEP',[0,0,0],[1,1,5]); print "First slice:\n $slice\n"; print "\n"; # Read another slice, 2nd timestep = starting point [1,0,0] my $slice = $nc->get('SEP',[1,0,0],[1,1,5]); print "Second slice:\n $slice\n"; print "\n"; # -------------------------------------------------------------------------- # Close the NetCDF file # -------------------------------------------------------------------------- $nc->close;
- write
Code Block #!/usr/bin/env perl use strict; # ========================================================================== # Example script for creating a NetCDF file # ========================================================================== # # This script shows an example of how to write a NetCDF file in Perl. # It makes use of the Perl module PDL::NetCDF, a perl-netCDF interface # based on the use of "piddles" (PDL). # In order to be able to use this script, you need: # - a Perl installation # - the PDL module of Perl # (re. http://cpansearch.perl.org/src/CSOE/PDL-2.4.3/INSTALL) # - NetCDF installation (with OPeNDAP support) # (re. http://www.unidata.ucar.edu/software/netcdf/index.html) # - the installation of the PDL::NetCDF module # (re. http://search.cpan.org/~dhunt/PDL-NetCDF-4.03/) # # ========================================================================== # -------------------------------------------------------------------------- # Use PDL::NetCDF to write NetCDF files in Perl # -------------------------------------------------------------------------- use PDL; use PDL::Char; use PDL::NetCDF; # # Time::localtime for nice time functions use Time::localtime; # # -------------------------------------------------------------------------- # Open a new NetCDF file ($nc will contain the NetCDF file object) # -------------------------------------------------------------------------- my $ncfile = "MyNetCDF.nc"; unlink $ncfile if ( -f $ncfile); my $nc = PDL::NetCDF->new($ncfile); # -------------------------------------------------------------------------- # Add some global attributes to this NetCDF # -------------------------------------------------------------------------- my $tm = localtime; my $today = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday); $nc->putatt ('Global attribute', 'global_attr'); $nc->putatt ($today , 'creation_date'); # # -------------------------------------------------------------------------- # Create a "piddle" to write # -------------------------------------------------------------------------- my $pdl = pdl [[1, 2, 3], [4, 5, 6]]; # -------------------------------------------------------------------------- # Write this piddle to our new NetCDF # -------------------------------------------------------------------------- # Specify variable name to put PDL in, plus names of the dimensions. # Dimension lengths are taken from the PDL, in this case, dim1 = 2 and # dim2 = 3. $nc->put ('SomeVariable', ['dim1', 'dim2'], $pdl); # -------------------------------------------------------------------------- # Add some attributes to this array # -------------------------------------------------------------------------- $nc->putatt ('My first attribute', 'attr_name_1', 'SomeVariable'); $nc->putatt ('Another attribute' , 'attr_name_2', 'SomeVariable'); $nc->putatt (double([1,2,3]) , 'double_attr', 'SomeVariable'); # -------------------------------------------------------------------------- # Create and add a character array to the NetCDF file # -------------------------------------------------------------------------- my $str = PDL::Char->new (['Station1', 'Station2', 'Station3']); $nc->put ('stations', ['dim_station', 'dim_charlen'], $str); # -------------------------------------------------------------------------- # And add some attributes to this string array # -------------------------------------------------------------------------- $nc->putatt ('String attribute', 'string_attr_name', 'stations'); # -------------------------------------------------------------------------- # Close the NetCDF file # -------------------------------------------------------------------------- $nc->close; # -------------------------------------------------------------------------- # Now you can inspect what has been created using "ncdump" # -------------------------------------------------------------------------- print "\n" . "=================\n" . "Output of ncdump:\n" . "=================\n" . "\n"; system ("ncdump $ncfile");
Code provided by Regien Brouwer of the MATROOS operational storm surge database.