- Timestamp:
- 01/27/10 18:29:48 (15 years ago)
- Location:
- branches/alma
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alma/python/scantable.py
r1676 r1677 1868 1868 return s 1869 1869 1870 def scale(self, factor, tsys=True, insitu=None):1870 def scale(self, factor, tsys=True, mode='channel', insitu=None): 1871 1871 """ 1872 1872 Return a scan where all spectra are scaled by the give 'factor' 1873 1873 Parameters: 1874 factor: the scaling factor 1874 factor: the scaling factor (float or 1D float list) 1875 1875 insitu: if False a new scantable is returned. 1876 1876 Otherwise, the scaling is done in-situ … … 1878 1878 tsys: if True (default) then apply the operation to Tsys 1879 1879 as well as the data 1880 mode: operation mode for list scaling factor. Possible 1881 values are 'channel' (channel-by-channel) or 1882 'row' (row-by-row). 1880 1883 """ 1881 1884 if insitu is None: insitu = rcParams['insitu'] 1882 1885 self._math._setinsitu(insitu) 1883 1886 varlist = vars() 1884 s = scantable(self._math._unaryop(self, factor, "MUL", tsys)) 1887 import numpy 1888 if type(factor) == list: 1889 if mode == 'row': 1890 s = scantable( self._math._arrayop( self.copy(), factor, "MUL", tsys, 'row' ) ) 1891 elif mode == 'channel': 1892 s = scantable( self._math._arrayop( self.copy(), factor, "MUL", tsys, 'channel' ) ) 1893 else: 1894 asaplog.push( 'scantable.scale(): Unknown operation mode. No scaling.' ) 1895 print_log() 1896 s = self.copy() 1897 else: 1898 s = scantable(self._math._unaryop(self.copy(), factor, "MUL", tsys)) 1885 1899 s._add_history("scale", varlist) 1886 1900 print_log() … … 2027 2041 s = None 2028 2042 if isinstance(other, scantable): 2029 s = scantable(self._math._binaryop(self , other, "ADD"))2043 s = scantable(self._math._binaryop(self.copy(), other, "ADD")) 2030 2044 elif isinstance(other, float): 2031 s = scantable(self._math._unaryop(self, other, "ADD", False)) 2032 else: 2033 raise TypeError("Other input is not a scantable or float value") 2045 s = scantable(self._math._unaryop(self.copy(), other, "ADD", False)) 2046 elif isinstance(other, list): 2047 if len(other) == self.nrow(): 2048 s = scantable(self._math._arrayop(self.copy(), other, "ADD", False, 'row')) 2049 elif len(other) == self.nchan(0): 2050 s = scantable(self._math._arrayop(self.copy(), other, "ADD", False, 'channel')) 2051 else: 2052 casalog.post( 'Length of list operand should equal to either scantable.nchan() or scantable.nrow().', 'ERROR' ) 2053 else: 2054 raise TypeError("Other input is not a scantable or float value or float list") 2034 2055 s._add_history("operator +", varlist) 2035 2056 print_log() … … 2043 2064 s = None 2044 2065 if isinstance(other, scantable): 2045 s = scantable(self._math._binaryop(self , other, "SUB"))2066 s = scantable(self._math._binaryop(self.copy(), other, "SUB")) 2046 2067 elif isinstance(other, float): 2047 s = scantable(self._math._unaryop(self, other, "SUB", False)) 2048 else: 2049 raise TypeError("Other input is not a scantable or float value") 2068 s = scantable(self._math._unaryop(self.copy(), other, "SUB", False)) 2069 elif isinstance(other, list): 2070 if len(other) == self.nrow(): 2071 s = scantable(self._math._arrayop(self.copy(), other, "SUB", False, 'row')) 2072 elif len(other) == self.nchan(0): 2073 s = scantable(self._math._arrayop(self.copy(), other, "SUB", False, 'channel')) 2074 else: 2075 casalog.post( 'Length of list operand should equal to either scantable.nchan() or scantable.nrow().', 'ERROR' ) 2076 else: 2077 raise TypeError("Other input is not a scantable or float value or float list") 2050 2078 s._add_history("operator -", varlist) 2051 2079 print_log() … … 2059 2087 s = None 2060 2088 if isinstance(other, scantable): 2061 s = scantable(self._math._binaryop(self , other, "MUL"))2089 s = scantable(self._math._binaryop(self.copy(), other, "MUL")) 2062 2090 elif isinstance(other, float): 2063 s = scantable(self._math._unaryop(self, other, "MUL", False)) 2064 else: 2065 raise TypeError("Other input is not a scantable or float value") 2091 s = scantable(self._math._unaryop(self.copy(), other, "MUL", False)) 2092 elif isinstance(other, list): 2093 if len(other) == self.nrow(): 2094 s = scantable(self._math._arrayop(self.copy(), other, "MUL", False, 'row')) 2095 elif len(other) == self.nchan(0): 2096 s = scantable(self._math._arrayop(self.copy(), other, "MUL", False, 'channel')) 2097 else: 2098 casalog.post( 'Length of list operand should equal to either scantable.nchan() or scantable.nrow().', 'ERROR' ) 2099 else: 2100 raise TypeError("Other input is not a scantable or float value or float list") 2066 2101 s._add_history("operator *", varlist) 2067 2102 print_log() 2068 2103 return s 2069 2070 2104 2071 2105 def __div__(self, other): … … 2076 2110 s = None 2077 2111 if isinstance(other, scantable): 2078 s = scantable(self._math._binaryop(self , other, "DIV"))2112 s = scantable(self._math._binaryop(self.copy(), other, "DIV")) 2079 2113 elif isinstance(other, float): 2080 2114 if other == 0.0: 2081 2115 raise ZeroDivisionError("Dividing by zero is not recommended") 2082 s = scantable(self._math._unaryop(self, other, "DIV", False)) 2083 else: 2084 raise TypeError("Other input is not a scantable or float value") 2116 s = scantable(self._math._unaryop(self.copy(), other, "DIV", False)) 2117 elif isinstance(other, list): 2118 if len(other) == self.nrow(): 2119 s = scantable(self._math._array2dop(self.copy(), other, "DIV", False, 'row')) 2120 elif len(other) == self.nchan(0): 2121 s = scantable(self._math._arrayop(self.copy(), other, "DIV", False, 'channel')) 2122 else: 2123 casalog.post( 'Length of list operand should equal to either scantable.nchan() or scantable.nrow().', 'ERROR' ) 2124 else: 2125 raise TypeError("Other input is not a scantable or float value or float list") 2085 2126 s._add_history("operator /", varlist) 2086 2127 print_log() -
branches/alma/src/STMath.cpp
r1675 r1677 497 497 if ( tsys ) { 498 498 ts += val; 499 tsysCol.put(i, ts); 500 } 501 } 502 } 503 return out; 504 } 505 506 CountedPtr< Scantable > STMath::arrayOperate( const CountedPtr< Scantable >& in, 507 const std::vector<float> val, 508 const std::string& mode, 509 bool tsys, 510 const std::string& op ) 511 { 512 CountedPtr< Scantable > out ; 513 if ( op == "channel" ) { 514 out = arrayOperateChannel( in, val, mode, tsys ) ; 515 } 516 else if ( op == "row" ) { 517 out = arrayOperateRow( in, val, mode, tsys ) ; 518 } 519 else { 520 throw( AipsError( "Unknown array operation mode." ) ) ; 521 } 522 return out ; 523 } 524 525 CountedPtr< Scantable > STMath::arrayOperateChannel( const CountedPtr< Scantable >& in, 526 const std::vector<float> val, 527 const std::string& mode, 528 bool tsys ) 529 { 530 // conformity of SPECTRA and TSYS 531 if ( tsys ) { 532 TableIterator titer(in->table(), "IFNO"); 533 while ( !titer.pastEnd() ) { 534 ArrayColumn<Float> specCol( in->table(), "SPECTRA" ) ; 535 ArrayColumn<Float> tsysCol( in->table(), "TSYS" ) ; 536 Array<Float> spec = specCol.getColumn() ; 537 Array<Float> ts = tsysCol.getColumn() ; 538 if ( !spec.conform( ts ) ) { 539 throw( AipsError( "SPECTRA and TSYS must conform in shape if you want to apply operation on Tsys." ) ) ; 540 } 541 titer.next() ; 542 } 543 } 544 545 // check if all spectra in the scantable have the same number of channel 546 vector<uInt> nchans; 547 vector<uInt> ifnos = in->getIFNos() ; 548 for ( uInt i = 0 ; i < ifnos.size() ; i++ ) { 549 nchans.push_back( in->nchan( ifnos[i] ) ) ; 550 } 551 Vector<uInt> mchans( nchans ) ; 552 if ( anyNE( mchans, mchans[0] ) ) { 553 throw( AipsError("All spectra in the input scantable must have the same number of channel for vector operation." ) ) ; 554 } 555 556 // check if vector size is equal to nchan 557 Vector<Float> fact( val ) ; 558 if ( fact.nelements() != mchans[0] ) { 559 throw( AipsError("Vector size must be same as number of channel.") ) ; 560 } 561 562 // check divided by zero 563 if ( ( mode == "DIV" ) && anyEQ( fact, (float)0.0 ) ) { 564 throw( AipsError("Divided by zero is not recommended." ) ) ; 565 } 566 567 CountedPtr< Scantable > out = getScantable(in, false); 568 Table& tab = out->table(); 569 ArrayColumn<Float> specCol(tab,"SPECTRA"); 570 ArrayColumn<Float> tsysCol(tab,"TSYS"); 571 for (uInt i=0; i<tab.nrow(); ++i) { 572 Vector<Float> spec; 573 Vector<Float> ts; 574 specCol.get(i, spec); 575 tsysCol.get(i, ts); 576 if (mode == "MUL" || mode == "DIV") { 577 if (mode == "DIV") fact = (float)1.0 / fact; 578 spec *= fact; 579 specCol.put(i, spec); 580 if ( tsys ) { 581 ts *= fact; 582 tsysCol.put(i, ts); 583 } 584 } else if ( mode == "ADD" || mode == "SUB") { 585 if (mode == "SUB") fact *= (float)-1.0 ; 586 spec += fact; 587 specCol.put(i, spec); 588 if ( tsys ) { 589 ts += fact; 590 tsysCol.put(i, ts); 591 } 592 } 593 } 594 return out; 595 } 596 597 CountedPtr< Scantable > STMath::arrayOperateRow( const CountedPtr< Scantable >& in, 598 const std::vector<float> val, 599 const std::string& mode, 600 bool tsys ) 601 { 602 // conformity of SPECTRA and TSYS 603 if ( tsys ) { 604 TableIterator titer(in->table(), "IFNO"); 605 while ( !titer.pastEnd() ) { 606 ArrayColumn<Float> specCol( in->table(), "SPECTRA" ) ; 607 ArrayColumn<Float> tsysCol( in->table(), "TSYS" ) ; 608 Array<Float> spec = specCol.getColumn() ; 609 Array<Float> ts = tsysCol.getColumn() ; 610 if ( !spec.conform( ts ) ) { 611 throw( AipsError( "SPECTRA and TSYS must conform in shape if you want to apply operation on Tsys." ) ) ; 612 } 613 titer.next() ; 614 } 615 } 616 617 // check if vector size is equal to nrow 618 Vector<Float> fact( val ) ; 619 if ( fact.nelements() != in->nrow() ) { 620 throw( AipsError("Vector size must be same as number of row.") ) ; 621 } 622 623 // check divided by zero 624 if ( ( mode == "DIV" ) && anyEQ( fact, (float)0.0 ) ) { 625 throw( AipsError("Divided by zero is not recommended." ) ) ; 626 } 627 628 CountedPtr< Scantable > out = getScantable(in, false); 629 Table& tab = out->table(); 630 ArrayColumn<Float> specCol(tab,"SPECTRA"); 631 ArrayColumn<Float> tsysCol(tab,"TSYS"); 632 if (mode == "DIV") fact = (float)1.0 / fact; 633 if (mode == "SUB") fact *= (float)-1.0 ; 634 for (uInt i=0; i<tab.nrow(); ++i) { 635 Vector<Float> spec; 636 Vector<Float> ts; 637 specCol.get(i, spec); 638 tsysCol.get(i, ts); 639 if (mode == "MUL" || mode == "DIV") { 640 spec *= fact[i]; 641 specCol.put(i, spec); 642 if ( tsys ) { 643 ts *= fact[i]; 644 tsysCol.put(i, ts); 645 } 646 } else if ( mode == "ADD" || mode == "SUB") { 647 spec += fact[i]; 648 specCol.put(i, spec); 649 if ( tsys ) { 650 ts += fact[i]; 651 tsysCol.put(i, ts); 652 } 653 } 654 } 655 return out; 656 } 657 658 CountedPtr< Scantable > STMath::array2dOperate( const CountedPtr< Scantable >& in, 659 const std::vector< std::vector<float> > val, 660 const std::string& mode, 661 bool tsys ) 662 { 663 // conformity of SPECTRA and TSYS 664 if ( tsys ) { 665 TableIterator titer(in->table(), "IFNO"); 666 while ( !titer.pastEnd() ) { 667 ArrayColumn<Float> specCol( in->table(), "SPECTRA" ) ; 668 ArrayColumn<Float> tsysCol( in->table(), "TSYS" ) ; 669 Array<Float> spec = specCol.getColumn() ; 670 Array<Float> ts = tsysCol.getColumn() ; 671 if ( !spec.conform( ts ) ) { 672 throw( AipsError( "SPECTRA and TSYS must conform in shape if you want to apply operation on Tsys." ) ) ; 673 } 674 titer.next() ; 675 } 676 } 677 678 // some checks 679 vector<uInt> nchans; 680 for ( uInt i = 0 ; i < in->nrow() ; i++ ) { 681 nchans.push_back( (in->getSpectrum( i )).size() ) ; 682 } 683 //Vector<uInt> mchans( nchans ) ; 684 vector< Vector<Float> > facts ; 685 for ( uInt i = 0 ; i < nchans.size() ; i++ ) { 686 Vector<Float> tmp( val[i] ) ; 687 // check divided by zero 688 if ( ( mode == "DIV" ) && anyEQ( tmp, (float)0.0 ) ) { 689 throw( AipsError("Divided by zero is not recommended." ) ) ; 690 } 691 // conformity check 692 if ( tmp.nelements() != nchans[i] ) { 693 stringstream ss ; 694 ss << "Row " << i << ": Vector size must be same as number of channel." ; 695 throw( AipsError( ss.str() ) ) ; 696 } 697 facts.push_back( tmp ) ; 698 } 699 700 701 CountedPtr< Scantable > out = getScantable(in, false); 702 Table& tab = out->table(); 703 ArrayColumn<Float> specCol(tab,"SPECTRA"); 704 ArrayColumn<Float> tsysCol(tab,"TSYS"); 705 for (uInt i=0; i<tab.nrow(); ++i) { 706 Vector<Float> fact = facts[i] ; 707 Vector<Float> spec; 708 Vector<Float> ts; 709 specCol.get(i, spec); 710 tsysCol.get(i, ts); 711 if (mode == "MUL" || mode == "DIV") { 712 if (mode == "DIV") fact = (float)1.0 / fact; 713 spec *= fact; 714 specCol.put(i, spec); 715 if ( tsys ) { 716 ts *= fact; 717 tsysCol.put(i, ts); 718 } 719 } else if ( mode == "ADD" || mode == "SUB") { 720 if (mode == "SUB") fact *= (float)-1.0 ; 721 spec += fact; 722 specCol.put(i, spec); 723 if ( tsys ) { 724 ts += fact; 499 725 tsysCol.put(i, ts); 500 726 } -
branches/alma/src/STMath.h
r1673 r1677 117 117 casa::CountedPtr<Scantable> 118 118 unaryOperate( const casa::CountedPtr<Scantable>& in, float val, 119 const std::string& mode, bool tsys=false ); 120 121 // array operation 122 casa::CountedPtr<Scantable> 123 arrayOperate( const casa::CountedPtr<Scantable>& in, 124 const std::vector<float> val, 125 const std::string& mode, bool tsys=false, 126 const std::string& op="channel" ); 127 128 // channel operation 129 casa::CountedPtr<Scantable> 130 arrayOperateChannel( const casa::CountedPtr<Scantable>& in, 131 const std::vector<float> val, 132 const std::string& mode, bool tsys=false ); 133 134 // row operation 135 casa::CountedPtr<Scantable> 136 arrayOperateRow( const casa::CountedPtr<Scantable>& in, 137 const std::vector<float> val, 138 const std::string& mode, bool tsys=false ); 139 140 // 2d array operation 141 casa::CountedPtr<Scantable> 142 array2dOperate( const casa::CountedPtr<Scantable>& in, 143 const std::vector< std::vector<float> > val, 119 144 const std::string& mode, bool tsys=false ); 120 145 -
branches/alma/src/STMathWrapper.h
r1673 r1677 73 73 { return ScantableWrapper(STMath::unaryOperate(in.getCP(), val, mode, tsys)); } 74 74 75 ScantableWrapper arrayOperate( const ScantableWrapper& in, 76 const std::vector<float> val, 77 const std::string& mode, bool tsys=false, 78 const std::string& op="channel" ) 79 { return ScantableWrapper(STMath::arrayOperate(in.getCP(), val, mode, tsys, op)); } 80 81 ScantableWrapper array2dOperate( const ScantableWrapper& in, 82 const std::vector< std::vector<float> > val, 83 const std::string& mode, bool tsys=false ) 84 { return ScantableWrapper(STMath::array2dOperate(in.getCP(), val, mode, tsys)); } 85 75 86 ScantableWrapper binaryOperate( const ScantableWrapper& left, 76 87 const ScantableWrapper& right, -
branches/alma/src/python_STMath.cpp
r1673 r1677 49 49 .def("_averagebeams", &STMathWrapper::averageBeams) 50 50 .def("_unaryop", &STMathWrapper::unaryOperate) 51 .def("_arrayop", &STMathWrapper::arrayOperate) 52 //.def("_array2dop", &STMathWrapper::array2dOperate) 51 53 .def("_binaryop", &STMathWrapper::binaryOperate) 52 54 .def("_auto_quotient", &STMathWrapper::autoQuotient)
Note:
See TracChangeset
for help on using the changeset viewer.