Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewind(fp) has no effect when reading from stdin #458

Open
2 of 5 tasks
ecbland opened this issue Aug 26, 2021 · 0 comments
Open
2 of 5 tasks

rewind(fp) has no effect when reading from stdin #458

ecbland opened this issue Aug 26, 2021 · 0 comments
Labels

Comments

@ecbland
Copy link

ecbland commented Aug 26, 2021

BUG

A few routines in RST use rewind(fp) to navigate back to the start of a file. This works well when reading from an actual file, but it does not always work when reading from stdin. I discovered this while fixing #424.

Priority

  • Minor - typo, incorrect naming, fails in a specific uncommon situation
  • Major - misrepresentation of data, failure in a common situation
  • Urgent - failure in usage, large misrepresentation of data

RST version

Does the bug occur in an official RST release?

  • Yes. RST version: v4.6 and several previous versions
  • No. Branch containing the bug (e.g. develop): ______

Example of the bug

Notice that the first two records (06:01:00 and 06:01:03) are missing when reading from stdin

# -----------------------------------------------------------
# trim_fit reading from a fitacf file
# -----------------------------------------------------------
make_fit 20150305.0601.00.inv.rawacf > 20150305.0601.00.inv.fitacf
trim_fit -st 06:00 -et 07:00 20150305.0601.00.inv.fitacf > from_file.fitacf
dmapdump from_file.fitacf | grep -A 2 time.hr | head -12

	short	"time.hr" = 6
	short	"time.mt" = 1
	short	"time.sc" = 0
--
	short	"time.hr" = 6
	short	"time.mt" = 1
	short	"time.sc" = 3
--
	short	"time.hr" = 6
	short	"time.mt" = 1
	short	"time.sc" = 6
--


# -----------------------------------------------------------
# trim_fit reading from stdin
# -----------------------------------------------------------
make_fit 20150305.0601.00.inv.rawacf | trim_fit -st 06:00 -et 07:00 > from_stdin.fitacf
dmapdump from_stdin.fitacf | grep -A 2 time.hr | head -12

	short	"time.hr" = 6
	short	"time.mt" = 1
	short	"time.sc" = 6
--
	short	"time.hr" = 6
	short	"time.mt" = 1
	short	"time.sc" = 9
--
	short	"time.hr" = 6
	short	"time.mt" = 1
	short	"time.sc" = 12
--

The following routines can read from stdin and use rewind(fp):

Note that the other trim_* routines are not affected -- they just skip over the start of the file so we get the same output reading from a file and from stdin.


time_plot has a similar bug, introduced in #235 (by me 🤣 )
It doesn't explicitly use rewind(fp) but it does essentially the same thing with FitSeek/OldFitSeek

# this produces a blank plot...
make_fit 20150305.0401.00.inv.rawacf | time_plot -x -a -b 5

#if we specify -ex or -et, the routine works correctly
make_fit 20150305.0401.00.inv.rawacf | time_plot -x -a -b 5 -ex 02:00
make_fit 20150305.0401.00.inv.rawacf | time_plot -x -a -b 5 -et 06:00

Potential Bug Location

/* rewind file pointer in case we want to trim first record */
rewind(fp);

/* rewind file pointer in case we want to trim first record */
rewind(fp);

//** Read the file again, apply the median filter, and write a new file
rewind(fp); // rewind the file pointer

if (etime !=-1) {
if (edate==-1) etime+=stime - ( (int) stime % (24*3600));
else etime+=edate;
} else if (extime !=0) etime=stime+extime;
else {
/* determine end time from the last record in the file
if the total time range is less than 10min then set it to 10min.
this has been implemented only for fit/fitacf format files */
int status=0;
if (fitflg && old) {
while (status!=-1) status=OldFitRead(oldfitfp,prm,fit);
etime=TimeYMDHMSToEpoch(prm->time.yr,prm->time.mo,prm->time.dy,
prm->time.hr,prm->time.mt,
prm->time.sc+prm->time.us/1.0e6);
TimeEpochToYMDHMS(stime,&yr,&mo,&dy,&hr,&mt,&sc);
status=OldFitSeek(oldfitfp,yr,mo,dy,hr,mt,0,NULL);
if (status==-1) {
fprintf(stderr,"Error determining start/end time of file. Please specify using -st and -et options.\n");
exit(-1);
}
status=OldFitRead(oldfitfp,prm,fit);
} else if (fitflg) {
double atme;
status=FitFseek(fitfp,prm->time.yr+1,prm->time.mo,prm->time.dy,
prm->time.hr,prm->time.mt,0,&atme,inx);
if (status!=-1) { /* status should be -1 if end of file was reached successfully */
fprintf(stderr,"Error determining start/end time of file. Please specify using -st and -et options.\n");
exit(-1);
}

Potential Solution(s)

I don't know whether this problem can actually be solved. It seems to be a limitation of reading from stdin, but I'll defer to the experts 😄

@ecbland ecbland added the bug label Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant