#! /usr/bin/env perl # # This is an example program which writes some 4D pressure and # temperatures. The companion program pres_temp_4D_rd.pl shows how # to read the netCDF data file created by this program. # # Heiko Klein 2007-02-06 # use strict; use warnings; use PDL::Lite; use PDL::NetCDF; use Fcntl; my $NLAT = 6; my $NLON = 12; my $SAMPLE_PRESSURE = 900.0; my $SAMPLE_TEMP = 9.0; my $START_LAT = 25.0; my $START_LON = -125.0; my $NREC = 2; my $NLEVEL = 2; my $ncfile = new PDL::NetCDF("pres_temp_4D.nc", {REVERSE_DIMS => 1, MODE => O_CREAT}); my $latitude = (PDL::Basic::sequence($NLAT) * 5 + $START_LAT)->float; my $longitude = (PDL::Basic::sequence($NLON) * 5 + $START_LON)->float; $ncfile->put('latitude', ['latitude'], $latitude); $ncfile->put('longitude', ['longitude'], $longitude); $ncfile->putatt('degrees_north', 'units', 'latitude'); $ncfile->putatt('degrees_east', 'units', 'longitude'); # defining the dimensions of the data, which will be set in slices # this is done by writing a dummy in the correct type (float) $ncfile->putslice('temperature', ['longitude', 'latitude', 'level', 'time'], [$NLON, $NLAT, $NLEVEL, PDL::NetCDF::NC_UNLIMITED()], [0,0,0,0], [0,0,0,0], PDL::Core::float([])); $ncfile->putslice('pressure', ['longitude', 'latitude', 'level', 'time'], [$NLON, $NLAT, $NLEVEL, PDL::NetCDF::NC_UNLIMITED()], [0,0,0,0], [0,0,0,0], PDL::Core::float([])); $ncfile->putatt('hPa', 'units', 'pressure'); $ncfile->putatt('celcius', 'units', 'temperature'); # creating/adding the data in time-slices for (my $t = 0; $t < $NREC; $t++) { my $pressure = PDL::Core::zeroes($NLON, $NLAT, $NLEVEL)->float; my $temp = PDL::Core::zeroes($NLON, $NLAT, $NLEVEL)->float; for (my $l = 0; $l < $NLEVEL; $l++) { for (my $i = 0; $i < $NLAT; $i++) { for (my $j = 0; $j < $NLON; $j++) { $pressure->mslice($j, $i, $l) .= $SAMPLE_PRESSURE + $i*$NLON + $j + $l*$NLON*$NLAT; # no change in t here $temp->mslice($j, $i, $l) .= $SAMPLE_TEMP + $i*$NLON + $j + $l*$NLAT*$NLON; # no change in t here } } } # writing one data-slice of time, dimension and dimension-names no longer # needed since defined before $ncfile->putslice('temperature', [],[], [0,0,0,$t], [$NLON, $NLAT, $NLEVEL, 1], $temp); $ncfile->putslice('pressure', [],[], [0,0,0,$t], [$NLON, $NLAT, $NLEVEL, 1], $pressure); } $ncfile->close;