由于一些Fortran编译器(ibm xlf90)不支持form='Binary'方式,因此经常采用form='unformatted',access='sequential'方式写二进制文件。但是该方式生产的二进制文件,GrADS绘图软件经常打不开。因此写了生产GrADS绘图软件站点二进制文件的C语言函数库,因为考虑了Fortran参数传递采用引用传递,所以可以直接在Fortran程序中调用。
std.c :
/* These functions are writed by Yongjun Zheng
for createing GrADS station binary file.
Copyright (c) 2006 Yongjun ZHENG
Author : Yongjun ZHENG
Date : 5/9/2006
*/
#include <stdio.h>
#include <fcntl.h>
#ifdef UNDERSCORE
#define open_std open_std_
#define read_std_header read_std_header_
#define read_std_upper read_std_upper_
#define read_std_surface read_std_surface_
#define write_std_header write_std_header_
#define write_std_upper write_std_upper_
#define write_std_surface write_std_surface_
#define close_std close_std_
#endif
#define PERMS 0600
void open_std(int *unit, char *filename, int *action) {
if (*action==0) {
*unit = open(filename, O_RDONLY, PERMS);
if (*unit == -1)
printf("Error: can't open %s!/n", filename);
} else {
*unit = creat(filename, PERMS);
if (*unit == -1)
printf("Error: can't create %s!/n", filename);
}
}
void read_std_header(int *unit, char *stdid, float *lat, float *lon, float *time, int *nlev, int *flag) {
read(*unit, stdid, 8);
read(*unit, lat, 4);
read(*unit, lon, 4);
read(*unit, time, 4);
read(*unit, nlev, 4);
read(*unit, flag, 4);
}
void read_std_upper(int *unit, int *nlevel, int *nvar, float *p, float *var) {
int k, nz, nv;
nz=*nlevel;
nv=*nvar;
for (k=0; k<nz; k++) {
read(*unit, p+k, 4);
read(*unit, var+nv*k, 4*nv);
}
}
void read_std_surface(int *unit, int *nvar, float *var) {
int nv;
nv=*nvar;
read(*unit, var, 4*nv);
}
void write_std_header(int *unit, char *stdid, float *lat, float *lon, float *time, int *nlev, int *flag) {
write(*unit, stdid, 8);
write(*unit, lat, 4);
write(*unit, lon, 4);
write(*unit, time, 4);
write(*unit, nlev, 4);
write(*unit, flag, 4);
}
void write_std_upper(int *unit, int *nlevel, int *nvar, float *p, float *var) {
int k, nz, nv;
nz=*nlevel;
nv=*nvar;
for (k=0; k<nz; k++) {
write(*unit, p+k, 4);
write(*unit, var+nv*k, 4*nv);
}
}
void write_std_surface(int *unit, int *nvar, float *var) {
int nv;
nv=*nvar;
write(*unit, var, 4*nv);
}
void close_std(int *unit) {
close(*unit);
}
生成GrADS绘图软件站点二进制文件的C语言函数库
最新推荐文章于 2024-03-15 17:07:34 发布