我正在编写一个从C到python的脚本来生成二进制格式的站点数据,以便在GrADS中使用。这里有关于结构的文档:http://cola.gmu.edu/grads/gadoc/aboutstationdata.html#station
Python脚本生成二进制文件,但GrADS只能读取第一个站,而C生成的二进制文件与python输出文件非常不同。 我认为问题是在文件中写一个结构(ReportHeader)。
C脚本:
#include <stdio.h>
/* Structure that describes a report header in a stn file */
struct rpthdr {
char id[8]; /* Character station id */
float lat; /* Latitude of report */
float lon; /* Longitude of report */
float t; /* Time in relative grid units */
int nlev; /* Number of levels following */
int flag; /* Level independent var set flag */
} hdr;
main () {
FILE *ifile, *ofile;
char rec[80];
int flag,year,month,yrsav,mnsav,i;
float val1,val2,val3,val4,val5;
/* Open files */
ifile = fopen ("listabin1112.txt","r");
ofile = fopen ("arquivo1112.dat","wb");
if (ifile==NULL || ofile==NULL) {
printf ("Error opening files\n");
return 0 ;
}
/* Read,write loop */
flag = 1;
while (fgets(rec,79,ifile)!=NULL) {
/* Format conversion */
sscanf (rec," %f %f %f %f %f %f %f",&hdr.lat,&hdr.lon,&val1,&val2,&val3,&val4,&val5);
for (i=0; i<8; i++) {
hdr.id[i] = rec[i+25];
}
printf("%f\n", hdr.lat);
/*Time group terminator if need */
if (flag) {
yrsav = year;
mnsav = month;
flag = 0;
}
if (yrsav!=year || mnsav!=month) {
hdr.nlev = 0;
fwrite (&hdr,sizeof(struct rpthdr), 1, ofile);
}
yrsav = year;
mnsav = month;
/* Write this report */
hdr.nlev = 1;
hdr.flag = 1;
hdr.t = 0.0;
fwrite (&hdr,sizeof(struct rpthdr), 1, ofile);
fwrite (&val1,sizeof(float), 1, ofile);
fwrite (&val2,sizeof(float), 1, ofile);
fwrite (&val3,sizeof(float), 1, ofile);
fwrite (&val4,sizeof(float), 1, ofile);
fwrite (&val5,sizeof(float), 1, ofile);
}
hdr.nlev = 0;
fwrite (&hdr,sizeof(struct rpthdr), 1, ofile);
}
我的python脚本:
from ctypes import *
import struct
class ReportHeader(Structure):
_fields_ = [('id', c_char * 8),
('lat', c_float),
('lon', c_float),
('t', c_float),
('nlev', c_int),
('flag', c_int)]
if __name__ == '__main__':
ifile = "listabin1112.txt"
ofile = "teste.dat"
of = open(ofile, "wb")
hdr = ReportHeader()
""" Read, write loop """
ids = 10000000
with open(ifile) as f:
for line in f:
lat, lon, val1, val2, val3, val4, val5, cidade = line.rstrip().split()
hdr.lat = float(lat) - 0.000001
hdr.lon = float(lon) - 0.000001
hdr.id = str(ids)
hdr.flag = 1
hdr.nlev = 1
hdr.t = 0.
ids += 1
print hdr.lat
""" Escreve """
of.write(hdr)
of.write(struct.pack('f', float(val1)))
of.write(struct.pack('f', float(val2)))
of.write(struct.pack('f', float(val3)))
of.write(struct.pack('f', float(val4)))
of.write(struct.pack('f', float(val5)))
hdr.nlev = 0
of.write(hdr)
of.close()
答案 0 :(得分:1)
由于没有任何样本输入,我将代码简化为仅使用样本数据创建二进制文件:
<强> C 强>
#include <stdio.h>
#include <string.h>
struct rpthdr
{
char id[8]; /* Character station id */
float lat; /* Latitude of report */
float lon; /* Longitude of report */
float t; /* Time in relative grid units */
int nlev; /* Number of levels following */
int flag; /* Level independent var set flag */
} hdr;
int main(void)
{
FILE* ofile;
float val1, val2, val3, val4, val5;
/* Open files */
ofile = fopen("arquivo1112.dat", "wb");
/* Write this report */
strcpy(hdr.id,"1234567");
hdr.lat = 45.123f;
hdr.lon = 110.456f;
hdr.nlev = 1;
hdr.flag = 2;
hdr.t = 1.0f;
val1 = 2.0f;
val2 = 3.0f;
val3 = 4.0f;
val4 = 5.0f;
val5 = 6.0f;
fwrite(&hdr, sizeof(struct rpthdr), 1, ofile);
fwrite(&val1, sizeof(float), 1, ofile);
fwrite(&val2, sizeof(float), 1, ofile);
fwrite(&val3, sizeof(float), 1, ofile);
fwrite(&val4, sizeof(float), 1, ofile);
fwrite(&val5, sizeof(float), 1, ofile);
}
<强>的Python 强>
from ctypes import *
import struct
class ReportHeader(Structure):
_fields_ = [('id', c_char * 8),
('lat', c_float),
('lon', c_float),
('t', c_float),
('nlev', c_int),
('flag', c_int)]
ofile = "teste.dat"
with open(ofile, "wb") as of:
hdr = ReportHeader()
hdr.id = b'1234567'
hdr.lat = 45.123
hdr.lon = 110.456
hdr.nlev = 1
hdr.flag = 2
hdr.t = 1.0
val1 = 2.0
val2 = 3.0
val3 = 4.0
val4 = 5.0
val5 = 6.0
of.write(hdr)
of.write(struct.pack('5f',val1,val2,val3,val4,val5))
结果(两个文件都是48个字节)
C:\test1.exe
C:\test2.py
C:\>fc arquivo1112.dat teste.dat
Comparing files arquivo1112.dat and TESTE.DAT
FC: no differences encountered
因此,当您为浮点数编辑代码时修复了代码,或者问题是处理输入数据。
答案 1 :(得分:0)
C代码使用float
,您的python代码使用d
格式为 double
;您必须使用f
来匹配C格式。