Extract data from .nc file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract data from .nc file

Shailendra Pratap
Hi,
Please help me. I am trying to get information on "soi" from the NetCDF
file (please see the attached).

The file is showing this info-

float soi(time=2001, *MCrun=20, members=100*);
  :description = "soi";
  :long_name = "Southern Oscillation Index";
  :units = "";
  :level = "sfc";

And Dimentions----

5 dimensions:
        *time  Size:2001*
            description: time
            long_name: Time
            standard_name: time
            units: *days since 0000-01-01 00:00:00*
            calendar: noleap
            actual_range: 0
             *actual_range: 730000*
      *  MCrun  Size:20*


I am not aware of *MCrun* and *members *gaven in a file. I wants to
create csv output and ggplot (line) in form of years.

like this format---

*Longitude     Latitude     Year     soi*


Please help me, my script is here-

rm(list=ls(all=TRUE))
library(ncdf4)
library(ggplot2)
library(ncdf4.helpers)
setwd("D:/work/")
ncfile <-nc_open("posterior_climate_indices_MCruns_ensemble_full_LMRv2.1.nc
")
head(ncfile)
lon <- ncvar_get(ncfile, "lon_npac")
lat<- ncvar_get(ncfile, "lat_npac", verbose = F)
tt<- ncvar_get(ncfile, "time")
units<- ncatt_get(ncfile, "time", "units")
soi <- ncvar_get(ncfile, "soi")
soi_final = data.frame(cbind(lon, lat, tt, soi))

#output
write.csv(amo_final, "soi.csv", row.names = FALSE)

#plot
ggplot(data = soi_final, aes(x = time, y = soi)) +
  geom_line() +
  xlab("Year") + ylab("soi") +
  ggtitle("SOI from 1-2001") +
  theme_classic()


thank you

wish you good health.



Regards
S. Singh

 posterior_climate_indices_MCruns_ensemble_full_...
<https://drive.google.com/file/d/1r5PzXxShmeOcoBDXUk7Nfr3EzCqmQwGb/view?usp=drive_web>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Extract data from .nc file

R help mailing list-2
Hi Shailendra:

You didn't provide the error messages you received,  which makes it difficult to answer.  I will say here is at least one typo,  in:

> write.csv(amo_final, "soi.csv", row.names = FALSE)

You have only defined "soi_final". But I would also be surprised if either of the "cbind()" or "dataframe()" commands works,  as everything is of different sizes.

HTH,

-Roy

> On Feb 24, 2021, at 6:19 AM, Shailendra Pratap <[hidden email]> wrote:
>
> Hi,
> Please help me. I am trying to get information on "soi" from the NetCDF
> file (please see the attached).
>
> The file is showing this info-
>
> float soi(time=2001, *MCrun=20, members=100*);
>  :description = "soi";
>  :long_name = "Southern Oscillation Index";
>  :units = "";
>  :level = "sfc";
>
> And Dimentions----
>
> 5 dimensions:
>        *time  Size:2001*
>            description: time
>            long_name: Time
>            standard_name: time
>            units: *days since 0000-01-01 00:00:00*
>            calendar: noleap
>            actual_range: 0
>             *actual_range: 730000*
>      *  MCrun  Size:20*
>
>
> I am not aware of *MCrun* and *members *gaven in a file. I wants to
> create csv output and ggplot (line) in form of years.
>
> like this format---
>
> *Longitude     Latitude     Year     soi*
>
>
> Please help me, my script is here-
>
> rm(list=ls(all=TRUE))
> library(ncdf4)
> library(ggplot2)
> library(ncdf4.helpers)
> setwd("D:/work/")
> ncfile <-nc_open("posterior_climate_indices_MCruns_ensemble_full_LMRv2.1.nc
> ")
> head(ncfile)
> lon <- ncvar_get(ncfile, "lon_npac")
> lat<- ncvar_get(ncfile, "lat_npac", verbose = F)
> tt<- ncvar_get(ncfile, "time")
> units<- ncatt_get(ncfile, "time", "units")
> soi <- ncvar_get(ncfile, "soi")
> soi_final = data.frame(cbind(lon, lat, tt, soi))
>
> #output
> write.csv(amo_final, "soi.csv", row.names = FALSE)
>
> #plot
> ggplot(data = soi_final, aes(x = time, y = soi)) +
>  geom_line() +
>  xlab("Year") + ylab("soi") +
>  ggtitle("SOI from 1-2001") +
>  theme_classic()
>
>
> thank you
>
> wish you good health.
>
>
>
> Regards
> S. Singh
>
> posterior_climate_indices_MCruns_ensemble_full_...
> <https://drive.google.com/file/d/1r5PzXxShmeOcoBDXUk7Nfr3EzCqmQwGb/view?usp=drive_web>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

**********************
"The contents of this message do not reflect any position of the U.S. Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new street address***
110 McAllister Way
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: [hidden email] www: https://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected"
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Extract data from .nc file

Rasmus Liland-3
Dear Shailendra and Roy,

Yes, the info ncvar_get retrieves is of
different lengths.  soi_final does not
have a time variable for the x axis, and
tt is a integer vector
int [1:2001(1d)] 0 365 730 1095 1460 1825 2190 2555 2920 3285 ...

The file is a HDF5 file:

        rasmus@twosixty ~ % file posterior_climate_indices_MCruns_ensemble_full_LMRv2.1.nc
        posterior_climate_indices_MCruns_ensemble_full_LMRv2.1.nc: Hierarchical Data Format (version 5) data

I have read the HDF5 files dsk
https://github.com/GATB/dsk creates
before using hdf5r.  It's more
detailed for digging around in there:

        fname <- "posterior_climate_indices_MCruns_ensemble_full_LMRv2.1.nc"
        file <- hdf5r::h5file(fname, mode = "a")
        hdf5r::list.datasets(file)
        str(file)
        names(file)
        file[["lon_npac"]][["key_info"]][["space"]]
        names(file[["lon_npac"]][["key_info"]])
        file$close_all()

But, I think you perhaps don't want more
level of detail ...

Best,
Rasmus

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (849 bytes) Download Attachment