爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 26467|回复: 34

下垫面修改的write_geogrid.c的问题

[复制链接]
发表于 2014-8-17 14:38:40 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
最近在看下面面修改的问题,了解到下垫面修改的过程中,将新的高分辨率土地利用影像进行重分类,重采样,投影变换等一系列处理后,将结果装换成[size=16px]ascii码文件,然后使用write_geogrib.c文件,经过编译后生成a.out,运行之后就能生成需要格式的土地利用数据。然而,我在查看了一下原来read_geogrib.c和write_geogrib.c两个的源代码后,发现里面没有主函数,因此,是不是要再重新写一个影像转换的程序,在程序中调用这些函数才能完成从ascii文件转换成所要的二进制文件呢?本人有C语言的学习基础,对源代码里的有些部分不太懂,还请大家帮我解释一下,谢谢
下面附分别附上read_geogrid.c和 write_geogrid.c两个程序:
/* File: read_geogrid.c
   Sample subroutine to read an array from the geogrid binary format.
   Notes: Depending on the compiler and compiler flags, the name of
   the read_geogrid() routine may need to be adjusted with respect
   to the number of trailing underscores when calling from Fortran.
   Michael G. Duda, NCAR/MMM
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _UNDERSCORE
#define read_geogrid read_geogrid_
#endif
#ifdef _DOUBLEUNDERSCORE
#define read_geogrid read_geogrid__
#endif
#define BIG_ENDIAN    0
#define LITTLE_ENDIAN 1
int read_geogrid(
      char * fname,            /* The name of the file to read from */
      int * len,               /* The length of the filename */
      float * rarray,          /* The array to be filled */
      int * nx,                /* x-dimension of the array */
      int * ny,                /* y-dimension of the array */
      int * nz,                /* z-dimension of the array */
      int * isigned,           /* 0=unsigned data, 1=signed data */
      int * endian,            /* 0=big endian, 1=little endian */
      float * scalefactor,     /* value to multiply array elements by before truncation to integers */
      int * wordsize,          /* number of bytes to use for each array element */
      int * status)
{
   int i, ival, cnt, narray;
   int A2, B2;
   int A3, B3, C3;
   int A4, B4, C4, D4;
   unsigned char * c;
   char local_fname[1024];
   FILE * bfile;
   *status = 0;
   narray = (*nx) * (*ny) * (*nz);
   /* Make a null-terminated local copy of the filename */
   strncpy(local_fname,fname,*len);
   local_fname[*len]='\0';
   /* Attempt to open file for reading */
   if (!(bfile = fopen(local_fname,"rb")))
   {
      *status = 1;
      return 1;
   }
   /* Allocate memory to hold bytes from file and read data */
   c = (unsigned char *)malloc(sizeof(unsigned char)*(*wordsize) * narray);
   cnt = fread((void *)c, sizeof(unsigned char), narray*(*wordsize), bfile);

   fclose(bfile);
   if (cnt == 0)
   {
      *status = 1;
      return 1;
   }
   /*
      Set up byte offsets for each wordsize depending on byte order.
      A, B, C, D give the offsets of the LSB through MSB (i.e., for
      word ABCD, A=MSB, D=LSB) in the array from the beginning of a word
   */
   if (*endian == BIG_ENDIAN) {
      A2 = 0; B2 = 1;
      A3 = 0; B3 = 1; C3 = 2;
      A4 = 0; B4 = 1; C4 = 2; D4 = 3;
   }
   else {
      B2 = 0; A2 = 1;
      C3 = 0; B3 = 1; A3 = 2;
      D4 = 0; C4 = 1; B4 = 2; A4 = 3;
   }
   /* Convert words from native byte order */
   switch(*wordsize) {
      case 1:
         for(i=0; i<narray; i++)
         {
            ival = (int)(c);      
            if ((*isigned) && (ival > (1 << 7))) ival -= (1 << 8);
            rarray = (float)ival;
         }
         break;
      case 2:
         for(i=0; i<narray; i++)
         {
            ival = (int)((c[2*i+A2]<<8) | (c[2*i+B2]));      
            if ((*isigned) && (ival > (1 << 15))) ival -= (1 << 16);
            rarray = (float)ival;
         }
         break;
      case 3:
         for(i=0; i<narray; i++)
         {
            ival = (int)((c[3*i+A3]<<16) | (c[3*i+B3]<<8) | c[3*i+C3]);      
            if ((*isigned) * (ival > (1 << 23))) ival -= (1 << 24);
            rarray = (float)ival;
         }
         break;
      case 4:
         for(i=0; i<narray; i++)
         {
            ival = (int)((c[4*i+A4]<<24) | (c[4*i+B4]<<16) | (c[4*i+C4]<<8) | c[4*i+D4]);      
            if ((*isigned) && (ival > (1 << 31))) ival -= (1 << 32);
            rarray = (float)ival;
         }
         break;
   }
   free(c);
   /* Scale real-valued array by scalefactor */
   if (*scalefactor != 1.0)
   {
      for (i=0; i<narray; i++)
         rarray = rarray * (*scalefactor);
   }
   return 0;
}



