Skip to content

Commit

Permalink
Modifying grid_filter to exclude grid vectors from a file that do not…
Browse files Browse the repository at this point in the history
… meet user-defined slant range criteria
  • Loading branch information
egthomas committed Jul 12, 2024
1 parent 0f0eb38 commit e0cb7f0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@
</option>
<option><on>--version</on><od>print the RST version number and exit.</od>
</option>
<option><on>-cpid</on><od>exclude data according to control program ID (not yet implemented!).</od>
</option>
<option><on>-minrng <ar>minrange</ar></on><od>exclude data from slant ranges nearer than <ar>minrange</ar> km (default is 0 km).</od>
</option>
<option><on>-maxrng <ar>maxrange</ar></on><od>exclude data from slant ranges greater than <ar>maxrange</ar> km (default is 10,000 km).</od>
</option>
<option><on><ar>gridname</ar></on><od>filename of the <code>grdmap</code> format file. If this is omitted, the file will be read from standard input.</od>
</option>
<option><on>-old</on><od>the input file is a <code>grd</code> format file.</od>
</option>
<synopsis><p>Supersample a grid format file in time.</p></synopsis>
<description><p>Supersample a grid format file in time.</p>
<p>The task supersamples by skipping over records in the output file for a fixed length of time. By default the task will write a record every 10 minutes</p></description>

<synopsis><p>Removes grid vectors from a <code>grdmap</code> file using range and/or CPID criteria.</p></synopsis>
<description><p>Removes grid vectors from a grid file.</p>
<p>The task searches for and removes grid vectors that do not meet user-defined slant range or control program ID (CPID) criteria. By default the task will not exclude any data.</p></description>

<example>
<command>grid_filter -old 20040830.grd &gt; 20040830.grd.s</command>
<description>Supersample the <code>grd</code> format file "<code>20040830.grd</code>" to produce the file "<code>20040830.grd.s</code>".</description>
<description>Filter the <code>grd</code> format file "<code>20040830.grd</code>" to produce the file "<code>20040830.grd.f</code>".</description>
</example>

</binary>
122 changes: 97 additions & 25 deletions codebase/superdarn/src.bin/tk/tool/grid_filter.1.7/grid_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include "option.h"
#include "rtypes.h"
#include "rtime.h"
#include "rfile.h"
#include "griddata.h"
#include "gridread.h"
Expand All @@ -39,42 +41,56 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "hlpstr.h"


struct OptionData opt;
struct GridData *igrd;
struct GridData *ogrd;


int filter_grid(struct GridData *out, struct GridData *in,
double minrng, double maxrng);

struct OptionData opt;
struct GridData *grd;

int skip=10*60; /* skip time */

int rst_opterr(char *txt) {
fprintf(stderr,"Option not recognized: %s\n",txt);
fprintf(stderr,"Please try: grid_filter --help\n");
return(-1);
}


int main(int argc,char *argv[]) {

double tval=0,dval=0;
int c=0;

FILE *fp;

int old=0;

int arg=0;
unsigned char help=0;
unsigned char option=0;
unsigned char version=0;

grd=GridMake();
unsigned char vb=0;
unsigned char cpid=0;

double minrng=0;
double maxrng=10000;

int yr,mo,dy,hr,mt;
double sc;

igrd=GridMake();
ogrd=GridMake();

OptionAdd(&opt,"-help",'x',&help);
OptionAdd(&opt,"-option",'x',&option);
OptionAdd(&opt,"-version",'x',&version);

OptionAdd(&opt,"vb",'x',&vb);
OptionAdd(&opt,"old",'x',&old);

OptionAdd(&opt,"cpid",'x',&cpid);
OptionAdd(&opt,"minrng",'d',&minrng);
OptionAdd(&opt,"maxrng",'d',&maxrng);

arg=OptionProcess(1,argc,argv,&opt,rst_opterr);

if (arg==-1) {
Expand All @@ -96,42 +112,98 @@ int main(int argc,char *argv[]) {
exit(0);
}


if (arg !=argc) {
fp=fopen(argv[arg],"r");
if (fp==NULL) {
fprintf(stderr,"File not found.\n");
exit(1);
}
} else fp=stdin;

if (old) {
while (OldGridFread(fp,grd) !=-1) {

if (c==0) dval=grd->st_time-((int) grd->st_time % (24*3600));
if ((grd->st_time-dval)>=tval) {
OldGridFwrite(stdout,grd);
tval+=skip;
while (OldGridFread(fp,igrd) !=-1) {

filter_grid(ogrd,igrd,minrng,maxrng);

if (vb) {
TimeEpochToYMDHMS(ogrd->st_time,&yr,&mo,&dy,&hr,&mt,&sc);
fprintf(stderr,"%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d > %d pts (%.2f%%)\n",
yr,mo,dy,hr,mt,(int) sc,igrd->vcnum,ogrd->vcnum,
(float)ogrd->vcnum/igrd->vcnum*100);
}

OldGridFwrite(stdout,ogrd);
}
if (fp !=stdin) fclose(fp);
} else {
while (GridFread(fp,grd) !=-1) {

if (c==0) dval=grd->st_time-((int) grd->st_time % (24*3600));
if ((grd->st_time-dval)>=tval) {
GridFwrite(stdout,grd);
tval+=skip;
while (GridFread(fp,igrd) !=-1) {

filter_grid(ogrd,igrd,minrng,maxrng);

if (vb) {
TimeEpochToYMDHMS(ogrd->st_time,&yr,&mo,&dy,&hr,&mt,&sc);
fprintf(stderr,"%.4d-%.2d-%.2d %.2d:%.2d:%.2d %d > %d pts (%.2f%%)\n",
yr,mo,dy,hr,mt,(int) sc,igrd->vcnum,ogrd->vcnum,
(float)ogrd->vcnum/igrd->vcnum*100);
}

GridFwrite(stdout,ogrd);
}
if (fp !=stdin) fclose(fp);
}

if (fp !=stdin) fclose(fp);

return 0;
}


int filter_grid(struct GridData *out, struct GridData *in,
double minrng, double maxrng) {

int i,j=0;
int vcnt=0;

out->st_time = in->st_time;
out->ed_time = in->ed_time;
out->stnum = in->stnum;
out->xtd = in->xtd;

/* Copy station info */
if (in->stnum > 0) {
if (out->sdata == NULL) out->sdata = malloc(sizeof(struct GridSVec)*in->stnum);
else out->sdata = realloc(out->sdata,sizeof(struct GridSVec)*in->stnum);
memcpy(out->sdata,in->sdata,sizeof(struct GridSVec)*in->stnum);
} else if (out->sdata != NULL) {
free(out->sdata);
out->sdata=NULL;
}

/* Search through all grid vectors in record to see which meet criteria */
for (i=0; i<in->vcnum; i++) {
if (in->data[i].srng >= minrng && in->data[i].srng < maxrng) {

if (out->data == NULL) out->data = malloc(sizeof(struct GridGVec));
else out->data = realloc(out->data,sizeof(struct GridGVec)*(vcnt+1));

memcpy(&out->data[vcnt],&in->data[i],sizeof(struct GridGVec));

vcnt++;
}
}

out->vcnum = vcnt;

/* Update number of grid vectors in station info */
for (i=0; i<out->stnum; i++) out->sdata[i].npnt = 0;

for (i=0; i<out->vcnum; i++) {
for (j=0; j<out->stnum; j++) {
if (out->data[i].st_id == out->sdata[j].st_id) {
out->sdata[j].npnt++;
break;
}
}
}

return 1;
}

0 comments on commit e0cb7f0

Please sign in to comment.