透過R來讀取nc檔,並轉換成Excel可處理的檔案格式。

安裝套件並載入套件

# install.packages("ncdf4")
# install.packages("ncdf.tools")

library(ncdf4)
library(ncdf.tools) 

# load an user-created function, ReadNetCdfTimeData()
source("C:\\Users\\es901\\Documents\\dsR\\NetCDF\\ReadNetCdfTimeData.R")

設定要擷取的格點位置,並開啟檔案

xlon = 44 # lon[44] = 120.9375
ylat = 41 # lat[41] = 23.7202 

# set file name
ncfname <- "D:\\ncfile\\rcp26\\huss_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc" 
# --- open a NetCDF file -----------------------
ncin <- nc_open(ncfname) # open the file 

先用 print() 看看檔案裡的內容和格式,以便後續的資料處理

print(ncin) # View NetCDF information
## File D:\ncfile\rcp26\huss_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc (NC_FORMAT_CLASSIC):
## 
##      5 variables (excluding dimension variables):
##         double time_bnds[bnds,time]   
##         double lat_bnds[bnds,lat]   
##         double lon_bnds[bnds,lon]   
##         double height[]   
##             units: m
##             axis: Z
##             positive: up
##             long_name: height
##             standard_name: height
##         float huss[lon,lat,time]   
##             standard_name: specific_humidity
##             long_name: Near-Surface Specific Humidity
##             units: 1
##             original_name: SQ
##             cell_methods: time: mean (interval: 15 minutes)
##             cell_measures: area: areacella
##             history: 2011-03-10T05:34:32Z altered by CMOR: Treated scalar dimension: 'height'. 2011-03-10T05:34:32Z altered by CMOR: replaced missing value flag (1e+38) with standard missing value (1e+20).
##             coordinates: height
##             missing_value: 1.00000002004088e+20
##             _FillValue: 1.00000002004088e+20
##             associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_atmos_fx_CanESM2_rcp26_r0i0p0.nc areacella: areacella_fx_CanESM2_rcp26_r0i0p0.nc
## 
##      4 dimensions:
##         time  Size:1140   *** is unlimited ***
##             bounds: time_bnds
##             units: days since 1850-1-1
##             calendar: 365_day
##             axis: T
##             long_name: time
##             standard_name: time
##         lat  Size:64
##             bounds: lat_bnds
##             units: degrees_north
##             axis: Y
##             long_name: latitude
##             standard_name: latitude
##         lon  Size:128
##             bounds: lon_bnds
##             units: degrees_east
##             axis: X
##             long_name: longitude
##             standard_name: longitude
##         bnds  Size:2
## 
##     31 global attributes:
##         institution: CCCma (Canadian Centre for Climate Modelling and Analysis, Victoria, BC, Canada)
##         institute_id: CCCma
##         experiment_id: rcp26
##         source: CanESM2 2010 atmosphere: CanAM4 (AGCM15i, T63L35) ocean: CanOM4 (OGCM4.0, 256x192L40) and CMOC1.2 sea ice: CanSIM1 (Cavitating Fluid, T63 Gaussian Grid) land: CLASS2.7 and CTEM1
##         model_id: CanESM2
##         forcing: GHG,Oz,SA,BC,OC,LU,Sl (GHG includes CO2,CH4,N2O,CFC11,effective CFC12. Sl is the repeat of the 23rd solar cycle, years 1997-2008, after year 2008.)
##         parent_experiment_id: historical
##         parent_experiment_rip: r1i1p1
##         branch_time: 56940
##         contact: cccma_info@ec.gc.ca
##         references: http://www.cccma.ec.gc.ca/models
##         initialization_method: 1
##         physics_version: 1
##         tracking_id: 36711046-0a0a-46c1-9a76-41674e4fbc0f
##         branch_time_YMDH: 2006:01:01:00
##         CCCma_runid: IBZ
##         CCCma_parent_runid: IGM
##         CCCma_data_licence: 1) GRANT OF LICENCE - The Government of Canada (Environment Canada) is the 
## owner of all intellectual property rights (including copyright) that may exist in this Data 
## product. You (as "The Licensee") are hereby granted a non-exclusive, non-assignable, 
## non-transferable unrestricted licence to use this data product for any purpose including 
## the right to share these data with others and to make value-added and derivative 
## products from it. This licence is not a sale of any or all of the owner's rights.
## 2) NO WARRANTY - This Data product is provided "as-is"; it has not been designed or 
## prepared to meet the Licensee's particular requirements. Environment Canada makes no 
## warranty, either express or implied, including but not limited to, warranties of 
## merchantability and fitness for a particular purpose. In no event will Environment Canada 
## be liable for any indirect, special, consequential or other damages attributed to the 
## Licensee's use of the Data product.
##         product: output
##         experiment: RCP2.6
##         frequency: mon
##         creation_date: 2011-03-10T05:34:32Z
##         history: 2011-03-10T05:34:32Z CMOR rewrote data to comply with CF standards and CMIP5 requirements.
##         Conventions: CF-1.4
##         project_id: CMIP5
##         table_id: Table Amon (31 January 2011) 53b766a395ac41696af40aab76a49ae5
##         title: CanESM2 model output prepared for CMIP5 RCP2.6
##         parent_experiment: historical
##         modeling_realm: atmos
##         realization: 1
##         cmor_version: 2.5.4

