File: fields.cc

package info (click to toggle)
metview 5.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 614,356 kB
  • sloc: cpp: 560,586; ansic: 44,641; xml: 19,933; f90: 17,984; sh: 7,454; python: 5,565; yacc: 2,318; lex: 1,372; perl: 701; makefile: 88
file content (68 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "atlas/array.h"
#include "atlas/field/Field.h"
#include "atlas/field/FieldSet.h"
#include "atlas/library.h"
#include "atlas/runtime/Log.h"
#include "atlas/util/Metadata.h"

using atlas::Field;
using atlas::FieldSet;
using atlas::Log;
using atlas::array::make_datatype;
using atlas::array::make_shape;
using atlas::array::make_view;

int main( int argc, char* argv[] ) {
    atlas::initialise( argc, argv );

    // Define fields
    Field field_pressure( "pressure", make_datatype<double>(), make_shape( 100 ) );
    Field field_wind( "wind", make_datatype<double>(), make_shape( 100, 2 ) );

    // Access field data
    auto pressure = make_view<double, 1>( field_pressure );
    auto wind     = make_view<double, 2>( field_wind );

    // Assign values to fields
    for ( size_t jnode = 0; jnode < 100; ++jnode ) {
        pressure( jnode )          = 101325.0;
        wind( jnode, size_t( 0 ) ) = 0.01 + double( jnode );
        wind( jnode, size_t( 1 ) ) = 0.02 + double( jnode );
    }

    // Add info to fields
    std::string unitsP, unitsW;
    field_pressure.metadata().set( "units", "[Pa]" );
    field_pressure.metadata().get( "units", unitsP );
    field_wind.metadata().set( "units", "[m/s]" );
    field_wind.metadata().get( "units", unitsW );

    // Define fieldSet
    FieldSet fields;
    fields.add( field_pressure );  // Add field_pressure to fieldSet
    fields.add( field_wind );      // Add field_wind to fieldSet

    // Retrieve field from fieldSet
    Field field_pressure2 = fields.field( "pressure" );
    Field field_wind2     = fields.field( "wind" );

    // Print some useful info
    Log::info() << "name   = " << field_wind.name() << std::endl;
    Log::info() << "size   = " << field_wind.size() << std::endl;
    Log::info() << "units  = " << unitsW << std::endl;
    Log::info() << "rank   = " << field_wind.rank() << std::endl;
    Log::info() << "shape  = " << field_wind.shape( 0 ) << "    " << field_wind.shape( 1 ) << std::endl;
    Log::info() << "memory = " << field_wind.bytes() << " bytes" << std::endl;
    Log::info() << "type   = " << field_wind.datatype().str() << std::endl;
    Log::info() << "kind   = " << field_wind.datatype().kind() << std::endl;

    // Print some values
    Log::info() << "pressure(9) = " << pressure( 9 ) << std::endl;
    Log::info() << "wind(9, 0)  = " << wind( 9, 0 ) << std::endl;
    Log::info() << "wind(9, 1)  = " << wind( 9, 1 ) << std::endl;

    atlas::finalize();
    atlas::mpi::finalize();

    return 0;
}