[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

netcdf-perl examples



#!/usr/local/bin/perl

# Example program to encode variable length strings into a NetCDF file.
# S. Diggs (address@hidden) 1999.11.15

use diagnostics;
#use strict;
use NetCDF;

unlink( "string_example.nc" );

# strings
my @atmospheric_conditions = (  "cloudy", "sunny",
                                "fog",
                                "rain" );

my @count_ac = ($#atmospheric_conditions);

print STDOUT "dimensions of array are @count_ac\n";

#
my $ncid  = NetCDF::create("string_example.nc", NetCDF::WRITE);
#my $dimid = NetCDF::dimdef($ncid, 'recNum', NetCDF::UNLIMITED);
$string_dimension = 80;
my $sD = NetCDF::dimdef($ncid, 'string_dimension', $string_dimension);
#
NetCDF::attput($ncid, NetCDF::GLOBAL, "STRING_EXAMPLE", NetCDF::CHAR,
                        "This is an example of string encoding");

my $varid_ac   = NetCDF::vardef($ncid, 'ATM_COND', NetCDF::CHAR, $sD);
;

# leaving define mode
NetCDF::endef($ncid);

#NetCDF::close($ncid);

#exit(0);

print STDERR "Leaving define mode....\n";

#put data into netcdf file

my $i=0;
my @start = (0);
my @new_count = (80);   
$S80 = "\0" x $string_dimension ;

foreach my $element (@atmospheric_conditions)   {

        #@new_count = (length($element));
        #print STDERR " Variable $i length = @new_count\n\t",
        #               "starting at @start\n\n";
        $element = padstr( $element, $string_dimension);
        NetCDF::varput($ncid, $varid_ac, \@start, \@new_count, 
                \$element);
        $i++;
        #bump the new starting point by adding the old ending point
        # to where we are now
        #@start = (($new_count[0] + $start[0]));
        last;
}

NetCDF::close($ncid);
#done!

# code to demonstrate UNLIMITED records

$cdlfile = "sd.cdl" ;
$ncfile = "sd.nc" ;
$ncgen = "/upc/netcdf/bin/ncgen" ;
system( "$ncgen -o $ncfile $cdlfile" ) ;
$ncid = NetCDF::open( "$ncfile", WRITE ) ; 
$S80 = "\0" x $string_dimension ;
$ii = 0;

foreach my $element (@atmospheric_conditions)   {

        $element = padstr( $element, $string_dimension);
        @dataref = ( \$element );
        #$results = NetCDF::recput($ncid, $ii, [@dataref]);
        $results = NetCDF::recput($ncid, $ii, [(\$element)]);
        print "$results\n" if( $results );
        $ii++;
}

@dataref = ( \$S80 );

for( $record = 0; $record <= 3; $record++ ) {
        $results = NetCDF::recget( $ncid, $record,  \@dataref );
        print "$results\n" if( $results );
        $ac = ${$dataref[ 0 ]} ;
        print "record = $record , $ac\n" ;
}
NetCDF::close($ncid);
        
# pad str to correct length
sub padstr
{
( $str, $len ) = @_ ;

my( $size, $i ) ;

$size = length( $str ) ;

for( $i = $size; $i < $len; $i++ ) {
        $str .= "\0" ;
        #print "$str,\n" ;
}
if( $size > $len ) {
        print STDOUT "String length is over $len chars long:\n $str\n" ;
        $str = substr( $str, 0, $len ) ;
        #exit 0 ;
}
return $str ;
}


netcdf string_example {
dimensions:
        recNum = UNLIMITED ; // (0 currently)
        string_dimension = 80 ;
variables:
        char ATM_COND(recNum, string_dimension) ;

// global attributes:
                :STRING_EXAMPLE = "This is an example of string encoding" ;
}
===============================================================================
Robb Kambic                                Unidata Program Center
Software Engineer III                      Univ. Corp for Atmospheric Research
address@hidden             WWW: http://www.unidata.ucar.edu/
===============================================================================


pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy