我有1545个工作站的纬度,我想从文件中提取每个工作站的数据。我可以提取一个特定的位置,但是我不知道如何一次提取许多电台并将其存储在单独的.csv
文件中。
以下代码仅适用于一个电台:
ls()
rm(list = ls())
library(ncdf4)
library(raster)
library(rgdal)
setwd("D:/HAII Data/Soil moisture comparison/ncfile")
SWI <- nc_open('SWI_20170101_to_20171231.nc')
print(SWI)
metadata <- capture.output(print(SWI))
lon <- ncvar_get(SWI,"lon")
lat <- ncvar_get(SWI, "lat")
time <- ncvar_get(SWI, "time")
start.date = as.Date('1970-01-01')
times.sec <- ncvar_get(SWI, 'time')
times.day <- times.sec/(24*3600)
datex <- start.date + times.day
print(datex)
time = datex
swi.array <- ncvar_get(SWI, "SWI10_SWI_100") # store the data in a 3-
dimensional array
dim(swi.array)
fillvalue <- ncatt_get(SWI, "SWI10_SWI_100", "_FillValue")
fillvalue
nc_close(SWI)
r_brick <- brick(swi.array, xmn=min(lat), xmx=max(lat), ymn=min(lon),
ymx=max(lon), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84
+no_defs+ towgs84=0,0,0"))
r_brick <- flip(t(r_brick), direction='y')
toolik_lon <- 102.0028191
toolik_lat <- 17.47608337
toolik_series <- extract(r_brick,
SpatialPoints(cbind(toolik_lon,toolik_lat)), method='bilinear')
toolik_df <- data.frame(time, SWI=t(toolik_series))
write.table(toolik_df, "D:/HAII Data/Soil moisture
comparison/data/csvfile/STN0964.csv",sep = ",")
toolik_df
#
1个变量(不包括尺寸变量):
浮动SWI10_SWI_100 [lon,lat,time]
_FillValue:NaN
long_name:土壤水分指数
单位:%
3 dimensions:
time Size:36 *** is unlimited ***
_FillValue: NaN
units: seconds since 1970-01-01
calendar: standard
lat Size:149
_FillValue: NaN
standard_name: latitude
long_name: latitude
units: degrees_north
lon Size:84
_FillValue: NaN
standard_name: longitude
long_name: longitude
units: degrees_east
1 global attributes:
Conventions: CF-1.6
答案 0 :(得分:0)
由于我没有示例日志,所以一切都是guess测的:
library(ncdf4)
library(raster)
library(rgdal)
files <- list.files(pattern = "\\.nc$") #list all nc files
length.files <- length(files)
station <- function(SWI){
#all routine will be here:
print(SWI)
metadata <- capture.output(print(SWI))
lon <- ncvar_get(SWI,"lon")
lat <- ncvar_get(SWI, "lat")
time <- ncvar_get(SWI, "time")
start.date = as.Date('1970-01-01')
times.sec <- ncvar_get(SWI, 'time')
times.day <- times.sec/(24*3600)
datex <- start.date + times.day
#print(datex)
time = datex
swi.array <- ncvar_get(SWI, "SWI10_SWI_100") # store the data in a 3-dimensional array
#dim(swi.array)
fillvalue <- ncatt_get(SWI, "SWI10_SWI_100", "_FillValue")
#fillvalue
r_brick <- brick(swi.array, xmn=min(lat), xmx=max(lat), ymn=min(lon),
ymx=max(lon), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84
+no_defs+ towgs84=0,0,0"))
r_brick <- flip(t(r_brick), direction='y')
toolik_lon <- 102.0028191 #is this a constant? if not take it out and made it as arg on for-loop
toolik_lat <- 17.47608337
toolik_series <- extract(r_brick,
SpatialPoints(cbind(toolik_lon,toolik_lat)), method='bilinear')
toolik_df <- data.frame(time, SWI=t(toolik_series))
#write.table(toolik_df, "D:/HAII Data/Soil moisture comparison/data/csvfile/STN0964.csv",sep = ",")
#toolik_df #commented out unless you need to see it?
return(toolik_df)
}
#using for-loop to see inner workings
output.list <- list()
for(i in seq(from=1, to=length.files, by=1)){
file <- files[i]
input.swi <- nc_open(file)
output <- station(input.swi)
#output.list[[i]] <- output #uncomment if you want to save it into list
write.table(output, paste0(i, ".csv", sep = ",")
nc_close(input.swi)
}
答案 1 :(得分:0)
天哪,您陷入了很多麻烦。比这容易得多。
library(ncdf4)
library(raster)
SWI <- brick("D:/HAII Data/Soil moisture comparison/ncfile/SWI_20170101_to_20171231.nc")
#example points
lon <- c(102, 103)
lat <- c(17, 18)
lonlat <- cbind(lon, lat)
toolik_series <- extract(SWI, lonlat, method='bilinear')