NCL supports import and export for several data types including netCDF and HDF. Here is a list of supported operations:
-1. 0. 1. 2.then executing the NCL commands:
input_data = asciiread("float_data",4,"float") print(input_data)
yields:
Variable: input_data Type: float Total Size: 16 bytes 4 values Number of Dimensions: 1 Dimensions and sizes: [4] Coordinates: Number Of Attributes: 1 _FillValue (0) -1 (1) 0 (2) 1 (3) 2For numeric data, the format of the data file is free-form and all non-numeric characters are ignored. So, for example, if the file "int_data" looked like:
1 <--First data item, 2 <--Second data item 3, 4, 5XXX6YYYthen executing the NCL commands:
input_data = asciiread("int_data",(/2,3/),"integer") print(input_data)
yields:
Variable: input_data Type: integer Total Size: 24 bytes 6 values Number of Dimensions: 2 Dimensions and sizes: [2] x [3] Coordinates: Number Of Attributes: 1 _FillValue (0,0) 1 (0,1) 2 (0,2) 3 (1,0) 4 (1,1) 5 (1,2) 6If there are more data values in the file than are requested, then only the requested number of values are used. If there are fewer data values in the file than are requested, then the missing values are assigned the default missing value for the given data type. If the data type in an asciiread is "string", then each line of the input file is returned as a single data value.
a = (/1,2,3/) asciiwrite("int_file",a)would create a file named "int_file" that looks like:
1 2 3
fval = cbinread("c_bin_data",3,"float")would store the three values in "c_bin_data" in the NCL array fval. If there are more values in the data file than specified in the second argument to cbinread, then the requested number of data values will be returned; if there are not as many values in the data file as requested, then the missing values are assigned the default missing value for the given data type.
Note: The data file read in by cbinread must have been created on a machine of the same architecture as the one that NCL is being run on. For example, if you created a binary file on a machine with a 64-bit word size and tried to read it back on a 32-bit machine, you will not get what you want.
Binary files created using Fortran unformatted writes can be imported directly into NCL by using the NCL fbinread function. For example, if "f_bin_data" is a binary file as described that contains three floating point numbers, then the NCL command
fval = fbinread("f_bin_data",3,"float")would store the three values in "f_bin_data" in the NCL array fval. If there are more values in the data file than specified in the second argument to fbinread, then the requested number of data values will be returned; if there are not as many values in the data file as requested, then an error will result.
Note: The data file read in by fbinread must have been created on a machine of the same architecture as the one that NCL is being run on.
fval = (/ 1., 2., 3. /) cbinwrite("c_bin_data",fval)would write the binary file "c_bin_data". NCL automatically figures out how many values to write and the data type being written.
Writing binary files using a Fortran unformatted write with fbinwrite is entirely analogous to using cbinwrite.
As an example, suppose the file "simple.cdl" (cdl files are ASCII versions of netCDF files that can be converted to netCDF files by using the netCDF library function ncgen) contains the following simple file:
netcdf simple { // A very simple file for demo purposes. dimensions: xdim = 5; variables: float xarray(xdim); xarray:long_name = "A small array"; xarray:units = "dimensionless"; xarray:valid_range = 0.f, 7.f ; xarray:_FillValue = -9999.0f; float xdim(xdim); xdim:long_name = "X array dimension"; xdim:units = "dimensionless"; :history = "created by SCD for demo purposes"; :title = "Simple demo"; data: xdim = -20, -10, 0, 10, 20; xarray = -1., 0., 1., 2., 3.; }Let simple.cdf be the netCDF file created from the simple.cdl file by using ncgen. The following NCL interactive session illustrates importing simple.cdf into NCL and using some of the metadata operators:
ncl 0> file1 = addfile("simple.cdf","r") ncl 1> print (file1) filename: simple path: simple.cdf dimensions: xdim = 5 variables: float xarray(xdim) float xdim(xdim) ncl 2> print(file1->xarray) Variable: xarray (file variable) Type: float Total Size: 20 bytes 5 values Number of Dimensions: 1 Dimensions and sizes: [xdim | 5] Coordinates: xdim: [xx..xx] Number Of Attributes: 4 long_name units valid_range _FillValue (0) -1 (1) 0 (2) 1 (3) 2 (4) 3 ncl 3> print(file1!0) (0) xdim ncl 4> print(file1@history) (0) created by SCD for demo purposes ncl 5> print(file1->xarray({-10:10})) Variable: xarray (file variable subsection) Type: float Total Size: 12 bytes 3 values Number of Dimensions: 1 Dimensions and sizes: [xdim | 3] Coordinates: xdim: [xx..xx] Number Of Attributes: 4 long_name units valid_range _FillValue (0) 0 (1) 1 (2) 2
ncl 0> file1 = addfile("file1.cdf","c") ncl 1> file1->xarray = (/2.,3.,4.,5.,6./) ncl 2> file1!0 = "xdim" ncl 3> file1&xdim = (/-20,-10,0,10,20/) ncl 4> file1->xarray@units = "millimeters" ncl 5> file1@title = "NCL-created netCDF" ncl 6> quitThis creates a netCDF file "file1.cdf". A dump of this netCDF file obtained by using the "ngdump" utility in the netCDF library yields:
netcdf file1 { dimensions: xdim = 5 ; variables: float xarray(xdim) ; xarray:units = "millimeters" ; long xdim(xdim) ; // global attributes: :title = "NCL-created netCDF" ; data: xarray = 2 , 3 , 4 , 5 , 6 ; xdim = -20, -10, 0, 10, 20 ; }Existing netCDF files can be modified by importing them using "addfile" with a final argument of "w" and making the desired changes. The imported netCDF file will be overwritten when NCL is terminated.
Also there is currently no way to access 8 bit and 24 bit HDF images from NCL.
NG4.1 Home, Index, Examples, Glossary, Feedback, UG Contents, UG WhereAmI?