cdfwrite¤
cdfwrite
¤
Classes:
| Name | Description |
|---|---|
CDF |
Creates an empty CDF file. |
CDF
¤
Creates an empty CDF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Union[str, Path]
|
The path name of the CDF (with or without .cdf extension) |
required |
|
dict
|
The optional specification of the CDF file. The keys for the dictionary are:
|
None
|
Methods:
| Name | Description |
|---|---|
close |
Closes the CDF Class. |
write_globalattrs |
Writes the global attributes. |
write_var |
Writes a variable, along with variable attributes and data. |
write_variableattrs |
Writes a variable's attributes, provided the variable already exists. |
Source code in cdflib/cdfwrite.py
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 | |
close
¤
close() -> None
Closes the CDF Class.
1. If compression was set, this is where the compressed file is
written.
2. If a checksum is needed, this will place the checksum at the end
of the file.
Source code in cdflib/cdfwrite.py
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 | |
write_globalattrs
¤
write_globalattrs(globalAttrs)
Writes the global attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Global attribute name(s) and their value(s) pair(s). The value(s) is a dictionary of entry number and value pair(s). For example:: For a non-string value, use a list with the value and its CDF data type. For example:: If the data type is not provided, a corresponding CDF data type is assumed:: CDF allows multi-values for non-string data for an attribute:: For multi-entries from a global variable, they should be presented in this form:: |
required |
Source code in cdflib/cdfwrite.py
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 | |
write_var
¤
Writes a variable, along with variable attributes and data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
The specifications of the variable. The required/optional keys for creating a variable: Required keys:
For zVariables:
For rVariables:
Optional keys:
|
required |
|
dict
|
{attribute:value} pairs. The attribute is the name of a variable attribute. The value can have its data type specified for the numeric data. If not, based on Python's type, a corresponding CDF type is assumed: CDF_INT4 for int, CDF_DOUBLE for float, CDF_EPOCH16 for complex and and CDF_INT8 for long. For example, the following defined attributes will have the same types in the CDF:: With data type (in the list form):: |
None
|
|
The data for the variable. If the variable is a regular variable without sparse records, it must be in a single structure of bytes, or numpy.ndarray for numeric variable, or str or list of strs for string variable. If the variable has sparse records, var_data should be presented in a list/tuple with two elements, the first being a list/tuple that contains the physical record number(s), the second being the variable data in bytes, numpy.ndarray, or a list of strings. Variable data can have just physical records' data (with the same number of records as the first element) or have data from both physical records and virtual records (which with filled data). The var_data has the form:: See the sample for its setup. |
None
|
Source code in cdflib/cdfwrite.py
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 | |
write_variableattrs
¤
write_variableattrs(variableAttrs)
Writes a variable's attributes, provided the variable already exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
Variable attribute name and its entry value pair(s). The entry value is also a dictionary of variable id and value pair(s). Variable id can be the variable name or its id number in the file. Use write_var function if the variable does not exist. For example:: |
required |
Source code in cdflib/cdfwrite.py
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 | |
Sample Usage¤
>>> import cdfwrite
>>> import cdfread
>>> import numpy as np
>>>
>>> cdf_master = cdfread.CDF('/path/to/master_file.cdf')
>>> if (cdf_master.file != None):
>>> # Get the cdf's specification
>>> info=cdf_master.cdf_info()
>>> cdf_file=cdfwrite.CDF('/path/to/swea_file.cdf',cdf_spec=info,delete=True)
>>> if (cdf_file.file == None):
>>> cdf_master.close()
>>> raise OSError('Problem writing file.... Stop')
>>>
>>> # Get the global attributes
>>> globalaAttrs=cdf_master.globalattsget(expand=True)
>>> # Write the global attributes
>>> cdf_file.write_globalattrs(globalaAttrs)
>>> zvars=info['zVariables']
>>> print('no of zvars=',len(zvars))
>>> # Loop thru all the zVariables
>>> for x in range (0, len(zvars)):
>>> # Get the variable's specification
>>> varinfo=cdf_master.varinq(zvars[x])
>>> #print('Z =============>',x,': ', varinfo['Variable'])
>>> # Get the variable's attributes
>>> varattrs=cdf_master.varattsget(zvars[x], expand=True)
>>> if (varinfo['Sparse'].lower() == 'no_sparse'):
>>> # A variable with no sparse records... get the variable data
>>> vardata=.......
>>> # Create the zVariable, write out the attributes and data
>>> cdf_file.write_var(varinfo, var_attrs=varattrs, var_data=vardata)
>>> else:
>>> # A variable with sparse records...
>>> # data is in this form [physical_record_numbers, data_values]
>>> # physical_record_numbers (0-based) contains the real record
>>> # numbers. For example, a variable has only 3 physical records
>>> # at [0, 5, 10]:
>>> varrecs=[0,5,10]
>>> # data_values could contain only the physical records' data or
>>> # both the physical and virtual records' data.
>>> # For example, a float variable of 1-D with 3 elements with only
>>> # 3 physical records at [0,5,10]:
>>> # vardata = [[ 5.55000000e+01, -1.00000002e+30, 6.65999985e+01],
>>> # [ 6.66659973e+02, 7.77770020e+02, 8.88880005e+02],
>>> # [ 2.00500000e+02, 2.10600006e+02, 2.20699997e+02]]
>>> # Or, with virtual record data embedded in the data:
>>> # vardata = [[ 5.55000000e+01, -1.00000002e+30, 6.65999985e+01],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ 6.66659973e+02, 7.77770020e+02, 8.88880005e+02],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ -1.00000002e+30, -1.00000002e+30, -1.00000002e+30],
>>> # [ 2.00500000e+02, 2.10600006e+02, 2.20699997e+02]]
>>> # Records 1, 2, 3, 4, 6, 7, 8, 9 are all virtual records with pad
>>> # data (variable defined with 'pad_sparse').
>>> vardata=np.asarray([.,.,.,..])
>>> # Create the zVariable, and optionally write out the attributes
>>> # and data
>>> cdf_file.write_var(varinfo, var_attrs=varattrs,
>>> var_data=[varrecs,vardata])
>>> rvars=info['rVariables']
>>> print('no of rvars=',len(rvars))
>>> # Loop thru all the rVariables
>>> for x in range (0, len(rvars)):
>>> varinfo=cdf_master.varinq(rvars[x])
>>> print('R =============>',x,': ', varinfo['Variable'])
>>> varattrs=cdf_master.varattsget(rvars[x], expand=True)
>>> if (varinfo['Sparse'].lower() == 'no_sparse'):
>>> vardata=.......
>>> # Create the rVariable, write out the attributes and data
>>> cdf_file.write_var(varinfo, var_attrs=varattrs, var_data=vardata)
>>> else:
>>> varrecs=[.,.,.,..]
>>> vardata=np.asarray([.,.,.,..])
>>> cdf_file.write_var(varinfo, var_attrs=varattrs,
>>> var_data=[varrecs,vardata])
>>> cdf_master.close()
>>> cdf_file.close()