讀取時間(使用 user-created function)

all.time <- ReadNetCdfTimeData(ncfname)
t <- all.time[[1]]$time                 # time before conversion
t.date <- all.time[[1]]$date.time       # time format = "YYYY-MM-DD HH:MM:SS"

依序讀取其他資料(nc檔使用完後請確實關閉,可節省記憶體空間)

# --- get longitude ---------------------------------------------------- 
lon <- ncvar_get(ncin, "lon") 
nlon <- dim(lon) # number of longitude

# --- get latitude -----------------------------------------------------
lat <- ncvar_get(ncin, "lat") 
nlat <- dim(lat) # number of latitude

# --- get specific_humidity --------------------------------------------
huss <- ncvar_get(ncin, "huss")
nc_close(ncin)

# --- get Surface Downwelling Shortwave Radiation ----------------------
ncfname <- "D:\\ncfile\\rcp26\\rsds_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
rsds <- ncvar_get(ncin, "rsds")
nc_close(ncin)

# --- get Surface Air Pressure -----------------------------------------
ncfname <- "D:\\ncfile\\rcp26\\ps_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
ps <- ncvar_get(ncin, "ps")
nc_close(ncin)

# --- get Near-Surface Air Temperature ---------------------------------
ncfname <- "D:\\ncfile\\rcp26\\tas_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
tas <- ncvar_get(ncin, "tas")
nc_close(ncin)

# --- get Daily Maximum Near-Surface Air Temperature -------------------
ncfname <- "D:\\ncfile\\rcp26\\tasmax_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
tasmax <- ncvar_get(ncin, "tasmax")
nc_close(ncin)

# --- get Daily Minimum Near-Surface Air Temperature -------------------
ncfname <- "D:\\ncfile\\rcp26\\tasmin_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
tasmin <- ncvar_get(ncin, "tasmin")
nc_close(ncin)

# --- get Eastward Near-Surface Wind -----------------------------------
ncfname <- "D:\\ncfile\\rcp26\\uas_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
uas <- ncvar_get(ncin, "uas")
nc_close(ncin)

# --- get Northward Near-Surface Wind ----------------------------------
ncfname <- "D:\\ncfile\\rcp26\\vas_Amon_CanESM2_rcp26_r1i1p1_200601-210012.nc"
ncin <- nc_open(ncfname)
vas <- ncvar_get(ncin, "vas")
nc_close(ncin)

只擷取所選經緯度的資料,並轉換資料格式

# --- extract data  ----------------------------------------------------
Torigin <- as.numeric(t)
TIME <- as.character(t.date)
HUSS <- as.numeric(huss[xlon, ylat, ])
RSDS <- as.numeric(rsds[xlon, ylat, ])
PS   <- as.numeric(ps[xlon, ylat, ])
TAS  <- as.numeric(tas[xlon, ylat, ])
TASMAX <- as.numeric(tasmax[xlon, ylat, ])
TASMIN <- as.numeric(tasmin[xlon, ylat, ])
UAS  <- as.numeric(uas[xlon, ylat, ])
VAS  <- as.numeric(vas[xlon, ylat, ])

# --- calculation  ----------------------------------------------------
WIND <- (UAS^2 + VAS^2)^0.5
PS <- PS / 100              # units hPa                
TAS <- TAS - 273.15         # units degree C  
TASMAX <- TASMAX - 273.15   # units degree C      
TASMIN <- TASMIN - 273.15   # units degree C   

儲存csv檔至指定資料夾

# --- save as CSV file ------------------------------------------------
dataframe <- data.frame(Torigin, TIME, PS, HUSS, RSDS, TAS, TASMAX, TASMIN, WIND)
write.csv(dataframe, file = "D:\\ncfile\\rcp26\\CanESM2_rcp26_200601_210012_TW.csv", row.names = F)