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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
|
# (C) Copyright 2012-2018 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.
#
import ctypes
import ctypes.util
import json
import sys
import numpy as np
from numpy.ctypeslib import ndpointer
try:
import ecmwflibs as findlibs
except ImportError:
import findlibs
lib = findlibs.find("MagPlus")
if lib is None:
raise RuntimeError("Cannot find MagPlus library")
dll = ctypes.CDLL(lib)
class FILE(ctypes.Structure):
pass
FILE_p = ctypes.POINTER(FILE)
####################################################################
def get_library_path():
return lib
####################################################################
def _string_to_char(x):
return x.encode()
def _char_to_string(x):
return x.decode()
def _convert_strings(fn):
convert = False
for a in fn.argtypes:
if a is c_char_p:
convert = True
if fn.restype is c_char_p:
convert = True
if not convert:
return fn
def wrapped(*args):
new_args = []
for a, t in zip(args, fn.argtypes):
if t is c_char_p:
a = string_to_char(a)
new_args.append(a)
r = fn(*new_args)
if fn.restype is c_char_p:
r = char_to_string(r)
return r
return wrapped
def _noop(x):
return x
if sys.version_info[0] > 2:
convert_strings = _convert_strings
char_to_string = _char_to_string
string_to_char = _string_to_char
else:
convert_strings = _noop
char_to_string = _noop
string_to_char = _noop
####################################################################
c_int = ctypes.c_int
c_int_p = ctypes.POINTER(c_int)
c_double = ctypes.c_double
c_double_p = ctypes.POINTER(c_double)
c_char = ctypes.c_char
c_char_p = ctypes.c_char_p
c_void_p = ctypes.c_void_p
####################################################################
def checked_error_in_last_paramater(fn):
def wrapped(*args):
err = c_int(0)
err_p = ctypes.cast(ctypes.addressof(err), c_int_p)
params = [a for a in args]
params.append(err_p)
result = fn(*params)
if err.value:
raise MagicsError(err)
return result
return wrapped
def checked_return_code(fn):
def wrapped(*args):
err = fn(*args)
if err:
raise MagicsError(char_to_string(err))
return wrapped
####################################################################
def return_type(fn, ctype):
def wrapped(*args):
result = ctype()
result_p = ctypes.cast(ctypes.addressof(result), ctypes.POINTER(ctype))
params = [a for a in args]
params.append(result_p)
fn(*params)
return result.value
return wrapped
####################################################################
py_open = dll.py_open
py_open.restype = c_char_p
@checked_return_code
def init():
return py_open()
####################################################################
py_close = dll.py_close
py_close.restype = c_char_p
@checked_return_code
def finalize():
return py_close()
####################################################################
py_coast = dll.py_coast
py_coast.restype = c_char_p
@checked_return_code
def coast():
return py_coast()
####################################################################
py_grib = dll.py_grib
py_grib.restype = c_char_p
@checked_return_code
def grib():
return py_grib()
def oldversion():
msg = "You are using an old version of magics ( < 4.0.0)"
return msg.encode()
try:
version = dll.version
version.restype = ctypes.c_char_p
version.argtypes = None
except Exception:
version = oldversion
try:
tile = dll.py_tile
except Exception:
print("Tile not enabled: You are using an old version of magics ( < 4.1.0)")
tile = oldversion
home = dll.home
home.restype = ctypes.c_char_p
home.argtypes = None
metanetcdf = dll.py_metanetcdf
metanetcdf.restype = ctypes.c_char_p
metanetcdf.argtypes = None
metagrib = dll.py_metagrib
metagrib.restype = ctypes.c_char_p
metagrib.argtypes = None
metainput = dll.py_metainput
metainput.restype = ctypes.c_char_p
metainput.argtypes = None
try:
py_detect = dll.py_detect
py_detect.restype = ctypes.c_char_p
py_detect.argtypes = (ctypes.c_char_p, ctypes.c_char_p)
py_detect = convert_strings(py_detect)
detect = py_detect
except Exception:
detect = dll.detect
detect.restype = ctypes.c_char_p
detect.argtypes = (ctypes.c_char_p, ctypes.c_char_p)
detect = convert_strings(detect)
####################################################################
py_cont = dll.py_cont
py_cont.restype = c_char_p
@checked_return_code
def cont():
return py_cont()
####################################################################
py_legend = dll.py_legend
py_legend.restype = c_char_p
@checked_return_code
def legend():
return py_legend()
####################################################################
py_odb = dll.py_odb
py_odb.restype = c_char_p
@checked_return_code
def odb():
return py_odb()
####################################################################
py_obs = dll.py_obs
py_obs.restype = c_char_p
@checked_return_code
def obs():
return py_obs()
####################################################################
py_raw = dll.py_raw
py_raw.restype = c_char_p
@checked_return_code
def raw():
return py_raw()
####################################################################
py_netcdf = dll.py_netcdf
py_netcdf.restype = c_char_p
@checked_return_code
def netcdf():
return py_netcdf()
####################################################################
py_image = dll.py_image
py_image.restype = c_char_p
@checked_return_code
def image():
return py_image()
####################################################################
py_plot = dll.py_plot
py_plot.restype = c_char_p
@checked_return_code
def plot():
return py_plot()
####################################################################
py_text = dll.py_text
py_text.restype = c_char_p
@checked_return_code
def text():
return py_text()
####################################################################
py_wind = dll.py_wind
py_wind.restype = c_char_p
@checked_return_code
def wind():
return py_wind()
####################################################################
py_line = dll.py_line
py_line.restype = c_char_p
@checked_return_code
def line():
return py_line()
####################################################################
py_symb = dll.py_symb
py_symb.restype = c_char_p
@checked_return_code
def symb():
return py_symb()
####################################################################
py_boxplot = dll.py_boxplot
py_boxplot.restype = c_char_p
@checked_return_code
def boxplot():
return py_boxplot()
####################################################################
py_taylor = dll.py_taylor
py_taylor.restype = c_char_p
@checked_return_code
def taylor():
return py_taylor()
####################################################################
py_tephi = dll.py_tephi
py_tephi.restype = c_char_p
@checked_return_code
def tephi():
return py_tephi()
####################################################################
py_graph = dll.py_graph
py_graph.restype = c_char_p
@checked_return_code
def graph():
return py_graph()
####################################################################
py_axis = dll.py_axis
py_axis.restype = c_char_p
@checked_return_code
def axis():
return py_axis()
####################################################################
py_geo = dll.py_geo
py_geo.restype = c_char_p
@checked_return_code
def geo():
return py_geo()
####################################################################
py_import = dll.py_import
py_import.restype = c_char_p
@checked_return_code
def mimport():
return py_import()
####################################################################
py_info = dll.py_info
py_info.restype = c_char_p
@checked_return_code
def info():
return py_info()
####################################################################
py_input = dll.py_input
py_input.restype = c_char_p
@checked_return_code
def minput():
return py_input()
####################################################################
py_eps = dll.py_eps
py_eps.restype = c_char_p
@checked_return_code
def eps():
return py_eps()
####################################################################
#
# Please note: these two functions changed compared to the previous SWIG based Python interface
#
py_metgraph = dll.py_metgraph
py_metgraph.restype = c_char_p
@checked_return_code
def metgraph():
return py_metgraph()
py_epsinput = dll.py_epsinput
py_epsinput.restype = c_char_p
@checked_return_code
def epsinput():
return py_epsinput()
####################################################################
#
# Please note: this function was called mmetbufr to the previous SWIG based Python interface
#
py_metbufr = dll.py_metbufr
py_metbufr.restype = c_char_p
@checked_return_code
def metbufr():
return py_metbufr()
####################################################################
py_epsgraph = dll.py_epsgraph
py_epsgraph.restype = c_char_p
@checked_return_code
def epsgraph():
return py_epsgraph()
####################################################################
py_epscloud = dll.py_epscloud
py_epscloud.restype = c_char_p
@checked_return_code
def epscloud():
return py_epscloud()
####################################################################
py_epslight = dll.py_epslight
py_epslight.restype = c_char_p
@checked_return_code
def epslight():
return py_epslight()
####################################################################
py_epsplumes = dll.py_epsplumes
py_epsplumes.restype = c_char_p
@checked_return_code
def epsplumes():
return py_epsplumes()
####################################################################
py_epswind = dll.py_epswind
py_epswind.restype = c_char_p
@checked_return_code
def epswind():
return py_epswind()
####################################################################
py_epswave = dll.py_epswave
py_epswave.restype = c_char_p
@checked_return_code
def epswave():
return py_epswave()
####################################################################
py_epsbar = dll.py_epsbar
py_epsbar.restype = c_char_p
@checked_return_code
def epsbar():
return py_epsbar()
####################################################################
py_epsshading = dll.py_epsshading
py_epsshading.restype = c_char_p
@checked_return_code
def epsshading():
return py_epsshading()
####################################################################
py_wrepjson = dll.py_wrepjson
py_wrepjson.restype = c_char_p
@checked_return_code
def wrepjson():
return py_wrepjson()
####################################################################
py_geojson = dll.py_geojson
py_geojson.restype = c_char_p
@checked_return_code
def geojson():
return py_geojson()
####################################################################
py_mapgen = dll.py_mapgen
py_mapgen.restype = c_char_p
@checked_return_code
def mapgen():
return py_mapgen()
####################################################################
py_table = dll.py_table
py_table.restype = c_char_p
@checked_return_code
def mtable():
return py_table()
####################################################################
py_seti = dll.py_seti
py_seti.restype = c_char_p
# py_seti.argtypes = (c_char,)
@checked_return_code
def seti(name, value):
name = string_to_char(name)
return py_seti(name, value)
def known_drivers():
try:
drivers = dll.py_knowndrivers()
drivers = json.loads(drivers.decode())
return drivers["drivers"]
except Exception:
return "known_drivers is not implemented in this version"
####################################################################
py_set1i = dll.py_set1i
py_set1i.restype = c_char_p
# py_set1i.argtypes = (c_char_p, c_int_p, c_int)
@checked_return_code
def set1i(name, data):
# array = np.empty((size,), dtype=np.float64)
# array_p = array.ctypes.data_as(c_double_p)
# _set1r(name, array_p, size)
size = len(data)
name = string_to_char(name)
array_p = (ctypes.c_int * size)(*data)
return py_set1i(ctypes.c_char_p(name), array_p, size)
####################################################################
array_2d_int = ndpointer(dtype=int, ndim=2, flags="CONTIGUOUS")
set2i = dll.py_set2i
set2i.restype = None
set2i.argtypes = (c_char_p, array_2d_int, c_int, c_int)
set2i = convert_strings(set2i)
####################################################################
setr = dll.py_setr
setr.restype = None
setr.argtypes = (c_char_p, c_double)
setr = convert_strings(setr)
####################################################################
py_set1r = dll.py_set1r
py_set1r.restype = c_char_p
# py_set1r.argtypes = (c_char_p, c_double_p, c_int)
@checked_return_code
def set1r(name, data):
size = len(data)
name = string_to_char(name)
array_p = (ctypes.c_double * size)(*data)
return py_set1r(ctypes.c_char_p(name), array_p, size)
####################################################################
array_2d_double = ndpointer(dtype=np.double, ndim=2, flags="CONTIGUOUS")
set2r = dll.py_set2r
set2r.restype = None
set2r.argtypes = (c_char_p, array_2d_double, c_int, c_int)
set2r = convert_strings(set2r)
####################################################################
setc = dll.py_setc
setc.restype = None
setc.argtypes = (c_char_p, c_char_p)
setc = convert_strings(setc)
####################################################################
py_set1c = dll.py_set1c
py_set1c.restype = c_char_p
# py_set1c.argtypes = (c_char_p, c_char_p, c_int)
@checked_return_code
def set1c(name, data):
new_data = [string_to_char(s) for s in data]
name = string_to_char(name)
data_p = (c_char_p * (len(new_data)))(*new_data)
return py_set1c(ctypes.c_char_p(name), data_p, len(new_data))
####################################################################
new_page = dll.py_new
new_page.restype = None
new_page.argtypes = (c_char_p,)
new_page = convert_strings(new_page)
####################################################################
reset = dll.py_reset
reset.restype = None
reset.argtypes = (c_char_p,)
reset = convert_strings(reset)
####################################################################
class MagicsError(Exception):
def __init__(self, err):
super(MagicsError, self).__init__(str(err))
####################################################################
def no_log(a, b):
print(
"Log listeners not handled in this version, consider using a version > 4.0.0 "
)
def not_implemented():
print("Not Implemented, consider upgrading a version > 4.4.0 ")
try:
set_python = dll.py_set_python
set_python.restype = None
set_python.argtypes = None
keep_compatibility = dll.py_keep_compatibility
keep_compatibility.restype = None
keep_compatibility.argtypes = None
mute = dll.py_mute
mute.restype = None
mute.argtypes = None
unmute = dll.py_unmute
unmute.restype = None
unmute.argtypes = None
knowndrivers = dll.py_knowndrivers
knowndrivers.restype = ctypes.c_char_p
knowndrivers.argtypes = None
except Exception:
set_python = not_implemented
keep_compatibility = not_implemented
mute = not_implemented
unmute = not_implemented
knowndrivers = not_implemented
try:
strict_mode = dll.py_strict_mode
strict_mode.restype = None
strict_mode.argtypes = None
except Exception:
strict_mode = not_implemented
log = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, c_char_p)
try:
warning_log = dll.mag_add_warning_listener
warning_log.restype = None
warning_log.argtypes = (ctypes.c_void_p, log)
error_log = dll.mag_add_error_listener
error_log.restype = None
error_log.argtypes = (ctypes.c_void_p, log)
info_log = dll.mag_add_info_listener
info_log.restype = None
info_log.argtypes = (ctypes.c_void_p, log)
debug_log = dll.mag_add_debug_listener
debug_log.restype = None
debug_log.argtypes = (ctypes.c_void_p, log)
except Exception:
error_log = no_log
warning_log = no_log
debug_log = no_log
info_log = no_log
@log
def magics_log(data, msg):
print("SUPER-->", msg.decode())
return 0
# if __name__ == "__main__":
# print "..."
|