From 89b507bd7c63b66e6899b7f31e82f40032bd412d Mon Sep 17 00:00:00 2001 From: perillaroc Date: Tue, 24 Sep 2019 10:11:54 +0800 Subject: [PATCH] :sparkles: add callAPI_to_gridArray2D. #1 --- example/grid_array_2d.py | 38 +++++++++++++++++++++ nuwe_cimiss/client.py | 21 ++++++++++++ nuwe_cimiss/data.py | 74 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 example/grid_array_2d.py diff --git a/example/grid_array_2d.py b/example/grid_array_2d.py new file mode 100644 index 0000000..cdb8b51 --- /dev/null +++ b/example/grid_array_2d.py @@ -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() diff --git a/nuwe_cimiss/client.py b/nuwe_cimiss/client.py index f95c2c5..1abea64 100644 --- a/nuwe_cimiss/client.py +++ b/nuwe_cimiss/client.py @@ -9,6 +9,7 @@ from nuwe_cimiss.data import ( Array2D, DataBlock, + GridArray2D, ) logger = logging.getLogger() @@ -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) diff --git a/nuwe_cimiss/data.py b/nuwe_cimiss/data.py index 6e03079..45ae066 100644 --- a/nuwe_cimiss/data.py +++ b/nuwe_cimiss/data.py @@ -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])