Search code examples
rradiansjulian-date

Convert Julian days into radians (or similar)


I have a data frame with n rows each of which corresponds to a single event in space and time. The data frame has columns containing spatial coordinates and the date in Julian days as well as several other columns of additional data.

There are various things I would like to do with my data but as an example I want to rasterise some of the columns and output some maps. For most of my columns I can do this easily with something like this:

df.raster <- rasterize(df.sp, base.raster, field = "column", fun=median) 
plot(df.raster)

However, for Julian days this doesn't make sense because its cyclical. 365/366 is adjacent to 1 but R doesn't know this so using the median function isn't going to provide me with a meaningful number. I'm looking for a way to convert my column of Julian days into a new column which reflects this and enables me to create a raster of meaningful values for Julian day.

My Julian days column runs from 1-366 reflecting the day on which an event took place within a particular year. My data covers multiple years but my Julian days column starts from 1 again at the start of every year.

I've tried a few things including converting to radians but nothing has worked so far. Any help would be much appreciated!


Solution

  • To get what I wanted I first had to scale my "Julian days" column to degrees, then I could convert degrees to radians using the as_radians function in the aspace package and then I could use circular statistics on the radians:

    # Scale Julian days to degrees
    
    df$degrees <- (df$jday/366)*360
    
    # Convert degrees to radians
    
    df$radians <- as_radians(df$degrees)
    
    # Convert df to a spatial object
    
    df.sp <- df
    coordinates(df.sp) <- ~ x + y
    proj4string(df.sp) <- proj4string(coordinates)
    
    # Rasterise radians
    
    radians.raster <- rasterize(df.sp, base.raster, field = "radians", fun = mean.circular)
    
    # Plot rasterised radians
    
    plot(radians.raster)
    

    Currently the figures will be slightly inaccurate as (when converting to degrees) leap years should be divided by 366 and non-leap years by 365 but I'll fix this with a simple loop which looks up the year (also included in my df) for each row and uses 366/365 appropriately.