void smurf_sc2fft( int *status ) { int avpspec=0; /* Flag for doing average power spectrum */ double avpspecthresh=0; /* Threshold noise for detectors in avpspec */ Grp * basegrp = NULL; /* Basis group for output filenames */ smfArray *bbms = NULL; /* Bad bolometer masks */ smfArray *concat=NULL; /* Pointer to a smfArray */ size_t contchunk; /* Continuous chunk counter */ smfArray *darks = NULL; /* dark frames */ int ensureflat; /* Flag for flatfielding data */ Grp *fgrp = NULL; /* Filtered group, no darks */ smfArray *flatramps = NULL;/* Flatfield ramps */ AstKeyMap *heateffmap = NULL; /* Heater efficiency data */ size_t gcount=0; /* Grp index counter */ size_t i; /* Loop counter */ smfGroup *igroup=NULL; /* smfGroup corresponding to igrp */ Grp *igrp = NULL; /* Input group of files */ int inverse=0; /* If set perform inverse transform */ int isfft=0; /* Are data fft or real space? */ dim_t maxconcat=0; /* Longest continuous chunk length in samples */ size_t ncontchunks=0; /* Number continuous chunks outside iter loop */ smfData *odata=NULL; /* Pointer to output smfData to be exported */ Grp *ogrp = NULL; /* Output group of files */ size_t outsize; /* Total number of NDF names in the output group */ int polar=0; /* Flag for FFT in polar coordinates */ int power=0; /* Flag for squaring amplitude coeffs */ size_t size; /* Number of files in input group */ smfData *tempdata=NULL; /* Temporary smfData pointer */ int weightavpspec=0; /* Flag for 1/noise^2 weighting */ ThrWorkForce *wf = NULL; /* Pointer to a pool of worker threads */ int zerobad; /* Zero VAL__BADD before taking FFT? */ /* Main routine */ ndfBegin(); /* Find the number of cores/processors available and create a pool of threads of the same size. */ wf = thrGetWorkforce( thrGetNThread( SMF__THREADS, status ), status ); /* Get input file(s) */ kpg1Rgndf( "IN", 0, 1, "", &igrp, &size, status ); /* Filter out darks */ smf_find_science( igrp, &fgrp, 1, NULL, NULL, 1, 1, SMF__NULL, &darks, &flatramps, &heateffmap, NULL, status ); /* input group is now the filtered group so we can use that and free the old input group */ size = grpGrpsz( fgrp, status ); grpDelet( &igrp, status); igrp = fgrp; fgrp = NULL; /* We now need to combine files from the same subarray and same sequence to form a continuous time series */ smf_grp_related( igrp, size, 1, 0, 0, NULL, NULL, &maxconcat, NULL, &igroup, &basegrp, NULL, status ); /* Get output file(s) */ size = grpGrpsz( basegrp, status ); if( size > 0 ) { kpg1Wgndf( "OUT", basegrp, size, size, "More output files required...", &ogrp, &outsize, status ); } else { msgOutif(MSG__NORM, " ", TASK_NAME ": All supplied input frames were DARK," " nothing to do", status ); } /* Get group of bolometer masks and read them into a smfArray */ smf_request_mask( "BBM", &bbms, status ); /* Obtain the number of continuous chunks and subarrays */ if( *status == SAI__OK ) { ncontchunks = igroup->chunk[igroup->ngroups-1]+1; } msgOutiff( MSG__NORM, "", "Found %zu continuous chunk%s", status, ncontchunks, (ncontchunks > 1 ? "s" : "") ); /* Are we flatfielding? */ parGet0l( "FLAT", &ensureflat, status ); /* Are we doing an inverse transform? */ parGet0l( "INVERSE", &inverse, status ); /* Are we using polar coordinates instead of cartesian for the FFT? */ parGet0l( "POLAR", &polar, status ); /* Are we going to assume amplitudes are squared? */ parGet0l( "POWER", &power, status ); /* Are we going to zero bad values first? */ parGet0l( "ZEROBAD", &zerobad, status ); /* Are we calculating the average power spectrum? */ parGet0l( "AVPSPEC", &avpspec, status ); if( avpspec ) { power = 1; parGet0d( "AVPSPECTHRESH", &avpspecthresh, status ); parGet0l( "WEIGHTAVPSPEC", &weightavpspec, status ); } /* If power is true, we must be in polar form */ if( power && !polar) { msgOutif( MSG__NORM, " ", TASK_NAME ": power spectrum requested so setting POLAR=TRUE", status ); polar = 1; } gcount = 1; for( contchunk=0;(*status==SAI__OK)&&contchunk<ncontchunks; contchunk++ ) { size_t idx; /* Concatenate this continuous chunk but forcing a raw data read. We will need quality. */ smf_concat_smfGroup( wf, NULL, igroup, darks, NULL, flatramps, heateffmap, contchunk, ensureflat, 1, NULL, 0, NULL, NULL, 0, 0, 0, &concat, NULL, status ); /* Now loop over each subarray */ /* Export concatenated data for each subarray to NDF file */ for( idx=0; (*status==SAI__OK)&&idx<concat->ndat; idx++ ) { if( concat->sdata[idx] ) { smfData * idata = concat->sdata[idx]; int provid = NDF__NOID; dim_t nbolo; /* Number of detectors */ dim_t ndata; /* Number of data points */ /* Apply a mask to the quality array and data array */ smf_apply_mask( idata, bbms, SMF__BBM_QUAL|SMF__BBM_DATA, 0, status ); smf_get_dims( idata, NULL, NULL, &nbolo, NULL, &ndata, NULL, NULL, status ); /* Check for double precision data */ if( idata->dtype != SMF__DOUBLE ) { *status = SAI__ERROR; errRep( "", FUNC_NAME ": data are not double precision.", status ); } /* Are we zeroing VAL__BADD? */ if( (*status==SAI__OK) && zerobad ) { double *data= (double *) idata->pntr[0]; for( i=0; i<ndata; i++ ) { if( data[i] == VAL__BADD ) { data[i] = 0; } } } /* Check whether we need to transform the data at all */ isfft = smf_isfft(idata,NULL,NULL,NULL,NULL,NULL,status); if( isfft && avpspec && (*status == SAI__OK) ) { *status = SAI__ERROR; errRep( "", FUNC_NAME ": to calculate average power spectrum input data cannot " "be FFT", status ); } if( (*status == SAI__OK) && (isfft == inverse) ) { if( avpspec ) { /* If calculating average power spectrum do the transforms with smf_bolonoise so that we can also measure the noise of each detector */ double *whitenoise=NULL; smf_qual_t *bolomask=NULL; double mean, sig, freqlo; size_t ngood, newgood; whitenoise = astCalloc( nbolo, sizeof(*whitenoise) ); bolomask = astCalloc( nbolo, sizeof(*bolomask) ); freqlo = 1. / (idata->hdr->steptime * idata->hdr->nframes); smf_bolonoise( wf, idata, 1, freqlo, SMF__F_WHITELO, SMF__F_WHITEHI, 1, 0, whitenoise, NULL, &odata, status ); /* Initialize quality */ for( i=0; i<nbolo; i++ ) { if( whitenoise[i] == VAL__BADD ) { bolomask[i] = SMF__Q_BADB; } else { /* smf_bolonoise returns a variance, so take sqrt */ whitenoise[i] = sqrt(whitenoise[i]); } } ngood=-1; newgood=0; /* Iteratively cut n-sigma noisy outlier detectors */ while( ngood != newgood ) { ngood = newgood; smf_stats1D( whitenoise, 1, nbolo, bolomask, 1, SMF__Q_BADB, &mean, &sig, NULL, NULL, status ); msgOutiff( MSG__DEBUG, "", TASK_NAME ": mean=%lf sig=%lf ngood=%li\n", status, mean, sig, ngood); newgood=0; for( i=0; i<nbolo; i++ ) { if( whitenoise[i] != VAL__BADD ){ if( (whitenoise[i] - mean) > avpspecthresh *sig ) { whitenoise[i] = VAL__BADD; bolomask[i] = SMF__Q_BADB; } else { newgood++; } } } } msgOutf( "", TASK_NAME ": Calculating average power spectrum of best %li " " bolometers.", status, newgood); /* If using 1/noise^2 weights, calculate 1/whitenoise^2 in-place to avoid allocating another array */ if( weightavpspec ) { msgOutif( MSG__VERB, "", TASK_NAME ": using 1/noise^2 weights", status ); for( i=0; i<nbolo; i++ ) { if( whitenoise[i] && (whitenoise[i] != VAL__BADD) ) { whitenoise[i] = 1/(whitenoise[i]*whitenoise[i]); } } } /* Calculate the average power spectrum of good detectors */ tempdata = smf_fft_avpspec( odata, bolomask, 1, SMF__Q_BADB, weightavpspec ? whitenoise : NULL, status ); smf_close_file( &odata, status ); whitenoise = astFree( whitenoise ); bolomask = astFree( bolomask ); odata = tempdata; tempdata = NULL; /* Store the number of good bolometers */ parPut0i( "NGOOD", newgood, status ); } else { /* Otherwise do forward/inverse transforms here as needed */ /* If inverse transform convert to cartesian representation first */ if( inverse && polar ) { smf_fft_cart2pol( wf, idata, 1, power, status ); } /* Tranform the data */ odata = smf_fft_data( wf, idata, NULL, inverse, 0, status ); smf_convert_bad( wf, odata, status ); if( inverse ) { /* If output is time-domain, ensure that it is ICD bolo-ordered */ smf_dataOrder( odata, 1, status ); } else if( polar ) { /* Store FFT of data in polar form */ smf_fft_cart2pol( wf, odata, 0, power, status ); } } /* open a reference input file for provenance propagation */ ndgNdfas( basegrp, gcount, "READ", &provid, status ); /* Export the data to a new file */ smf_write_smfData( odata, NULL, NULL, ogrp, gcount, provid, MSG__VERB, 0, status ); /* Free resources */ ndfAnnul( &provid, status ); smf_close_file( &odata, status ); } else { msgOutif( MSG__NORM, " ", "Data are already transformed. No output will be produced", status ); } } /* Update index into group */ gcount++; } /* Close the smfArray */ smf_close_related( &concat, status ); } /* Write out the list of output NDF names, annulling the error if a null parameter value is supplied. */ if( *status == SAI__OK ) { grpList( "OUTFILES", 0, 0, NULL, ogrp, status ); if( *status == PAR__NULL ) errAnnul( status ); } /* Tidy up after ourselves: release the resources used by the grp routines */ grpDelet( &igrp, status); grpDelet( &ogrp, status); if (basegrp) grpDelet( &basegrp, status ); if( igroup ) smf_close_smfGroup( &igroup, status ); if( flatramps ) smf_close_related( &flatramps, status ); if (heateffmap) heateffmap = smf_free_effmap( heateffmap, status ); if (bbms) smf_close_related( &bbms, status ); ndfEnd( status ); /* Ensure that FFTW doesn't have any used memory kicking around */ fftw_cleanup(); }
/* Main entry */ void smurf_fixsteps( int *status ) { /* Local Variables */ AstKeyMap *keymap; /* Default config parameter values */ AstKeyMap *sub_instruments; /* Info about sub-instruments */ FILE *fd = NULL; /* File descriptor */ Grp *igrp = NULL; /* Input group of files */ Grp *ogrp = NULL; /* Output group of files */ dim_t dcfitbox; /* DCFITBOX config parameter */ dim_t dcsmooth; /* DCSMOOTH config parameter */ dim_t nx; /* Length of first pixel axis */ double dcthresh; /* DCTHRESH config parameter */ double sizetol; /* Tolerance allowed on step height */ int changed; /* Have any step fixes changed? */ int dclimcorr; /* DCLIMCORR config parameter */ int dcmaxsteps; /* DCMAXSTEPS config parameter */ int first; /* Index of first change to report */ int itemp; /* Intermediate value */ int meanshift; /* Use a mean shift filter? */ int nnew; /* Number of new step fixes */ int nold; /* Number of old step fixes */ size_t nrej; /* Number of rejected bolometers */ size_t outsize; /* Total number of NDF names in the output group */ size_t size; /* Number of files in input group */ smfData *data = NULL; /* Output smfData */ smfData *indata = NULL; /* Input smfData */ smfStepFix *newsteps = NULL; /* New step fix descriptions */ smfStepFix *oldsteps = NULL; /* Old step fix descriptions */ ThrWorkForce *wf = NULL; /* Pointer to a pool of worker threads */ /* Check inherited status */ if (*status != SAI__OK) return; /* begin an NDF context. */ ndfBegin(); /* Get the name of the input NDF. */ kpg1Rgndf( "IN", 1, 1, "", &igrp, &size, status ); /* Get output file(s) */ kpg1Wgndf( "OUT", igrp, size, 0, "More output files required...", &ogrp, &outsize, status ); /* Open the input data file, read-only. */ smf_open_file( igrp, 1, "Read", 0, &indata, status ); /* Since we will be modifying the data values, we need a deep copy. */ data = smf_deepcopy_smfData( indata, 0, 0, 0, 0, status ); /* Place cleaning parameters into a keymap and set defaults. Note that we use the map-maker defaults file here so that we populate the locked keymap with all the parameters that people may come across to allow them to load their map-maker config directly this application. */ sub_instruments = smf_subinst_keymap( SMF__SUBINST_NONE, data, NULL, 0, status ); keymap = kpg1Config( "CONFIG", "$SMURF_DIR/smurf_makemap.def", sub_instruments, status ); sub_instruments = astAnnul( sub_instruments ); /* Set the default for each of the step fixing config parameters. */ astMapGet0I( keymap, "DCSMOOTH", &itemp ); parDef0i( "DCSMOOTH", itemp, status ); astMapGet0I( keymap, "DCFITBOX", &itemp ); parDef0i( "DCFITBOX", itemp, status ); astMapGet0I( keymap, "DCMAXSTEPS", &itemp ); parDef0i( "DCMAXSTEPS", itemp, status ); astMapGet0I( keymap, "DCLIMCORR", &itemp ); parDef0i( "DCLIMCORR", itemp, status ); astMapGet0D( keymap, "DCTHRESH", &dcthresh ); parDef0d( "DCTHRESH", dcthresh, status ); /* Get values for the config params */ parGet0i( "DCSMOOTH", &itemp, status ); dcsmooth = itemp; parGet0i( "DCFITBOX", &itemp, status ); dcfitbox = itemp; parGet0i( "DCMAXSTEPS", &itemp, status ); dcmaxsteps = itemp; parGet0i( "DCLIMCORR", &itemp, status ); dclimcorr = itemp; parGet0d( "DCTHRESH", &dcthresh, status ); parGet0l( "MEANSHIFT", &meanshift, status ); /* Find the number of cores/processors available and create a pool of threads of the same size. */ wf = thrGetWorkforce( thrGetNThread( SMF__THREADS, status ), status ); /* Fix the steps. */ smf_fix_steps( wf, data, dcthresh, dcsmooth, dcfitbox, dcmaxsteps, dclimcorr, meanshift, &nrej, &newsteps, &nnew, status ); /* Display a summary of what was done by the step fixer. */ msgBlank( status ); if( nrej == 0 ) { msgOut( "", "No bolometers were rejected", status ); } else if( nrej == 1 ) { msgOut( "", "One bolometer was rejected", status ); } else { msgSeti( "NREJ", nrej ); msgOut( "", "^NREJ bolometers were rejected", status ); } parPut0i( "NREJECTED", nrej, status ); if( nnew == 0 ) { msgOut( "", "No steps were fixed", status ); } else if( nnew == 1 ) { msgOut( "", "One step was fixed", status ); } else { msgSeti( "NNEW", nnew ); msgOut( "", "^NNEW steps were fixed", status ); } parPut0i( "NFIXED", nnew, status ); /* If required, write out to a text file details of the steps that were fixed. */ fd = smf_open_textfile( "NEWSTEPS", "w", "<none>", status ); if( fd ) { smf1_write_steps( fd, indata, nnew, newsteps, dcthresh, dcsmooth, dcfitbox, dcmaxsteps, dclimcorr, nrej, status ); fclose( fd ); } /* If required, create the output NDF. */ if( outsize > 0 && indata && indata->file ) { smf_write_smfData( data, NULL, NULL, ogrp, 1, indata->file->ndfid, MSG__VERB, 0, status ); } /* Save the length of the first pixel axis. */ nx = data ? data->dims[ 0 ] : 0; /* Close the NDFs. */ smf_close_file( &data, status ); smf_close_file( &indata, status ); /* Attempt to open a file containing descriptions of steps fixed by a previous invocation of this program. */ fd = smf_open_textfile( "OLDSTEPS", "r", "<none>", status ); if( fd ) { /* Get SIZETOL - the minimum significant fractional error in step sizes. */ parGet0d( "SIZETOL", &sizetol, status ); /* Read the contents of the file, issuing a warning if the global properties read from the file (e.g. parameters used, no. of steps found, etc) differ from those of the current invocation. */ msgBlank( status ); oldsteps = smf1_read_steps( fd, dcthresh, dcsmooth, dcfitbox, dcmaxsteps, dclimcorr, nrej, nnew, &nold, status ); /* Get the index of the first change to report. */ parGet0i( "FIRST", &first, status ); /* Compare the new step fixes with the old step fixes, issuing a warning for the first step fix that has changed. */ changed = smf1_check_steps( "CONTINUE", first, nx, sizetol, nold, nnew, oldsteps, newsteps, status ); /* Store a flag indicating if any sstep fixes have chnaged. */ parPut0l( "CHANGED", changed, status ); /* Tell the user if nothing has changed. */ if( ! changed ) { msgOut( "", "There are no significant differences " "between old and new step fixes.", status ); } msgBlank( status ); /* Close the old steps file, and free the memory holding the old step descriptions. */ fclose( fd ); oldsteps = astFree( oldsteps ); } /* Free resources. */ newsteps = astFree( newsteps ); grpDelet( &igrp, status ); grpDelet( &ogrp, status ); /* End the NDF context. */ ndfEnd( status ); /* If anything went wrong issue a context message. */ if( *status != SAI__OK ) msgOutif( MSG__VERB, " ", "FIXSTEPS failed.", status ); }
/* Main entry */ void smurf_jsadicer( int *status ) { /* Local Variables */ AstFitsChan *fc; Grp *igrp = NULL; Grp *ogrp = NULL; char *pname; char basename[ 255 ]; int indf; int trim; size_t ntile; size_t size; smfJSATiling tiling; /* Check inherited status */ if (*status != SAI__OK) return; /* Begin AST and NDF contexts. */ astBegin; ndfBegin(); /* Get the name of the input NDF. */ kpg1Rgndf( "IN", 1, 1, "", &igrp, &size, status ); ndgNdfas( igrp, 1, "READ", &indf, status ); /* Get the base name for the output NDFs. */ if( *status == SAI__OK ) { parGet0c( "OUT", basename, sizeof(basename), status ); if( *status == PAR__NULL ) { errAnnul( status ); pname = basename; grpGet( igrp, 1, 1, &pname, sizeof(basename), status ); } } /* See how the output NDFs are to be trimmed. */ parGet0i( "TRIM", &trim, status ); /* Get a FitsChan holding the contents of the FITS extension from the input NDF. Annul the error if the NDF has no FITS extension. */ if( *status == SAI__OK ) { kpgGtfts( indf, &fc, status ); if( *status == KPG__NOFTS ) { errAnnul( status ); fc = NULL; } } /* Select a JSA instrument and get the parameters defining the layout of tiles for the selected instrument. */ smf_jsainstrument( "INSTRUMENT", fc, SMF__INST_NONE, &tiling, status ); /* Create a new group to hold the names of the output NDFs that have been created. This group does not include any NDFs that correspond to tiles that contain no input data. */ ogrp = grpNew( "", status ); /* Dice the map into output NDFs. */ smf_jsadicer( indf, basename, trim, tiling.instrument, &ntile, ogrp, status ); /* Write out the list of output NDF names, annulling the error if a null parameter value is supplied. */ if( *status == SAI__OK && ogrp ) { grpList( "OUTFILES", 0, 0, NULL, ogrp, status ); if( *status == PAR__NULL ) errAnnul( status ); } /* Write the number of tiles being created to an output parameter. */ parPut0i( "NTILE", ntile, status ); /* Free resources. */ grpDelet( &igrp, status ); grpDelet( &ogrp, status ); /* End the NDF and AST context. */ ndfEnd( status ); astEnd; /* If anything went wrong issue a context message. */ if( *status != SAI__OK ) msgOutif( MSG__VERB, " ", "JSADICER failed.", status ); }
void smurf_jsatileinfo( int *status ) { /* Local Variables */ AstCmpRegion *overlap; AstFitsChan *fc; AstFrameSet *fs; AstObject *obj; AstRegion *region; AstRegion *target; HDSLoc *cloc = NULL; HDSLoc *xloc = NULL; char *jcmt_tiles; char *tilendf = NULL; char text[ 200 ]; double dec[ 8 ]; double dist; double dlbnd[ 2 ]; double dubnd[ 2 ]; double gx[ 8 ]; double gy[ 8 ]; double maxdist; double norm_radec[2]; double point1[ 2 ]; double point2[ 2 ]; double ra[ 8 ]; double ra0; double dec0; int *ipntr; int axes[ 2 ]; int create; int dirlen; int el; int exists; int flag; int i; int indf1; int indf2; int indf3; int itile; int iv; int jtile; int lbnd[ 2 ]; int local_origin; int nc; int place; int tlbnd[ 2 ]; int tubnd[ 2 ]; int ubnd[ 2 ]; smf_jsaproj_t proj; int xt; int yt; smfJSATiling skytiling; void *pntr; /* Check inherited status */ if( *status != SAI__OK ) return; /* Start a new AST context. */ astBegin; /* Get the instrument to use abnd get the parameters describing the layout of its JSA tiles. */ smf_jsainstrument( "INSTRUMENT", NULL, SMF__INST_NONE, &skytiling, status ); /* Return the maximum tile index. */ parPut0i( "MAXTILE", skytiling.ntiles - 1, status ); /* Abort if an error has occurred. */ if( *status != SAI__OK ) goto L999; /* Decide what sort of projection to use. */ parChoic( "PROJ", "HPX", "HPX,HPX12,XPHN,XPHS", 1, text, sizeof(text), status ); proj = smf_jsaproj_fromstr( text, 1, status ); /* If required, create an all-sky NDF in which each pixel covers the area of a single tile, and holds the integer tile index. The NDF has an initial size of 1x1 pixels, but is expanded later to the required size. */ lbnd[ 0 ] = ubnd[ 0 ] = lbnd[ 1 ] = ubnd[ 1 ] = 1; ndfCreat( "ALLSKY", "_INTEGER", 2, lbnd, ubnd, &indf3, status ); /* If a null (!) value was supplied for parameter ALLSKY, annull the error and pass on. */ if( *status == PAR__NULL ) { errAnnul( status ); /* Otherwise, create a FrameSet describing the whole sky in which each pixel corresponds to a single tile. */ } else { smf_jsatile( -1, &skytiling, 0, proj, NULL, &fs, NULL, lbnd, ubnd, status ); /* Change the bounds of the output NDF. */ ndfSbnd( 2, lbnd, ubnd, indf3, status ); /* Store the FrameSet in the NDF. */ ndfPtwcs( fs, indf3, status ); /* Map the data array. */ ndfMap( indf3, "Data", "_INTEGER", "WRITE/BAD", (void **) &ipntr, &el, status ); /* Create all-sky map using XPH projection. */ if( *status == SAI__OK ) { /* Loop round every tile index. */ for( jtile = 0; jtile < skytiling.ntiles; jtile++ ) { /* Get the zero-based (x,y) indices of the tile within an HPX projection. This flips the bottom left half-facet up to the top right. */ smf_jsatilei2xy( jtile, &skytiling, &xt, &yt, NULL, status ); /* Convert these HPX indices to the corresponding indices within the required projection. Note, the lower left facet is split by the above call to smf_jsatilei2xy tile (i.e. (xt,yt) indices are *not* in the "raw" mode). For instance, (0,0) is not a valid tile. */ smf_jsatilexyconv( &skytiling, proj, xt, yt, 0, &xt, &yt, status ); /* Get the vector index of the corresponding element of the all-sky NDF. */ if( proj == SMF__JSA_HPX || proj == SMF__JSA_HPX12 ) { iv = xt + 5*skytiling.ntpf*yt; } else { iv = xt + 4*skytiling.ntpf*yt; } /* Report an error if this element has already been assigned a tile index. Otherwise, store the tile index. */ if( ipntr[ iv ] == VAL__BADI ) { ipntr[ iv ] = jtile; } else if( *status == SAI__OK ) { *status = SAI__ERROR; errRepf( "", "%s projection assigns multiple tiles to " "pixel (%d,%d).", status, text, xt, yt ); break; } } } /* Store NDF title. */ sprintf( text, "JSA tile indices for %s data", skytiling.name ); ndfCput( text, indf3, "TITLE", status ); /* Store the instrument as a component in the SMURF extension. */ ndfXnew( indf3, "SMURF", "INSTRUMENT", 0, 0, &xloc, status ); ndfXpt0c( skytiling.name, indf3, "SMURF", "INSTRUMENT", status ); datAnnul( &xloc, status ); /* Close the NDF. */ ndfAnnul( &indf3, status ); } /* Abort if an error has occurred. */ if( *status != SAI__OK ) goto L999; /* Get the zero-based index of the required tile. If a null value is supplied, annull the error and skip to the end. */ parGdr0i( "ITILE", 0, 0, skytiling.ntiles - 1, 0, &itile, status ); if( *status == PAR__NULL ) { errAnnul( status ); goto L999; } /* See if the pixel origin is to be at the centre of the tile. */ parGet0l( "LOCAL", &local_origin, status ); /* Display the tile number. */ msgBlank( status ); msgSeti( "ITILE", itile ); msgSeti( "MAXTILE", skytiling.ntiles - 1); msgOut( " ", " Tile ^ITILE (from 0 to ^MAXTILE):", status ); /* Get the FITS header, FrameSet and Region defining the tile, and the tile bounds in pixel indices. */ smf_jsatile( itile, &skytiling, local_origin, proj, &fc, &fs, ®ion, lbnd, ubnd, status ); /* Write the FITS headers out to a file, annulling the error if the header is not required. */ if( *status == SAI__OK ) { atlDumpFits( "HEADER", fc, status ); if( *status == PAR__NULL ) errAnnul( status ); } /* If required, write the Region out to a text file. */ if( *status == SAI__OK ) { atlCreat( "REGION", (AstObject *) region, status ); if( *status == PAR__NULL ) errAnnul( status ); } /* Store the lower and upper pixel bounds of the tile. */ parPut1i( "LBND", 2, lbnd, status ); parPut1i( "UBND", 2, ubnd, status ); /* Display pixel bounds on the screen. */ msgSeti( "XL", lbnd[ 0 ] ); msgSeti( "XU", ubnd[ 0 ] ); msgSeti( "YL", lbnd[ 1 ] ); msgSeti( "YU", ubnd[ 1 ] ); msgOut( " ", " Pixel bounds: (^XL:^XU,^YL:^YU)", status ); /* Get the RA,Dec at the tile centre. */ if( astTest( fs, "SkyRef" ) ) { ra0 = astGetD( fs, "SkyRef(1)" ); dec0 = astGetD( fs, "SkyRef(2)" ); /* Format the central RA and Dec. and display. Call astNorm on the coordinates provided that the frame set has the correct number of axes (which it should as it comes from smf_jsatile). */ norm_radec[0] = ra0; norm_radec[1] = dec0; if (astGetI(fs, "Naxes") == 2) astNorm(fs, norm_radec); msgSetc( "RACEN", astFormat( fs, 1, norm_radec[ 0 ] )); msgSetc( "DECCEN", astFormat( fs, 2, norm_radec[ 1 ] )); msgOut( " ", " Centre (ICRS): RA=^RACEN DEC=^DECCEN", status ); /* Transform a collection of points on the edge of the region (corners and side mid-points) from GRID coords to RA,Dec. */ point1[ 0 ] = 0.5; point1[ 1 ] = 0.5; point2[ 0 ] = ubnd[ 0 ] - lbnd[ 0 ] + 1; point2[ 1 ] = ubnd[ 1 ] - lbnd[ 1 ] + 1; gx[ 0 ] = point1[ 0 ]; /* Bottom left */ gy[ 0 ] = point1[ 1 ]; gx[ 1 ] = point1[ 0 ]; /* Centre left */ gy[ 1 ] = gy[ 0 ]; gx[ 2 ] = point1[ 0 ]; /* Top left */ gy[ 2 ] = point2[ 1 ]; gx[ 3 ] = gx[ 0 ]; /* Top centre */ gy[ 3 ] = point2[ 1 ]; gx[ 4 ] = point2[ 0 ]; /* Top right */ gy[ 4 ] = point2[ 1 ]; gx[ 5 ] = point2[ 0 ]; /* Centre right */ gy[ 5 ] = gy[ 0 ]; gx[ 6 ] = point2[ 0 ]; /* Bottom right */ gy[ 6 ] = point1[ 1 ]; gx[ 7 ] = gx[ 0 ]; /* Bottom centre */ gy[ 7 ] = point1[ 1 ]; astTran2( fs, 8, gx, gy, 1, ra, dec ); /* Find the arc-distance from the centre to the furthest point from the centre. */ point1[ 0 ] = ra0; point1[ 1 ] = dec0; maxdist = -1.0; for( i = 1; i < 8; i++ ) { if( ra[ i ] != AST__BAD && dec[ i ] != AST__BAD ) { point2[ 0 ] = ra[ i ]; point2[ 1 ] = dec[ i ]; dist = astDistance( fs, point1, point2 ); if( dist > maxdist ) maxdist = dist; } } /* Convert from radius to diameter. */ maxdist *= 2.0; /* Format this size as a dec value (i.e. arc-distance) and display it. */ if( maxdist > 0.0 ) { msgSetc( "SIZE", astFormat( fs, 2, maxdist ) ); msgOut( " ", " Size: ^SIZE", status ); } else { maxdist = AST__BAD; msgOut( " ", " Size: <unknown>", status ); } /* If a discontinuity passes through the tile, the centre and size may be unknown. */ } else { ra0 = AST__BAD; dec0 = AST__BAD; maxdist = AST__BAD; msgOut( " ", " Centre (ICRS): RA=<unknown> DEC=<unknown>", status ); msgOut( " ", " Size: <unknown>", status ); } /* Write the tile centre ra and dec in radians to the output parameters. */ parPut0d( "RACEN", norm_radec[ 0 ], status ); parPut0d( "DECCEN", norm_radec[ 1 ], status ); /* Write the size to the output parameter as radians. */ parPut0d( "SIZE", maxdist, status ); /* Get the translation of the environment variable JSA_TILE_DIR. */ jcmt_tiles = getenv( "JSA_TILE_DIR" ); /* Initialise the path to the tile's NDF to hold the root directory. Use the current working directory if JSA_TILE_DIR is undefined. */ if( jcmt_tiles ) { nc = 0; tilendf = astAppendString( tilendf, &nc, jcmt_tiles ); } else { nc = 512; jcmt_tiles = astMalloc( nc ); while( !getcwd( jcmt_tiles, nc ) ) { nc *= 2; jcmt_tiles = astRealloc( jcmt_tiles, nc ); } nc = 0; tilendf = astAppendString( tilendf, &nc, jcmt_tiles ); jcmt_tiles = astFree( jcmt_tiles ); } /* Complete the path to the tile's NDF. */ tilendf = astAppendString( tilendf, &nc, "/" ); tilendf = astAppendString( tilendf, &nc, skytiling.subdir ); dirlen = nc; sprintf( text, "/tile_%d.sdf", itile ); tilendf = astAppendString( tilendf, &nc, text ); /* Write it to the output parameter. */ parPut0c( "TILENDF", tilendf, status ); /* See if the NDF exists, and store the flag in the output parameter. */ exists = access( tilendf, F_OK ) ? 0 : 1; parPut0l( "EXISTS", exists, status ); /* If the NDF does not exist, create it if required. */ parGet0l( "CREATE", &create, status ); if( !exists && create && *status == SAI__OK ) { /* Write the NDF info to the screen. */ msgSetc( "NDF", tilendf ); msgOutif( MSG__NORM, " ", " NDF: ^NDF (created)", status ); /* Temporarily terminate the NDF path at the end of the subdirectory. */ tilendf[ dirlen ] = 0; /* Create the required directory (does nothing if the directory already exists). It is given read/write/search permissions for owner and group, and read/search permissions for others. */ (void) mkdir( tilendf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); /* Replace the character temporarily overwritten above. */ tilendf[ dirlen ] = '/'; /* Now create the tile's NDF. */ ndfPlace( NULL, tilendf, &place, status ); ndfNew( skytiling.type, 2, lbnd, ubnd, &place, &indf1, status ); /* Fill its data array with zeros. */ ndfMap( indf1, "Data", skytiling.type, "WRITE/ZERO", &pntr, &el, status ); /* Store the WCS FrameSet. */ ndfPtwcs( fs, indf1, status ); /* If the instrument jsatiles.have variance, fill the variance array with zeros. */ if( skytiling.var ) { ndfMap( indf1, "Variance", skytiling.type, "WRITE/ZERO", &pntr, &el, status ); } /* Create a SMURF extension. */ ndfXnew( indf1, SMURF__EXTNAME, SMURF__EXTTYPE, 0, NULL, &xloc, status ); /* Store the tile number and instrument name in the extension. */ datNew0I( xloc, "TILE", status ); datFind( xloc, "TILE", &cloc, status ); datPut0I( cloc, itile, status ); datAnnul( &cloc, status ); datNew0C( xloc, "INSTRUMENT", strlen( skytiling.name ), status ); datFind( xloc, "INSTRUMENT", &cloc, status ); datPut0C( cloc, skytiling.name, status ); datAnnul( &cloc, status ); /* Create a weights NDF within the SMURF extension, and fill its data array with zeros. */ ndfPlace( xloc, "WEIGHTS", &place, status ); ndfNew( skytiling.type, 2, lbnd, ubnd, &place, &indf2, status ); ndfMap( indf2, "Data", skytiling.type, "WRITE/ZERO", &pntr, &el, status ); ndfPtwcs( fs, indf2, status ); ndfAnnul( &indf2, status ); /* Annul the extension locator and the main NDF identifier. */ datAnnul( &xloc, status ); ndfAnnul( &indf1, status ); /* Write the NDF info to the screen. */ } else { msgSetc( "NDF", tilendf ); msgSetc( "E", exists ? "exists" : "does not exist" ); msgOut( " ", " NDF: ^NDF (^E)", status ); } /* Initialise TBND and TLBND to indicate no overlap. */ tlbnd[ 0 ] = 1; tlbnd[ 1 ] = 1; tubnd[ 0 ] = 0; tubnd[ 1 ] = 0; /* Attempt to to get an AST Region (assumed to be in some 2D sky coordinate system) using parameter "TARGET". */ if( *status == SAI__OK ) { kpg1Gtobj( "TARGET", "Region", (void (*)( void )) F77_EXTERNAL_NAME(ast_isaregion), &obj, status ); /* Annul the error if none was obtained. */ if( *status == PAR__NULL ) { errAnnul( status ); /* Otherwise, use the supplied object. */ } else { target = (AstRegion *) obj; /* If the target Region is 3-dimensional, remove the third axis, which is assumed to be a spectral axis. */ if( astGetI( target, "Naxes" ) == 3 ) { axes[ 0 ] = 1; axes[ 1 ] = 2; target = astPickAxes( target, 2, axes, NULL ); } /* See if there is any overlap between the target and the tile. */ overlap = NULL; flag = astOverlap( region, target ); if( flag == 0 ) { msgOut( "", " Cannot convert between the coordinate system of the " "supplied target and the tile.", status ); } else if( flag == 1 || flag == 6 ) { msgOut( "", " There is no overlap between the target and the tile.", status ); } else if( flag == 2 ) { msgOut( "", " The tile is contained within the target.", status ); tlbnd[ 0 ] = lbnd[ 0 ]; tlbnd[ 1 ] = lbnd[ 1 ]; tubnd[ 0 ] = ubnd[ 0 ]; tubnd[ 1 ] = ubnd[ 1 ]; } else if( flag == 3 ) { overlap = astCmpRegion( region, target, AST__AND, " " ); } else if( flag == 4 ) { overlap = astCmpRegion( region, target, AST__AND, " " ); } else if( flag == 5 ) { msgOut( "", " The target and tile are identical.", status ); tlbnd[ 0 ] = lbnd[ 0 ]; tlbnd[ 1 ] = lbnd[ 1 ]; tubnd[ 0 ] = ubnd[ 0 ]; tubnd[ 1 ] = ubnd[ 1 ]; } else if( *status == SAI__OK ) { *status = SAI__OK; errRepf( "", "Unexpected value %d returned by astOverlap " "(programming error).", status, flag ); } /* If a region containing the intersection of the tile and target was created above, map it into the grid coordinate system of the tile. */ if( overlap ) { overlap = astMapRegion( overlap, astGetMapping( fs, AST__CURRENT, AST__BASE ), astGetFrame( fs, AST__BASE ) ); /* Get its GRID bounds. */ astGetRegionBounds( overlap, dlbnd, dubnd ); /* Convert to integer. */ tlbnd[ 0 ] = ceil( dlbnd[ 0 ] - 0.5 ); tlbnd[ 1 ] = ceil( dlbnd[ 1 ] - 0.5 ); tubnd[ 0 ] = ceil( dubnd[ 0 ] - 0.5 ); tubnd[ 1 ] = ceil( dubnd[ 1 ] - 0.5 ); /* Convert to PIXEL indices within the tile. */ tlbnd[ 0 ] += lbnd[ 0 ] - 1; tlbnd[ 1 ] += lbnd[ 1 ] - 1; tubnd[ 0 ] += lbnd[ 0 ] - 1; tubnd[ 1 ] += lbnd[ 1 ] - 1; msgOutf( "", " The target overlaps section (%d:%d,%d:%d).", status, tlbnd[ 0 ], tubnd[ 0 ], tlbnd[ 1 ], tubnd[ 1 ] ); } } } /* Store the pixel index bounds of the tiles overlap with the target. */ parPut1i( "TLBND", 2, tlbnd, status ); parPut1i( "TUBND", 2, tubnd, status ); /* Arrive here if an error occurs. */ L999:; /* Free resources. */ tilendf = astFree( tilendf ); /* End the AST context. */ astEnd; /* Issue a status indication.*/ msgBlank( status ); if( *status == SAI__OK ) { msgOutif( MSG__VERB, "", "JSATILEINFO succeeded.", status); } else { msgOutif( MSG__VERB, "", "JSATILEINFO failed.", status); } }
void clumpinfo( int *status ) { /* *+ * Name: * CLUMPINFO * Purpose: * Obtain information about one or more previously identified clumps. * Language: * C * Type of Module: * ADAM A-task * Description: * This application returns various items of information about a * single clump, or a collection of clumps, previously identified * using FINDCLUMPS or EXTRACTCLUMPS. * Usage: * clumpinfo ndf clumps quiet * ADAM Parameters: * CLUMPS = LITERAL (Read) * Specifies the indices of the clumps to be included in the * returned information. It can take any of the following values: * * - "ALL" or "*" -- All clumps. * * - "xx,yy,zz" -- A list of clump indices. * * - "xx:yy" -- Clump indices between xx and yy inclusively. When * xx is omitted the range begins from one; when yy is omitted the * range ends with the final clump index. * * - Any reasonable combination of above values separated by * commas. * FLBND( ) = _DOUBLE (Write) * The lower bounds of the bounding box enclosing the selected * clumps in the current WCS Frame of the input NDF. Celestial axis * values are always in units of radians, but spectral axis units * will be in the spectral units used by the current WCS Frame. * FUBND( ) = _DOUBLE (Write) * The upper bounds of the bounding box enclosing the selected * clumps. See parameter FLBND for more details. * LBOUND( ) = _INTEGER (Write) * The lower pixel bounds of bounding box enclosing the selected * clumps. * NCLUMPS = _INTEGER (Write) * The total number of clumps descrriptions stored within the supplied * NDF. * NDF = NDF (Read) * The NDF defining the previously identified clumps. This * should contain a CUPID extension describing all the identified * clumps, in the format produced by FINDCLUMPS or EXTRACTCLUMPS. * QUIET = _LOGICAL (Read) * If TRUE, then no information is written out to the screen, * although the output parameters are still assigned values. [FALSE] * UBOUND( ) = _INTEGER (Write) * The upper pixel bounds of bounding box enclosing the selected * clumps. * Notes: * - It is hoped to extend the range of information reported by this * application as new requirements arise. * Synopsis: * void clumpinfo( int *status ); * Copyright: * Copyright (C) 2007 Particle Physics & Astronomy Research Council. * All Rights Reserved. * Licence: * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street,Fifth Floor, Boston, MA * 02110-1301, USA * Authors: * DSB: David S. Berry * {enter_new_authors_here} * History: * 22-MAR-2007 (DSB): * Original version. * {enter_further_changes_here} * Bugs: * {note_any_bugs_here} *- */ /* Local Variables: */ AstFrame *cfrm; /* Pointer to current WCS Frame */ AstMapping *cmap; /* Pointer to PIXEL->current Frame Mapping */ CupidClumpInfo info; /* Structure holding returned information */ Grp *grp = NULL; /* GRP group holding input NDF name */ HDSLoc *aloc = NULL; /* Locator for CLUMPS array in CUPID extension */ HDSLoc *cloc = NULL; /* Locator for a single CLUMP structure */ HDSLoc *xloc = NULL; /* Locator for CUPID extension */ char *p; /* Pointer into tmpstr string */ char tmpstr[ 100 ]; /* Buffer for temporary strings */ const char *dom; /* Pointer to axis Domain name */ double flbnd[ NDF__MXDIM ]; /* Lower bounds of WCS bounding box */ double fubnd[ NDF__MXDIM ]; /* Upper bounds of WCS bounding box */ double plbnd[ NDF__MXDIM ]; /* Lower bounds of PIXEL bounding box */ double pubnd[ NDF__MXDIM ]; /* Upper bounds of PIXEL bounding box */ int *clump_flags = NULL; /* Flags indicating if each clump is to be used */ int *clump_indices = NULL;/* List of indices of clumps to be used */ int i; /* Loop count */ int iclump; /* One-based clump index */ int indf; /* NDF identifier for input NDF */ int ipix; /* Index of PIXEL Frame */ size_t nclumps; /* No. of clump descriptions within the supplied NDF */ int nuse; /* Number of clumps to be used */ int primary; /* Value for locator primary flag */ int quiet; /* Supress screen output? */ size_t size; /* Number of values in group "*grp" */ int there; /* Does the enquired object exist? */ /* Abort if an error has already occurred. */ if( *status != SAI__OK ) return; /* Begin an AST context */ astBegin; /* Start an NDF context */ ndfBegin(); /* Obtain the input NDF and get a locator for the array of clump descriptions within it. ----------------------------------------------------------------- */ /* Get an identifier for the input NDF. We use NDG (via kpg1_Rgndf) instead of calling ndfAssoc directly since NDF/HDS has problems with file names containing spaces, which NDG does not have. */ kpg1Rgndf( "NDF", 1, 1, "", &grp, &size, status ); ndgNdfas( grp, 1, "READ", &indf, status ); grpDelet( &grp, status ); /* Check the NDF has a suitable CUPID extension containing an array of clump cut-outs. Get an HDS locator for the array. */ ndfXstat( indf, "CUPID", &there, status ); if( !there ) { if( *status == SAI__OK ) { *status = SAI__ERROR; ndfMsg( "NDF", indf ); errRep( "", "The NDF \"^NDF\" does not contain a CUPID extension " "such as created by FINDCLUMPS or EXTRACTCLUMPS.", status ); } } else { ndfXloc( indf, "CUPID", "READ", &xloc, status ); datThere( xloc, "CLUMPS", &there, status ); if( !there ) { if( *status == SAI__OK ) { *status = SAI__ERROR; ndfMsg( "NDF", indf ); errRep( "", "The CUPID extension within NDF \"^NDF\" does not " "contain an array of clumps such as created by " "FINDCLUMPS or EXTRACTCLUMPS.", status ); } } else { datFind( xloc, "CLUMPS", &aloc, status ); primary = 1; datPrmry( 1, &aloc, &primary, status ); } datAnnul( &xloc, status ); } /* Abort if we have no clumps array locator, or if an error occurred. */ if( !aloc || *status != SAI__OK ) goto L999; /* Calculate the required clump information, and store it in the "info" structure. ----------------------------------------------------------------- */ /* Indicate that the structure holding the returned information has not yet been initialised. */ info.init = 0; /* Get the WCS FrameSet from the input NDF, and store a pointer to it in the "info" structure. */ kpg1Gtwcs( indf, &(info.iwcs), status ); /* Get the number of clumps defined within the input NDF. */ datSize( aloc, &nclumps, status ); /* Get the list of clump indices to iclude in the returned information. */ clump_flags = astMalloc( sizeof( int )*nclumps ); clump_indices = astMalloc( sizeof( int )*nclumps ); kpg1Gilst( 1, (int) nclumps, (int) nclumps, "CLUMPS", clump_flags, clump_indices, &nuse, status ); /* Loop round all clumps that are to be used. */ for( i = 0; i < nuse && *status == SAI__OK; i++ ) { iclump = clump_indices[ i ]; /* Get a locator for this clump. */ datCell( aloc, 1, &iclump, &cloc, status ); /* Update the returned information to include this clump. */ cupidClumpInfo1( cloc, &info, status ); /* Annul the clump structure locator. */ datAnnul( &cloc, status ); } /* Write out the information to the screen and to appropriate output parameters. ----------------------------------------------------------------- */ /* See if screen output is required. */ parGet0l( "QUIET", &quiet, status ); if( !quiet ) msgBlank( status ); /* The number of clumps defined within the input NDF... */ parPut0i( "NCLUMPS", (int) nclumps, status ); if( ! quiet ) { msgSeti( "NCLUMPS", (int) nclumps ); msgOut( "", " Total no. of clumps: ^NCLUMPS", status ); } /* Pixel index bounding box... */ parPut1i( "LBOUND", info.npix, info.lbnd, status ); parPut1i( "UBOUND", info.npix, info.ubnd, status ); if( !quiet ) { p = tmpstr + sprintf( tmpstr, "( " ); for( i = 0; i < info.npix; i++) { p += sprintf( p, "%d:%d", info.lbnd[ i ], info.ubnd[ i ] ); if( i < info.npix - 1 ) p += sprintf( p, ", " ); } p += sprintf( p, " )" ); msgSetc( "SECTION", tmpstr ); msgOut( "", " Pixel index bounding box: ^SECTION", status ); } /* WCS bounding box (first convert the pixel index bounding box into WCS coords)... */ cfrm = astGetFrame( info.iwcs, AST__CURRENT ); kpg1Asffr( info.iwcs, "PIXEL", &ipix, status ); cmap = astGetMapping( info.iwcs, ipix, AST__CURRENT ); for( i = 0; i < info.npix; i++ ) { plbnd[ i ] = info.lbnd[ i ] - 1.0; pubnd[ i ] = info.ubnd[ i ]; } for( i = 0; i < info.nwcs; i++ ) { astMapBox( cmap, plbnd, pubnd, 1, i + 1, flbnd + i, fubnd + i, NULL, NULL); } astNorm( cfrm, flbnd ); astNorm( cfrm, fubnd ); parPut1d( "FLBND", info.nwcs, flbnd, status ); parPut1d( "FUBND", info.nwcs, fubnd, status ); if( !quiet ) { msgOut( "", " WCS bounding box:", status ); for( i = 0; i < info.nwcs; i++) { msgSetc( "L", astFormat( cfrm, i + 1, flbnd[ i ] ) ); msgSetc( "U", astFormat( cfrm, i + 1, fubnd[ i ] ) ); sprintf( tmpstr, "Domain(%d)", i + 1 ); dom = astGetC( cfrm, tmpstr ); if( dom && strcmp( dom, "SKY" ) ) { sprintf( tmpstr, "Unit(%d)", i + 1 ); msgSetc( "UNT", astGetC( cfrm, tmpstr ) ); } else { msgSetc( "UNT", "" ); } sprintf( tmpstr, "Label(%d)", i + 1 ); msgSetc( "LAB", astGetC( cfrm, tmpstr ) ); msgOut( "", " ^LAB: ^L -> ^U ^UNT", status ); } } if( !quiet ) msgBlank( status ); /* Tidy up. -------- */ L999: ; /* Free resources. */ clump_flags = astFree( clump_flags ); clump_indices = astFree( clump_indices ); if( aloc ) datAnnul( &aloc, status ); /* End the NDF context */ ndfEnd( status ); /* End the AST context */ astEnd; /* If an error has occurred, issue another error report identifying the program which has failed (i.e. this one). */ if( *status != SAI__OK ) { errRep( "CLUMPINFO_ERR", "CLUMPINFO: Failed to obtain information " "about one or more previously identified clumps.", status ); } }