Ignore:
Timestamp:
03/08/06 11:57:12 (18 years ago)
Author:
mar637
Message:

added linefinder

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/scantable.py

    r876 r880  
    10201020        else: return s
    10211021
    1022 #     def auto_poly_baseline(self, mask=None, edge=(0,0), order=0,
    1023 #                            threshold=3, insitu=None):
    1024 #         """
    1025 #         Return a scan which has been baselined (all rows) by a polynomial.
    1026 #         Spectral lines are detected first using linefinder and masked out
    1027 #         to avoid them affecting the baseline solution.
    1028 #
    1029 #         Parameters:
    1030 #             mask:       an optional mask retreived from scantable
    1031 #             edge:       an optional number of channel to drop at
    1032 #                         the edge of spectrum. If only one value is
    1033 #                         specified, the same number will be dropped from
    1034 #                         both sides of the spectrum. Default is to keep
    1035 #                         all channels
    1036 #             order:      the order of the polynomial (default is 0)
    1037 #             threshold:  the threshold used by line finder. It is better to
    1038 #                         keep it large as only strong lines affect the
    1039 #                         baseline solution.
    1040 #             insitu:     if False a new scantable is returned.
    1041 #                         Otherwise, the scaling is done in-situ
    1042 #                         The default is taken from .asaprc (False)
    1043 #
    1044 #         Example:
    1045 #             scan2=scan.auto_poly_baseline(order=7)
    1046 #         """
    1047 #         if insitu is None: insitu = rcParams['insitu']
    1048 #         varlist = vars()
    1049 #         from asap.asapfitter import fitter
    1050 #         from asap.asaplinefind import linefinder
    1051 #         from asap import _is_sequence_or_number as _is_valid
    1052 #
    1053 #         if not _is_valid(edge, int):
    1054 #             raise RuntimeError, "Parameter 'edge' has to be an integer or a \
    1055 #             pair of integers specified as a tuple"
    1056 #
    1057 #         # setup fitter
    1058 #         f = fitter()
    1059 #         f.set_function(poly=order)
    1060 #
    1061 #         # setup line finder
    1062 #         fl=linefinder()
    1063 #         fl.set_options(threshold=threshold)
    1064 #
    1065 #         if not insitu:
    1066 #             workscan=self.copy()
    1067 #         else:
    1068 #             workscan=self
    1069 #
    1070 #         rows=range(workscan.nrow())
    1071 #         from asap import asaplog
    1072 #         for i in rows:
    1073 #             asaplog.push("Processing:")
    1074 #             asaplog.push(msg)
    1075 #             fl.set_scan(workscan,mask,edge)
    1076 #             fl.find_lines(i)
    1077 #             f.set_scan(workscan, fl.get_mask())
    1078 #             f.x = workscan._getabcissa(i)
    1079 #             f.y = workscan._getspectrum(i)
    1080 #             f.data = None
    1081 #             f.fit()
    1082 #             x = f.get_parameters()
    1083 #             workscan._setspectrum(f.fitter.getresidual(), i)
    1084 #         workscan._add_history("poly_baseline", varlist)
    1085 #         if insitu:
    1086 #             self._assign(workscan)
    1087 #         else:
    1088 #             return workscan
    1089 #
     1022    def auto_poly_baseline(self, mask=None, edge=(0,0), order=0,
     1023                           threshold=3, insitu=None):
     1024        """
     1025        Return a scan which has been baselined (all rows) by a polynomial.
     1026        Spectral lines are detected first using linefinder and masked out
     1027        to avoid them affecting the baseline solution.
     1028
     1029        Parameters:
     1030            mask:       an optional mask retreived from scantable
     1031            edge:       an optional number of channel to drop at
     1032                        the edge of spectrum. If only one value is
     1033                        specified, the same number will be dropped from
     1034                        both sides of the spectrum. Default is to keep
     1035                        all channels
     1036            order:      the order of the polynomial (default is 0)
     1037            threshold:  the threshold used by line finder. It is better to
     1038                        keep it large as only strong lines affect the
     1039                        baseline solution.
     1040            insitu:     if False a new scantable is returned.
     1041                        Otherwise, the scaling is done in-situ
     1042                        The default is taken from .asaprc (False)
     1043
     1044        Example:
     1045            scan2=scan.auto_poly_baseline(order=7)
     1046        """
     1047        if insitu is None: insitu = rcParams['insitu']
     1048        varlist = vars()
     1049        from asap.asapfitter import fitter
     1050        from asap.asaplinefind import linefinder
     1051        from asap import _is_sequence_or_number as _is_valid
     1052
     1053        if not _is_valid(edge, int):
     1054            raise RuntimeError, "Parameter 'edge' has to be an integer or a \
     1055            pair of integers specified as a tuple"
     1056
     1057        # setup fitter
     1058        f = fitter()
     1059        f.set_function(poly=order)
     1060
     1061        # setup line finder
     1062        fl=linefinder()
     1063        fl.set_options(threshold=threshold)
     1064
     1065        if not insitu:
     1066            workscan=self.copy()
     1067        else:
     1068            workscan=self
     1069
     1070        rows=range(workscan.nrow())
     1071        from asap import asaplog
     1072        asaplog.push("Processing:")
     1073        for r in rows:
     1074            msg = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" %        (workscan.getscan(r),workscan.getbeam(r),workscan.getif(r),workscan.getpol(r), workscan.getcycle(r))
     1075            asaplog.push(msg, False)
     1076            fl.set_scan(workscan, mask, edge)
     1077            fl.find_lines(r)
     1078            f.set_scan(workscan, fl.get_mask())
     1079            f.x = workscan._getabcissa(r)
     1080            f.y = workscan._getspectrum(r)
     1081            f.data = None
     1082            f.fit()
     1083            x = f.get_parameters()
     1084            workscan._setspectrum(f.fitter.getresidual(), r)
     1085        workscan._add_history("poly_baseline", varlist)
     1086        if insitu:
     1087            self._assign(workscan)
     1088        else:
     1089            return workscan
     1090
    10901091#     def rotate_linpolphase(self, angle):
    10911092#         """
Note: See TracChangeset for help on using the changeset viewer.