zhanghouxing的个人博客分享 http://blog.sciencenet.cn/u/zhanghouxing

博文

DHSVM 径流 Subdaily to Daily

已有 4185 次阅读 2016-9-13 09:05 |系统分类:科研笔记|关键词:学者| 模型, 源代码, 程序

Update 2016/11/30

今天我又重新写了subdaily to monthly的程序,现在放在这里一并和大家分享。

截个图:


DHSVM_stream.rar


DHSVM模型流域出口的河川径流的格式(3小时为例):

现在要将Subdaily的streamflow转化为daily,我写了一个C的程序来实现,先贴结果.


源代码如下:


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MaxLine 1024


int main(int argc, char *argv[])

{

   FILE *InputFid, *OutputFid;

   int TempStrComp, CountForDay;

   long offset;

   char strline01[MaxLine];

   char StartLine[MaxLine];

   double TempDischarge, ReadDischarge, MeanDailyDischarge;

   char StrDate01[11],TempStrDate[11];

   const char  *input_name = "Streamflow.Only";

   const char  *Output_name = "Streamflow_daily.Only";

   char *TempStrDischarge;


   if ((InputFid=fopen(input_name,"r"))==NULL){

       printf("Open %s Failed", input_name);

       return;

   }


    if ((OutputFid=fopen(Output_name,"w+"))==NULL){

       printf("Open %s Failed", Output_name);

       return;

   }


  /* Write header, DATE OUTLET */

   fgets(strline01,MaxLine,InputFid);

   fprintf(OutputFid,"%s  %sn", "DATE", "Discharge(m^3/timestep)");


  /* Provide StartLine */

   fgets(strline01,MaxLine,InputFid);

   strncpy(TempStrDate, strline01, 10);

   TempStrDate[10]=''; /* it is important */


   /* No value in the second line */

   TempDischarge=0.0;

   CountForDay=0;


   while (!feof(InputFid))

   {


       /* read lines sequentially */

       fgets(strline01,MaxLine,InputFid);

       /* get the Date */

       strncpy(StrDate01, strline01,10);

       StrDate01[10]='';


       if (strcmp(TempStrDate,StrDate01)==0)

       {

           /* find the location of two spaces */

           TempStrDischarge=strchr(strline01,'  ');

           /* convert float to double */

           ReadDischarge=strtod(TempStrDischarge, NULL);


           TempDischarge += ReadDischarge;

           CountForDay += 1;

       }

       else

       {

           MeanDailyDischarge = TempDischarge/CountForDay;

           fprintf(OutputFid,"%s  %.4fn",TempStrDate, MeanDailyDischarge);


           /*--------- initiate in new day---------------- */

           /* find the location of two spaces */

           TempStrDischarge=strchr(strline01,'  ');

           /* convert float to double */

           ReadDischarge=strtod(TempStrDischarge, NULL);

           strncpy(TempStrDate, strline01, 10);

           TempDischarge=ReadDischarge;

           CountForDay=1;

       }

       /* Initiate each line at each step */

       strcpy(strline01," ");

   }

   fclose(InputFid);

   fclose(OutputFid);

   return 0;

}




https://m.sciencenet.cn/blog-922140-1002636.html

上一篇:C语言字符处理
下一篇:MATLAB 写cell到EXCEL文件

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-2 20:22

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部