/* File: write_geogrid.c
   Sample subroutine to write an array into the geogrid binary format.
   Side effects: Upon completion, a file named 00001-<NX>.00001-<NY> is
   created, where <NX> is the argument nx and <NY> is the argument ny,
   both in i5.5 format.
  
   Notes: Depending on the compiler and compiler flags, the name of
   the write_geogrid() routine may need to be adjusted with respect
   to the number of trailing underscores when calling from Fortran.
   Michael G. Duda, NCAR/MMM
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _UNDERSCORE
#define write_geogrid write_geogrid_
#endif
#ifdef _DOUBLEUNDERSCORE
#define write_geogrid write_geogrid__
#endif
#define BIG_ENDIAN    0
#define LITTLE_ENDIAN 1
int write_geogrid(
      float * rarray,          /* The array to be written */
      int * nx,                /* x-dimension of the array */
      int * ny,                /* y-dimension of the array */
      int * nz,                /* z-dimension of the array */
      int * isigned,           /* 0=unsigned data, 1=signed data */
      int * endian,            /* 0=big endian, 1=little endian */
      float * scalefactor,     /* value to divide array elements by before truncation to integers */
      int * wordsize )         /* number of bytes to use for each array element */
{
   int i, narray;
   int A2, B2;
   int A3, B3, C3;
   int A4, B4, C4, D4;
   unsigned int * iarray;
   unsigned char * barray;
   char fname[24];
   FILE * bfile;
   narray = (*nx) * (*ny) * (*nz);
   iarray = (unsigned int *)malloc(sizeof(int) * narray);
   barray = (unsigned char *)malloc(sizeof(unsigned char) * narray * (*wordsize));
   /* Scale real-valued array by scalefactor and convert to integers */
   for (i=0; i<narray; i++)
      iarray = (unsigned int)(rarray / (*scalefactor));
   /*
      Set up byte offsets for each wordsize depending on byte order.
      A, B, C, D give the offsets of the LSB through MSB (i.e., for
      word ABCD, A=MSB, D=LSB) in the array from the beginning of a word
   */
   if (*endian == BIG_ENDIAN) {
      A2 = 0; B2 = 1;
      A3 = 0; B3 = 1; C3 = 2;
      A4 = 0; B4 = 1; C4 = 2; D4 = 3;
   }
   else {
      B2 = 0; A2 = 1;
      C3 = 0; B3 = 1; A3 = 2;
      D4 = 0; C4 = 1; B4 = 2; A4 = 3;
   }
   /* Place words into storage byte order */
   switch(*wordsize) {
      case 1:
         for(i=0; i<narray; i++) {
            if (iarray < 0 && *isigned) iarray += (1 << 8);
            barray[(*wordsize)*i] = (unsigned char)(iarray & 0xff);
         }
         break;
      case 2:
         for(i=0; i<narray; i++) {
            if (iarray < 0 && *isigned) iarray += (1 << 16);
            barray[(*wordsize)*i+A2] = (unsigned char)((iarray >> 8) & 0xff);
            barray[(*wordsize)*i+B2] = (unsigned char)( iarray       & 0xff);
         }
         break;
      case 3:
         for(i=0; i<narray; i++) {
            if (iarray < 0 && *isigned) iarray += (1 << 24);
            barray[(*wordsize)*i+A3] = (unsigned char)((iarray >> 16) & 0xff);
            barray[(*wordsize)*i+B3] = (unsigned char)((iarray >> 8)  & 0xff);
            barray[(*wordsize)*i+C3] = (unsigned char)( iarray        & 0xff);
         }
         break;
      case 4:
         for(i=0; i<narray; i++) {
            if (iarray < 0 && *isigned) iarray += (1 << 32);
            barray[(*wordsize)*i+A4] = (unsigned char)((iarray >> 24) & 0xff);
            barray[(*wordsize)*i+B4] = (unsigned char)((iarray >> 16) & 0xff);
            barray[(*wordsize)*i+C4] = (unsigned char)((iarray >> 8)  & 0xff);
            barray[(*wordsize)*i+D4] = (unsigned char)( iarray        & 0xff);
         }
         break;
   }
   sprintf(fname,"%5.5i-%5.5i.%5.5i-%5.5i",1,*nx,1,*ny);
   /* Write array to file */
   bfile = fopen(fname,"wb");
   fwrite(barray,sizeof(unsigned char),narray*(*wordsize),bfile);
   fclose(bfile);
   free(iarray);
   free(barray);
   return 0;
}
密码修改失败请联系微信:mofangbao
发表于 2014-11-27 09:36:10 | 显示全部楼层
请问这个问题解决了吗 write_geogrid.c文件怎么编译的,我怎么编译的时候出现了很多警告,不让编译
密码修改失败请联系微信:mofangbao
回复 支持 1 反对 0

