I'm writing a script from C to python to generate a station data in binary format to be utilized in GrADS. Here the documentation about the structure: http://cola.gmu.edu/grads/gadoc/aboutstationdata.html#station
Python script's generating a binary file, but GrADS can read only the first station and the binary file generated from C is very different of the python output file. I'm thinking the problem is when a write de struct (ReportHeader) in file.
The C script:
#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);
}
And my python script:
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()