Skip to content

Commit

Permalink
✨ add callAPI_to_gridArray2D. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
perillaroc committed Sep 24, 2019
1 parent 2b463e8 commit 89b507b
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
38 changes: 38 additions & 0 deletions example/grid_array_2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# coding=UTF-8
from nuwe_cimiss.client import CimissClient
import click
import pathlib


@click.command()
@click.option("--user", help="user name", required=True)
@click.option("--password", help="password name", required=True)
@click.option("--client-config", help="client config file")
def cli(user, password, client_config=None):

interface_id = "getNafpEleGridByTimeAndLevelAndValidtime"

server_id = "NAFP_FOR_FTM_KWBC_GLB"

params = {
"dataCode": "NAFP_FOR_FTM_KWBC_GLB",
"time": "20190921000000",
"fcstEle": "TEM",
"levelType": "1",
"fcstLevel": "0",
"validTime": "0",
}

client = CimissClient(
user=user,
password=password,
config_file=client_config)
result = client.callAPI_to_gridArray2D(
interface_id, params
)

print(result)


if __name__ == "__main__":
cli()
21 changes: 21 additions & 0 deletions nuwe_cimiss/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from nuwe_cimiss.data import (
Array2D,
DataBlock,
GridArray2D,
)

logger = logging.getLogger()
Expand Down Expand Up @@ -103,6 +104,26 @@ def callAPI_to_dataBlock(
exception_handler=Connection.generate_exception_handler(data_block),
)

def callAPI_to_gridArray2D(
self,
interface_id: str,
params: dict,
server_id: str = None
) -> GridArray2D:
data = GridArray2D()

method = self.callAPI_to_gridArray2D.__name__

return self._do_request(
interface_id,
method,
params,
server_id,
success_handler=Connection.generate_pack_success_handler(data),
failure_handler=Connection.generate_pack_failure_handler(data),
exception_handler=Connection.generate_exception_handler(data),
)

def _load_config(self) -> None:
if self.config_file is not None:
self.config_file = pathlib.Path(self.config_file)
Expand Down
74 changes: 74 additions & 0 deletions nuwe_cimiss/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,77 @@ def load_from_protobuf_object(self, ret_data_block: pb.RetDataBlock):
self.request = RequestInfo.create_from_protobuf(ret_data_block.request)
self.data_name = ret_data_block.dataName
self.data = ret_data_block.byteArray


class GridArray2D(ResponseData):
protobuf_object_type = pb.RetGridArray2D

def __init__(
self,
data: List[float] = None,
request: RequestInfo = None,
start_lat: float = 0,
start_lon: float = 0,
end_lat: float = 0,
end_lon: float = 0,
lat_count: int = 0,
lon_count: int = 0,
lon_step: float = 0,
lat_step: float = 0,
lats: List[float] = None,
lons: List[float] = None,
units: str = "",
user_element_name: str = "",
):
super().__init__(request=request)
self.data = data
self.start_lat = start_lat
self.start_lon = start_lon
self.end_lat = end_lat
self.end_lon = end_lon
self.lat_count = lat_count
self.lon_count = lon_count
self.lon_step = lon_step
self.lat_step = lat_step
self.lats = lats
self.lons = lons
self.units = units
self.user_element_name = user_element_name

def load_from_protobuf_content(self, content: bytes):
protobuf_object = self.protobuf_object_type()
protobuf_object.ParseFromString(content)
self.load_from_protobuf_object(protobuf_object)

def load_from_protobuf_object(self, ret_grid_array_2d: pb.RetGridArray2D):
self.request = RequestInfo.create_from_protobuf(ret_grid_array_2d.request)

if self.request.error_code != 0:
return

self.start_lat = ret_grid_array_2d.startLat
self.start_lon = ret_grid_array_2d.startLon
self.end_lat = ret_grid_array_2d.endLat
self.end_lon = ret_grid_array_2d.endLon
self.lat_count = ret_grid_array_2d.latCount
self.lon_count = ret_grid_array_2d.lonCount
self.lon_step = ret_grid_array_2d.lonStep
self.lat_step = ret_grid_array_2d.latStep

if ret_grid_array_2d.lats is not None:
self.lats = ret_grid_array_2d.lats
else:
self.lats = [self.start_lat + i * self.lat_step for i in range(self.lat_count)]

if ret_grid_array_2d.lons is not None:
self.lons = ret_grid_array_2d.lons
else:
self.lons = [self.start_lon + i * self.lon_step for i in range(self.lon_count)]

self.units = ret_grid_array_2d.units
self.user_element_name = ret_grid_array_2d.userEleName

row_count = self.request.row_count
data_count = len(ret_grid_array_2d.data)
col_count = int(data_count/row_count)
self.data = np.array(ret_grid_array_2d.data).reshape([row_count, col_count])

0 comments on commit 89b507b

Please sign in to comment.