简体   繁体   中英

Error while creating netcdf file using lat long time variable in Python

I have written this code to write the NETCDF file utilizing lat long time variable (eg precip). I am reading all datasets from csv files. Therefore, I have made two csv files: (1) containing precipitation time series data (rows 11, columns 9) and (2) containing lat & Longitude (eg X, Y). When I am running the code, NETCDF file is generating but it is not writing in a proper manner. Dimensions, time and other attribute information is coming wrong. My code is given below as

import pandas as pd
import numpy as np
import netCDF4

stn_precip='stn_precip.csv'
orig_precip='precip_data.csv'
stations = pd.read_csv(stn_precip)
stncoords = stations.iloc[:,:]
orig = pd.read_csv(orig_precip)

lons = stncoords['X']
lats = stncoords['Y']
nstations = np.size(lons)

ncout = netCDF4.Dataset('precip_3.nc', 'w')

ncout.createDimension('station',nstations)
ncout.createDimension('time',orig.shape[0])

lons_out = lons.tolist()
lats_out = lats.tolist()
time_out = orig.index.tolist()

lats = ncout.createVariable('latitude',np.dtype('float32').char,('station',))
lons = ncout.createVariable('longitude',np.dtype('float32').char,('station',))
time = ncout.createVariable('time',np.dtype('float32').char,('time',))
precip = ncout.createVariable('precip',np.dtype('float32').char,('time', 'station'))

lats[:] = lats_out
lons[:] = lons_out
time[:] = time_out
precip[:] = orig
ncout.close()

The csv files contain data like this

在此处输入图像描述

在此处输入图像描述

After execution of the above code with the given data, I am getting this (seems wrong)

<xarray.Dataset>
Dimensions:    (station: 9, time: 10)
Coordinates:
  * time       (time) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Dimensions without coordinates: station
Data variables:
    latitude   (station) float32 ...
    longitude  (station) float32 15.875 15.875 15.875 15.875 15.875 15.875 ...
    precip     (time, station) float32 ...

I want NETCDF output something like this

Dimensions:  (lat: 1, lon: 9, time: 11)
Coordinates:
  * time     (time) datetime64[ns] 1901-01-01 1901-01-02 1901-01-03 ...
  * lon      (lon) float64 80.875..............................82.875
  * lat      (lat) float64 15.875..............................15.875
Data variables:
    precip       (time, lat, lon) float32 ...
Attributes:
    CDI:          Climate Data Interface version 1.9.5 (http://mpimet.mpg.de/...
    Conventions:  CF-1.6
    history:      Sun Dec 30 02:15:30 2018: cdo -f nc import_binary rf.ctl RF...
    CDO:          Climate Data Operators version 1.9.5 (http://mpimet.mpg.de/...

Well, you can't have time,lat,lon as dimensions on your data because...they're not the dimensionality of your data. The precip data is dimensioned on (think function of) station and time, so those are your dimensions. This is because you don't have a grid where lat and lon are independently varying; instead, they both vary with what station you're looking at, which is why station is a dimension.

What it sounds like you might want, though, is for lat/lon to show up properly as coordinates for the precip variable. For that, you want add to your precip variable an attribute coordinates with the value 'latitude longitude' , as prescribed by the Climate and Forecasting (CF) metadata convention for netCDF. This tells tools following the conventions that latitude and longitude are auxiliary coordinate variables. The code below:

import numpy as np
import netCDF4

nstations = 3
lats = np.linspace(25, 50, nstations)
lons = np.linspace(-120, -60, nstations)
time_out = np.arange(5)
precip_out = np.random.randn(time_out.size, nstations)

ncout = netCDF4.Dataset('precip_3.nc', 'w')

ncout.createDimension('station',nstations)
ncout.createDimension('time', time_out.size)

lons_out = lons.tolist()
lats_out = lats.tolist()

lats = ncout.createVariable('latitude', 'float32', ('station',))
lons = ncout.createVariable('longitude', 'float32' , ('station',))
time = ncout.createVariable('time', 'float32', ('time',))
precip = ncout.createVariable('precip', 'float32', ('time', 'station'))
precip.coordinates = 'latitude longitude'

lats[:] = lats_out
lons[:] = lons_out
time[:] = time_out
precip[:] = precip_out
ncout.close()

yields the following xarray output for me:

Dimensions:    (station: 3, time: 5)
Coordinates:
    latitude   (station) float32 ...
    longitude  (station) float32 ...
  * time       (time) float32 0.0 1.0 2.0 3.0 4.0
Dimensions without coordinates: station
Data variables:
    precip     (time, station) float32 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM