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..
Category : Python
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) ..
pr_data[‘PrTot’]=xr.DataArray(PrTot.astype(np.float32), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) pr_data[‘R1mm’]=xr.DataArray(R1mm.astype(np.int16), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) pr_data[‘R10mm’]=xr.DataArray(R10mm.astype(np.int16), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) pr_data[‘R20mm’]=xr.DataArray(R20mm.astype(np.int16), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) pr_data[‘SDII’]=xr.DataArray(SDII.astype(np.float32), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) pr_data[‘Rx1day’]=xr.DataArray(Rx1day.astype(np.float32), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) pr_data[‘Rx5day’]=xr.DataArray(Rx5day.astype(np.float32), coords=pr_data.coords, dims=pr_data.tp.dims, attrs=pr_data.attrs) See example code: /media/Data1/OnClimate/Precipitation_indexes_EAR5_GreatLakesRegion.ipynb Pay attention to line: dims=pr_dat..
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 ..
import netCDF4 as nc import datetime import netcdftime fn = ‘/media/Data1/workdir/GANs/Data/reanalysis-era5-land_T2mPrAreaLat515545Lon_8885.nc’ ds = nc.Dataset(fn) nctime=ds[‘time’][:] t_cal=ds[‘time’].calendar t_unit = ds.variables[‘time’].units datevar = [] datevar.append(netcdftime.num2date(nctime,units = t_unit,calendar = t_cal..
Following is my python code to plot stock chart on a background image from local file. The code is not optimal, I will improve it later. Hope it be useful. The example is plot ABX.TO.csv (download from yahoo finance) data on a logo collection image (my file:/media/Data1/XIU/AACodes/NASDAQ_stocks.png). Code # plot stock data with a background-image ..
from pyspark.sql import SparkSession from pyspark.sql.types import * #data types from pyspark.sql import functions as F #functions spark=SparkSession.builder.appName(‘XIU-Daily’).getOrCreate() input_fn = ‘s-p-tsx-60-futures_01.csv’ df = spark.read.csv(input_fn,header=True,inferSchema=True) df.show(3) +——————-+—–+ | date|value| +——————-+—–+ |1999-09-07 00:00:00|416.5| |1999-09-08 00:00:00|417.2| |1999-09-09 00:00:00|421.5| +——————-+—–+ df=df.withColumn(‘Date’,F.date_format(‘date’,’yyyy-MM-dd’)) #change date format df=df.withColumn(‘current_date’,F.current_date()) #current date df=df.withColumn(‘year’,F.year(‘date’)) df=df.withColumn(‘month’,F.month(‘date’)) df=df.withColumn(‘dayofmonth’,F.dayofmonth(‘date’)) df=df.withColumn(‘minute’,F.minute(‘date’)) df=df.withColumn(‘second’,F.second(‘date’)) df=df.withColumn(‘dayofyear’,F.dayofyear(‘date’)) df=df.withColumn(‘dayofweek’,F.dayofweek(‘date’)) df=df.withColumn(‘weekofyear’,F.weekofyear(‘date’)) df=df.withColumn(‘quarter’,F.quarter(‘date’)) df=df.withColumn(‘next_day_Mon’,F.next_day(‘date’,’Mon’)) df=df.withColumn(‘next_day_Tue’,F.next_day(‘date’,’Tue’)) df=df.withColumn(‘next_day_Wed’,F.next_day(‘date’,’Wed’)) ..
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 ..
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 ..
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..