Multiply different values to timeseries data
Added by Suvarna Tikle over 3 years ago
Dear All,
I have variable res data for 14 months. Would like to apply various factors for each month. Example , Dec*12,Jan*1,Feb*2 etc.
I am trying to write bash script but how to apply separate factor for different months. Can one help me to correct the code?
#!/bin/bash
module load cdo
cdo splityear tonns_gridarea_fixgrid_Mul_res_CAMS_LE_Indore.nc month_%.nc # split the input file by year into separate files for each month
for file in month_*.[DJFMAMJJASONDJ]*.nc; do # loop over the files for each month
month=$(echo $file | cut -d '_' -f 2) # extract the month from the filename
factor=1,2,3,4,5,6,7,8,9,10,11,12,13,14 # set the factor for this month
cdo expr,"var*$factor" $file factor_${month}_$file # apply the factor to the data in the file
done
cdo cat factor_* output.nc # concatenate the files back together into a single output file
Best,
Suvarna
Replies (4)
RE: Multiply different values to timeseries data - Added by Suvarna Tikle over 3 years ago
This is in continuation of earlier query. I also tried with below code but hard luck.
#!/bin/bash
module load cdo
#define the factors for each month
declare -A factors=(
["2018-12-01"]=31*60*60*24
["2019-01-01"]=31*60*60*24
["2019-02-01"]=28*60*60*24
["2019-03-01"]=30*60*60*24
["2019-04-01"]=30*60*60*24
["2019-05-01"]=31*60*60*24
["2019-06-01"]=30*60*60*24
["2019-07-01"]=31*60*60*24
["2019-08-01"]=31*60*60*24
["2019-09-01"]=30*60*60*24
["2019-10-01"]=31*60*60*24
["2019-11-01"]=30*60*60*24
["2019-12-01"]=31*60*60*24
["2020-01-01"]=31*60*60*24
)
# Loop over each month in the factors array
for month in "${!factors[@]}"; do
# Define the CDO expression for this month
expr="$month=${factors[$month]}*${month}"
# Apply the expression to the input file and save the output to a temporary file
cdo expr,"$expr" tonns_gridarea_fixgrid_Mul_res_CAMS_LE_Indore.nc tmp_$month.nc
done
# Merge the temporary files into a single output file
cdo mergetime tmp_*.nc tonns_monthly_CAMS_NMVOC.nc
# Clean up the temporary files
rm tmp_*.nc
~
Ncdump of file is-
netcdf tonns_gridarea_fixgrid_Mul_res_CAMS_LE_Indore {
dimensions:
time = UNLIMITED ; // (14 currently)
lat = 16 ;
lon = 20 ;
variables:
double res(time, lat, lon) ;
res:cell_method = "time: sum (interval: 1 month comment: gregorian calendar) " ;
res:long_name = "Emissions of NMVOCs for res sector" ;
res:molecular_weight = "72" ;
res:sector_name = "Residential, commercial and other combustion" ;
res:standard_name = "tendency_of_atmosphere_mass_content_of_nmvocs_due_to_emissions_from_res_sector" ;
res:units = "kg m-2 s-1" ;
float lon(lon) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:axis = "X" ;
float lat(lat) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:axis = "Y" ;
double cell_area(lat, lon) ;
cell_area:standard_name = "area" ;
cell_area:long_name = "area of grid cell" ;
cell_area:units = "m2" ;
float time(time) ;
time:standard_name = "time" ;
time:long_name = "time" ;
time:units = "hours since 1850-01-01 00:00:00" ;
time:calendar = "gregorian" ;
time:axis = "T" ;
Best,
Suvarna
RE: Multiply different values to timeseries data - Added by Ralf Mueller over 3 years ago
hi Survana!
you can use the before and after the source code. Than the indentation is more readable
cheers
ralf
RE: Multiply different values to timeseries data - Added by Suvarna Tikle over 3 years ago
#!/bin/bash
module load cdo
- Define the factors for each month
declare -A factors=(
[2018_12_01]=2678400
[2019_01_01]=2678400
[2019_02_01]=2419200
[2019_03_01]=2592000
[2019_04_01]=2592000
[2019_05_01]=2678400
[2019_06_01]=2592000
[2019_07_01]=2678400
[2019_08_01]=2678400
[2019_09_01]=2592000
[2019_10_01]=2678400
[2019_11_01]=2592000
[2019_12_01]=2678400
[2020_01_01]=2678400
)
- Loop over each month in the factors array
for month in "${!factors[@]}"; do
- Define the CDO expression for this month
expr="(${month}_rate=${factors[$month]}*tonns_gridarea_fixgrid_Mul_res_CAMS_LE_Indore)"
- Apply the expression to the input file and save the output to a temporary file
cdo expr,"$expr" tonns_gridarea_fixgrid_Mul_res_CAMS_LE_Indore.nc tmp_${month}_rate.nc
done
- Merge the temporary files into a single output file
cdo mergetime tmp_*_rate.nc tonns_monthly_CAMS_NMVOC.nc
- Clean up the temporary files
rm tmp_*_rate.nc
RE: Multiply different values to timeseries data - Added by Suvarna Tikle over 3 years ago
problem has been resolved.