Tag : Python

Method to write and read a single N-Dimension numpy array to a .npy file. Sample code import numpy as np #write a 3-dimension varaible trend38 np.save(‘indexes_trend38_sens_slope.npy’,trend38) #read the 3-dimention variable to trend38 (use different name is ok) trend38=np.load(‘indexes_trend38_sens_slope.npy’) My original full version code:Indexes_ERA5_GreatLakesRegion_sens_s..

Read more

Add multiple variables with the same dimension to an existing nc file. Read the target nc file and include the new variables into the file and save to the new file. Sample code import xarray as xr #read nc file fn1 = ‘Annual_average_temperature.nc’ TxTn90p10p=xr.open_dataset(fn1) #add new variables TxTn90p10p[‘TX90p’]=xr.DataArray(TX90p.astype(np.float32), coords=TxTn90p10p.coords, dims=TxTn90p10p.t2m.dims, attrs=TxTn90p10p.attrs) TxTn90p10p[‘TX10p’]=xr.DataArray(TX10p.astype(np.float32), coords=TxTn90p10p.coords, dims=TxTn90p10p.t2m.dims, attrs=TxTn90p10p.attrs) ..

Read more

from scipy.ndimage.filters import gaussian_filter lats = tp.coords[‘latitude’][:] lons = tp.coords[‘longitude’][:] ct_x=[-79.617,-75.717,-86.917,-93.783] ct_y=[43.667,45.383,49.767,51.067] ct_n=[‘Toronto’,’Ottawa’,’Geraldton’,’Red Lake’] projection = ccrs.PlateCarree() provinc_bodr = cartopy.feature.NaturalEarthFeature(category=’cultural’, name=’admin_1_states_provinces_lines’, scale=’50m’, facecolor=’none’, edgecolor=’k’) axes_class = (GeoAxes, dict(map_projection=projection)) # lons, lats = np.meshgrid(lons, lats) title_text=[“season=DJF(m/s)”, “season=MAM(m/s)”, “season=JJA(m/s)”, “season=SON(m/s)”] fig = plt.figure(figsize=(15,15)) axgr = AxesGrid(fig, 111, axes_class=axes_class, nrows_ncols=(1, 1), axes_pad=0.6, cbar_location=’right’, cbar_mode=’single’, cbar_pad=0.2, cbar_size=’2%’, label_mode=”) # ..

Read more

Introduction This article includes some Python functions for calculating the core climate extreme index. I post them here for my future reference, and share them with people who are interested in python or climate change. No guarantee of accuracy. The definitions of the core climate extreme indices 1.Description of Climate Extreme Indexes by OCDP 2.Definitions ..

Read more

import pandas as pd from datetime import datetime fn=’s-p-tsx-60-futures_01.csv’ sp=pd.read_csv(fn) sp=sp.rename(columns={‘ value’:’value’}) sp[‘date’]=pd.to_datetime(sp.date) sp[‘Year’]=pd.DatetimeIndex(sp[‘date’]).year sp[‘Month’]=pd.DatetimeIndex(sp[‘date’]).month sp[‘dayofweek’]=sp[‘date’].dt.dayofweek sp[‘dayofmonth’]=pd.DatetimeIndex(sp[‘date’]).day sp[‘dayofyear’]=pd.DatetimeIndex(sp[‘date’]).dayofyear sp.tail(5) date value Year Month dayofweek dayofmonth dayofyear 5176 2020-04-23 857.4 2020 4 3 23 114 5177 2020-04-24 868.7 2020 4 4 24 115 5178 2020-04-27 879.7 2020 4 0 27 118 5179 2020-04-28 888.5 2020 4 ..

Read more

start_date=dataWorld.index[0] end_date=dataWorld.index[-1] dateData=pd.date_range(start=start_date,end=end_date) dataWorld[‘Date’]=dateData dataWorld=dataWorld.set_index(‘Date’) forecastDays=60 dateForecast= pd.date_range(start=end_date,periods=forecastDays+1)[1:] dateObsForecast=dateData.append(dat..

Read more

GaussianModel Build-in model: GaussianModel(pdf) Build-in model: GaussianModel (CDF) LorentzianModel Build-in model: LorentzianModel(pdf) Build-in model: LorentzianModel (CDF) SplitLorentzianModel VoigtModel Build-in model: VoigtModel(pdf) Build-in model: VoigtModel (CDF) PseudoVoigtModel SkewedVoigtModel MoffatModel Build-in model:MoffatModel Pearson7Model Build-in model: Pearson7Model(pdf) StudentsTModel Build-in model: StudentsTModel(pdf) Build-in model: StudentsTModel (CDF) BreitWignerModel Build-in model: BreitWignerModel(pdf) LognormalModel Build-in model: LognormalModel(pdf) Build-in model: LognormalModel (CDF) DampedOcsillatorModel ..

Read more

This example is used for fitting world covid-19 cases number import numpy as np import pandas as pd from datetime import datetime from lmfit import Minimizer, Parameters, report_fit import chart_studio.plotly as py import cufflinks as cf def Cauchy_cumulative_hazard_fit(x,loc,scale,decaybase): decayterm=np.power(decaybase,(x-loc)) decayterm[np.whe..

Read more