How to put Timestamp at the start of the Month in CDO? - netcdf

Currently I am working with cmip6 models monthly precipitation .nc files. I want the dates to start from 1st day of each month.
Here is the part of result of cdo sinfo of input file :
1950-01-16 12:00:00 1950-02-15 00:00:00 1950-03-16 12:00:00 1950-04-16 00:00:00
As you can see the date starts from 16 or 15. Even after applying cdo --timestat_date first monmean to my input file, there was no change in dates of my output file. I tried on other models' files too but in vain.
My CDO ver:
CDI library version : 2.0.5
cgribex library version : 2.0.1
ecCodes library version : 2.26.0
NetCDF library version : 4.8.1 of Apr 25 2022 17:43:42 $
hdf5 library version : 1.12.1 threadsafe
exse library version : 1.4.2
FILE library version : 1.9.1

The easiest way to do this in CDO is probably to reset the time axis or set the day. In your case the dataset appears to start on 1st January 1950 and has every month since then. So, one the following ought to work.
cdo settaxis,1950-01-01,12:00:00,1mon infile outfile
cdo setday,1 infile outfile
Note that the command line option you used, --timestat_date first, assigns the first time available within each month to the output file when calculating the monthly mean. Thus it would have worked as desired if you had used daily data as input. However, as your input is already a monthly mean with a single timestep available, it will simply return the time from the original dataset in this case.
cdo --timestat_date first monmean infile outfile

Related

unable to open .dat files on R even with haven installed

So I use SGA tools for processing my images. It gives back results in .dat files. Now in order to work on this data in R, I tried to import the .dat file using the haven package. I installed haven and then its library, but I am not able to import data still and it gives this error message.
Error: Failed to parse C:/Users/QuRana/Desktop/SGA Tools/Plate_Image_Example (1).dat: This version of the file format is not supported.
When I use this command install.packages("haven"), haven is loaded, but then when I load library using library(haven) nothing appears on my console except for this
> library(haven)
Then when I use this code:
datatrial1 <- read_dta("C:/Users/QuRana/Desktop/SGA Tools/Plate_Image_Example (1).dat")
It gives me the error mentioned above. When I try converting my .dat file to a .csv file and load my data, the imported data adds additional "t" values before the values in columns except for the first one like this:
Flags: S - Colony spill or edge interference C - Low colony circularity
# row\tcol\tsize\tcircularity\tflags
1\t1\t4355\t0.9053\t
1\t2\t4456\t0.8401\t
1\t3\t3439\t0.8219\t
1\t4\t3215\t0.8707\t
All the t's before the numeric values are not what I want. Another issue that I am facing is I cannot install the gitter package on my R version which is R 4.2.2.
You can read your tab separated file like so `read.delim("file_path", header = TRUE, sep = "\t")

How to subset netcdf CRU V4.00 data in R using lat-lon?

I want to subset a region from cru global data. The "cmsaf" package , box_mergetime function can subset the CMIP5 and CORDEX .nc data but in CRU .nc data it gives following error.
>library(cmsaf)
>wd<-getwd()
>box_mergetime("tmp", wd, "cru_ts4.00.1901.1910", "output", 67,98,8,38)
get file information
[1] "vobjtovarid4: error #F: I could not find the requsted var (or dimvar) in the file!"
[1] "var (or dimvar) name: longitude"
[1] "file name: C:/Users/Deepak/Documents/eg/ip/cru_ts4.00.1901.1910.tmp.nc"
Error in vobjtovarid4(nc, varid, verbose = verbose, allowdimvar = TRUE) :
Variable not found
link of data file.
The reason for this error message was a missing standard name of longitude and latitude in CRU NetCDF data. I fixed this issue and with cmsaf version 1.8.0 it should work now. The cmsaf package offers similar functions as cdo (e.g., sellonlatbox, timmean, fldmean are included).
It is easy to do this from the command line with CDO before reading into R
Select a box:
cdo sellonlatbox,lon1,lon2,lat1,lat2 in.nc out.nc
Do a time mean:
cdo timmean in.nc out.nc
Do a space mean:
cdo fldmean in.nc out.nc
If you are using Ubuntu you can install cdo easily with
sudo apt-get install cdo
and under windows you can install it under CYGWIN. (update: rather than cygwin, these days far better to simply install linux subsystem under windows 10, very easy to do and then you have ubuntu straight out of the box)

Incomplete wav file from load.wave, saved by save.wave

If you record and save a wave file in R using the audio package, there is an error trying to load it again. The object is to store the audio files for later retrieval.
library(audio)
k = 3 # three seconds
x <- rep(NA_real_, 44100*2*k)
# record and save wav file
record(x, 44100, 2)
wait(k)
play(x)
save.wave(x, "test.wav")
# load file again
y <- load.wave("test.wav")
After the last command we get:
Error in load.wave("test.wav") : incomplete file
A previous post has suggested an extra data problem, but why wouldn't a file type be internally consistent within a package?
It looks like the version 0.1-5 on CRAN (https://cran.r-project.org/web/packages/audio/index.html) does not include the last commit / fix done in 2014 (http://www.rforge.net/audio/git.html).
When I clone the git project and re-build the audio package, I can now successfully run the following (which is broken using the CRAN package):
save.wave(audioSample(sin(1:48000/10), 48000), "test.wav")
play(load.wave("test.wav"))
Apart from this, there are two other potentially useful packages sound and tuneR on CRAN. They have been published more recently.

getSymbols (quantmod) giving wrong dates

I'm using the quantmod package to fetch stock data. The code
Data = getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
Results in an xts as expected, however on closer examination it shows a volume of trades for 2012-10-21 (October 21st) which was a sunday, and is therefore clearly erroneous. Several other sundays are also included. Unfortunately the errors surrounding the weekends seem to have moved the rest of the data out of alignment.
Has anyone experienced similar problems fetching tickers with quantmod before, and if so, are they aware of a way around them?
Thanks
As you mentioned in the comments, this looks like a timezone issue, possibly due to POSIX date conversion in the xts function (see this answer).
I am able to reproduce the issue in a fresh R session when Sys.getenv("TZ") is an empty character string. Setting the timezone to any valid timezone (not all tested), for example, "America/Chicago" yields expected dates, i.e., no Sundays:
In a fresh session (December 16th 2012 was a Sunday):
Sys.getenv("TZ")
# [1] ""
library(quantmod)
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-13" "2012-12-16" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20"
Then change the timezone
Sys.setenv(TZ="America/Chicago")
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-14" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20" "2012-12-21"
No Sundays.

quantmod 3d graphics

I am trying to use the code on the quantmod website for a 3d graph. I followed the instructions and entered the year as 2010 (since 2008 link was not found). However, when I enter this command at the R prompt:
chartSeries3d0(TR)
I get the following error:
Error in if (on == "years") { : missing value where TRUE/FALSE needed
I am relatively new to R, so can anyone help me fix it.
The Treasury has re-designed its website since that example was created, so the code is not downloading any data (no matter what year you enter). Instead of using the getUSTreasuries function, you can just pull the data from FRED.
library(quantmod)
source("http://www.quantmod.com/examples/chartSeries3d/chartSeries3d.alpha.R")
getSymbols("DGS1MO;DGS3MO;DGS6MO;DGS1;DGS2;DGS3;DGS5;DGS7;DGS10;DGS20;DGS30",
src="FRED")
TR <- merge(DGS1MO,DGS3MO,DGS6MO,DGS1,DGS2,DGS3,DGS5,
DGS7,DGS10,DGS20,DGS30, all=FALSE)
colnames(TR) <- c("1mo","3mo","6mo","1yr","2yr","3yr","5yr",
"7yr","10yr","20yr","30yr")
TR <- na.locf(TR)
chartSeries3d0(TR["2011"])

Resources