// $Header: /cvsroot/nco/nco/data/tst.nco,v 1.7 2006/06/17 23:25:12 zender Exp $ -*-C-*-

// Purpose: ncap script for geophysical computations

/* Usage:
   ncap -v -O -D 1 -S ${HOME}/nco/data/tst.nco ${HOME}/nco/data/in.nc ${HOME}/nco/data/foo.nc
   ncks -C -H -u -v mxdgr_lcl ${HOME}/nco/data/foo.nc */

rds_earth=6.370e+06; // (6.370e+06) [m] Radius of sphere of same volume as Earth
rds_earth@long_name="Radius of sphere of same volume as Earth";
rds_earth@units="meter";

pi=3.1415926535897932384626433832795029L; // (3.1415926535897932384626433832795029L) [frc] 3
pi@long_name="Pi";
pi@units="fraction";

lat_lcl_dgr=16.75; // [dgr] Local latitude
lat_lcl_dgr@long_name="Local latitude";
lat_lcl_dgr@units="degree";

lat_lcl=lat_lcl_dgr*(pi/180.0); // [rdn] Local latitude
lat_lcl@long_name="Local latitude";
lat_lcl@units="radian";

rds_lcl=rds_earth*cos(lat_lcl); // [m] Distance from axis of rotation at local latitude
rds_lcl@long_name="Distance from axis of rotation at local latitude";
rds_lcl@units="meter";

crc_lcl=2.0*pi*rds_lcl; // [m] Circumference of earth along local latitude
crc_lcl@long_name="Circumference of earth along local latitude";
crc_lcl@units="meter";

mxdgr_lcl=crc_lcl/360.0; // [m dgr-1] Meters per degree at local latitude
mxdgr_lcl@long_name="Meters per degree at local latitude";
mxdgr_lcl@units="meter degree-1";

// Compute fraction of lognormal distribution that lies between two endpoints
gsd=2.0; // [frc] Geometric standard deviation
gsd@long_name="Geometric standard deviation";
gsd@units="fraction";

dmt_vma=2.5; // [m] Volume median diameter analytic
dmt_vma@long_name="Volume median diameter analytic";
dmt_vma@units="meter";

dmt_min=5.0; // [m] Minimum diameter
dmt_min@long_name="Minimum diameter";
dmt_min@units="meter";

dmt_max=10.0; // [m] Maximum diameter
dmt_max@long_name="Maximum diameter";
dmt_max@units="meter";

// ncap -v -O -s "gsd=2.0;dmt_vma=2.5;dmt_min=5.0;dmt_max=10.0;ovr_frc=0.5*(erf(log(dmt_max/dmt_vma)/(sqrt(2.0)*log(gsd)))-erf(log(dmt_min/dmt_vma)/(sqrt(2.0)*log(gsd))))));" ${HOME}/nco/data/in.nc ${HOME}/nco/data/foo.nc;ncks -H -C -u ${HOME}/nco/data/foo.nc
ovr_frc=0.5*(erf(log(dmt_max/dmt_vma)/(sqrt(2.0)*log(gsd)))-erf(log(dmt_min/dmt_vma)/(sqrt(2.0)*log(gsd)))))); // [frc] Fraction of lognormal distribution between endpoints
ovr_frc@long_name="Fraction of lognormal distribution between endpoints";
ovr_frc@units="fraction";

// Compute specific extinction coefficient of dust from bin-resolved mixing ratios
mss_frc_ttl=2.09e-09+9.13e-09+8.18e-09+1.58e-09; // [kg kg-1] Total dust mixing ratio
mss_frc1=2.09e-09/mss_frc_ttl;
mss_frc2=9.13e-09/mss_frc_ttl;
mss_frc3=8.18e-09/mss_frc_ttl;
mss_frc4=1.58e-09/mss_frc_ttl;
ext_cff_mss1=2.893e3*mss_frc1;
ext_cff_mss2=8.350e2*mss_frc2;
ext_cff_mss3=3.825e2*mss_frc3;
ext_cff_mss4=1.961e2*mss_frc4;
ext_cff_mss_ttl=ext_cff_mss1+ext_cff_mss2+ext_cff_mss3+ext_cff_mss4;

// This command is placed here for consistency with NCO User's Guide ncap example
a=3;b=4;c=sqrt(a^2+b^2);

/*
foo=; // 
foo@long_name="";
foo@units="";
*/
