Monthly average for a given location in a daily netcdf file - netcdf

I have a netcdf file with the daily precipitation (for a whole decade) in every latitude and longitude, it's in the form (lon,lat,time). I want to get the monthly average for the longitude=-118.25:-84.75 and for the latitude=13.25:33.25. I need to write another netcdf file in which the variable is monthly precipitation given by (lon,lat,time) but i dont know how to extract the ranges and how to obtain the monthly average since the months are repeated each year.

Just use the tool called cdo and operator sellonlatbox:
cdo -sellonlatbox -118.25,-84.75,13.25,33.25 filein fileout
filein is the name of your input file and fileout is the name of the output.
Afterwards you can use operator monmean to calculate monthly means:
cdo -monmean fileout final_file

Related

How can I convert vector to raster?

I am working on SDM project using Max-Ent, and Random Forest.
When I prepare tiff, or raster files of environmental data, I have difficulties, because of I’m a beginner.
For example: We have a csv file (1981_1991_ta_totalAverage.csv), which is contains average min temperature from 1981 to 1991.
Question 1: How can I convert this csv file to raster format?
I tried to convert it. When I need raster map of Korea, I used bclim6.asc (BIO6=Minimum Temperature of the Coldest Month for Korea. It seems something wrong. Question 2: How can I get taster file of Korea?

Saving NetCDF as subsets of the time dimension using xarray

I have a NetCDF with one variable (front) and four dimensions (time, altitude, lat and lon). Downloaded from https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdGAtfnt10day_LonPM180.html
It is a monthly composite, i.e. <xarray.DataArray 'time' (time: 151)>
array(['2001-01-16T12:00:00.000000000', '2001-02-16T00:00:00.000000000', ...'2013-12-16T12:00:00.000000000'], dtype='datetime64[ns]').
I would like to create a single file (either a NetCDF or Geotif it doesn't matter which) for each timestamp.
I've tried:
ds = xr.open_dataset("Front_monthly2001to2013.nc", decode_times=True)
months, datasets = zip(*ds.groupby("time.month")) #.groupby("time.month")
paths = ["test%s.nc" % m for m in months]
xr.save_mfdataset(datasets, paths)
But then it groups all the months and I get 12 output files instead of one for each year-month.
How do I save by year-month (which is the same, in my case, as each timestamp)?
Thanks in advance
You can use the datetime components of your time dimension to re-format the timestamp to year & month and use it for grouping:
months, datasets = zip(*ds.groupby(ds.time.dt.strftime("%Y%m")))

How to convert annual netCDF data to daily from the command line?

Before I resort to using Python, I would like to know if there is a simple way from the command line to convert an annual netCDF file into a file with daily data simply by duplication (including leap years), i.e. each annual value is duplicated 365 (366) times with the appropriate date stamp.
In the file each data value has the date stamped for the first day of each year:
cdo showdate population_ssp2soc_0p5deg_annual_2006-2100_density.nc4
2006-01-01 2007-01-01 2008-01-01 2009-01-01 2010-01-01 ...etc
I know it seems like a strange thing to do (the file size will be 365.25 times bigger!), but I want to read the data into a Fortran program that uses a daily timestep and do not want to mess around with dates in the Fortran code.
There might be a more efficient way of doing this. But you could first merge the original file and then a second which is the first, but time shifted to the final day of the year. Then temporally interpolate:
cdo -mergetime population_ssp2soc_0p5deg_annual_2006-2100_density.nc4 -shifttime,-1day -shifttime,1year population_ssp2soc_0p5deg_annual_2006-2100_density.nc4 temp.nc
cdo inttime,2006-01-01,12:00:00,1day temp.nc outfile.nc

Monthly sum of wet days from daily data using Climate Data Operators (CDO)

I have climate data with a daily temporal resolution and would like a count of days that have precipitation (e.g., greater than 1mm/day) by month and by year.
I've tried eca_pd,1 and eca_rr1, but these commands return wet-day totals for all years.
For example, cdo eca_pd,1 infile outfile
Is there a command to return wet-days for each month and/or year?
You can accomplish this task with CDO's masking function, for more details beyond the answer below, you can also refer to my video guide on masking using cdo.
The first step is to make an equivalent file with 1 if P>threshold (1mm/day in your case) and 0 otherwise. For this we use the "greater than or equal to a constant" gec function (or ge="greater than" if you prefer):
cdo gec,1 input.nc mask.nc
(assuming units are mm/day in your input file).
Then you can simply sum this mask over the period (months, years etc) that you want your statistic
cdo monsum mask.nc nwetdays_mon.nc
cdo yearsum mask.nc nwetdays_year.nc
Of course you can pipe this if you like to do this on one line: e.g.
cdo monsum -gec,1 input.nc nwetdays_mon.nc
We can take this even further if you want to work out the climatology for a particular month. If you have a multiyear dataset then you can use the wonderful "ymonstat" commands. So for example, once you have calculated your monthly series of wet days above, you can calculate the average for each month with
cdo ymonmean nwetdays_mon.nc nwetdays_mon_clim.nc
You can then difference the series from this monthly climatology to give you the anomaly of wet days in each month over the series
cdo ymonsub nwetdays_mon.nc nwetdays_mon_clim.nc nwetdays_mon_anom.nc
I hope that helps!
(ps: I usually always find it is easier to calculate these kinds of statistics directly with CDO in this way, I rarely find that the built in climate functions calculate exactly the statistic as/how I want).
With NCO's ncap2, create a binary flag then total it in the desired dimension(s):
ncap2 -s 'rainy=(precip > 1);rainy_days=rainy.total($time)' in.nc out.nc
You can also do this in cf-python, essentially using the same methodology as the CDO example above, but in a Python environment, using the where and collapse methods:
import cf
# Read the dataset
f = cf.read('filename.nc')[0]
# Mask out dry days (assuming that your data
# units are 'mm day-1' or 'kg m-2 day-1', etc.)
wet = f.where(cf.le(1), cf.masked)
# If the data are in units of 'metres/day', say, then you could do:
# wet = f.where(cf.le(0.001), cf.masked)
# or
# wet = f.where(cf.le(1, 'mm day-1'), cf.masked)
# etc.
# Count the wet day occurrences by month
count_monthly = wet.collapse('T: sample_size', group=cf.M())
# Count the wet day occurrences by year
count_yearly = wet.collapse('T: sample_size', group=cf.Y())
# Get the data as numpy arrays
print(count_monthly.array)
print(count_yearly.array)
# Count the wet day totals by month
wet_day_sum_monthly = wet.collapse('T: sum', group=cf.M())
# Count the wet day totals by year
wet_day_sum_yearly = wet.collapse('T: sum', group=cf.Y())

Is there a way to perform batch interpolation in ArcGIS from 1 excel file?

I have an excel file with station weather data. For each station, for each year (70 years), I have temperature data, as well as associated latitude and longitude of the stations. I want to create interpolated raster maps (using IDW) for each year for temperature.
My excel files are set up like this, but with 70 years of data:
I would therefore like 70 interpolated maps for each year of temperature. It also may be important to note that the stations for each year are not all the same.
I am willing to try to do this as a batch process in ArcGIS, but find that can be tedious. Is there a faster way to do this, through arcpy or even through R?

Resources