NCO: Copying data from one netcdf file into another only for a specific area - replace

I have two NetCDF files with the exact same dimensions (time,lat,lon). Below is the header of one of the files.
netcdf file1 {
dimensions:
lon = 360 ;
lat = 177 ;
time = 360 ;
variables:
double lon(lon) ;
lon:units = "degrees_east" ;
lon:long_name = "Longitude" ;
double lat(lat) ;
lat:units = "degrees_north" ;
lat:long_name = "Latitude" ;
double time(time) ;
time:long_name = "Time" ;
time:units = "months since 1989-05-01 00:00" ;
double tmp(time, lat, lon) ;
tmp:_FillValue = -999000000. ;
}
I would like to copy values from one file into the other, but only for a small region determined by lat1,lat2 and lon1,lon2. Both files have the same time coordinates.
Something like: (lon1<lon<=lon2) & (lat1<lat<=lat2) file1 = file2
I was wondering if I could do that with NCO.
Any help will be appreciated. Thank

Read the manual section on the ncap2 where function. It describes how to use where on a hyperslab. That will do it:
*var_tmp=var2(:,0,:,:);
where (var1 < 0.5) var_tmp=1234;
var2(;,0,:,;)=var_tmp;

Related

Decompressing a netCDF file

I have a comrpessed netCDF file called in.nc with a (_DeflateLevel=4). I want to decompress the file.
I've already tried using the following command but it doesn't change anything:
nccopy -d 0 in.nc out.nc
Here are the in.nc specifications:
time_counter = UNLIMITED ; // (12 currently)
y = 552 ;
x = 552 ;
variables:
float Q16c(time_counter, y, x) ;
Q16c:long_name = "Deep Sediment Particulate organic Matter" ;
Q16c:units = "mg C/m2" ;
Q16c:online_operation = "average" ;
Q16c:interval_operation = "900 s" ;
Q16c:interval_write = "1 month" ;
Q16c:cell_methods = "time: mean (interval: 900 s)" ;
Q16c:_FillValue = 1.e+20f ;
Q16c:missing_value = 1.e+20f ;
Q16c:coordinates = "time_centered nav_lat nav_lon" ;
Q16c:_Storage = "chunked" ;
Q16c:_ChunkSizes = 1, 552, 552 ;
Q16c:_DeflateLevel = 4 ;
Q16c:_Endianness = "little" ;

Looping in Mata with OLS

I need help with looping in Mata. I have to write a code for Beta coefficients for OLS in Mata using a loop. I am not sure how to call for the variables and create the code. Here is what I have so far.
foreach j of local X {
if { //for X'X
matrix XX = [mata:XX = cross(X,1 , X,1)]
XX
}
else {
mata:Xy = cross(X,1 , y,0)
Xy
}
I am getting an error message "invalid syntax".
I'm not sure what you need the loop for. Perhaps you can provide more information about that. However the following example may help you implement OLS in mata.
Load example data from bcuse:
ssc install bcuse
clear
bcuse bwght
mata
x = st_data(., ("male", "parity","lfaminc","packs"))
cons = J(rows(x), 1, 1)
X = (x, cons)
y = st_data(., ("lbwght"))
beta_hat = (invsym(X'*X))*(X'*y)
e_hat = y - X * beta_hat
s2 = (1 / (rows(X) - cols(X))) * (e_hat' * e_hat)
B = J(cols(X), cols(X), 0)
n = rows(X)
for (i=1; i<=n; i++) {
B =B+(e_hat[i,1]*X[i,.])'*(e_hat[i,1]*X[i,.])
}
V_robust = (n/(n-cols(X)))*invsym(X'*X)*B*invsym(X'*X)
se_robust = sqrt(diagonal(V_robust))
V_ols = s2 * invsym(X'*X)
se_ols = sqrt(diagonal(V_ols))
beta_hat
se_robust
end
This is far from the only way to implement OLS using mata. See the Stata Blog for another example using quadcross, I like my example because it preserves a little more of the matrix algebra in the code.

OrderedDict() returned from dataset.variable

I want to parse NetCDF file using NETCDF4 and Python
My code is :
>>> from netCDF4 import Dataset
>>> dataset = Dataset('data.nc')
>>> print dataset.variables
OrderedDict()
Why OrderedDict() is returned ?
Actually the Netcdf format is new for me here is a part of it :
group: PRODUCT {
dimensions:
scanline = 289 ;
ground_pixel = 215 ;
corner = 4 ;
time = 1 ;
layer = 50 ;
variables:
int scanline(scanline) ;
scanline:units = "1" ;
So I want to access the variables and tried everything in my mind but all failed..
One of my trails is :
>print dataset.variables.keys()
[]
But it returned []
So any idea how to access these variables ?
Thanks in advance,
Hala
I found the answer in http://unidata.github.io/netcdf4-python/#netCDF4.Dataset.renameGroup
The answer is :
print dataset["PRODUCT"].variables['ground_pixel'][0]
Have a nice day

Concatenate line feeds

I am trying to concatenate three string variables.
Data X ;
a = "A" ;
b = "B" ;
c = "C" ;
z = catx ( '0D0A'x, a, b, c ) ;
run;
I am trying to display the string values like this in the final dataset, so that the values appear one below the other -
A
B
C
But by using '0D0A'x option, the string appears as ABC. I have to display the z variable in an excel. If I had to output the same into a HTML file then I would have used "\n" as an option in CATX function. Is there a way where I can introduce new line characters.
I adapted your example slightly:
libname test excel "%sysfunc(pathname(work))\text.xls";
Data test.X ;
a = "A" ;
b = "B" ;
c = "C" ;
z = catx ( '0D0A'x, a, b, c ) ;
run;
libname test clear;
x "explorer ""%sysfunc(pathname(work))\text.xls""";
If you use this approach, you get a value in cell D2 which contains the line breaks as expected. However, in order for them to display correctly, you have to enable the 'wrap text' option for the cell formatting.

Print subset of matrix

I'm trying to create a code to run a simple perceptron in SAS base.
I'd like to print in each iteration (or store in a table) the result and the target, but I get an error when I try to print y[i,]:
proc iml;
use percept; read all var{x1 X2} into X;
read all var{Y} into Y;
W={0,0}; b=0; k=0; L=nrow(X); eta=.8; o=0;
print w b k L eta;
do step = 1 to 6;
mistakes=0;
do i=1 to L;
o=(X[i, ]*W + b);
if Y[i, ]*o <= 0 then do;
W = W + eta*(Y[i, ]-o)*X[i,]`;
b = b + eta*(Y[i, ]-o)*1;
k=k+1; mistakes=mistakes+1;
print o Y[i, ] W b k mistakes;
end;
end;
end;
I get the error:
Syntax error, expecting one of the following: C, COLNAME, F, FORMAT,
L, LABEL, R,
ROWNAME, ], |). The option or parameter is not recognized and will be ignored.
Do I have any other form to print the target?
Thanks a lot!
Per the documentation on PRINT, you need to do it like this:
print(Y[i,])
This is because they overload the [ ] to indicate formatting, rownames/colnames, etc., which is rather silly (but presumably to imitate some other language?). So you just need to wrap (Y[i,]) like so.
Here's a silly example.
proc iml;
use sashelp.class;
read all var{name,sex} into class;
read all var{height,weight,age} into classN;
y = mean(classN[,2]);
print class;
print (class[1:2,]);
print y (class[1:2,]);
quit;