使用道具 举报

发表于 2014-10-24 17:09:09 | 显示全部楼层
请问这个问题解决了吗 直接a.out 运行出来的文件我的出错了
密码修改失败请联系微信:mofangbao
发表于 2014-11-27 09:37:05 | 显示全部楼层
或者说你这个write_geogrid.c程序是怎么用的,求指导
密码修改失败请联系微信:mofangbao
发表于 2015-4-9 16:56:18 | 显示全部楼层
清晨的雨 发表于 2014-11-27 09:36
请问这个问题解决了吗 write_geogrid.c文件怎么编译的,我怎么编译的时候出现了很多警告,不让编译

你能简单介绍下你怎么编译的吗?最近为这问题头疼中,谢谢。
密码修改失败请联系微信:mofangbao
发表于 2015-9-1 19:40:34 | 显示全部楼层
同问,哪位高手帮忙解答一下吧!
密码修改失败请联系微信:mofangbao
发表于 2015-9-2 14:28:03 | 显示全部楼层
同问,哪位高手帮忙解答一下好吗!
密码修改失败请联系微信:mofangbao
发表于 2015-10-13 10:36:31 | 显示全部楼层
这个write_geogrib.c文件在哪啊。。。
密码修改失败请联系微信:mofangbao
发表于 2015-10-15 10:18:59 | 显示全部楼层
dongqiru77 发表于 2015-10-13 10:36
这个write_geogrib.c文件在哪啊。。。

给需要的人,已经找到,路径:/WPS/geogrid/src
密码修改失败请联系微信:mofangbao
发表于 2015-10-23 11:20:28 | 显示全部楼层
我也为这个问题发愁,不知道怎么转换为二进制的文件。楼主要是解决了,能否告诉我
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表