📄 twoptfile.c
字号:
/*********************************************************
WRITE FLUENCE DATA TO DISK
Returns non-zero on error
1 -> File error (can't open file, unexpected EOF, etc.)
2 -> Invalid input parameter
3 -> System error (out of memory, etc.)
*********************************************************/
/*
* This file is part of tMCimg.
*
* tMCimg 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.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include "config.h"
#include "tMCimg.h"
#if (HAVE_ASSERT_H)
#include <assert.h>
#endif
int save2pt(const char *basen, int ns, const TWOPT_T *udata,
const TWOPT_T *vdata, int ndata, const char *ext)
{
char filenm[FILENMLEN];
FILE *fp;
int nu, nv;
#if (HAVE_SNPRINTF)
if (ext == NULL || *ext != '\0')
{
if (ns < 0)
snprintf(filenm, FILENMLEN, "%s.2pt", basen);
else
snprintf(filenm, FILENMLEN, "%s.2pt.%d", basen, ns);
}
else
{
if (ns < 0)
snprintf(filenm, FILENMLEN, "%s.2pt.%s", basen, ext);
else
snprintf(filenm, FILENMLEN, "%s.2pt.%d.%s", basen, ns, ext);
}
#else
if (ext == NULL || *ext != '\0')
{
if (ns < 0)
sprintf(filenm, "%s.2pt", basen);
else
sprintf(filenm, "%s.2pt.%d", basen, ns);
}
else
{
if (ns < 0)
sprintf(filenm, "%s.2pt.%s", basen, ext);
else
sprintf(filenm, "%s.2pt.%d.%s", basen, ns, ext);
}
#endif
if ((fp = fopen(filenm, "wb")) == NULL)
{
#if (HAVE_PERROR)
perror("Can't open 2pt file for writing\n");
#else
fprintf(stderr, "Can't open 2pt file for writing\n");
#endif
return 1;
}
printf("Writing 2pt file %s\n", filenm);
#if (HAVE_ASSERT)
assert(udata != NULL);
#endif
nu = (int)fwrite(udata, sizeof(TWOPT_T), (size_t)ndata, fp);
if (nu != ndata)
{
fclose(fp);
#if (HAVE_PERROR)
perror("Error while writing 2pt file (real part)\n");
#else
fprintf(stderr, "Error while writing 2pt file (real part)\n");
#endif
return 1;
}
/* vdata == NULL is allowed for CW data, pad out file with zeros
* for the imaginary part in that case. Ditto time-domain.
*/
if (vdata == NULL)
{
printf("Skipping imaginary 2pt data (assuming real-valued data)\n");
}
else if ((nv = (int)fwrite(vdata,
sizeof(TWOPT_T), (size_t)ndata, fp)) != ndata)
{
#if (HAVE_PERROR)
perror("Error while writing 2pt file (imaginary part)\n");
#else
fprintf(stderr,"Error while writing 2pt file (imaginary part)\n");
#endif
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}
/*********************************************************
READ THE FLUENCE DATA FROM DISK
Returns non-zero on error
1 -> File error (can't open file, unexpected EOF, etc.)
2 -> Invalid input parameter
3 -> System error (out of memory, etc.)
*********************************************************/
int load2pt(TWOPT_T *II, TWOPT_T *JJ,
int nvox, int ntim, const char *filenm)
{
FILE *fp;
int i, n;
if ((II == NULL) && (JJ == NULL))
{
fprintf(stderr, "LOAD2PT: no output tables specified\n");
return 2;
}
if (nvox < 1 || ntim < 1)
{
fprintf(stderr, "LOAD2pt: Illegal table size\n");
return 2;
}
if (filenm == NULL)
{
fprintf(stderr, "LOAD2pt: Illegal filename\n");
return 2;
}
if ((fp = fopen(filenm, "rb")) == NULL)
{
#if (HAVE_PERROR)
perror("Can't open 2pt file");
#else
fprintf(stderr,"Can't open 2pt file");
#endif
fprintf(stderr, "Error reading from file \"%s\"\n", filenm);
return 1;
}
rewind(fp);
if (II != NULL)
{
if ((n = (int) fread(II, sizeof(TWOPT_T),
(size_t) (nvox * ntim), fp)) != nvox * ntim)
{
#if (HAVE_PERROR)
perror("Error while reading 2pt file (real part)\n");
#else
fprintf(stderr,"Error while reading 2pt file (real part)\n");
#endif
fclose(fp);
return 1;
}
}
else
{
if ((n = fseek(fp, nvox * ntim * sizeof(TWOPT_T), SEEK_CUR)) != 0)
{
#if (HAVE_PERROR)
perror("EOF error while skipping real part of 2pt file\n");
#else
fprintf(stderr,"EOF error while skipping real part of 2pt file\n");
#endif
fclose(fp);
return 1;
}
}
/* For CW data, there is no reason to write this to disk (the imaginary
* part will always be zero), so I don't bother. If the imaginary part
* doesn't exist at all, assume this is the case and zero out the memory.
* Partial reads are true errors. */
if (JJ != NULL)
{
n = (int) fread(JJ, sizeof(TWOPT_T), (size_t) (nvox * ntim), fp);
/* Assume the caller knows what he's doing and gave me the proper
* amount of memory */
if (n == 0)
for (i = 0; i < nvox * ntim; i++)
JJ[i] = 0.0;
if ((n != 0) && (n != nvox * ntim))
{
#if (HAVE_PERROR)
perror("Error while reading 2pt file (imaginary part)\n");
#else
fprintf(stderr,"Error while reading 2pt file (imaginary part)\n");
#endif
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -