Changeset 1446 for branches/alma/src/STMath.cpp
- Timestamp:
- 11/12/08 17:04:01 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alma/src/STMath.cpp
r1387 r1446 191 191 } 192 192 //write out 193 Vector<uChar> flg(msk.shape()); 194 convertArray(flg, !msk); 195 flagColOut.put(i, flg); 196 specColOut.put(i, acc.getSpectrum()); 197 tsysColOut.put(i, acc.getTsys()); 198 intColOut.put(i, acc.getInterval()); 199 mjdColOut.put(i, acc.getTime()); 200 // we should only have one cycle now -> reset it to be 0 201 // frequency switched data has different CYCLENO for different IFNO 202 // which requires resetting this value 203 cycColOut.put(i, uInt(0)); 193 if (acc.state()) { 194 Vector<uChar> flg(msk.shape()); 195 convertArray(flg, !msk); 196 flagColOut.put(i, flg); 197 specColOut.put(i, acc.getSpectrum()); 198 tsysColOut.put(i, acc.getTsys()); 199 intColOut.put(i, acc.getInterval()); 200 mjdColOut.put(i, acc.getTime()); 201 // we should only have one cycle now -> reset it to be 0 202 // frequency switched data has different CYCLENO for different IFNO 203 // which requires resetting this value 204 cycColOut.put(i, uInt(0)); 205 } else { 206 ostringstream oss; 207 oss << "For output row="<<i<<", all input rows of data are flagged. no averaging" << endl; 208 pushLog(String(oss)); 209 } 204 210 acc.reset(); 205 211 } … … 947 953 { 948 954 949 955 950 956 STSelector sel; 951 957 CountedPtr< Scantable > ws = getScantable(s, false); 952 958 CountedPtr< Scantable > sig, sigwcal, ref, refwcal; 953 CountedPtr< Scantable > calsig, calref, out; 959 CountedPtr< Scantable > calsig, calref, out, out1, out2; 960 Bool nofold=False; 954 961 955 962 //split the data … … 973 980 calref = dototalpower(refwcal, ref, tcal=tcal); 974 981 975 out=dosigref(calsig,calref,smoothref,tsysv,tau); 976 982 out1=dosigref(calsig,calref,smoothref,tsysv,tau); 983 out2=dosigref(calref,calsig,smoothref,tsysv,tau); 984 985 Table& tabout1=out1->table(); 986 Table& tabout2=out2->table(); 987 ROScalarColumn<uInt> freqidCol1(tabout1, "FREQ_ID"); 988 ROScalarColumn<uInt> freqidCol2(tabout2, "FREQ_ID"); 989 ROArrayColumn<Float> specCol(tabout2, "SPECTRA"); 990 Vector<Float> spec; specCol.get(0, spec); 991 uInt nchan = spec.nelements(); 992 uInt freqid1; freqidCol1.get(0,freqid1); 993 uInt freqid2; freqidCol2.get(0,freqid2); 994 Double rp1, rp2, rv1, rv2, inc1, inc2; 995 out1->frequencies().getEntry(rp1, rv1, inc1, freqid1); 996 out2->frequencies().getEntry(rp2, rv2, inc2, freqid2); 997 if (rp1==rp2) { 998 Double foffset = rv1 - rv2; 999 uInt choffset = static_cast<uInt>(foffset/abs(inc2)); 1000 if (choffset >= nchan) { 1001 cerr<<"out-band frequency switching, no folding"<<endl; 1002 nofold = True; 1003 } 1004 } 1005 1006 if (nofold) { 1007 std::vector< CountedPtr< Scantable > > tabs; 1008 tabs.push_back(out1); 1009 tabs.push_back(out2); 1010 out = merge(tabs); 1011 } 1012 else { //folding is not implemented yet 1013 out = out1; 1014 } 1015 977 1016 return out; 978 1017 } 979 980 1018 981 1019 CountedPtr< Scantable > STMath::freqSwitch( const CountedPtr< Scantable >& in ) … … 1565 1603 id = out->frequencies().addEntry(rp, rv, inc); 1566 1604 freqidcol.put(k,id); 1567 String name,fname;Double rf; 1605 //String name,fname;Double rf; 1606 Vector<String> name,fname;Vector<Double> rf; 1568 1607 (*it)->molecules().getEntry(rf, name, fname, rec.asuInt("MOLECULE_ID")); 1569 1608 id = out->molecules().addEntry(rf, name, fname); … … 1981 2020 return out; 1982 2021 } 2022 2023 // Averaging spectra with different channel/resolution 2024 CountedPtr<Scantable> 2025 STMath::new_average( const std::vector<CountedPtr<Scantable> >& in, 2026 const bool& compel, 2027 const std::vector<bool>& mask, 2028 const std::string& weight, 2029 const std::string& avmode ) 2030 throw ( casa::AipsError ) 2031 { 2032 if ( avmode == "SCAN" && in.size() != 1 ) 2033 throw(AipsError("Can't perform 'SCAN' averaging on multiple tables.\n" 2034 "Use merge first.")); 2035 2036 CountedPtr<Scantable> out ; // processed result 2037 if ( compel ) { 2038 std::vector< CountedPtr<Scantable> > newin ; // input for average process 2039 uInt insize = in.size() ; // number of scantables 2040 2041 // TEST: do normal average in each table before IF grouping 2042 cout << "Do preliminary averaging" << endl ; 2043 vector< CountedPtr<Scantable> > tmpin( insize ) ; 2044 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2045 vector< CountedPtr<Scantable> > v( 1, in[itable] ) ; 2046 tmpin[itable] = average( v, mask, weight, avmode ) ; 2047 } 2048 2049 // warning 2050 cout << "Average spectra with different spectral resolution" << endl ; 2051 cout << endl ; 2052 2053 // temporarily set coordinfo 2054 vector<string> oldinfo( insize ) ; 2055 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2056 vector<string> coordinfo = in[itable]->getCoordInfo() ; 2057 oldinfo[itable] = coordinfo[0] ; 2058 coordinfo[0] = "Hz" ; 2059 tmpin[itable]->setCoordInfo( coordinfo ) ; 2060 } 2061 2062 // columns 2063 ScalarColumn<uInt> freqIDCol ; 2064 ScalarColumn<uInt> ifnoCol ; 2065 ScalarColumn<uInt> scannoCol ; 2066 2067 2068 // check IF frequency coverage 2069 //cout << "Check IF settings in each table" << endl ; 2070 vector< vector<uInt> > freqid( insize ); 2071 vector< vector<double> > iffreq( insize ) ; 2072 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2073 uInt rows = tmpin[itable]->nrow() ; 2074 uInt freqnrows = tmpin[itable]->frequencies().table().nrow() ; 2075 for ( uInt irow = 0 ; irow < rows ; irow++ ) { 2076 if ( freqid[itable].size() == freqnrows ) { 2077 break ; 2078 } 2079 else { 2080 freqIDCol.attach( tmpin[itable]->table(), "FREQ_ID" ) ; 2081 ifnoCol.attach( tmpin[itable]->table(), "IFNO" ) ; 2082 uInt id = freqIDCol( irow ) ; 2083 if ( freqid[itable].size() == 0 || count( freqid[itable].begin(), freqid[itable].end(), id ) == 0 ) { 2084 //cout << "itable = " << itable << ": IF " << id << " is included in the list" << endl ; 2085 vector<double> abcissa = tmpin[itable]->getAbcissa( irow ) ; 2086 freqid[itable].push_back( id ) ; 2087 iffreq[itable].push_back( abcissa[0] - 0.5 * ( abcissa[1] - abcissa[0] ) ) ; 2088 iffreq[itable].push_back( abcissa[abcissa.size()-1] + 0.5 * ( abcissa[1] - abcissa[0] ) ) ; 2089 } 2090 } 2091 } 2092 } 2093 2094 // debug 2095 //cout << "IF settings summary:" << endl ; 2096 //for ( uInt i = 0 ; i < freqid.size() ; i++ ) { 2097 //cout << " Table" << i << endl ; 2098 //for ( uInt j = 0 ; j < freqid[i].size() ; j++ ) { 2099 //cout << " id = " << freqid[i][j] << " (min,max) = (" << iffreq[i][2*j] << "," << iffreq[i][2*j+1] << ")" << endl ; 2100 //} 2101 //} 2102 //cout << endl ; 2103 2104 // IF grouping based on their frequency coverage 2105 //cout << "IF grouping based on their frequency coverage" << endl ; 2106 2107 // IF group 2108 // ifgrp[numgrp][nummember*2] 2109 // ifgrp = [table00, freqrow00, table01, freqrow01, ...], 2110 // [table11, freqrow11, table11, freqrow11, ...], 2111 // ... 2112 // ifgfreq[numgrp*2] 2113 // ifgfreq = [min0, max0, min1, max1, ...] 2114 vector< vector<uInt> > ifgrp ; 2115 vector<double> ifgfreq ; 2116 2117 // parameter for IF grouping 2118 // groupmode = OR retrieve all region 2119 // AND only retrieve overlaped region 2120 //string groupmode = "AND" ; 2121 string groupmode = "OR" ; 2122 uInt sizecr = 0 ; 2123 if ( groupmode == "AND" ) 2124 sizecr = 2 ; 2125 else if ( groupmode == "OR" ) 2126 sizecr = 0 ; 2127 2128 vector<double> sortedfreq ; 2129 for ( uInt i = 0 ; i < iffreq.size() ; i++ ) { 2130 for ( uInt j = 0 ; j < iffreq[i].size() ; j++ ) { 2131 if ( count( sortedfreq.begin(), sortedfreq.end(), iffreq[i][j] ) == 0 ) 2132 sortedfreq.push_back( iffreq[i][j] ) ; 2133 } 2134 } 2135 sort( sortedfreq.begin(), sortedfreq.end() ) ; 2136 for ( vector<double>::iterator i = sortedfreq.begin() ; i != sortedfreq.end()-1 ; i++ ) { 2137 ifgfreq.push_back( *i ) ; 2138 ifgfreq.push_back( *(i+1) ) ; 2139 } 2140 ifgrp.resize( ifgfreq.size()/2 ) ; 2141 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2142 for ( uInt iif = 0 ; iif < freqid[itable].size() ; iif++ ) { 2143 double range0 = iffreq[itable][2*iif] ; 2144 double range1 = iffreq[itable][2*iif+1] ; 2145 for ( uInt j = 0 ; j < ifgrp.size() ; j++ ) { 2146 double fmin = max( range0, ifgfreq[2*j] ) ; 2147 double fmax = min( range1, ifgfreq[2*j+1] ) ; 2148 if ( fmin < fmax ) { 2149 ifgrp[j].push_back( itable ) ; 2150 ifgrp[j].push_back( freqid[itable][iif] ) ; 2151 } 2152 } 2153 } 2154 } 2155 vector< vector<uInt> >::iterator fiter = ifgrp.begin() ; 2156 vector<double>::iterator giter = ifgfreq.begin() ; 2157 while( fiter != ifgrp.end() ) { 2158 if ( fiter->size() <= sizecr ) { 2159 fiter = ifgrp.erase( fiter ) ; 2160 giter = ifgfreq.erase( giter ) ; 2161 giter = ifgfreq.erase( giter ) ; 2162 } 2163 else { 2164 fiter++ ; 2165 advance( giter, 2 ) ; 2166 } 2167 } 2168 2169 // freqgrp[numgrp][nummember] 2170 // freqgrp = [ifgrp00, ifgrp01, ifgrp02, ...], 2171 // [ifgrp10, ifgrp11, ifgrp12, ...], 2172 // ... 2173 // freqrange[numgrp*2] 2174 // freqrange = [min0, max0, min1, max1, ...] 2175 vector< vector<uInt> > freqgrp ; 2176 double freqrange = 0.0 ; 2177 uInt grpnum = 0 ; 2178 for ( uInt i = 0 ; i < ifgrp.size() ; i++ ) { 2179 // Assumed that ifgfreq was sorted 2180 if ( grpnum != 0 && freqrange == ifgfreq[2*i] ) { 2181 freqgrp[grpnum-1].push_back( i ) ; 2182 } 2183 else { 2184 vector<uInt> grp0( 1, i ) ; 2185 freqgrp.push_back( grp0 ) ; 2186 grpnum++ ; 2187 } 2188 freqrange = ifgfreq[2*i+1] ; 2189 } 2190 2191 2192 // print IF groups 2193 cout << "IF Group summary: " << endl ; 2194 cout << " GROUP_ID [FREQ_MIN, FREQ_MAX]: (TABLE_ID, FREQ_ID)" << endl ; 2195 for ( uInt i = 0 ; i < ifgrp.size() ; i++ ) { 2196 cout << " GROUP " << setw( 2 ) << i << " [" << ifgfreq[2*i] << "," << ifgfreq[2*i+1] << "]: " ; 2197 for ( uInt j = 0 ; j < ifgrp[i].size()/2 ; j++ ) { 2198 cout << "(" << ifgrp[i][2*j] << "," << ifgrp[i][2*j+1] << ") " ; 2199 } 2200 cout << endl ; 2201 } 2202 cout << endl ; 2203 2204 // print frequency group 2205 cout << "Frequency Group summary: " << endl ; 2206 cout << " GROUP_ID [FREQ_MIN, FREQ_MAX]: IF_GROUP_ID" << endl ; 2207 for ( uInt i = 0 ; i < freqgrp.size() ; i++ ) { 2208 cout << " GROUP " << setw( 2 ) << i << " [" << ifgfreq[2*freqgrp[i][0]] << "," << ifgfreq[2*freqgrp[i][freqgrp[i].size()-1]+1] << "]: " ; 2209 for ( uInt j = 0 ; j < freqgrp[i].size() ; j++ ) { 2210 cout << freqgrp[i][j] << " " ; 2211 } 2212 cout << endl ; 2213 } 2214 cout << endl ; 2215 2216 // membership check 2217 // groups[numtable][numIF][nummembership] 2218 vector< vector< vector<uInt> > > groups( insize ) ; 2219 for ( uInt i = 0 ; i < insize ; i++ ) { 2220 groups[i].resize( freqid[i].size() ) ; 2221 } 2222 for ( uInt igrp = 0 ; igrp < ifgrp.size() ; igrp++ ) { 2223 for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) { 2224 uInt tableid = ifgrp[igrp][2*imem] ; 2225 vector<uInt>::iterator iter = find( freqid[tableid].begin(), freqid[tableid].end(), ifgrp[igrp][2*imem+1] ) ; 2226 if ( iter != freqid[tableid].end() ) { 2227 uInt rowid = distance( freqid[tableid].begin(), iter ) ; 2228 groups[tableid][rowid].push_back( igrp ) ; 2229 } 2230 } 2231 } 2232 2233 // print membership 2234 //for ( uInt i = 0 ; i < insize ; i++ ) { 2235 //cout << "Table " << i << endl ; 2236 //for ( uInt j = 0 ; j < groups[i].size() ; j++ ) { 2237 //cout << " FREQ_ID " << setw( 2 ) << freqid[i][j] << ": " ; 2238 //for ( uInt k = 0 ; k < groups[i][j].size() ; k++ ) { 2239 //cout << setw( 2 ) << groups[i][j][k] << " " ; 2240 //} 2241 //cout << endl ; 2242 //} 2243 //} 2244 2245 // set back coordinfo 2246 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2247 vector<string> coordinfo = tmpin[itable]->getCoordInfo() ; 2248 coordinfo[0] = oldinfo[itable] ; 2249 tmpin[itable]->setCoordInfo( coordinfo ) ; 2250 } 2251 2252 // Create additional table if needed 2253 bool oldInsitu = insitu_ ; 2254 setInsitu( false ) ; 2255 vector< vector<uInt> > addrow( insize ) ; 2256 vector<uInt> addtable( insize, 0 ) ; 2257 vector<uInt> newtableids( insize ) ; 2258 vector<uInt> newifids( insize, 0 ) ; 2259 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2260 //cout << "Table " << setw(2) << itable << ": " ; 2261 for ( uInt ifrow = 0 ; ifrow < groups[itable].size() ; ifrow++ ) { 2262 addrow[itable].push_back( groups[itable][ifrow].size()-1 ) ; 2263 //cout << addrow[itable][ifrow] << " " ; 2264 } 2265 addtable[itable] = *max_element( addrow[itable].begin(), addrow[itable].end() ) ; 2266 //cout << "(" << addtable[itable] << ")" << endl ; 2267 } 2268 newin.resize( insize ) ; 2269 copy( tmpin.begin(), tmpin.end(), newin.begin() ) ; 2270 for ( uInt i = 0 ; i < insize ; i++ ) { 2271 newtableids[i] = i ; 2272 } 2273 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2274 for ( uInt iadd = 0 ; iadd < addtable[itable] ; iadd++ ) { 2275 CountedPtr<Scantable> add = getScantable( newin[itable], false ) ; 2276 vector<int> freqidlist ; 2277 for ( uInt i = 0 ; i < groups[itable].size() ; i++ ) { 2278 if ( groups[itable][i].size() > iadd + 1 ) { 2279 freqidlist.push_back( freqid[itable][i] ) ; 2280 } 2281 } 2282 stringstream taqlstream ; 2283 taqlstream << "SELECT FROM $1 WHERE FREQ_ID IN [" ; 2284 for ( uInt i = 0 ; i < freqidlist.size() ; i++ ) { 2285 taqlstream << i ; 2286 if ( i < freqidlist.size() - 1 ) 2287 taqlstream << "," ; 2288 else 2289 taqlstream << "]" ; 2290 } 2291 string taql = taqlstream.str() ; 2292 //cout << "taql = " << taql << endl ; 2293 STSelector selector = STSelector() ; 2294 selector.setTaQL( taql ) ; 2295 add->setSelection( selector ) ; 2296 newin.push_back( add ) ; 2297 newtableids.push_back( itable ) ; 2298 newifids.push_back( iadd + 1 ) ; 2299 } 2300 } 2301 2302 // udpate ifgrp 2303 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2304 for ( uInt iadd = 0 ; iadd < addtable[itable] ; iadd++ ) { 2305 for ( uInt ifrow = 0 ; ifrow < groups[itable].size() ; ifrow++ ) { 2306 if ( groups[itable][ifrow].size() > iadd + 1 ) { 2307 uInt igrp = groups[itable][ifrow][iadd+1] ; 2308 for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) { 2309 if ( ifgrp[igrp][2*imem] == newtableids[iadd+insize] && ifgrp[igrp][2*imem+1] == freqid[newtableids[iadd+insize]][ifrow] ) { 2310 ifgrp[igrp][2*imem] = insize + iadd ; 2311 } 2312 } 2313 } 2314 } 2315 } 2316 } 2317 2318 // print IF groups again for debug 2319 //cout << "IF Group summary: " << endl ; 2320 //cout << " GROUP_ID [FREQ_MIN, FREQ_MAX]: (TABLE_ID, FREQ_ID)" << endl ; 2321 //for ( uInt i = 0 ; i < ifgrp.size() ; i++ ) { 2322 //cout << " GROUP " << setw( 2 ) << i << " [" << ifgfreq[2*i] << "," << ifgfreq[2*i+1] << "]: " ; 2323 //for ( uInt j = 0 ; j < ifgrp[i].size()/2 ; j++ ) { 2324 //cout << "(" << ifgrp[i][2*j] << "," << ifgrp[i][2*j+1] << ") " ; 2325 //} 2326 //cout << endl ; 2327 //} 2328 //cout << endl ; 2329 2330 // reset SCANNO and IF: IF number is reset by the result of sortation 2331 cout << "All scan number is set to 0" << endl ; 2332 //cout << "All IF number is set to IF group index" << endl ; 2333 cout << endl ; 2334 insize = newin.size() ; 2335 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2336 uInt rows = newin[itable]->nrow() ; 2337 Table &tmpt = newin[itable]->table() ; 2338 freqIDCol.attach( tmpt, "FREQ_ID" ) ; 2339 scannoCol.attach( tmpt, "SCANNO" ) ; 2340 ifnoCol.attach( tmpt, "IFNO" ) ; 2341 for ( uInt irow=0 ; irow < rows ; irow++ ) { 2342 scannoCol.put( irow, 0 ) ; 2343 uInt freqID = freqIDCol( irow ) ; 2344 vector<uInt>::iterator iter = find( freqid[newtableids[itable]].begin(), freqid[newtableids[itable]].end(), freqID ) ; 2345 if ( iter != freqid[newtableids[itable]].end() ) { 2346 uInt index = distance( freqid[newtableids[itable]].begin(), iter ) ; 2347 ifnoCol.put( irow, groups[newtableids[itable]][index][newifids[itable]] ) ; 2348 } 2349 else { 2350 throw(AipsError("IF grouping was wrong in additional tables.")) ; 2351 } 2352 } 2353 } 2354 oldinfo.resize( insize ) ; 2355 setInsitu( oldInsitu ) ; 2356 2357 // temporarily set coordinfo 2358 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2359 vector<string> coordinfo = newin[itable]->getCoordInfo() ; 2360 oldinfo[itable] = coordinfo[0] ; 2361 coordinfo[0] = "Hz" ; 2362 newin[itable]->setCoordInfo( coordinfo ) ; 2363 } 2364 2365 // save column values in the vector 2366 vector< vector<uInt> > freqTableIdVec( insize ) ; 2367 vector< vector<uInt> > freqIdVec( insize ) ; 2368 vector< vector<uInt> > ifNoVec( insize ) ; 2369 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2370 ScalarColumn<uInt> freqIDs ; 2371 freqIDs.attach( newin[itable]->frequencies().table(), "ID" ) ; 2372 ifnoCol.attach( newin[itable]->table(), "IFNO" ) ; 2373 freqIDCol.attach( newin[itable]->table(), "FREQ_ID" ) ; 2374 for ( uInt irow = 0 ; irow < newin[itable]->frequencies().table().nrow() ; irow++ ) { 2375 freqTableIdVec[itable].push_back( freqIDs( irow ) ) ; 2376 } 2377 for ( uInt irow = 0 ; irow < newin[itable]->table().nrow() ; irow++ ) { 2378 freqIdVec[itable].push_back( freqIDCol( irow ) ) ; 2379 ifNoVec[itable].push_back( ifnoCol( irow ) ) ; 2380 } 2381 } 2382 2383 // reset spectra and flagtra: pick up common part of frequency coverage 2384 //cout << "Pick common frequency range and align resolution" << endl ; 2385 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2386 uInt rows = newin[itable]->nrow() ; 2387 int nminchan = -1 ; 2388 int nmaxchan = -1 ; 2389 vector<uInt> freqIdUpdate ; 2390 for ( uInt irow = 0 ; irow < rows ; irow++ ) { 2391 uInt ifno = ifNoVec[itable][irow] ; // IFNO is reset by group index 2392 double minfreq = ifgfreq[2*ifno] ; 2393 double maxfreq = ifgfreq[2*ifno+1] ; 2394 //cout << "frequency range: [" << minfreq << "," << maxfreq << "]" << endl ; 2395 vector<double> abcissa = newin[itable]->getAbcissa( irow ) ; 2396 int nchan = abcissa.size() ; 2397 double resol = abcissa[1] - abcissa[0] ; 2398 //cout << "abcissa range : [" << abcissa[0] << "," << abcissa[nchan-1] << "]" << endl ; 2399 if ( minfreq <= abcissa[0] ) 2400 nminchan = 0 ; 2401 else { 2402 //double cfreq = ( minfreq - abcissa[0] ) / resol ; 2403 double cfreq = ( minfreq - abcissa[0] + 0.5 * resol ) / resol ; 2404 nminchan = int(cfreq) + ( ( cfreq - int(cfreq) <= 0.5 ) ? 0 : 1 ) ; 2405 } 2406 if ( maxfreq >= abcissa[abcissa.size()-1] ) 2407 nmaxchan = abcissa.size() - 1 ; 2408 else { 2409 //double cfreq = ( abcissa[abcissa.size()-1] - maxfreq ) / resol ; 2410 double cfreq = ( abcissa[abcissa.size()-1] - maxfreq + 0.5 * resol ) / resol ; 2411 nmaxchan = abcissa.size() - 1 - int(cfreq) - ( ( cfreq - int(cfreq) >= 0.5 ) ? 1 : 0 ) ; 2412 } 2413 //cout << "channel range (" << irow << "): [" << nminchan << "," << nmaxchan << "]" << endl ; 2414 if ( nmaxchan > nminchan ) { 2415 newin[itable]->reshapeSpectrum( nminchan, nmaxchan, irow ) ; 2416 int newchan = nmaxchan - nminchan + 1 ; 2417 if ( count( freqIdUpdate.begin(), freqIdUpdate.end(), freqIdVec[itable][irow] ) == 0 && newchan < nchan ) 2418 freqIdUpdate.push_back( freqIdVec[itable][irow] ) ; 2419 } 2420 else { 2421 throw(AipsError("Failed to pick up common part of frequency range.")) ; 2422 } 2423 } 2424 for ( uInt i = 0 ; i < freqIdUpdate.size() ; i++ ) { 2425 uInt freqId = freqIdUpdate[i] ; 2426 Double refpix ; 2427 Double refval ; 2428 Double increment ; 2429 2430 // update row 2431 newin[itable]->frequencies().getEntry( refpix, refval, increment, freqId ) ; 2432 refval = refval - ( refpix - nminchan ) * increment ; 2433 refpix = 0 ; 2434 newin[itable]->frequencies().setEntry( refpix, refval, increment, freqId ) ; 2435 } 2436 } 2437 2438 2439 // reset spectra and flagtra: align spectral resolution 2440 //cout << "Align spectral resolution" << endl ; 2441 vector<double> gmaxdnu( freqgrp.size(), 0.0 ) ; 2442 vector<uInt> gmemid( freqgrp.size(), 0 ) ; 2443 for ( uInt igrp = 0 ; igrp < ifgrp.size() ; igrp++ ) { 2444 double maxdnu = 0.0 ; // maximum (coarsest) frequency resolution 2445 int minchan = INT_MAX ; // minimum channel number 2446 Double refpixref = -1 ; // reference of 'reference pixel' 2447 Double refvalref = -1 ; // reference of 'reference frequency' 2448 Double refinc = -1 ; // reference frequency resolution 2449 uInt refreqid ; 2450 uInt reftable = INT_MAX; 2451 // process only if group member > 1 2452 if ( ifgrp[igrp].size() > 2 ) { 2453 // find minchan and maxdnu in each group 2454 for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) { 2455 uInt tableid = ifgrp[igrp][2*imem] ; 2456 uInt rowid = ifgrp[igrp][2*imem+1] ; 2457 vector<uInt>::iterator iter = find( freqIdVec[tableid].begin(), freqIdVec[tableid].end(), rowid ) ; 2458 if ( iter != freqIdVec[tableid].end() ) { 2459 uInt index = distance( freqIdVec[tableid].begin(), iter ) ; 2460 vector<double> abcissa = newin[tableid]->getAbcissa( index ) ; 2461 int nchan = abcissa.size() ; 2462 double dnu = abcissa[1] - abcissa[0] ; 2463 //cout << "GROUP " << igrp << " (" << tableid << "," << rowid << "): nchan = " << nchan << " (minchan = " << minchan << ")" << endl ; 2464 if ( nchan < minchan ) { 2465 minchan = nchan ; 2466 maxdnu = dnu ; 2467 newin[tableid]->frequencies().getEntry( refpixref, refvalref, refinc, rowid ) ; 2468 refreqid = rowid ; 2469 reftable = tableid ; 2470 } 2471 } 2472 } 2473 // regrid spectra in each group 2474 cout << "GROUP " << igrp << endl ; 2475 cout << " Channel number is adjusted to " << minchan << endl ; 2476 cout << " Corresponding frequency resolution is " << maxdnu << "Hz" << endl ; 2477 for ( uInt imem = 0 ; imem < ifgrp[igrp].size()/2 ; imem++ ) { 2478 uInt tableid = ifgrp[igrp][2*imem] ; 2479 uInt rowid = ifgrp[igrp][2*imem+1] ; 2480 freqIDCol.attach( newin[tableid]->table(), "FREQ_ID" ) ; 2481 //cout << "tableid = " << tableid << " rowid = " << rowid << ": " << endl ; 2482 //cout << " regridChannel applied to " ; 2483 if ( tableid != reftable ) 2484 refreqid = newin[tableid]->frequencies().addEntry( refpixref, refvalref, refinc ) ; 2485 for ( uInt irow = 0 ; irow < newin[tableid]->table().nrow() ; irow++ ) { 2486 uInt tfreqid = freqIdVec[tableid][irow] ; 2487 if ( tfreqid == rowid ) { 2488 //cout << irow << " " ; 2489 newin[tableid]->regridChannel( minchan, maxdnu, irow ) ; 2490 freqIDCol.put( irow, refreqid ) ; 2491 freqIdVec[tableid][irow] = refreqid ; 2492 } 2493 } 2494 //cout << endl ; 2495 } 2496 } 2497 else { 2498 uInt tableid = ifgrp[igrp][0] ; 2499 uInt rowid = ifgrp[igrp][1] ; 2500 vector<uInt>::iterator iter = find( freqIdVec[tableid].begin(), freqIdVec[tableid].end(), rowid ) ; 2501 if ( iter != freqIdVec[tableid].end() ) { 2502 uInt index = distance( freqIdVec[tableid].begin(), iter ) ; 2503 vector<double> abcissa = newin[tableid]->getAbcissa( index ) ; 2504 minchan = abcissa.size() ; 2505 maxdnu = abcissa[1] - abcissa[0] ; 2506 } 2507 } 2508 for ( uInt i = 0 ; i < freqgrp.size() ; i++ ) { 2509 if ( count( freqgrp[i].begin(), freqgrp[i].end(), igrp ) > 0 ) { 2510 if ( maxdnu > gmaxdnu[i] ) { 2511 gmaxdnu[i] = maxdnu ; 2512 gmemid[i] = igrp ; 2513 } 2514 break ; 2515 } 2516 } 2517 } 2518 cout << endl ; 2519 2520 // set back coordinfo 2521 for ( uInt itable = 0 ; itable < insize ; itable++ ) { 2522 vector<string> coordinfo = newin[itable]->getCoordInfo() ; 2523 coordinfo[0] = oldinfo[itable] ; 2524 newin[itable]->setCoordInfo( coordinfo ) ; 2525 } 2526 2527 // accumulate all rows into the first table 2528 // NOTE: assumed in.size() = 1 2529 vector< CountedPtr<Scantable> > tmp( 1 ) ; 2530 if ( newin.size() == 1 ) 2531 tmp[0] = newin[0] ; 2532 else 2533 tmp[0] = merge( newin ) ; 2534 2535 //return tmp[0] ; 2536 2537 // average 2538 CountedPtr<Scantable> tmpout = average( tmp, mask, weight, avmode ) ; 2539 2540 //return tmpout ; 2541 2542 // combine frequency group 2543 cout << "Combine spectra based on frequency grouping" << endl ; 2544 cout << "IFNO is renumbered as frequency group ID (see above)" << endl ; 2545 vector<string> coordinfo = tmpout->getCoordInfo() ; 2546 oldinfo[0] = coordinfo[0] ; 2547 coordinfo[0] = "Hz" ; 2548 tmpout->setCoordInfo( coordinfo ) ; 2549 // create proformas of output table 2550 stringstream taqlstream ; 2551 taqlstream << "SELECT FROM $1 WHERE IFNO IN [" ; 2552 for ( uInt i = 0 ; i < gmemid.size() ; i++ ) { 2553 taqlstream << gmemid[i] ; 2554 if ( i < gmemid.size() - 1 ) 2555 taqlstream << "," ; 2556 else 2557 taqlstream << "]" ; 2558 } 2559 string taql = taqlstream.str() ; 2560 //cout << "taql = " << taql << endl ; 2561 STSelector selector = STSelector() ; 2562 selector.setTaQL( taql ) ; 2563 oldInsitu = insitu_ ; 2564 setInsitu( false ) ; 2565 out = getScantable( tmpout, false ) ; 2566 setInsitu( oldInsitu ) ; 2567 out->setSelection( selector ) ; 2568 // regrid rows 2569 ifnoCol.attach( tmpout->table(), "IFNO" ) ; 2570 for ( uInt irow = 0 ; irow < tmpout->table().nrow() ; irow++ ) { 2571 uInt ifno = ifnoCol( irow ) ; 2572 for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) { 2573 if ( count( freqgrp[igrp].begin(), freqgrp[igrp].end(), ifno ) > 0 ) { 2574 vector<double> abcissa = tmpout->getAbcissa( irow ) ; 2575 double bw = ( abcissa[1] - abcissa[0] ) * abcissa.size() ; 2576 int nchan = (int)( bw / gmaxdnu[igrp] ) ; 2577 tmpout->regridChannel( nchan, gmaxdnu[igrp], irow ) ; 2578 break ; 2579 } 2580 } 2581 } 2582 // combine spectra 2583 ArrayColumn<Float> specColOut ; 2584 specColOut.attach( out->table(), "SPECTRA" ) ; 2585 ArrayColumn<uChar> flagColOut ; 2586 flagColOut.attach( out->table(), "FLAGTRA" ) ; 2587 ScalarColumn<uInt> ifnoColOut ; 2588 ifnoColOut.attach( out->table(), "IFNO" ) ; 2589 ScalarColumn<uInt> polnoColOut ; 2590 polnoColOut.attach( out->table(), "POLNO" ) ; 2591 ScalarColumn<uInt> freqidColOut ; 2592 freqidColOut.attach( out->table(), "FREQ_ID" ) ; 2593 Table &tab = tmpout->table() ; 2594 TableIterator iter( tab, "POLNO" ) ; 2595 vector< vector<uInt> > sizes( freqgrp.size() ) ; 2596 while( !iter.pastEnd() ) { 2597 vector< vector<Float> > specout( freqgrp.size() ) ; 2598 vector< vector<uChar> > flagout( freqgrp.size() ) ; 2599 ArrayColumn<Float> specCols ; 2600 specCols.attach( iter.table(), "SPECTRA" ) ; 2601 ArrayColumn<uChar> flagCols ; 2602 flagCols.attach( iter.table(), "FLAGTRA" ) ; 2603 ifnoCol.attach( iter.table(), "IFNO" ) ; 2604 ScalarColumn<uInt> polnos ; 2605 polnos.attach( iter.table(), "POLNO" ) ; 2606 uInt polno = polnos( 0 ) ; 2607 //cout << "POLNO iteration: " << polno << endl ; 2608 for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) { 2609 sizes[igrp].resize( freqgrp[igrp].size() ) ; 2610 for ( uInt imem = 0 ; imem < freqgrp[igrp].size() ; imem++ ) { 2611 for ( uInt irow = 0 ; irow < iter.table().nrow() ; irow++ ) { 2612 uInt ifno = ifnoCol( irow ) ; 2613 if ( ifno == freqgrp[igrp][imem] ) { 2614 Vector<Float> spec = specCols( irow ) ; 2615 Vector<uChar> flag = flagCols( irow ) ; 2616 vector<Float> svec ; 2617 spec.tovector( svec ) ; 2618 vector<uChar> fvec ; 2619 flag.tovector( fvec ) ; 2620 //cout << "spec.size() = " << svec.size() << " fvec.size() = " << fvec.size() << endl ; 2621 specout[igrp].insert( specout[igrp].end(), svec.begin(), svec.end() ) ; 2622 flagout[igrp].insert( flagout[igrp].end(), fvec.begin(), fvec.end() ) ; 2623 //cout << "specout[" << igrp << "].size() = " << specout[igrp].size() << endl ; 2624 sizes[igrp][imem] = spec.nelements() ; 2625 } 2626 } 2627 } 2628 for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) { 2629 uInt ifout = ifnoColOut( irow ) ; 2630 uInt polout = polnoColOut( irow ) ; 2631 if ( ifout == gmemid[igrp] && polout == polno ) { 2632 // set SPECTRA and FRAGTRA 2633 Vector<Float> newspec( specout[igrp] ) ; 2634 Vector<uChar> newflag( flagout[igrp] ) ; 2635 specColOut.put( irow, newspec ) ; 2636 flagColOut.put( irow, newflag ) ; 2637 // IFNO renumbering 2638 ifnoColOut.put( irow, igrp ) ; 2639 } 2640 } 2641 } 2642 iter++ ; 2643 } 2644 // update FREQUENCIES subtable 2645 vector<bool> updated( freqgrp.size(), false ) ; 2646 for ( uInt igrp = 0 ; igrp < freqgrp.size() ; igrp++ ) { 2647 uInt index = 0 ; 2648 uInt pixShift = 0 ; 2649 while ( freqgrp[igrp][index] != gmemid[igrp] ) { 2650 pixShift += sizes[igrp][index++] ; 2651 } 2652 for ( uInt irow = 0 ; irow < out->table().nrow() ; irow++ ) { 2653 if ( ifnoColOut( irow ) == gmemid[igrp] && !updated[igrp] ) { 2654 uInt freqidOut = freqidColOut( irow ) ; 2655 //cout << "freqgrp " << igrp << " freqidOut = " << freqidOut << endl ; 2656 double refpix ; 2657 double refval ; 2658 double increm ; 2659 out->frequencies().getEntry( refpix, refval, increm, freqidOut ) ; 2660 refpix += pixShift ; 2661 out->frequencies().setEntry( refpix, refval, increm, freqidOut ) ; 2662 updated[igrp] = true ; 2663 } 2664 } 2665 } 2666 2667 //out = tmpout ; 2668 2669 coordinfo = tmpout->getCoordInfo() ; 2670 coordinfo[0] = oldinfo[0] ; 2671 tmpout->setCoordInfo( coordinfo ) ; 2672 } 2673 else { 2674 // simple average 2675 out = average( in, mask, weight, avmode ) ; 2676 } 2677 2678 return out ; 2679 }
Note: See TracChangeset
for help on using the changeset viewer.