Advertisement
Advertisement
| 01.24.2008 at 07:43AM PST, ID: 23107949 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: |
***********C code need to be ported**************
#include <stdio.h>
/* Structure that describes a report header in a stn file */
struct rpthdr {
char id[8]; /* Station ID */
float lat; /* Latitude of Station */
float lon; /* Longitude of Station */
float t; /* Time in grid-relative 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 val;
/* Open files */
ifile = fopen ("rain.ch","r");
ofile = fopen ("rain.dat","wb");
if (ifile==NULL || ofile==NULL) {
printf("Error opening files\n");
return;
}
/* Read, write loop */
flag = 1;
while (fgets(rec,79,ifile)!=NULL) {
/* Format conversion */
sscanf (rec,"%i %i",&year,&month);
sscanf (rec+20," %g %g %g",&hdr.lat,&hdr.lon,&val);
for (i=0; i<8; i++) hdr.id[i] = rec[i+11];
/* Time group terminator if needed */
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 (&val,sizeof(float), 1, ofile);
}
hdr.nlev = 0;
fwrite (&hdr,sizeof(struct rpthdr), 1, ofile);
}
****************My Perl Code*********************
#!/usr/bin/perl
die "\nUsage: $0 infilename outfilename\n" if $#ARGV < 1;
($infilename, $outfilename) = @ARGV;
open(INF, "<$infilename") or die "\nCan't open $infilename for reading: $!\n";
open(OUTF, ">$outfilename") or die "\nCan't open $outfilename for writing: $!\n";
binmode OUTF;
$iFlag=1;
$n=1;
while (<INF>) {
# s/#.*//; # ignore comments by erasing them
# next if /^(\s)*$/; # skip blank lines
my($LINE)=$_;
chomp($LINE);
($STID,$LAT,$LON,$Yr,$Doy,$Hr,$Mn,$Sec,$TZen,$WZen,$SigZen,$PW,$SigPW,$Press,$Temp,$ZHD)= split(/\s+/,$LINE);
# print $PW,"\n";
if ( $iFlag == 1) {
$LastYr=$Yr;
$LastDoy=$Doy;
$LastHr=$Hr;
$LastMn=$Mn;
$LastSec=$Sec;
$iFlag=0 ;
}
if ( $LastYr != $Yr || $LastDoy != $Doy || $LastHr != $Hr || $LastMn != $Mn || $LastSec != $Sec ) {
$nLev=0;
print OUTF pack("A8",$STID), pack("d",$LAT),pack("d",$LON),pack("d",$TIM),pack("i",$nLev),pack("i",$nFlag);
print $n,"\n";
}
$nFlag=1;
$nLev=1;
$TIM=0.0;
$LastYr=$Yr;
$LastDoy=$Doy;
$LastHr=$Hr;
$LastMn=$SMn;
$LastSec=$Sec;
print $STID,$LAT,$LON,$TIM,$nLev,$nFlag;
print $PW;
print "\n";
# print OUTF pack("A8",$STID), pack("d",$LAT),pack("d",$LON),pack("d",$TIM),pack("i",$nLev),pack("i",$nFlag);
print OUTF pack("A8dddii",$STID,$LAT,$LON,$TIM,$nLev,$nFlag);
print OUTF pack("d",$PW);
}
$nLev=0;
print $STID,$LAT,$LON,$TIM,$nLev,$nFlag;
print $PW;
print "\n";
#print OUTF pack("A8",$STID), pack("d",$LAT),pack("d",$LON),pack("d",$TIM),pack("i",$nLev),pack("i",$nFlag);
print OUTF pack("A8dddii",$STID,$LAT,$LON,$TIM,$nLev,$nFlag);
close(OUTF) or die "Can't close $destfile: $!\n";
close(INF) or die "Can't close $srcfile: $!\n";
|