TransWikia.com

How to extract multiple values from specific coordinates and dates from a NetCDF time-series in R?

Geographic Information Systems Asked by LaraLopes on January 20, 2021

I need to extract information of a series of oceanographical variables in netCDF format for specific coordinates, each coordinate in a specific date as well. I’m using R and this is an example of my data.frame indicating the specific dates and coordinates from which I need the variable information:

    head(coord)
               id        date      lat       lon
    1 02C0412/168 2000-03-19 -2.790567 -40.51535
    2 02C1310/173 2000-06-24 -3.448806 -38.94047
    3 02C1611/174 2000-07-30 -3.238906 -39.23049
    4 02C1310/185 2000-09-10 -3.719167 -38.51279
    5 02C1212/183 2000-09-29 -3.722500 -38.48271
    6 02C0810/188 2001-02-01 -3.546642 -38.82553
...

I have downloaded NetCDF data from ERA5, including wind and current variables. Each file consists on hourly data for 7 variables for a year period, and I have a 20 year-series of such files. This is a look in one of this files, that can be accessed at this link:

ERA5_hourly_2000.nc (NC_FORMAT_64BIT):

 7 variables (excluding dimension variables):
    short u10[longitude,latitude,time]   
        scale_factor: 0.000294455302010294
        add_offset: -2.93219983995569
        _FillValue: -32767
        missing_value: -32767
        units: m s**-1
        long_name: 10 metre U wind component
    short v10[longitude,latitude,time]   
        scale_factor: 0.000315448506665416
        add_offset: 1.19679509557089
        _FillValue: -32767
        missing_value: -32767
        units: m s**-1
        long_name: 10 metre V wind component
    short acwh[longitude,latitude,time]   
        scale_factor: 4.00754661898261e-05
        add_offset: 2.15399693385504
        _FillValue: -32767
        missing_value: -32767
        units: m
        long_name: Altimeter corrected wave height
    short mwd[longitude,latitude,time]   
        scale_factor: 0.00549346166746507
        add_offset: 179.999191339803
        _FillValue: -32767
        missing_value: -32767
        units: Degree true
        long_name: Mean wave direction
    short mwp[longitude,latitude,time]   
        scale_factor: 0.000145147927045738
        add_offset: 8.31351985996592
        _FillValue: -32767
        missing_value: -32767
        units: s
        long_name: Mean wave period
    short wmb[longitude,latitude,time]   
        scale_factor: 0.0151374116857156
        add_offset: 502.992431294157
        _FillValue: -32767
        missing_value: -32767
        units: m
        long_name: Model bathymetry
    short swh[longitude,latitude,time]   
        scale_factor: 4.87830078138156e-05
        add_offset: 1.90321655741157
        _FillValue: -32767
        missing_value: -32767
        units: m
        long_name: Significant height of combined wind waves and swell

 3 dimensions:
    longitude  Size:49
        units: degrees_east
        long_name: longitude
    latitude  Size:41
        units: degrees_north
        long_name: latitude
    time  Size:8784
        units: hours since 1900-01-01 00:00:00.0
        long_name: time
        calendar: gregorian

2 global attributes:
    Conventions: CF-1.6
    history: 2020-10-07 22:50:31 GMT by grib_to_netcdf-2.16.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -S param -o /cache/data5/adaptor.mars.internal-1602109072.1308959-24210-3-38efe6b3-37bb-497a-bbe3-979f3877af72.nc /cache/tmp/38efe6b3-37bb-497a-bbe3-979f3877af72-adaptor.mars.internal-1602109072.1314538-24210-2-tmp.grib

I’ve opened the first variable (u10) as a raster brick and made a dayly average of the hourly values (I did that to each variable of the netCDF file).

u10.brick <- brick("C:/Users/.../ERA5/ERA5_hourly_2000.nc", varname = "u10")
index.d <- rep(seq(1,366, by=1),each=24) 
date24h <- seq(as.POSIXct("2000-01-01 00:00:00"), as.POSIXct("2000-12-31 23:00:00"), by="24 hours",tz="GMT")
u10.dayly <- stackApply(u10.brick, indices = index.d, fun = mean)
u10.dayly <- setZ(u10.dayly, date24h, "Date")  

Now that I have the dayly averages, I need to select the specific dates and coordinates.
I’ve seen how to extract a value directly from the netCDF file for a given location and timestep using index. But I need some kind of automated way of getting the coordinates in specific dates because a have a long list of locations so I will take forever doing this manually through index numbers. Maybe a loop would do it?

I’m new to this data format. Any help will be much appreciated.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP