python-xarray

Value error in multiplying xarray variable with 2D numpy array

被刻印的时光 ゝ 提交于 2019-12-12 03:34:02
问题 import xarray as xr xr.open_dataset(path_netcdf, chunks={'time': 10}) flow_data = hndl_tran['val'] new_arr = flow_data * vba I get this error: *** ValueError: total size of new array must be unchanged Here are the shapes of the 2 arrays: flow_data.shape (1165, 720, 1440) vba.shape (720L, 1440L) How can I fix this error? 回答1: Make your numpy into an xarray object before you do the multiplication: flow_data = xr.DataArray(hndl_tran['val']) or vice versa flow_data = np.array(flow_data) 回答2:

python-xarray rename data variable

折月煮酒 提交于 2019-12-12 02:44:03
问题 I've created a xarray.DataArray and I save it using xarray.DataArray.to_netcdf . I create it using: datatmp = np.full([nens, len(modanom.coords['time'].values), len(modanom.coords['latitude'].values), len(modanom.coords['longitude'].values)], np.nan) b = xr.DataArray(datatmp, coords=[range(1,nens + 1), modanom.coords['time'], modanom.coords['latitude'], modanom.coords['longitude']], dims=['ensemble', 'time', 'latitude', 'longitude']) i.e. I don't specify a name such as: b = xr.DataArray({

xarray/datetime64[ns]: remove or normalise time from datetime

元气小坏坏 提交于 2019-12-11 16:59:54
问题 I have a data array arr with coordinate 'time'. arr : <xarray.DataArray 'T' (time: 731)> array([244.40161, 244.39998, ..., 244.40936, 244.40549], dtype=float32) Coordinates: * time (time) datetime64[ns] 1979-01-01T09:00:00 ... 1980-12-31T09:00:00 Extracting the first 5 time coordinates, arr.time.values[:5] : array(['1979-01-01T09:00:00.000000000', '1979-01-02T09:00:00.000000000', '1979-01-03T09:00:00.000000000', '1979-01-04T09:00:00.000000000', '1979-01-05T09:00:00.000000000'], dtype=

Removing leap day from leap years in xarray dataset

做~自己de王妃 提交于 2019-12-11 15:54:55
问题 I have Netcdf file loaded in an xarray dataset and I want to make daily climatologies without the leap day that is, without 29th Feb included in it. I'm trying the Dataset.drop method by the syntax is not so intuitive for me. Here is the Dataset print(ds) >><xarray.Dataset> Dimensions: (lat: 1, lev: 1, lon: 720, time: 27133) Coordinates: * lon (lon) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5 * lev (lev) float32 1.0 * time (time) datetime64[ns] 2000-01-02T18:00:00 ... 2018-07-30

xarray slow to save netCDF

扶醉桌前 提交于 2019-12-11 15:39:49
问题 I find xarray to be very slow to save a dataset to netCDF. I suspect the reason for this is that the .to_netcdf() command first has to load the data before saving it. For an example I get the following timings: Example a) ds.to_netcdf(file_path) # ~6 minutes Example b) ds.load() # ~6 minutes ds.to_netcdf(file_path) # <1 second It would seem the slow down comes from loading. Is there any way circumventing this load or speeding this process up? An explicit example of (a) that I am working with

python xarray tick label size issue

纵饮孤独 提交于 2019-12-11 13:44:13
问题 I am new for xarray and cartopy. I want to ask how can I increase/decrease labelsize on x-/y- ticks. Following is my code. fig = plt.figure(figsize=(18,8)) ax = plt.axes(projection =ccrs.PlateCarree()) ax.add_feature(cartopy.feature.LAND,facecolor='wheat') ax.add_feature(cartopy.feature.OCEAN) ax.add_feature(cartopy.feature.STATES, linestyle='-',lw=1.0,edgecolor='white') ax.add_feature(cartopy.feature.BORDERS, linestyle='-',lw=2.5,edgecolor='white') gp = ds.isel(time=0).gpm.plot.pcolormesh(

Creating a new NetCDF from existing NetCDF file while preserving the compression of the original file

我只是一个虾纸丫 提交于 2019-12-11 09:53:07
问题 I am trying to create a new NetCDF file from an existing NetCDF file. I am only interested in using 12 variables from a list of 177 variables. You can find the sample NetCDF file from this ftp site here. I used the following code from a previous SO answer. You can find it here. import netCDF4 as nc file1 = '/media/sf_jason2/cycle_001/JA2_GPN_2PdP001_140_20080717_113355_20080717_123008.nc' file2 = '/home/sandbox/test.nc' toinclude = ['lat_20hz', 'lon_20hz', 'time_20hz', 'alt_20hz', 'ice_range

How to read a variable called “T” with xarray?

那年仲夏 提交于 2019-12-11 06:29:07
问题 All, This might be a FAQ, but my Google-fu has failed me. Namely, I read in a file generated by a weather model I work on with xarray a la: In [4]: data = xr.open_dataset("test_old.nc4") In [5]: data Out[5]: <xarray.Dataset> Dimensions: (lat: 49, lev: 48, lon: 96, time: 1) Coordinates: * lon (lon) float64 -180.0 -176.2 -172.5 -168.8 -165.0 -161.2 -157.5 ... * lat (lat) float64 -90.0 -86.25 -82.5 -78.75 -75.0 -71.25 -67.5 ... * lev (lev) float64 1e+03 975.0 950.0 925.0 900.0 875.0 850.0 825.0

xarray equivalent of pandas `qcut()` function

*爱你&永不变心* 提交于 2019-12-11 06:26:59
问题 I want to calculate the Decile Index - see the ex1-Calculate Decile Index (DI) with Python.ipynb . The pandas implementation is simple enough but I need help with applying the bin labels to a new variable / coordinate using the groupby_bins() functionality. working example (test dataset) import pandas as pd import numpy as np import xarray as xr time = pd.date_range('2010-01-01','2011-12-31',freq='M') lat = np.linspace(-5.175003, -4.7250023, 10) lon = np.linspace(33.524994, 33.97499, 10)

Loop through a dictionary of dataframes

冷暖自知 提交于 2019-12-10 23:56:43
问题 I have a set of dataframes that represent scenarios of demand that I have put into a dictionary. I need to loop through each dataframe in the dictionary to reindex and resample etc. and the return to the dictionary. The below code works perfectly when I loop through a list of dataframes but I need to maintain the identity of each scenario, hence the dictionary. This is the code that works with a list of dataframes: demand_dfs_list = [low_demand_df, med_low_demand_df, bc_demand_df, med_high