[gradsusr] Convert TXT to NetCDF
Dr.Gamal El Afandi
gamalafandy at yahoo.com
Wed Feb 1 08:22:27 EST 2012
Yes it is not complete because it is very big and can not be attached.
Anyway many thanks for your help.
With my kind regards
Gamal
___________________________________________________________
Gamal Salah El Afandi, Ph.D.
College of Agricultural, Environment and Nutrition Sciences
College of Engineering
Tuskegee University, Tuskegee Alabama USA
http://www.tuskegee.edu/
http://www.tuskegee.edu/academics/colleges/caens.aspx
http://www.tuskegee.edu/academics/colleges/ceps.aspx
http://maindb.unfccc.int/public/roe/expert_list.pl?id_country=66&;
http://www.itap.purdue.edu/pto/NATO_KEEO/partCoDir_en.html
http://www.ucar.edu/governance/iap/iapreps.shtml
>________________________________
> From: Yaqiang Wang <yaqiang.wang at gmail.com>
>To: Dr.Gamal El Afandi <gamalafandy at yahoo.com>; GrADS Users Forum <gradsusr at gradsusr.org>
>Sent: Tuesday, January 31, 2012 9:26 PM
>Subject: Re: [gradsusr] Convert TXT to NetCDF
>
>Your attached txt file is not complete because your program defined
>the array with 751*801. Following is a IronPython script sample to
>convert a txt data into NetCDF. Hope it will be helpful. The converted
>NetCDF data file and the figures are attached.
>
>import clr
>clr.AddReferenceByPartialName("System.Windows.Forms")
>clr.AddReferenceByPartialName("System.Drawing")
>from System import *
>from System.Windows.Forms import *
>from System.Drawing import *
>clr.AddReference("MeteoInfoC.dll")
>from MeteoInfoC import *
>from MeteoInfoC.Data import *
>from MeteoInfoC.Data.MeteoData import *
>
>import os.path
>from datetime import *
>import time
>
>#---- Set data folder
>DataDir = "F:\\Temp\\"
>
>#---- Define lon lat numbers
>lonNum = 751
>latNum = 801
>unDef = -999.0
>
>#---- Create netCDF data info object
>outfilepath = DataDir + "output.nc"
>outDataInfo = NetCDFDataInfo()
>outDataInfo.fileName = outfilepath
>outDataInfo.UNDEF = unDef
>outDataInfo.unlimdimid = 2
>
>#---- Add dimensions: lon, lat, time
>lonDim = outDataInfo.AddDimension("lon", lonNum)
>latDim = outDataInfo.AddDimension("lat", latNum)
>timeDim = outDataInfo.AddDimension("time", -1)
>
>#---- Add variables
>outDataInfo.AddVariable("lon", NetCDF4.NcType.NC_DOUBLE,
>Array[DimStruct]([lonDim]))
>outDataInfo.AddVariable("lat", NetCDF4.NcType.NC_DOUBLE,
>Array[DimStruct]([latDim]))
>outDataInfo.AddVariable("time", NetCDF4.NcType.NC_DOUBLE,
>Array[DimStruct]([timeDim]))
>outDataInfo.AddVariable("rain", NetCDF4.NcType.NC_DOUBLE,
>Array[DimStruct]([timeDim, latDim, lonDim]))
>#---- Add variable attributes
>outDataInfo.AddVariableAttribute("lon", "units", "degrees_east")
>outDataInfo.AddVariableAttribute("lon", "long_name", "longitude")
>outDataInfo.AddVariableAttribute("lat", "units", "degrees_north")
>outDataInfo.AddVariableAttribute("lat", "long_name", "latitude")
>outDataInfo.AddVariableAttribute("time", "units", "days since 1-1-1 00:00:00")
>outDataInfo.AddVariableAttribute("time", "long_name", "time")
>outDataInfo.AddVariableAttribute("rain", "units", "mm/day")
>outDataInfo.AddVariableAttribute("rain", "long_name", "precipitation")
>outDataInfo.AddVariableAttribute("rain", "missing_value", unDef)
>
>#---- Add global attributes
>outDataInfo.AddGlobalAttribute("title", "Daily rainfall estimate")
>outDataInfo.AddGlobalAttribute("institute", "Tuskegee University,
>Tuskegee Alabama USA")
>
>#---- Create netCDF file
>outDataInfo.CreateNCFile(outfilepath)
>
>#---- Write lon, lat, time data
>lonArray = Array.CreateInstance(Double, lonNum)
>latArray = Array.CreateInstance(Double, latNum)
>timeArray = Array.CreateInstance(Object, 1)
>
>lon1 = -20
>for i in range(lonNum):
> lon = lon1 + 0.1 * i
> lonArray[i] = lon
># print lon
>outDataInfo.WriteVar("lon", lonArray)
>
>lat1 = -40
>for i in range(latNum):
> lat = lat1 + 0.1 * i
> latArray[i] = lat
># print latArray[i]
>outDataInfo.WriteVar("lat", latArray)
>
>t = datetime.today()
>st = datetime(1, 1, 1)
>days = (t - st).days
>timeArray[0] = days
>#print timeArray[0]
>startArray = Array.CreateInstance(int, 1)
>countArray = Array.CreateInstance(int, 1)
>startArray[0] = 0
>countArray[0] = 1
>outDataInfo.WriteVara("time", startArray, countArray, timeArray)
>
>#---- Write rainfall data
>dataArray = Array.CreateInstance(Double, lonNum * latNum)
>#---- Open text data file
>infile = open(DataDir + "output.txt")
>i = 0
>for aline in infile:
> v = aline.split()[2]
> dataArray[i] = float(v)
># print i, dataArray[i]
> i = i + 1
>infile.close()
>#---- Write data
>startArray = Array.CreateInstance(int, 3)
>countArray = Array.CreateInstance(int, 3)
>startArray[0] = 0
>startArray[1] = 0
>startArray[2] = 0
>countArray[0] = 1
>countArray[1] = latDim.dimLen
>countArray[2] = lonDim.dimLen
>outDataInfo.WriteVara("rain", startArray, countArray, dataArray)
>
>#---- Close netCDF file
>outDataInfo.CloseNCFile()
>
>On Tue, Jan 31, 2012 at 11:55 AM, Dr.Gamal El Afandi
><gamalafandy at yahoo.com> wrote:
>> Dear all,
>> Could you please let me know if anyone have idea about converting the TXT or
>> ASCII data into NetCDF?
>> Please attached is the file required to be transformed into NetCDF.
>> Please below is the program I used to convert from binary to TXT
>>
>>
>> PROGRAM convert_rfe_bin2asc
>>
>> c cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
>> c Program: convert_rfe2.f
>> c
>> c Description: This program reads in a daily RFE (rainfall estimate) file
>> c (version 2) in .bin format and creates an ascii integer
>> c file. Each file contains rfe coordinate data ordered by
>> rows.
>> c Rainfall resolution is 0.1 degree with a domain of
>> c 20W-55E, 40S-40N.
>> c
>> c Inputs: 'input_file' = the 10-day binary precip file to be converted
>> c
>> c Output: 'output.txt' = the output ascii converted text file
>> c
>> c Modified: July, 2010 by Nick Novella (nicholas.novella at noaa.gov)
>> c
>> c cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
>>
>> REAL*4 rfe(751,801),arr(3,601551)
>> real*4 lat,lon
>> INTEGER:: i,j,ind
>> character*25:: input_file
>>
>> c ##### CHANGE FILE NAME BELOW TO INPUT FILE #####
>>
>> input_file="all_products.bin.20100621"
>>
>> c ##################################################
>>
>> c ##### Initialize the arrays #####
>> do i = 1,751
>> do j = 1,801
>> rfe (i,j) = -999
>> end do
>> end do
>>
>> c ##### Read in the input rainfall data #####
>> open (unit=22,file=input_file,access="direct",
>> 1 convert='big_endian',status="old",recl=751*801*4)
>> read (22, rec=1) rfe
>>
>> c ##### Open the output file #####
>> open (88,file="output.txt",access='sequential',
>> 1 status='unknown',form='formatted')
>>
>> c ##### The next code converts the input rainfall data. Each row is then
>> written
>> c to the output file.
>>
>> ind=1
>> lat= -40.0
>> lon= -20.0
>> do j=1,801
>> do i=1,751
>> arr(1,ind)=lat
>> arr(2,ind)=lon
>> arr(3,ind)=rfe(i,j)
>>
>> write (88,499) arr(1,ind), arr(2,ind), arr(3,ind)
>> 499 format(1X,f6.2,1X,f6.2,1X,f8.2)
>>
>> ind=ind+1
>> lon=lon+0.1
>> end do
>> lat=lat+0.1
>> lon=-20.0
>> end do
>>
>>
>>
>>
>>
>> END PROGRAM
>>
>> With my kind regards
>> Gamal
>> ___________________________________________________________
>> Gamal Salah El Afandi, Ph.D.
>> College of Agricultural, Environment and Nutrition Sciences
>> College of Engineering
>> Tuskegee University, Tuskegee Alabama USA
>>
>> http://www.tuskegee.edu/
>> http://www.tuskegee.edu/academics/colleges/caens.aspx
>> http://www.tuskegee.edu/academics/colleges/ceps.aspx
>> http://maindb.unfccc.int/public/roe/expert_list.pl?id_country=66&;
>> http://www.itap.purdue.edu/pto/NATO_KEEO/partCoDir_en.html
>> http://www.ucar.edu/governance/iap/iapreps.shtml
>>
>>
>>
>>
>> _______________________________________________
>> gradsusr mailing list
>> gradsusr at gradsusr.org
>> http://gradsusr.org/mailman/listinfo/gradsusr
>>
>
>
>
>--
>*************************************************
>Dr. Yaqiang Wang
>Chinese Academy of Meteorological Sciences (CAMS)
>46, Zhong-Guan-Cun South Avenue
>Beijing, 100081
>China
>
>yaqiang.wang at gmail.com
>
>http://www.meteothinker.com
>**************************************************
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gradsusr.org/pipermail/gradsusr/attachments/20120201/26242df3/attachment-0003.html
More information about the gradsusr
mailing list