[R] Please help
Pascal Oettli
kridox at ymail.com
Fri Jul 6 09:04:33 CEST 2012
Hello,
I did a mistake in my script:
> hour <- day*24; hour <- c(hour[1],cumsum(hour[1:length(hour)-1]))
should be replaced by:
> hour <- day*24; hour <- c(0,cumsum(hour[1:length(hour)-1]))
Regards
Le 05/07/2012 10:49, Pascal Oettli a écrit :
> Hello,
>
> A short code to include real monthly calendar to your data, similar to
> the one inside NCEP2 reanalysis.
>
> library(ncdf)
>
> lon <- seq(from=140.0251, to=146.6751, length.out=241)
> lat <- seq(from=-38.975, to=-31.025, length.out=160)
> x=dim.def.ncdf("Lon","degreesE",as.double(lon))
> y=dim.def.ncdf("Lat","degreesN",as.double(lat))
>
>
> y1 = 1800 # start of the period
> y2 = 2012 # end of the period
>
> year <- seq(y1,y2,1)
> day <-
> c(31,28,31,30,31,30,31,31,30,31,30,31)%*%matrix(1,1,length(year));
> day[2,leap.year(year)] <- 29; day <- as.vector(day)
> hour <- day*24; hour <- c(hour[1],cumsum(hour[1:length(hour)-1]))
> year <- rep(year,each=12)
>
> syntime <- hour[year==1968] #change the year or change this line to
> include more years
> t=dim.def.ncdf("Time","hours since 1800-1-1 00:00:00",syntime,unlim=TRUE)
>
>
> And your "mrain" matrix should have 3 dimensions (lon x lat x time),
> before to save it as a NetCDF file.
>
> Regards.
>
>
> Le 04/07/2012 23:13, Jun Chen a écrit :
>>
>> Hi Pascal,
>> I think I figure it out.
>> By doing the following, I can made "lon" & "lat" become a
>> class of dim.ncdf:
>> lon <- seq(from=140.0251, to=146.6751, length.out=241)
>> lat <- seq(from=-38.975, to=-31.025, length.out=160)
>> x=dim.def.ncdf("Lon","degreesE",as.double(lon))
>> y=dim.def.ncdf("Lat","degreesN",as.double(lat))
>>
>> However, after running the script, there is a error I really
>> unable to understand:
>> error at data.frame(..., check.names = FALSE) :
>> parameter value mean different rows 0, 31981
>>
>> If you can understand what the problem is, please as kind as
>> offer me a direction.
>>
>>
>>
>> Many thanks,
>> Jun
>>
>>
>>
>>
>>> Date: Wed, 4 Jul 2012 10:39:29 +0100
>>> From: kridox at ymail.com
>>> Subject: Re: [R] Please help
>>> To: chenshong at hotmail.com
>>> CC: r-help at r-project.org
>>>
>>> Hello,
>>>
>>> Following lines are wrong:
>>>> x=dim.def.ncdf("Lon","degreesE",140.0251:146.6751)
>>>> y=dim.def.ncdf("Lat","degreesN",(-31.025):(-38.975))
>>>
>>> You have 241 longitudes and 160 latitudes.
>>> lon <- seq(from=140.0251, to=146.6751, length.out=241)
>>> lat <- seq(from=-38.975, to=-31.025, length.out=160)
>>>
>>> Regards
>>>
>>>
>>>
>>> ----- Mail original -----
>>> De : Jun Chen <chenshong at hotmail.com>
>>> À : r-help at r-project.org
>>> Cc :
>>> Envoyé le : Mercredi 4 juillet 2012 10h52
>>> Objet : [R] Please help
>>>
>>>
>>> Dear All,
>>> I am a research student in environment. I have only little
>>> programming knowledge. I am currently doing the last project about
>>> rainfall impact on ground water quality in an area. It happens that I
>>> have to use R to read rainfall data (3 dimension) from ASC file
>>> (*.asc), and then write them into one NCDF file (*.nc).
>>>
>>> I have been working very hard on study R, but I still can
>>> not fix the problem. Could someone please as kind as point out that
>>> what the problems are in my R script?
>>>
>>> Firstly, this is an example of data in asc file:
>>> NCOLS 241
>>>
>>> NROWS 160
>>>
>>> XLLCORNER 140.00012207031
>>>
>>> YLLCORNER -39.000000000000
>>>
>>> CELLSIZE 0.50000000000000E-01
>>>
>>> NODATA_VALUE -99.00000
>>>
>>> 166.30 160.87 155.23 149.33 143.83 138.52
>>> 133.29 128.34 123.76 119.21
>>>
>>> 115.06 110.90 107.22 103.69 100.40 97.29
>>> 94.58 92.15 90.00 87.91
>>>
>>> 86.20 84.57 83.22 81.94 81.11 80.38 79.37
>>> 78.73 79.70 79.62
>>>
>>> -----------------------------------------------------------------------------------------------------------------------
>>>
>>>
>>>
>>>
>>> And then this is the script I wrote:
>>>
>>> setwd("E:/grid")
>>>
>>> #defining dimension
>>>
>>> x=dim.def.ncdf("Lon","degreesE",140.0251:146.6751)
>>>
>>> y=dim.def.ncdf("Lat","degreesN",(-31.025):(-38.975))
>>>
>>> t=dim.def.ncdf("Time","1968-01",1:12,unlim=TRUE)
>>>
>>>
>>>
>>> #setup variable
>>>
>>> varmr=var.def.ncdf("mr","mm",list(x,y,t),-99.00,
>>>
>>> longname="monthly rainfall")
>>>
>>>
>>> #create ncdf file
>>>
>>> ncnew=create.ncdf("rainfall.nc", varmr)
>>>
>>>
>>> #read input
>>>
>>> files=list.files(pattern=".asc")
>>>
>>> mrain=matrix(0:0,0,3)
>>>
>>>
>>> for(i in files)
>>>
>>> {rainfall=data.frame(readAsciiGrid(i))
>>>
>>> mrain=cbind(mrain,rainfall)
>>>
>>> }
>>>
>>> put.var.ncdf(ncnew, mrain)
>>>
>>>
>>> close.ncdf(ncnew)
>>>
>>> -------------------------------------------------------------------------------
>>>
>>>
>>> [[elided Hotmail spam]]
>>>
>>>
>>>
>>>
>>>
>>> Many thanks,
>>> Jun
>>>
>>>
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list