0
# Data Manipulation
1
2
Advanced operations for working with meteorological data grids, coordinate systems, and spatial data. Includes data access, nearest neighbor searches, grid transformations, and specialized GRIB/BUFR data operations.
3
4
## Capabilities
5
6
### Data Values Access
7
8
Core functions for reading and writing meteorological data values.
9
10
```python { .api }
11
def codes_get_values(msgid):
12
"""
13
Get all data values from message.
14
15
Parameters:
16
- msgid (int): Message handle ID
17
18
Returns:
19
list[float]: All data values as float array
20
"""
21
22
def codes_set_values(msgid, values):
23
"""
24
Set all data values in message.
25
26
Parameters:
27
- msgid (int): Message handle ID
28
- values (list[float]): Data values to set
29
"""
30
31
def codes_grib_get_data(gribid):
32
"""
33
Get data with coordinates.
34
35
Parameters:
36
- gribid (int): GRIB message handle ID
37
38
Returns:
39
tuple: (latitudes, longitudes, values) arrays
40
"""
41
```
42
43
### Nearest Neighbor Operations
44
45
Find nearest grid points and perform spatial queries.
46
47
```python { .api }
48
def codes_grib_find_nearest(gribid, lat, lon):
49
"""
50
Find nearest grid point to coordinates.
51
52
Parameters:
53
- gribid (int): GRIB message handle ID
54
- lat (float): Latitude in degrees
55
- lon (float): Longitude in degrees
56
57
Returns:
58
tuple: (nearest_lat, nearest_lon, value, distance, index)
59
"""
60
61
def codes_grib_find_nearest_multiple(gribid, lats, lons):
62
"""
63
Find nearest points for multiple coordinates.
64
65
Parameters:
66
- gribid (int): GRIB message handle ID
67
- lats (list[float]): Latitude coordinates
68
- lons (list[float]): Longitude coordinates
69
70
Returns:
71
list[tuple]: Results for each coordinate pair
72
"""
73
```
74
75
### Grid Information
76
77
Access grid geometry and coordinate system information.
78
79
```python { .api }
80
def codes_grib_get_grid_info(gribid):
81
"""
82
Get comprehensive grid information.
83
84
Parameters:
85
- gribid (int): GRIB message handle ID
86
87
Returns:
88
dict: Grid metadata and geometry
89
"""
90
```
91
92
## Usage Examples
93
94
### Extracting and Processing Grid Data
95
96
```python
97
import eccodes
98
import numpy as np
99
100
with open('temperature.grib', 'rb') as f:
101
msg = eccodes.codes_grib_new_from_file(f)
102
103
# Get all data values
104
values = eccodes.codes_get_values(msg)
105
print(f"Data range: {min(values):.2f} to {max(values):.2f}")
106
107
# Get coordinates and data together
108
lats, lons, data = eccodes.codes_grib_get_data(msg)
109
110
# Convert to numpy arrays for analysis
111
temp_data = np.array(data) - 273.15 # Convert K to °C
112
latitudes = np.array(lats)
113
longitudes = np.array(lons)
114
115
print(f"Temperature range: {temp_data.min():.1f}°C to {temp_data.max():.1f}°C")
116
117
eccodes.codes_release(msg)
118
```
119
120
### Nearest Neighbor Queries
121
122
```python
123
import eccodes
124
125
# Location coordinates (latitude, longitude)
126
cities = {
127
'London': (51.5074, -0.1278),
128
'Paris': (48.8566, 2.3522),
129
'Berlin': (52.5200, 13.4050)
130
}
131
132
with open('forecast.grib', 'rb') as f:
133
msg = eccodes.codes_grib_new_from_file(f)
134
135
for city, (lat, lon) in cities.items():
136
nearest = eccodes.codes_grib_find_nearest(msg, lat, lon)
137
grid_lat, grid_lon, value, distance, index = nearest
138
139
print(f"{city}: {value:.2f} (distance: {distance:.2f} km)")
140
print(f" Grid point: ({grid_lat:.3f}, {grid_lon:.3f})")
141
142
eccodes.codes_release(msg)
143
```