current position:Home>Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall

Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall

2022-06-24 12:38:48It's hard not to write code all day

0 Purpose

There are few online learning materials that depend on this package , So tidy this bag , Some things we can use in our work api To study

https://www.unidata.ucar.edu/

https://www.unidata.ucar.edu/software/netcdf/examples/programs/


https://docs.unidata.ucar.edu/netcdf-java/5.1/userguide/common_data_model_overview.html#data-access-layer-object-model



https://docs.unidata.ucar.edu/netcdf-java/5.1/userguide/using_netcdf_java_artifacts.html
python 

https://unidata.github.io/MetPy/latest/examples

1 Why learn this dependency

Now there are many formats of data , For example, meteorological data , Generally used nc Format file for storage , We got this file , In our project code , To parse , This nc The format file is binary stream , So you need to use professional tools to parse , This dependency is to help us resolve in our project nc Format

2 Common data model

Common Data Model It has three layers , They build on each other to add richer semantics in turn :

1   Data access layer , Also known as the syntactic layer , Handle data reading and writing .

2   The coordinate system layer identifies the coordinates of the data array . Coordinates are a complete representation of scientific data 
 General concept ; We have also established a special geographic reference coordinate system ,
 This is very important to the earth science community .

3  Scientific Feature Types Layers identify specific types of data ,
 For example, grid 、 Radial and point data , Add special methods for each type of data .

3 Data access layer object model

 Insert picture description here

3.1 Dataset

Dataset = Data sets

Data sets can be netCDF、HDF5、GRIB Wait for the documents 、OPeNDAP Data sets 、 A collection of files can be accessed through netCDF API Any other content accessed . We sometimes use the term CDM Data sets to represent any of these possibilities , And emphasize that the dataset does not have to be netCDF File format .

 The type of dataset we often use is :
nc  Format   GRB  Format ,DAT Format 

3.2 Group

Group = Group
Group is an attribute 、 dimension 、EnumTypedef、 Containers for variables and nested groups .Dataset Medium Groups Form a hierarchical tree , Just like a directory on a disk .Dataset There is always at least one Group, That is root Group, Its name is an empty string .

3.3 Variable

Variable = Variable
Variables are containers of data . It has a data type 、 A set of dimensions that define the shape of its array , And an optional set of attributes . Any shared dimensions it uses must be in the same group or parent group .

 Insert picture description here

3.4 Dimension

Dimensions are used to define the array shape of variables . It can be shared among variables , This provides a simple and powerful way to correlate variables . When sharing dimensions , It has a unique name within the Group . If there is no limit , The length of the dimension may increase . If it is variableLength, The actual length depends on the data , Only by reading the data can we find .variableLength Dimension Cannot be shared or unrestricted .

3.5 Attribute

Properties have names and values , And associate any metadata with variables or groups . The value is a scalar or one-dimensional array of strings or numbers , So the possible data types are (String、byte、short、int、long、float、double). Integer types (byte、short、int、long) It can be signed or unsigned .

3.6 structure

A structure is a variable that contains other variables , Be similar to C Structure in or in a relational database That's ok . Usually , The data in the structure is physically tightly stored on disk , Therefore, all the data in the structure can be retrieved efficiently at the same time . The variables contained in the structure are member variables , Can only be read in the context of its containing structure .

3.7 Sequence

A sequence is a one-dimensional structure , Its length is unknown before you actually read the data . To access data in a sequence , You can only traverse the sequence , Get data from one structure instance at a time .

3.8 EnumTypedef

EnumTypedef Is an enumeration of strings , from enum Type variables use .

3.9 Array

The array contains the actual data after reading variables from disk or network . You can call read() Or its variant to get an array from a variable . The array is rectangular ( Such as Fortran Array ). Every DataType There's a special Array type .

4 Coordinate system object model

 Insert picture description here

4.1 Variable

** A variable can have zero or more coordinate systems , It contains one or more axes .** If CoordinateAxis The dimension set of is a subset of the dimension set of variables , be CoordinateAxis Can only be variable CoordinateSystem Part of . This ensures that each data point in the variable is for CoordinateSystem Each of the CoordinateAxis Have a corresponding coordinate value .

5 api Study

5.1 open nc file open()


        String filename = "E:\\ Grid point \\nc\\66.nc";
        NetcdfFile ncfile = null;
        try {
    
            ncfile = NetcdfFile.open(filename);
            //  Console output 
            System.out.println(ncfile);

        } catch (IOException ioe) {
    
            System.out.println("trying to open " + filename+ioe);
        } finally {
    
            if (null != ncfile) try {
    
                ncfile.close();
            } catch (IOException ioe) {
    
                System.out.println("trying to open " + filename+ioe);
            }
        }

5.2 findVariable()

According to the specific variable name , Get the specific attributes in the current variable

private static void process(NetcdfFile ncfile){
    
        String varName = "hyai";
        Variable v = ncfile.findVariable(varName);
        if (null == v) return;
        try {
    
            System.out.println(v);
        } finally {
    

        }
    }

 Insert picture description here

5.3

6 Import dependence

This dependency is added to our project , It is equivalent to adding this parsing nc Documentation software

        <dependency>
            <groupId>edu.ucar</groupId>
            <artifactId>netcdfAll</artifactId>
            <version>5.1.0</version>
        </dependency>

7 open nc file

We have a nc file
 Insert picture description here

It means according to the code , Read the contents of this binary file , Let's take a look at what the code reads . Output the read thing to the console , And read TXT file ,


        String filename = "E:\\ Grid point \\nc\\66.nc";
        NetcdfFile ncfile = null;
        try {
    
            ncfile = NetcdfFile.open(filename);
            //  Console output 
            System.out.println(ncfile);

        } catch (IOException ioe) {
    
            System.out.println("trying to open " + filename+ioe);
        } finally {
    
            if (null != ncfile) try {
    
                ncfile.close();
            } catch (IOException ioe) {
    
                System.out.println("trying to open " + filename+ioe);
            }
        }

The output is

 Insert picture description here
You can find , The output is to use panoply Software open nc file ;
 Insert picture description here

copyright notice
author[It's hard not to write code all day],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/175/202206241030564288.html

Random recommended