<div dir="ltr"><div>Caros,<br><br>Na semana passada escrevi para lista sobre como obter dados de temperatura do mar e clorofila em arquivos HDF. Estava com problemas para reprojetar um mapa.<br><br></div>Depois de muita leitura escrvi um script para fazer o trabalho em lote para ambas variáveis utilizando arquivos nc, Compartilho-os abaixo e espero que sejam úteis para mais alguém.<br><div><br></div><div>Sugestões para melhorias ou avisos de erros são bem vindos.<br></div><div><br>Sds<br><br>Antonio Olinto<br><br><b>SST<br>===</b><br><br># Antonio Olinto Avila-da-Silva, Instituto de Pesca, Brasil<br># script to process Aqua MODIS Sea Surface Temperature<br># Monthly means 9 km resolution<br># files downloaded from <a href="http://oceancolor.gsfc.nasa.gov/cgi/l3">http://oceancolor.gsfc.nasa.gov/cgi/l3</a><br># all .L3m_MO_SST_sst_9km.nc files must be in the working directory<br># the script will open each nc file, read lon, lat and sst data,<br># select data from a specific area and write them into<br># a single csv file named MODISA_sst.csv<br># Some reference pages<br># <a href="http://geog.uoregon.edu/GeogR/topics/netCDF-read-ncdf4.html">http://geog.uoregon.edu/GeogR/topics/netCDF-read-ncdf4.html</a><br># <a href="https://scottishsnow.wordpress.com/2014/08/24/many-rastered-beast/">https://scottishsnow.wordpress.com/2014/08/24/many-rastered-beast/</a><br><br># load libraries<br># ncdf4 needs libnetcdf-dev netcdf-bin in Linux<br># install.packages(c("ncdf4","reshape2"))<br>library("ncdf4")<br>library("reshape2")<br><br># set working directory<br>setwd("/mnt/Dados02/MODIS/SST")   # indicate the path to the files<br>file.exists("MODISA_sst.csv")     # caution new data will be appended to this file if it already exists<br># file.rename("MODISA_sst.csv","MODISA_sst.old")<br># file.remove("MODISA_sst.csv")<br><br># list and remove objects<br>ls()<br>rm(list = ls())<br><br># set the study area<br>lonmax<--40<br>lonmin<--50<br>latmax<--22<br>latmin<--29<br><br># create a list of files and indicate its length<br>(f <- list.files(".", pattern="*.L3m_MO_SST_sst_9km.nc",full.names=F))<br>(lf<-length(f))<br><br># variable<br>var<-"sst"<br><br>for (i in 1:lf) {<br>  # progress indicator<br>  print(paste("Processing file",i,"from",length(f),sep=" "))<br>  # open netCDF file<br>  data<-nc_open(f)<br>  # extract data<br>  lon<-ncvar_get(data,"lon")<br>  lat<-ncvar_get(data,"lat")<br>  value<-ncvar_get(data,var)<br>  unit<-ncatt_get(data,var,"units")$value<br>  # matrix to data.frame<br>  dimnames(value)<-list(lon=lon,lat=lat)<br>  dat.var<-melt(value,id="lon")<br>  # select data from the study area taking out missing data<br>  dat.varSAtmp<-subset(dat.var,lon<=lonmax & lon>=lonmin & lat<=latmax & lat>=latmin & value<45)<br>  # extract date information<br>  dateini<-ncatt_get(data,0,"time_coverage_start")$value<br>  dateend<-ncatt_get(data,0,"time_coverage_end")$value<br>  datemean<-mean(c(as.Date(dateend,"%Y-%m-%dT%H:%M:%OSZ"),as.Date(dateini,"%Y-%m-%dT%H:%M:%OSZ")))<br>  year<-substring(datemean,0,4)<br>  month<-substring(datemean,6,7)<br>  # prepare final data set<br>  dat.varSA<-data.frame(rep(as.integer(year,nrow(dat.varSAtmp))),rep(as.integer(month,nrow(dat.varSAtmp))),<br>  dat.varSAtmp,rep(unit,nrow(dat.varSAtmp)),rep(var,nrow(dat.varSAtmp)))<br>  names(dat.varSA)<-c("year","month","lon","lat","value","unit","var")<br>  # save csv file<br>  fe<-file.exists("MODISA_sst.csv")<br>  write.table(dat.varSA,"MODISA_sst.csv",row.names=FALSE,col.names=!fe,sep=";",dec=",",append=fe)<br>  # close connection<br>  nc_close(data)<br>  # clean workspace<br>  rm(data,lon,lat,value,unit,dat.var,dat.varSAtmp,dateini,dateend,datemean,year,month,dat.varSA,fe)<br>}<br>rm(var,f,i,latmax,latmin,lf,lonmax,lonmin)<br><br><b>CHL_a<br>=====</b><br><br># Antonio Olinto Avila-da-Silva, Instituto de Pesca, Brasil<br># script to process Aqua MODIS Chlorophyll Concentration OCx Algorithm<br># Monthly means 9 km resolution<br># see <a href="http://oceancolor.gsfc.nasa.gov/WIKI/OCChlOCI.html">http://oceancolor.gsfc.nasa.gov/WIKI/OCChlOCI.html</a><br># files downloaded from <a href="http://oceancolor.gsfc.nasa.gov/cgi/l3">http://oceancolor.gsfc.nasa.gov/cgi/l3</a><br># all .L3m_MO_CHL_chlor_a_9km.nc files must be in the working directory<br># the script will open each nc file, read lon, lat and chl data,<br># select data from a specific area and write them into<br># a single csv file named MODISA_chl.csv<br># Some reference pages<br># <a href="http://geog.uoregon.edu/GeogR/topics/netCDF-read-ncdf4.html">http://geog.uoregon.edu/GeogR/topics/netCDF-read-ncdf4.html</a><br># <a href="https://scottishsnow.wordpress.com/2014/08/24/many-rastered-beast/">https://scottishsnow.wordpress.com/2014/08/24/many-rastered-beast/</a><br><br># load libraries<br># ncdf4 needs libnetcdf-dev netcdf-bin in Linux<br># install.packages(c("ncdf4","reshape2"))<br>library("ncdf4")<br>library("reshape2")<br><br># set working directory<br>setwd("/mnt/Dados02/MODIS/CHL")   # indicate the path to the files<br>file.exists("MODISA_chl.csv")     # caution new data will be appended to this file if it already exists<br># file.rename("MODISA_chl.csv","MODISA_chl.old")<br># file.remove("MODISA_chl.csv")<br><br># list and remove objects<br>ls()<br>rm(list = ls())<br><br># set the study area<br>lonmax<--40<br>lonmin<--50<br>latmax<--22<br>latmin<--29<br><br># create a list of files and indicate its length<br>(f <- list.files(".", pattern="*.L3m_MO_CHL_chlor_a_9km.nc",full.names=F))<br>(lf<-length(f))<br><br># variable<br>var<-"chlor_a"<br><br>for (i in 1:lf) {<br>  # progress indicator<br>  print(paste("Processing file",i,"from",length(f),sep=" "))<br>  # open netCDF file<br>  data<-nc_open(f)<br>  # extract data<br>  lon<-ncvar_get(data,"lon")<br>  lat<-ncvar_get(data,"lat")<br>  value<-ncvar_get(data,var)<br>  unit<-ncatt_get(data,var,"units")$value<br>  # matrix to data.frame<br>  dimnames(value)<-list(lon=lon,lat=lat)<br>  dat.var<-melt(value,id="lon")<br>  # select data from the study area taking out missing data<br>  dat.varSAtmp<-na.omit(subset(dat.var,lon<=lonmax & lon>=lonmin & lat<=latmax & lat>=latmin))<br>  # extract date information<br>  dateini<-ncatt_get(data,0,"time_coverage_start")$value<br>  dateend<-ncatt_get(data,0,"time_coverage_end")$value<br>  datemean<-mean(c(as.Date(dateend,"%Y-%m-%dT%H:%M:%OSZ"),as.Date(dateini,"%Y-%m-%dT%H:%M:%OSZ")))<br>  year<-substring(datemean,0,4)<br>  month<-substring(datemean,6,7)<br>  # prepare final data set<br>  dat.varSA<-data.frame(rep(as.integer(year,nrow(dat.varSAtmp))),rep(as.integer(month,nrow(dat.varSAtmp))),<br>  dat.varSAtmp,rep(unit,nrow(dat.varSAtmp)),rep(var,nrow(dat.varSAtmp)))<br>  names(dat.varSA)<-c("year","month","lon","lat","value","unit","var")<br>  # save csv file<br>  fe<-file.exists("MODISA_chl.csv")<br>  write.table(dat.varSA,"MODISA_chl.csv",row.names=FALSE,col.names=!fe,sep=";",dec=",",append=fe)<br>  # close connection<br>  nc_close(data)<br>  # clean workspace<br>  rm(data,lon,lat,value,unit,dat.var,dat.varSAtmp,dateini,dateend,datemean,year,month,dat.varSA,fe)<br>}<br>rm(var,f,i,latmax,latmin,lf,lonmax,lonmin)<br></div><div class="gmail_extra"><br></div></div>